feat: retrieve account info

This commit is contained in:
José Oliveira 2022-04-05 17:54:11 +01:00 committed by siiky
parent 49bd2d16e3
commit dd55cda986

View file

@ -3,8 +3,9 @@ const _ = require('lodash/fp')
const axios = require('axios') const axios = require('axios')
const NAME = 'LN' const NAME = 'LN'
const SUPPORTED_COINS = ['LN'] const SUPPORTED_COINS = ['LN', 'BTC']
const PHONE = '+3500000000000' const PHONE = '+3500000000000'
const TEST_AUTH_TOKEN = ''
const URI = 'https://api.staging.galoy.io/graphql' const URI = 'https://api.staging.galoy.io/graphql'
@ -13,7 +14,7 @@ const BN = require('../../../bn')
function request (graphqlQuery, token) { function request (graphqlQuery, token) {
const headers = { const headers = {
'content-type': 'application/json', 'content-type': 'application/json',
'Authorization': token || '' 'Authorization': `Bearer ${token || TEST_AUTH_TOKEN}`
} }
return Promise.resolve(true) return Promise.resolve(true)
.then(() => { .then(() => {
@ -25,7 +26,6 @@ function request (graphqlQuery, token) {
}) })
}) })
.then(r => { .then(r => {
console.log(r)
if (r.error) throw r.error if (r.error) throw r.error
return r.data return r.data
}) })
@ -49,7 +49,48 @@ function getGaloyAccount (token) {
me { me {
createdAt createdAt
defaultAccount { defaultAccount {
...AccountFragment defaultWalletId
wallets {
id
walletCurrency
balance
transactions {
edges {
node {
createdAt
direction
id
settlementAmount
settlementFee
status
initiationVia {
... on InitiationViaIntraLedger {
counterPartyUsername
counterPartyWalletId
}
... on InitiationViaLn {
paymentHash
}
... on InitiationViaOnChain {
address
}
}
settlementVia {
... on SettlementViaIntraLedger {
counterPartyUsername
counterPartyWalletId
}
... on SettlementViaLn {
preImage
}
... on SettlementViaOnChain {
transactionHash
}
}
}
}
}
}
} }
id id
phone phone
@ -61,7 +102,7 @@ function getGaloyAccount (token) {
} }
return request(accountInfo, token) return request(accountInfo, token)
.then(r => { .then(r => {
return r.data.data.defaultAccount return r.data.me.defaultAccount
}) })
.catch(err => { .catch(err => {
throw new Error(err) throw new Error(err)
@ -76,7 +117,8 @@ function fetchAuthToken (config) {
'query': `mutation userRequestAuthCode($input: UserRequestAuthCodeInput!) { 'query': `mutation userRequestAuthCode($input: UserRequestAuthCodeInput!) {
userRequestAuthCode(input: $input) { userRequestAuthCode(input: $input) {
errors { errors {
...ErrorFragment message
path
} }
success success
} }
@ -88,32 +130,41 @@ function fetchAuthToken (config) {
'query': `mutation captchaCreateChallenge { 'query': `mutation captchaCreateChallenge {
captchaCreateChallenge { captchaCreateChallenge {
errors { errors {
...ErrorFragment message
path
} }
result { result {
...CaptchaCreateChallengeResultFragment challengeCode
id
} }
} }
}`, }`,
'variables': {} 'variables': {}
} }
const captchaRequestAuthCode = { const captchaRequestAuthCode = code => ({
'operationName': 'captchaRequestAuthCode', 'operationName': 'captchaRequestAuthCode',
'query': `mutation captchaRequestAuthCode($input: CaptchaRequestAuthCodeInput!) { 'query': `mutation captchaRequestAuthCode($input: CaptchaRequestAuthCodeInput!) {
captchaRequestAuthCode(input: $input) { captchaRequestAuthCode(input: $input) {
errors { errors {
...ErrorFragment message
path
} }
success success
} }
}`, }`,
'variables': {} 'variables': { 'input': { 'phone': `${phone}`, 'challengeCode': `${code.challengeCode}`, 'secCode': `${code.challengeCode.slice(0, 5)}`, 'validationCode': `${code.challengeCode.slice(0, 5)}` } }
} })
return request(regularRequestAuthCode) return request(createCaptcha)
.then(r => { .then(r => {
console.log(r) const code = r.data.captchaCreateChallenge.result
return r if (code) {
// captchaRequestAuthCode parameters have to be processed by geetest
return request(captchaRequestAuthCode(code))
}
})
.catch(err => {
throw new Error(err)
}) })
} }
@ -121,43 +172,66 @@ function isLightning (address) {
return address.substr(0, 2) === 'ln' return address.substr(0, 2) === 'ln'
} }
function sendCoins (account, tx, settings, operatorId) { function sendFundsOnChain (walletId, address, cryptoAtoms, token) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
// Is walletId a mandatory field?
const sendOnChain = { const sendOnChain = {
'operationName': 'onChainPaymentSend', 'operationName': 'onChainPaymentSend',
'query': `mutation onChainPaymentSend($input: OnChainPaymentSendInput!) { 'query': `mutation onChainPaymentSend($input: OnChainPaymentSendInput!) {
onChainPaymentSend(input: $input) { onChainPaymentSend(input: $input) {
errors { errors {
...ErrorFragment message
path
} }
status status
} }
}`, }`,
'variables': { 'input': { 'address': `${toAddress}`, 'amount': `${cryptoAtoms}` } } 'variables': { 'input': { 'address': `${address}`, 'amount': `${cryptoAtoms}`, 'walletId': `${walletId}` } }
} }
return request(sendOnChain, token)
.then(result => {
return result.data.onChainPaymentSend
})
.catch(err => {
throw err
})
}
function sendFundsLN (walletId, invoice, token) {
const sendLN = { const sendLN = {
'operationName': 'lnInvoicePaymentSend', 'operationName': 'lnInvoicePaymentSend',
'query': `mutation lnInvoicePaymentSend($input: LnInvoicePaymentInput!) { 'query': `mutation lnInvoicePaymentSend($input: LnInvoicePaymentInput!) {
lnInvoicePaymentSend(input: $input) { lnInvoicePaymentSend(input: $input) {
errors { errors {
...ErrorFragment message
path
} }
status status
} }
}`, }`,
'variables': { 'input': { 'paymentRequest': `${toAddress}` } } 'variables': { 'input': { 'paymentRequest': `${invoice}`, 'walletId': `${walletId}` } }
} }
return request(sendLN, token)
.then(result => {
return result.data.onChainPaymentSend
})
.catch(err => {
throw err
})
}
function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetchAuthToken({ phone: PHONE })) // .then(() => fetchAuthToken({ phone: PHONE }))
.then(authToken => { .then(authToken => Promise.all([getGaloyAccount(authToken), authToken]))
if (isLightning) { .then(([account, authToken]) => {
return request(sendLN, authToken) const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets))
if (isLightning(toAddress)) {
return sendFundsLN(wallet.id, toAddress, authToken)
} }
return request(sendOnChain, authToken) return sendFundsOnChain(wallet.id, toAddress, cryptoAtoms, authToken)
}) })
.then(result => { .then(result => {
return result.data return result
}) })
.catch(err => { .catch(err => {
throw err throw err
@ -171,7 +245,8 @@ function newOnChainAddress (walletId, token) {
onChainAddressCreate(input: $input) { onChainAddressCreate(input: $input) {
address address
errors { errors {
...ErrorFragment message
path
} }
} }
}`, }`,
@ -192,10 +267,11 @@ function newInvoice (walletId, cryptoAtoms, token) {
'query': `mutation lnInvoiceCreate($input: LnInvoiceCreateInput!) { 'query': `mutation lnInvoiceCreate($input: LnInvoiceCreateInput!) {
lnInvoiceCreate(input: $input) { lnInvoiceCreate(input: $input) {
errors { errors {
...ErrorFragment message
path
} }
invoice { invoice {
...LnInvoiceFragment paymentRequest
} }
} }
}`, }`,
@ -212,12 +288,14 @@ function newInvoice (walletId, cryptoAtoms, token) {
function balance (account, cryptoCode, settings, operatorId) { function balance (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetchAuthToken({ phone: PHONE })) // .then(() => fetchAuthToken({ phone: PHONE }))
.then(authToken => getGaloyAccount(authToken)) .then(authToken => getGaloyAccount(authToken))
.then(account => { .then(account => {
console.log(account)
// account has a list of wallets, should we consider the balance of each one? // 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 // for now we'll pick the first BTC wallet that matches the defaultWalletId
const wallet = _.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets) const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets))
console.log(wallet)
return new BN(wallet.balance || 0) return new BN(wallet.balance || 0)
}) })
.catch(err => { .catch(err => {
@ -227,11 +305,11 @@ function balance (account, cryptoCode, settings, operatorId) {
function newAddress (account, info, tx, settings, operatorId) { function newAddress (account, info, tx, settings, operatorId) {
const { cryptoAtoms, cryptoCode } = tx const { cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(info.cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetchAuthToken({ phone: PHONE })) // .then(() => fetchAuthToken({ phone: PHONE }))
.then(authToken => [getGaloyAccount(authToken), authToken]) .then(authToken => Promise.all([getGaloyAccount(authToken), authToken]))
.then(([account, authToken]) => { .then(([account, authToken]) => {
const wallet = _.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets) const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets))
const promises = [ const promises = [
newOnChainAddress(wallet.id, authToken), newOnChainAddress(wallet.id, authToken),
newInvoice(wallet.id, cryptoAtoms, authToken) newInvoice(wallet.id, cryptoAtoms, authToken)
@ -251,7 +329,7 @@ function getStatus (account, tx, requested, settings, operatorId) {
function newFunding (account, cryptoCode, settings, operatorId) { function newFunding (account, cryptoCode, settings, operatorId) {
// Has to be a regular BTC address // Has to be a regular BTC address
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetchAuthToken({ phone: PHONE })) // .then(() => fetchAuthToken({ phone: PHONE }))
.then(authToken => [getGaloyAccount(authToken), authToken]) .then(authToken => [getGaloyAccount(authToken), authToken])
.then(([account, authToken]) => { .then(([account, authToken]) => {
const wallet = _.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets) const wallet = _.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets)
@ -277,11 +355,22 @@ function checkBlockchainStatus (cryptoCode) {
.then(() => Promise.resolve('ready')) .then(() => Promise.resolve('ready'))
} }
sendCoins({}, { // sendCoins({}, {
toAddress: 'lnbc10n1p3z7tpkpp5fjkptx4xtnh5n5vyrrhrw25f86mv0rwyu9a8tm3lrwnkl4u58zqdqg23jhxap3cqzpgxqyz5vqsp5g239kch2r7q9dty8rgs2h94h0d6tp8ssws9zte9qzvss2fr729zs9qyyssq86khhqz86dhftteqd0ymad32dfrdfdmdac8jw359wn4s0fx5gfyrectl4e49pt38gculfk3xeljv5ygd8ddry0m0z38lqt23j8aytycqrd4med', // toAddress: 'tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt',
cryptoCode: 'LN', // cryptoCode: 'BTC',
cryptoAtoms: 123123 // 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)
module.exports = { module.exports = {
NAME, NAME,