Skip to content

Commit

Permalink
Merge pull request #1659 from RafaelTaranto/chore/merge-9-into-10-202…
Browse files Browse the repository at this point in the history
…40314

Chore/merge 9 into 10 20240314
  • Loading branch information
RafaelTaranto authored Mar 14, 2024
2 parents afa3632 + 4012816 commit ef24cb2
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 605 deletions.
2 changes: 1 addition & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const logger = new winston.Logger({
})
],
rewriters: [
(...[,, meta]) => meta instanceof Error ? { message: meta.message, stack: meta.stack } : meta
(...[,, meta]) => meta instanceof Error ? { message: meta.message, stack: meta.stack, meta } : meta
],
exitOnError: false
})
Expand Down
4 changes: 3 additions & 1 deletion lib/new-admin/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ const mapLanguage = lang => {
}

const massageCryptos = cryptos => {
const betaList = ['LN']
const convert = crypto => ({
code: crypto['cryptoCode'],
display: crypto['display'],
codeDisplay: crypto['cryptoCodeDisplay'] ?? crypto['cryptoCode']
codeDisplay: crypto['cryptoCodeDisplay'] ?? crypto['cryptoCode'],
isBeta: betaList.includes(crypto.cryptoCode)
})

return _.map(convert, cryptos)
Expand Down
1 change: 1 addition & 0 deletions lib/new-admin/graphql/types/currency.type.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const typeDef = gql`
code: String!
display: String!
codeDisplay: String!
isBeta: Boolean
}
type Query {
Expand Down
114 changes: 74 additions & 40 deletions lib/plugins/wallet/galoy/galoy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const axios = require('axios')
const { utils: coinUtils } = require('@lamassu/coins')

const NAME = 'LN'
const SUPPORTED_COINS = ['LN', 'BTC']
const SUPPORTED_COINS = ['LN']

const BN = require('../../../bn')

function request (graphqlQuery, token, endpoint) {
const headers = {
'content-type': 'application/json',
'Authorization': `Bearer ${token}`
'X-API-KEY': token
}
return axios({
method: 'post',
Expand Down Expand Up @@ -38,12 +38,11 @@ function checkCryptoCode (cryptoCode) {
function getTransactionsByAddress (token, endpoint, walletId, address) {
const accountInfo = {
'operationName': 'me',
'query': `query me {
'query': `query me($walletId: WalletId!, , $address: OnChainAddress!) {
me {
defaultAccount {
wallets {
id
transactionsByAddress (address: "${address}") {
walletById(walletId: $walletId) {
transactionsByAddress (address: $address) {
edges {
node {
direction
Expand All @@ -56,11 +55,11 @@ function getTransactionsByAddress (token, endpoint, walletId, address) {
}
}
}`,
'variables': {}
'variables': { walletId, address }
}
return request(accountInfo, token, endpoint)
.then(r => {
return _.find(it => it.id === walletId, r.data.me.defaultAccount.wallets).transactionsByAddress
return r.data.me.defaultAccount.walletById.transactionsByAddress
})
.catch(err => {
throw new Error(err)
Expand All @@ -70,30 +69,34 @@ function getTransactionsByAddress (token, endpoint, walletId, address) {
function getGaloyWallet (token, endpoint, walletId) {
const accountInfo = {
'operationName': 'me',
'query': `query me {
'query': `query me($walletId: WalletId!) {
me {
defaultAccount {
wallets {
walletById(walletId: $walletId) {
id
walletCurrency
balance
}
}
}
}`,
'variables': {}
'variables': { walletId }
}
return request(accountInfo, token, endpoint)
.then(r => {
return _.find(it => it.id === walletId, r.data.me.defaultAccount.wallets)
return r.data.me.defaultAccount.walletById
})
.catch(err => {
throw new Error(err)
})
}

function isLightning (address) {
return address.substr(0, 2) === 'ln'
function isLnInvoice (address) {
return address.toLowerCase().startsWith('lnbc')
}

function isLnurl (address) {
return address.toLowerCase().startsWith('lnurl')
}

function sendFundsOnChain (walletId, address, cryptoAtoms, token, endpoint) {
Expand All @@ -108,14 +111,31 @@ function sendFundsOnChain (walletId, address, cryptoAtoms, token, endpoint) {
status
}
}`,
'variables': { 'input': { 'address': `${address}`, 'amount': `${cryptoAtoms}`, 'walletId': `${walletId}` } }
'variables': { 'input': { address, amount: cryptoAtoms.toString(), walletId } }
}
return request(sendOnChain, token, endpoint)
.then(result => {
return result.data.onChainPaymentSend
})
}

function sendFundsLNURL (walletId, lnurl, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
'operationName': 'lnurlPaymentSend',
'query': `mutation lnurlPaymentSend($input: LnurlPaymentSendInput!) {
lnurlPaymentSend(input: $input) {
errors {
message
path
}
status
}
}`,
'variables': { 'input': { 'lnurl': `${lnurl}`, 'walletId': `${walletId}`, 'amount': `${cryptoAtoms}` } }
}
return request(sendLnNoAmount, token, endpoint).then(result => result.data.lnurlPaymentSend)
}

function sendFundsLN (walletId, invoice, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
'operationName': 'lnNoAmountInvoicePaymentSend',
Expand All @@ -128,7 +148,7 @@ function sendFundsLN (walletId, invoice, cryptoAtoms, token, endpoint) {
status
}
}`,
'variables': { 'input': { 'paymentRequest': `${invoice}`, 'walletId': `${walletId}`, 'amount': `${cryptoAtoms}` } }
'variables': { 'input': { 'paymentRequest': invoice, walletId, amount: cryptoAtoms.toString() } }
}
return request(sendLnNoAmount, token, endpoint).then(result => result.data.lnNoAmountInvoicePaymentSend)
}
Expand All @@ -145,20 +165,22 @@ function sendProbeRequest (walletId, invoice, cryptoAtoms, token, endpoint) {
}
}
}`,
'variables': { 'input': { 'paymentRequest': `${invoice}`, 'walletId': `${walletId}`, 'amount': `${cryptoAtoms}` } }
'variables': { 'input': { paymentRequest: invoice, walletId, amount: cryptoAtoms.toString() } }
}
return request(sendProbeNoAmount, token, endpoint).then(result => result.data.lnNoAmountInvoiceFeeProbe)
}

function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))
.then(wallet => {
if (isLightning(toAddress)) {
return sendFundsLN(wallet.id, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
.then(() => {
if (isLnInvoice(toAddress)) {
return sendFundsLN(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
}
if (isLnurl(toAddress)) {
return sendFundsLNURL(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
}
return sendFundsOnChain(wallet.id, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
return sendFundsOnChain(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
})
.then(result => {
switch (result.status) {
Expand Down Expand Up @@ -198,14 +220,37 @@ function newOnChainAddress (walletId, token, endpoint) {
}
}
}`,
'variables': { 'input': { 'walletId': `${walletId}` } }
'variables': { 'input': { walletId } }
}
return request(createOnChainAddress, token, endpoint)
.then(result => {
return result.data.onChainAddressCreate.address
})
}

function newNoAmountInvoice (walletId, token, endpoint) {
const createInvoice = {
'operationName': 'lnNoAmountInvoiceCreate',
'query': `mutation lnNoAmountInvoiceCreate($input: LnNoAmountInvoiceCreateInput!) {
lnNoAmountInvoiceCreate(input: $input) {
errors {
message
path
}
invoice {
paymentRequest
}
}
}`,
'variables': { 'input': { walletId } }
}
return request(createInvoice, token, endpoint)
.then(result => {
return result.data.lnNoAmountInvoiceCreate.invoice.paymentRequest
})

}

function newInvoice (walletId, cryptoAtoms, token, endpoint) {
const createInvoice = {
'operationName': 'lnInvoiceCreate',
Expand All @@ -220,7 +265,7 @@ function newInvoice (walletId, cryptoAtoms, token, endpoint) {
}
}
}`,
'variables': { 'input': { 'walletId': `${walletId}`, 'amount': `${cryptoAtoms}` } }
'variables': { 'input': { walletId, amount: cryptoAtoms.toString() } }
}
return request(createInvoice, token, endpoint)
.then(result => {
Expand All @@ -239,17 +284,7 @@ function balance (account, cryptoCode, settings, operatorId) {
function newAddress (account, info, tx, settings, operatorId) {
const { cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))
.then(wallet => {
const promises = [
newOnChainAddress(wallet.id, account.apiSecret, account.endpoint),
newInvoice(wallet.id, cryptoAtoms, account.apiSecret, account.endpoint)
]
return Promise.all(promises)
})
.then(([onChainAddress, invoice]) => {
return `bitcoin:${onChainAddress}?amount=${cryptoAtoms}&lightning=${invoice}`
})
.then(() => newInvoice(account.walletId, cryptoAtoms, account.apiSecret, account.endpoint))
}

function getInvoiceStatus (token, endpoint, address) {
Expand All @@ -260,7 +295,7 @@ function getInvoiceStatus (token, endpoint, address) {
status
}
}`,
'variables': {"input": {"paymentRequest": address}}
'variables': { input: { paymentRequest: address } }
}
return request(query, token, endpoint)
.then(r => {
Expand All @@ -281,7 +316,7 @@ function getStatus (account, tx, requested, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => {
const address = coinUtils.parseUrl(cryptoCode, account.environment, toAddress, false)
if (isLightning(address)) {
if (isLnInvoice(address)) {
return getInvoiceStatus(account.apiSecret, account.endpoint, address)
.then(it => {
const isPaid = it === 'PAID'
Expand All @@ -292,8 +327,7 @@ function getStatus (account, tx, requested, settings, operatorId) {
// On-chain and intra-ledger transactions
return getTransactionsByAddress(account.apiSecret, account.endpoint, account.walletId, address)
.then(transactions => {
const txEdges = transactions.edges
const { SUCCESS: confirmed, PENDING: pending } = getBalance(txEdges)
const { SUCCESS: confirmed, PENDING: pending } = getBalance(transactions.edges)
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
Expand All @@ -307,7 +341,7 @@ function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))
.then(wallet => {
return newOnChainAddress(wallet.id, account.apiSecret, account.endpoint)
return newOnChainAddress(account.walletId, account.apiSecret, account.endpoint)
.then(onChainAddress => [onChainAddress, wallet.balance])
})
.then(([onChainAddress, balance]) => {
Expand Down
1 change: 1 addition & 0 deletions new-lamassu-admin/src/pages/Locales/Locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const GET_DATA = gql`
cryptoCurrencies {
code
display
isBeta
}
languages {
code
Expand Down
11 changes: 8 additions & 3 deletions new-lamassu-admin/src/pages/Locales/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const allFields = (getData, onChange, auxElements = []) => {
if (!data) return ''

return R.compose(
R.prop(code),
it => `${R.prop(code)(it)} ${it?.isBeta ? '(Beta)' : ''}`,
R.find(R.propEq(compare ?? 'code', it))
)(data)
}
Expand All @@ -36,7 +36,12 @@ const allFields = (getData, onChange, auxElements = []) => {
const countryData = getData(['countries'])
const currencyData = getData(['currencies'])
const languageData = getData(['languages'])
const cryptoData = getData(['cryptoCurrencies'])
const rawCryptoData = getData(['cryptoCurrencies'])
const cryptoData = rawCryptoData?.map(it => {
it.codeLabel = `${it.code}${it.isBeta ? ' (Beta)' : ''}`
return it
})

const timezonesData = timezoneList

const findSuggestion = it => {
Expand Down Expand Up @@ -104,7 +109,7 @@ const allFields = (getData, onChange, auxElements = []) => {
inputProps: {
options: cryptoData,
valueProp: 'code',
labelProp: 'code',
labelProp: 'codeLabel',
multiple: true,
optionsLimit: null,
onChange
Expand Down
1 change: 1 addition & 0 deletions new-lamassu-admin/src/pages/Wallet/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const GET_INFO = gql`
cryptoCurrencies {
code
display
isBeta
}
}
`
Expand Down
2 changes: 1 addition & 1 deletion new-lamassu-admin/src/pages/Wallet/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const getElements = (cryptoCurrencies, accounts, onChange, wizard = false) => {
const widthAdjust = wizard ? 11 : 0
const viewCryptoCurrency = it => {
const currencyDisplay = R.compose(
R.prop(['display']),
it => `${R.prop(['display'])(it)} ${it.isBeta ? '(Beta)' : ''}`,
R.find(R.propEq('code', it))
)(cryptoCurrencies)
return currencyDisplay
Expand Down
Loading

0 comments on commit ef24cb2

Please sign in to comment.