diff --git a/src/swap/changehero.ts b/src/swap/changehero.ts index 909f6350..c710794a 100644 --- a/src/swap/changehero.ts +++ b/src/swap/changehero.ts @@ -27,7 +27,7 @@ import { makeSwapPluginQuote, SwapOrder } from '../swap-helpers' -import { convertRequest, getAddress } from '../util/utils' +import { convertRequest, getAddress, makeEdgeMemos } from '../util/utils' import { EdgeSwapRequestPlugin } from './types' const pluginId = 'changehero' @@ -255,10 +255,13 @@ export function makeChangeHeroPlugin( spendTargets: [ { nativeAmount: amountExpectedFromNative, - publicAddress: quoteInfo.payinAddress, - uniqueIdentifier: quoteInfo.payinExtraId ?? undefined + publicAddress: quoteInfo.payinAddress } ], + memos: makeEdgeMemos( + request.fromWallet.currencyInfo, + quoteInfo.payinExtraId + ), networkFeeOption: request.fromCurrencyCode.toUpperCase() === 'BTC' ? 'high' : 'standard', swapData: { diff --git a/src/swap/changenow.ts b/src/swap/changenow.ts index e6484db6..6fa0759a 100644 --- a/src/swap/changenow.ts +++ b/src/swap/changenow.ts @@ -29,7 +29,7 @@ import { makeSwapPluginQuote, SwapOrder } from '../swap-helpers' -import { convertRequest, getAddress } from '../util/utils' +import { convertRequest, getAddress, makeEdgeMemos } from '../util/utils' import { EdgeSwapRequestPlugin } from './types' const pluginId = 'changenow' @@ -200,10 +200,10 @@ export function makeChangeNowPlugin( spendTargets: [ { nativeAmount, - publicAddress: payinAddress, - uniqueIdentifier: payinExtraId + publicAddress: payinAddress } ], + memos: makeEdgeMemos(request.fromWallet.currencyInfo, payinExtraId), networkFeeOption: fromCurrencyCode === 'BTC' ? 'high' : 'standard', swapData: { orderId: id, @@ -255,10 +255,10 @@ export function makeChangeNowPlugin( spendTargets: [ { nativeAmount: fromNativeAmount, - publicAddress: payinAddress, - uniqueIdentifier: payinExtraId + publicAddress: payinAddress } ], + memos: makeEdgeMemos(request.fromWallet.currencyInfo, payinExtraId), networkFeeOption: fromCurrencyCode === 'BTC' ? 'high' : 'standard', swapData: { orderId: id, diff --git a/src/swap/defi/lifi.ts b/src/swap/defi/lifi.ts index 675b5422..d73ee97a 100644 --- a/src/swap/defi/lifi.ts +++ b/src/swap/defi/lifi.ts @@ -25,6 +25,7 @@ import { fetchWaterfall, getAddress, hexToDecimal, + makeEdgeMemos, makeQueryParams, promiseWithTimeout } from '../../util/utils' @@ -311,11 +312,11 @@ export function makeLifiPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin { currencyCode: request.fromCurrencyCode, spendTargets: [ { - memo: approvalData, nativeAmount: '0', publicAddress: fromContractAddress } ], + memos: makeEdgeMemos(request.fromWallet.currencyInfo, approvalData), networkFeeOption: 'custom', customNetworkFee: { gasPrice: gasPriceGwei @@ -332,11 +333,11 @@ export function makeLifiPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin { currencyCode: request.fromCurrencyCode, spendTargets: [ { - memo: data, nativeAmount: mul(transactionRequest.value, '1'), publicAddress: approvalAddress } ], + memos: makeEdgeMemos(request.fromWallet.currencyInfo, data), networkFeeOption: 'custom', customNetworkFee: { // XXX Hack. Lifi doesn't properly estimate ethereum gas limits. Increase by 40% diff --git a/src/swap/defi/thorchain.ts b/src/swap/defi/thorchain.ts index 9970764b..1bafc936 100644 --- a/src/swap/defi/thorchain.ts +++ b/src/swap/defi/thorchain.ts @@ -35,6 +35,7 @@ import { fetchInfo, fetchWaterfall, getAddress, + makeEdgeMemos, makeQueryParams, promiseWithTimeout, QueryParams @@ -564,11 +565,11 @@ export function makeThorchainPlugin( currencyCode: request.fromCurrencyCode, spendTargets: [ { - memo: approvalData, nativeAmount: '0', publicAddress: sourceTokenContractAddress } ], + memos: makeEdgeMemos(request.fromWallet.currencyInfo, approvalData), metadata: { name: 'Thorchain', category: 'expense:Token Approval' @@ -581,12 +582,11 @@ export function makeThorchainPlugin( currencyCode: request.fromCurrencyCode, spendTargets: [ { - memo, nativeAmount: ethNativeAmount, publicAddress } ], - + memos: makeEdgeMemos(request.fromWallet.currencyInfo, memo), swapData: { isEstimate: false, payoutAddress: toAddress, diff --git a/src/swap/defi/thorchainDa.ts b/src/swap/defi/thorchainDa.ts index 90fe541f..acd7a8d8 100644 --- a/src/swap/defi/thorchainDa.ts +++ b/src/swap/defi/thorchainDa.ts @@ -34,6 +34,7 @@ import { fetchInfo, fetchWaterfall, getAddress, + makeEdgeMemos, makeQueryParams, promiseWithTimeout } from '../../util/utils' @@ -409,11 +410,11 @@ export function makeThorchainDaPlugin( currencyCode: fromMainnetCode, spendTargets: [ { - memo: approvalData, nativeAmount: '0', publicAddress: sourceTokenContractAddress } ], + memos: makeEdgeMemos(request.fromWallet.currencyInfo, approvalData), metadata: { name: 'Thorchain DEX Aggregator', category: 'expense:Token Approval' @@ -426,12 +427,11 @@ export function makeThorchainDaPlugin( currencyCode: request.fromCurrencyCode, spendTargets: [ { - memo, nativeAmount: ethNativeAmount, publicAddress } ], - + memos: makeEdgeMemos(request.fromWallet.currencyInfo, memo), swapData: { isEstimate, payoutAddress: toAddress, diff --git a/src/swap/defi/uni-v2-based/plugins/spookySwap.ts b/src/swap/defi/uni-v2-based/plugins/spookySwap.ts index 90032848..15b7138f 100644 --- a/src/swap/defi/uni-v2-based/plugins/spookySwap.ts +++ b/src/swap/defi/uni-v2-based/plugins/spookySwap.ts @@ -16,7 +16,7 @@ import { makeSwapPluginQuote, SwapOrder } from '../../../../swap-helpers' -import { convertRequest } from '../../../../util/utils' +import { convertRequest, makeEdgeMemos } from '../../../../util/utils' import { EdgeSwapRequestPlugin } from '../../../types' import { getInOutTokenAddresses, InOutTokenAddresses } from '../../defiUtils' import { @@ -113,7 +113,6 @@ export function makeSpookySwapPlugin( currencyCode: request.fromCurrencyCode, // what is being sent out, only if token. Blank if not token spendTargets: [ { - memo: swapTx.data, nativeAmount: swapTx.value != null ? swapTx.value.toString() : '0', // biggy/number string integer publicAddress: swapTx.to } @@ -125,6 +124,7 @@ export function makeSpookySwapPlugin( : '0', gasLimit: swapTx.gasLimit?.toString() ?? '0' }, + memos: makeEdgeMemos(request.fromWallet.currencyInfo, swapTx.data), networkFeeOption: 'custom', swapData: { isEstimate: false, diff --git a/src/swap/defi/uni-v2-based/plugins/tombSwap.ts b/src/swap/defi/uni-v2-based/plugins/tombSwap.ts index a9b0b8ce..ac3a94b9 100644 --- a/src/swap/defi/uni-v2-based/plugins/tombSwap.ts +++ b/src/swap/defi/uni-v2-based/plugins/tombSwap.ts @@ -16,7 +16,7 @@ import { makeSwapPluginQuote, SwapOrder } from '../../../../swap-helpers' -import { convertRequest } from '../../../../util/utils' +import { convertRequest, makeEdgeMemos } from '../../../../util/utils' import { EdgeSwapRequestPlugin } from '../../../types' import { getInOutTokenAddresses, InOutTokenAddresses } from '../../defiUtils' import { @@ -113,7 +113,6 @@ export function makeTombSwapPlugin( currencyCode: request.fromCurrencyCode, // what is being sent out, only if token. Blank if not token spendTargets: [ { - memo: swapTx.data, nativeAmount: swapTx.value != null ? swapTx.value.toString() : '0', // biggy/number string integer publicAddress: swapTx.to } @@ -125,6 +124,7 @@ export function makeTombSwapPlugin( : '0', gasLimit: swapTx.gasLimit?.toString() ?? '0' }, + memos: makeEdgeMemos(request.fromWallet.currencyInfo, swapTx.data), networkFeeOption: 'custom', swapData: { isEstimate: false, diff --git a/src/swap/defi/uni-v2-based/plugins/velodrome.ts b/src/swap/defi/uni-v2-based/plugins/velodrome.ts index 7e6b7164..2290e943 100644 --- a/src/swap/defi/uni-v2-based/plugins/velodrome.ts +++ b/src/swap/defi/uni-v2-based/plugins/velodrome.ts @@ -16,7 +16,7 @@ import { makeSwapPluginQuote, SwapOrder } from '../../../../swap-helpers' -import { convertRequest } from '../../../../util/utils' +import { convertRequest, makeEdgeMemos } from '../../../../util/utils' import { EdgeSwapRequestPlugin } from '../../../types' import VELODROME_V1_ROUTER_ABI from '../../abi/VELODROME_V1_ROUTER_ABI' import WRAPPED_OPTIMISM_ETH_ABI from '../../abi/WRAPPED_OPTIMISM_ETH_ABI' @@ -128,7 +128,6 @@ export function makeVelodromePlugin( currencyCode: request.fromCurrencyCode, // what is being sent out, only if token. Blank if not token spendTargets: [ { - memo: swapTx.data, nativeAmount: swapTx.value != null ? swapTx.value.toString() : '0', // biggy/number string integer publicAddress: swapTx.to } @@ -140,6 +139,7 @@ export function makeVelodromePlugin( : '0', gasLimit: swapTx.gasLimit?.toString() ?? '0' }, + memos: makeEdgeMemos(request.fromWallet.currencyInfo, swapTx.data), networkFeeOption: 'custom', swapData: { isEstimate: false, diff --git a/src/swap/exolix.ts b/src/swap/exolix.ts index b8441935..07dd320a 100644 --- a/src/swap/exolix.ts +++ b/src/swap/exolix.ts @@ -26,7 +26,7 @@ import { makeSwapPluginQuote, SwapOrder } from '../swap-helpers' -import { convertRequest, getAddress } from '../util/utils' +import { convertRequest, getAddress, makeEdgeMemos } from '../util/utils' import { EdgeSwapRequestPlugin } from './types' const pluginId = 'exolix' @@ -239,10 +239,13 @@ export function makeExolixPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin { spendTargets: [ { nativeAmount: fromNativeAmount, - publicAddress: quoteInfo.depositAddress, - uniqueIdentifier: quoteInfo.depositExtraId + publicAddress: quoteInfo.depositAddress } ], + memos: makeEdgeMemos( + request.fromWallet.currencyInfo, + quoteInfo.depositExtraId + ), networkFeeOption: request.fromCurrencyCode.toUpperCase() === 'BTC' ? 'high' : 'standard', swapData: { diff --git a/src/swap/godex.ts b/src/swap/godex.ts index a745e000..af7382f1 100644 --- a/src/swap/godex.ts +++ b/src/swap/godex.ts @@ -28,7 +28,7 @@ import { makeSwapPluginQuote, SwapOrder } from '../swap-helpers' -import { convertRequest, getAddress } from '../util/utils' +import { convertRequest, getAddress, makeEdgeMemos } from '../util/utils' import { asNumberString, EdgeSwapRequestPlugin } from './types' const pluginId = 'godex' @@ -267,10 +267,13 @@ export function makeGodexPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin { spendTargets: [ { nativeAmount: fromNativeAmount, - publicAddress: quoteInfo.deposit, - uniqueIdentifier: quoteInfo.deposit_extra_id ?? undefined + publicAddress: quoteInfo.deposit } ], + memos: makeEdgeMemos( + request.fromWallet.currencyInfo, + quoteInfo.deposit_extra_id + ), networkFeeOption: request.fromCurrencyCode.toUpperCase() === 'BTC' ? 'high' : 'standard', swapData: { diff --git a/src/swap/letsexchange.ts b/src/swap/letsexchange.ts index bacc2ecd..6cf36105 100644 --- a/src/swap/letsexchange.ts +++ b/src/swap/letsexchange.ts @@ -20,7 +20,7 @@ import { makeSwapPluginQuote, SwapOrder } from '../swap-helpers' -import { convertRequest, getAddress } from '../util/utils' +import { convertRequest, getAddress, makeEdgeMemos } from '../util/utils' import { asOptionalBlank } from './changenow' import { asNumberString, EdgeSwapRequestPlugin } from './types' @@ -276,10 +276,13 @@ export function makeLetsExchangePlugin( spendTargets: [ { nativeAmount: fromNativeAmount, - publicAddress: quoteInfo.deposit, - uniqueIdentifier: quoteInfo.deposit_extra_id + publicAddress: quoteInfo.deposit } ], + memos: makeEdgeMemos( + request.fromWallet.currencyInfo, + quoteInfo.deposit_extra_id + ), networkFeeOption: request.fromCurrencyCode.toUpperCase() === 'BTC' ? 'high' : 'standard', swapData: { diff --git a/src/swap/sideshift.ts b/src/swap/sideshift.ts index d05f5d80..80c2af14 100644 --- a/src/swap/sideshift.ts +++ b/src/swap/sideshift.ts @@ -21,7 +21,7 @@ import { makeSwapPluginQuote, SwapOrder } from '../swap-helpers' -import { convertRequest, getAddress } from '../util/utils' +import { convertRequest, getAddress, makeEdgeMemos } from '../util/utils' import { EdgeSwapRequestPlugin } from './types' // See https://help.sideshift.ai/en/articles/4559664-which-coins-and-tokens-are-listed for list of supported currencies @@ -218,10 +218,10 @@ const fetchSwapQuoteInner = async ( spendTargets: [ { nativeAmount: amountExpectedFromNative, - publicAddress: order.depositAddress, - uniqueIdentifier: order.depositMemo + publicAddress: order.depositAddress } ], + memos: makeEdgeMemos(request.fromWallet.currencyInfo, order.depositMemo), networkFeeOption: request.fromCurrencyCode.toUpperCase() === 'BTC' ? 'high' : 'standard', swapData: { diff --git a/src/swap/swapuz.ts b/src/swap/swapuz.ts index 6cd186a8..b8417369 100644 --- a/src/swap/swapuz.ts +++ b/src/swap/swapuz.ts @@ -29,7 +29,7 @@ import { SwapOrder } from '../swap-helpers' import { div18 } from '../util/biggystringplus' -import { convertRequest, getAddress } from '../util/utils' +import { convertRequest, getAddress, makeEdgeMemos } from '../util/utils' import { EdgeSwapRequestPlugin } from './types' const pluginId = 'swapuz' @@ -181,10 +181,10 @@ export function makeSwapuzPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin { spendTargets: [ { nativeAmount: request.nativeAmount, - publicAddress: addressFrom, - uniqueIdentifier: memoFrom + publicAddress: addressFrom } ], + memos: makeEdgeMemos(fromWallet.currencyInfo, memoFrom), networkFeeOption: fromCurrencyCode === 'BTC' ? 'high' : 'standard', swapData: { orderId: uid, diff --git a/src/util/utils.ts b/src/util/utils.ts index 2c3d1a48..5a3d4b6b 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -1,8 +1,10 @@ import { add } from 'biggystring' import { + EdgeCurrencyInfo, EdgeCurrencyWallet, EdgeFetchFunction, EdgeFetchResponse, + EdgeMemo, EdgeSwapRequest } from 'edge-core-js' @@ -186,3 +188,30 @@ export const hexToDecimal = (hex: string): string => { return add(`0x${hex}`, '0') } } + +/** + * Helper utility to create the spend info memos array. It assumes the first + * memo type is the appropriate choice for unique identifiers and data blobs + */ + +export const makeEdgeMemos = ( + currencyInfo: EdgeCurrencyInfo, + text: string | undefined | null +): EdgeMemo[] => { + if (text == null) return [] + + const defaultMemoType = currencyInfo.memoOptions?.[0] + if (defaultMemoType == null) { + throw new Error('Wallet does not support memos') + } + + const { type, hidden, memoName } = defaultMemoType + return [ + { + type, + value: text, + hidden, + memoName + } + ] +}