Skip to content

Commit

Permalink
Add utility to add default EdgeMemo to spend info
Browse files Browse the repository at this point in the history
  • Loading branch information
peachbits committed Sep 15, 2023
1 parent 85911ca commit e6a5c0d
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 37 deletions.
9 changes: 6 additions & 3 deletions src/swap/changehero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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: {
Expand Down
10 changes: 5 additions & 5 deletions src/swap/changenow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/swap/defi/lifi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
fetchWaterfall,
getAddress,
hexToDecimal,
makeEdgeMemos,
makeQueryParams,
promiseWithTimeout
} from '../../util/utils'
Expand Down Expand Up @@ -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
Expand All @@ -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%
Expand Down
6 changes: 3 additions & 3 deletions src/swap/defi/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
fetchInfo,
fetchWaterfall,
getAddress,
makeEdgeMemos,
makeQueryParams,
promiseWithTimeout,
QueryParams
Expand Down Expand Up @@ -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'
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/swap/defi/thorchainDa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
fetchInfo,
fetchWaterfall,
getAddress,
makeEdgeMemos,
makeQueryParams,
promiseWithTimeout
} from '../../util/utils'
Expand Down Expand Up @@ -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'
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/swap/defi/uni-v2-based/plugins/spookySwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/swap/defi/uni-v2-based/plugins/tombSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/swap/defi/uni-v2-based/plugins/velodrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions src/swap/exolix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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: {
Expand Down
9 changes: 6 additions & 3 deletions src/swap/godex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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: {
Expand Down
9 changes: 6 additions & 3 deletions src/swap/letsexchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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: {
Expand Down
6 changes: 3 additions & 3 deletions src/swap/sideshift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand Down
6 changes: 3 additions & 3 deletions src/swap/swapuz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions src/util/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { add } from 'biggystring'
import {
EdgeCurrencyInfo,
EdgeCurrencyWallet,
EdgeFetchFunction,
EdgeFetchResponse,
EdgeMemo,
EdgeSwapRequest
} from 'edge-core-js'

Expand Down Expand Up @@ -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
}
]
}

0 comments on commit e6a5c0d

Please sign in to comment.