From 85911cad307db5ce95e25c2ab3189637ea5ac6f9 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 14 Sep 2023 14:51:14 -0700 Subject: [PATCH 1/2] Upgrade edge-core-js to v1.7.0 --- package.json | 2 +- test/fakeCurrencyPlugin.ts | 1 + yarn.lock | 18 +++++++++--------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 529b1466..02b13326 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "buffer": "^6.0.3", "chai": "^4.2.0", "chai-as-promised": "^7.1.1", - "edge-core-js": "^1.4.0", + "edge-core-js": "^1.7.0", "eslint": "^7.14.0", "eslint-config-standard-kit": "0.15.1", "eslint-plugin-flowtype": "^5.2.0", diff --git a/test/fakeCurrencyPlugin.ts b/test/fakeCurrencyPlugin.ts index e49a343b..6c8bec60 100644 --- a/test/fakeCurrencyPlugin.ts +++ b/test/fakeCurrencyPlugin.ts @@ -312,6 +312,7 @@ class FakeCurrencyEngine { currencyCode, date: defaultTx.date, isSend: true, + memos: [], nativeAmount: total, networkFee: tokenSpend ? '0' : this.defaultSettings.networkFee, parentNetworkFee: tokenSpend diff --git a/yarn.lock b/yarn.lock index e0fc043f..8e424073 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2684,10 +2684,10 @@ duplexer3@^0.1.4: resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2" integrity sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI= -edge-core-js@^1.4.0: - version "1.4.0" - resolved "https://registry.yarnpkg.com/edge-core-js/-/edge-core-js-1.4.0.tgz#6e11c27cd6917d109ebf3600a0bc10afddb45508" - integrity sha512-xemar4vYRSJPwTFuA+MPJZw+gSPBg8Larhcw2/PbUDhQMnwHct6UVoxhn1YsVZK3vj7HnBEDeZC98nttQNdQXw== +edge-core-js@^1.7.0: + version "1.7.0" + resolved "https://registry.yarnpkg.com/edge-core-js/-/edge-core-js-1.7.0.tgz#c728cc8b904193bf5fd32b6003672bcd0757cc5f" + integrity sha512-t88JaqM0X030/X2KGeJuIAqRlSfxI83Hg5+0V1q4bfaVXHRpmbdaiiWXliUteB7mlMePu3uuslzRVmAgGOpwdQ== dependencies: aes-js "^3.1.0" base-x "^4.0.0" @@ -2695,7 +2695,7 @@ edge-core-js@^1.4.0: cleaners "^0.3.14" currency-codes "^1.1.2" disklet "^0.5.2" - edge-sync-client "^0.2.7" + edge-sync-client "^0.2.8" elliptic "^6.4.0" hash.js "^1.1.7" hmac-drbg "^1.0.1" @@ -2709,10 +2709,10 @@ edge-core-js@^1.4.0: yaob "^0.3.11" yavent "^0.1.3" -edge-sync-client@^0.2.7: - version "0.2.7" - resolved "https://registry.yarnpkg.com/edge-sync-client/-/edge-sync-client-0.2.7.tgz#57041f1a17565f24750803a99aaea517361fe9e1" - integrity sha512-ySWvsvHisg3e5gV/KBQymt0Fos1Nqupb6fbvxn5YXM5SmsOWz2iGRqocSc/Qxyd85WV7G/DjpJ8zWVqf0eUTrg== +edge-sync-client@^0.2.8: + version "0.2.8" + resolved "https://registry.yarnpkg.com/edge-sync-client/-/edge-sync-client-0.2.8.tgz#150c9d97676b5ddd158efd38565de1be29e525bc" + integrity sha512-5iaVYvws4fEk9gSkijo98nh46wyT2D0YSAYzwAyM4v/oHu+tVa18WrdGC+aP4HZVcrsg/LzrLu/NX9JyTsW9Cw== dependencies: base-x "^1.0.4" cleaners "^0.3.9" From e6a5c0d730dd0224ae3808e3e1389e456d3a781f Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 15 Sep 2023 14:45:50 -0700 Subject: [PATCH 2/2] Add utility to add default EdgeMemo to spend info --- src/swap/changehero.ts | 9 ++++-- src/swap/changenow.ts | 10 +++---- src/swap/defi/lifi.ts | 5 ++-- src/swap/defi/thorchain.ts | 6 ++-- src/swap/defi/thorchainDa.ts | 6 ++-- .../defi/uni-v2-based/plugins/spookySwap.ts | 4 +-- .../defi/uni-v2-based/plugins/tombSwap.ts | 4 +-- .../defi/uni-v2-based/plugins/velodrome.ts | 4 +-- src/swap/exolix.ts | 9 ++++-- src/swap/godex.ts | 9 ++++-- src/swap/letsexchange.ts | 9 ++++-- src/swap/sideshift.ts | 6 ++-- src/swap/swapuz.ts | 6 ++-- src/util/utils.ts | 29 +++++++++++++++++++ 14 files changed, 79 insertions(+), 37 deletions(-) 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 + } + ] +}