feat: fetch cash-out transaction status
This commit is contained in:
parent
09cb924106
commit
f37a73f983
3 changed files with 426 additions and 175 deletions
|
|
@ -1,10 +1,11 @@
|
|||
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
const invoice = require('@node-lightning/invoice')
|
||||
const axios = require('axios')
|
||||
const NAME = 'LN'
|
||||
const SUPPORTED_COINS = ['LN', 'BTC']
|
||||
const TX_PENDING = 'PENDING'
|
||||
const TX_SUCCESS = 'SUCCESS'
|
||||
|
||||
const URI = 'https://api.staging.galoy.io/graphql'
|
||||
|
||||
|
|
@ -13,7 +14,7 @@ const BN = require('../../../bn')
|
|||
function request (graphqlQuery, token) {
|
||||
const headers = {
|
||||
'content-type': 'application/json',
|
||||
'Authorization': `Bearer ${token || TEST_AUTH_TOKEN}`
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
return Promise.resolve(true)
|
||||
.then(() => {
|
||||
|
|
@ -108,65 +109,6 @@ function getGaloyAccount (token) {
|
|||
})
|
||||
}
|
||||
|
||||
function fetchAuthToken (config) {
|
||||
const phone = config.phone
|
||||
// userRequestAuthCode deprecated?
|
||||
const regularRequestAuthCode = {
|
||||
'operationName': 'userRequestAuthCode',
|
||||
'query': `mutation userRequestAuthCode($input: UserRequestAuthCodeInput!) {
|
||||
userRequestAuthCode(input: $input) {
|
||||
errors {
|
||||
message
|
||||
path
|
||||
}
|
||||
success
|
||||
}
|
||||
}`,
|
||||
'variables': { 'input': { 'phone': `${phone}` } }
|
||||
}
|
||||
const createCaptcha = {
|
||||
'operationName': 'captchaCreateChallenge',
|
||||
'query': `mutation captchaCreateChallenge {
|
||||
captchaCreateChallenge {
|
||||
errors {
|
||||
message
|
||||
path
|
||||
}
|
||||
result {
|
||||
challengeCode
|
||||
id
|
||||
}
|
||||
}
|
||||
}`,
|
||||
'variables': {}
|
||||
}
|
||||
const captchaRequestAuthCode = code => ({
|
||||
'operationName': 'captchaRequestAuthCode',
|
||||
'query': `mutation captchaRequestAuthCode($input: CaptchaRequestAuthCodeInput!) {
|
||||
captchaRequestAuthCode(input: $input) {
|
||||
errors {
|
||||
message
|
||||
path
|
||||
}
|
||||
success
|
||||
}
|
||||
}`,
|
||||
'variables': { 'input': { 'phone': `${phone}`, 'challengeCode': `${code.challengeCode}`, 'secCode': `${code.challengeCode.slice(0, 5)}`, 'validationCode': `${code.challengeCode.slice(0, 5)}` } }
|
||||
})
|
||||
|
||||
return request(createCaptcha)
|
||||
.then(r => {
|
||||
const code = r.data.captchaCreateChallenge.result
|
||||
if (code) {
|
||||
// captchaRequestAuthCode parameters have to be processed by geetest
|
||||
return request(captchaRequestAuthCode(code))
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
throw new Error(err)
|
||||
})
|
||||
}
|
||||
|
||||
function isLightning (address) {
|
||||
return address.substr(0, 2) === 'ln'
|
||||
}
|
||||
|
|
@ -220,14 +162,13 @@ function sendFundsLN (walletId, invoice, token) {
|
|||
function sendCoins (account, tx, settings, operatorId) {
|
||||
const { toAddress, cryptoAtoms, cryptoCode } = tx
|
||||
return checkCryptoCode(cryptoCode)
|
||||
// .then(() => fetchAuthToken({ phone: PHONE }))
|
||||
.then(authToken => Promise.all([getGaloyAccount(authToken), authToken]))
|
||||
.then(([account, authToken]) => {
|
||||
const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets))
|
||||
.then(() => getGaloyAccount(account.authToken))
|
||||
.then(galoyAccount => {
|
||||
const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === galoyAccount.defaultWalletId)(galoyAccount.wallets))
|
||||
if (isLightning(toAddress)) {
|
||||
return sendFundsLN(wallet.id, toAddress, authToken)
|
||||
return sendFundsLN(wallet.id, toAddress, account.authToken)
|
||||
}
|
||||
return sendFundsOnChain(wallet.id, toAddress, cryptoAtoms, authToken)
|
||||
return sendFundsOnChain(wallet.id, toAddress, cryptoAtoms, account.authToken)
|
||||
})
|
||||
.then(result => {
|
||||
return result
|
||||
|
|
@ -287,13 +228,11 @@ function newInvoice (walletId, cryptoAtoms, token) {
|
|||
|
||||
function balance (account, cryptoCode, settings, operatorId) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
// .then(() => fetchAuthToken({ phone: PHONE }))
|
||||
.then(authToken => getGaloyAccount(authToken))
|
||||
.then(account => {
|
||||
console.log(account)
|
||||
.then(() => getGaloyAccount(account.authToken))
|
||||
.then(galoyAccount => {
|
||||
// account has a list of wallets, should we consider the balance of each one?
|
||||
// for now we'll pick the first BTC wallet that matches the defaultWalletId
|
||||
const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets))
|
||||
// for now we'll get the first BTC wallet that matches the defaultWalletId
|
||||
const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === galoyAccount.defaultWalletId)(galoyAccount.wallets))
|
||||
console.log(wallet)
|
||||
return new BN(wallet.balance || 0)
|
||||
})
|
||||
|
|
@ -305,13 +244,12 @@ function balance (account, cryptoCode, settings, operatorId) {
|
|||
function newAddress (account, info, tx, settings, operatorId) {
|
||||
const { cryptoAtoms, cryptoCode } = tx
|
||||
return checkCryptoCode(cryptoCode)
|
||||
// .then(() => fetchAuthToken({ phone: PHONE }))
|
||||
.then(authToken => Promise.all([getGaloyAccount(authToken), authToken]))
|
||||
.then(([account, authToken]) => {
|
||||
const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets))
|
||||
.then(() => getGaloyAccount(account.authToken))
|
||||
.then(galoyAccount => {
|
||||
const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === galoyAccount.defaultWalletId)(galoyAccount.wallets))
|
||||
const promises = [
|
||||
newOnChainAddress(wallet.id, authToken),
|
||||
newInvoice(wallet.id, cryptoAtoms, authToken)
|
||||
newOnChainAddress(wallet.id, account.authToken),
|
||||
newInvoice(wallet.id, cryptoAtoms, account.authToken)
|
||||
]
|
||||
return Promise.all(promises)
|
||||
})
|
||||
|
|
@ -321,22 +259,39 @@ function newAddress (account, info, tx, settings, operatorId) {
|
|||
}
|
||||
|
||||
function getStatus (account, tx, requested, settings, operatorId) {
|
||||
// Type Transaction has a field status
|
||||
// but we're not sure if it's interchangeable with our status definition
|
||||
const { toAddress, cryptoAtoms, cryptoCode } = tx
|
||||
const mapStatus = status => {
|
||||
if (status === TX_PENDING) return 'authorized'
|
||||
if (status === TX_SUCCESS) return 'confirmed'
|
||||
return 'notSeen'
|
||||
}
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => getGaloyAccount(account.authToken))
|
||||
.then(galoyAccount => {
|
||||
const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === galoyAccount.defaultWalletId)(galoyAccount.wallets))
|
||||
const transactions = wallet.transactions.edges
|
||||
if (isLightning(toAddress)) {
|
||||
const paymentHash = invoice.decode(toAddress).paymentHash.toString('hex')
|
||||
const transaction = _.head(_.filter(tx => tx.node.initiationVia.paymentHash === paymentHash && tx.node.direction === 'RECEIVE')(transactions))
|
||||
return { receivedCryptoAtoms: cryptoAtoms, status: mapStatus(transaction.node.status) }
|
||||
}
|
||||
// On-chain tx
|
||||
const transaction = _.head(_.filter(tx => tx.node.initiationVia.address === toAddress)(transactions))
|
||||
return { receivedCryptoAtoms: cryptoAtoms, status: mapStatus(transaction.node.status) }
|
||||
})
|
||||
}
|
||||
|
||||
function newFunding (account, cryptoCode, settings, operatorId) {
|
||||
// Has to be a regular BTC address
|
||||
// Regular BTC address
|
||||
return checkCryptoCode(cryptoCode)
|
||||
// .then(() => fetchAuthToken({ phone: PHONE }))
|
||||
.then(authToken => Promise.all([getGaloyAccount(authToken), authToken]))
|
||||
.then(([account, authToken]) => {
|
||||
const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets))
|
||||
.then(() => getGaloyAccount(account.authToken))
|
||||
.then(galoyAccount => {
|
||||
const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === galoyAccount.defaultWalletId)(galoyAccount.wallets))
|
||||
const pendingBalance = _.sumBy(tx => {
|
||||
if (tx.node.status === TX_PENDING) return tx.node.settlementAmount
|
||||
return 0
|
||||
})(wallet.transactions.edges)
|
||||
return newOnChainAddress(wallet.id, authToken)
|
||||
return newOnChainAddress(wallet.id, account.authToken)
|
||||
.then(onChainAddress => [onChainAddress, wallet.balance, pendingBalance])
|
||||
})
|
||||
.then(([onChainAddress, balance, pendingBalance]) => {
|
||||
|
|
@ -358,22 +313,63 @@ function checkBlockchainStatus (cryptoCode) {
|
|||
.then(() => Promise.resolve('ready'))
|
||||
}
|
||||
|
||||
// sendCoins({}, {
|
||||
// toAddress: 'tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt',
|
||||
// cryptoCode: 'BTC',
|
||||
// cryptoAtoms: 1000
|
||||
// }, {}, {})
|
||||
// .then(r => console.log(r))
|
||||
|
||||
// balance({}, 'BTC', {}, {})
|
||||
// .then(r => console.log())
|
||||
// getGaloyAccount()
|
||||
// newAddress({}, {}, { cryptoCode: 'BTC', cryptoAtoms: 1 }, {})
|
||||
// .then(r => console.log(r))
|
||||
|
||||
// Test faucet addresses
|
||||
// tb1qfyt7cgds7z8ssthtnhh35s608059yzk0hwgcqd 1)
|
||||
// tb1qmflph389nz5ev05jypzvcglyguyrl2qhyaag6r 2)
|
||||
// function fetchAuthToken (config) {
|
||||
// const phone = config.phone
|
||||
// // userRequestAuthCode deprecated?
|
||||
// const regularRequestAuthCode = {
|
||||
// 'operationName': 'userRequestAuthCode',
|
||||
// 'query': `mutation userRequestAuthCode($input: UserRequestAuthCodeInput!) {
|
||||
// userRequestAuthCode(input: $input) {
|
||||
// errors {
|
||||
// message
|
||||
// path
|
||||
// }
|
||||
// success
|
||||
// }
|
||||
// }`,
|
||||
// 'variables': { 'input': { 'phone': `${phone}` } }
|
||||
// }
|
||||
// const createCaptcha = {
|
||||
// 'operationName': 'captchaCreateChallenge',
|
||||
// 'query': `mutation captchaCreateChallenge {
|
||||
// captchaCreateChallenge {
|
||||
// errors {
|
||||
// message
|
||||
// path
|
||||
// }
|
||||
// result {
|
||||
// challengeCode
|
||||
// id
|
||||
// }
|
||||
// }
|
||||
// }`,
|
||||
// 'variables': {}
|
||||
// }
|
||||
// const captchaRequestAuthCode = code => ({
|
||||
// 'operationName': 'captchaRequestAuthCode',
|
||||
// 'query': `mutation captchaRequestAuthCode($input: CaptchaRequestAuthCodeInput!) {
|
||||
// captchaRequestAuthCode(input: $input) {
|
||||
// errors {
|
||||
// message
|
||||
// path
|
||||
// }
|
||||
// success
|
||||
// }
|
||||
// }`,
|
||||
// 'variables': { 'input': { 'phone': `${phone}`, 'challengeCode': `${code.challengeCode}`, 'secCode': `${code.challengeCode.slice(0, 5)}`, 'validationCode': `${code.challengeCode.slice(0, 5)}` } }
|
||||
// })
|
||||
// return request(createCaptcha)
|
||||
// .then(r => {
|
||||
// const code = r.data.captchaCreateChallenge.result
|
||||
// if (code) {
|
||||
// // captchaRequestAuthCode parameters have to be processed by geetest
|
||||
// return request(captchaRequestAuthCode(code))
|
||||
// }
|
||||
// })
|
||||
// .catch(err => {
|
||||
// throw new Error(err)
|
||||
// })
|
||||
// }
|
||||
|
||||
module.exports = {
|
||||
NAME,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue