From ea47ef449da2c4c14cb55aec7578086dfc4674d7 Mon Sep 17 00:00:00 2001 From: Paul V Puey Date: Wed, 13 Sep 2023 12:50:09 -0700 Subject: [PATCH] Factor test to use fetchQuote function --- test/testpartner.ts | 222 +++++++++++++++++--------------------------- 1 file changed, 83 insertions(+), 139 deletions(-) diff --git a/test/testpartner.ts b/test/testpartner.ts index 4a8a2923..646425b1 100644 --- a/test/testpartner.ts +++ b/test/testpartner.ts @@ -8,6 +8,7 @@ import { } from 'cleaners' import { addEdgeCorePlugins, + EdgeCurrencyWallet, EdgeSwapQuote, EdgeSwapRequest, lockEdgeCorePlugins, @@ -40,6 +41,16 @@ const asUserDump = asObject({ data: asFakeUser }) +interface FetchQuoteParams { + fromWallet: EdgeCurrencyWallet + toWallet: EdgeCurrencyWallet + fromCurrencyCode: string + toCurrencyCode: string + quoteFor: 'from' | 'to' + exchangeAmount?: string + nativeAmount?: string +} + async function main(): Promise { const allPlugins = { bitcoin: makeFakePlugin(btcCurrencyInfo), @@ -70,7 +81,7 @@ async function main(): Promise { bitcoincash: true, ethereum: true, avalanche: true, - lifi: true, + // lifi: true, polygon: true, piratechain: true, thorchain: true @@ -97,205 +108,138 @@ async function main(): Promise { const arrrWallet = await account.waitForCurrencyWallet(arrrInfo?.id ?? '') const maticWallet = await account.waitForCurrencyWallet(maticInfo?.id ?? '') - // Test a FROM quote - const fromRequest: EdgeSwapRequest = { + const fetchQuote = async ({ + fromWallet, + toWallet, + fromCurrencyCode, + toCurrencyCode, + quoteFor, + exchangeAmount, + nativeAmount + }: FetchQuoteParams): Promise => { + console.log(`Request: ${fromCurrencyCode} to ${toCurrencyCode}`) + if (exchangeAmount != null) { + console.log(`Amount: ${quoteFor} ${exchangeAmount}`) + if (quoteFor === 'from') { + nativeAmount = await fromWallet.denominationToNative( + exchangeAmount, + fromCurrencyCode + ) + } else { + nativeAmount = await toWallet.denominationToNative( + exchangeAmount, + toCurrencyCode + ) + } + } else if (nativeAmount != null) { + console.log(`Request: ${fromCurrencyCode} to ${toCurrencyCode}`) + console.log(`Amount: ${quoteFor} nativeAmount: ${nativeAmount}`) + } else { + throw new Error('No nativeAmount or exchangeAmount') + } + + const request: EdgeSwapRequest = { + fromWallet, + toWallet, + fromCurrencyCode, + toCurrencyCode, + nativeAmount, + quoteFor + } + const quote = await account.fetchSwapQuote(request).catch(e => { + console.log(e) + console.log(e.message) + return null + }) + logQuote(quote) + console.log(`-----------------------------`) + return quote + } + + await fetchQuote({ fromWallet: ethWallet, fromCurrencyCode: 'UNI', toWallet: avaxWallet, toCurrencyCode: 'JOE', - nativeAmount: await ethWallet.denominationToNative('100', 'UNI'), + exchangeAmount: '100', quoteFor: 'from' - } - console.log(`fromRequest:`) - console.log( - JSON.stringify( - { ...fromRequest, fromWallet: undefined, toWallet: undefined }, - null, - 2 - ) - ) - console.log(`------------`) + }) - // Test a FROM ARRR to BTC quote - const fromRequest2: EdgeSwapRequest = { + await fetchQuote({ fromWallet: arrrWallet, fromCurrencyCode: 'ARRR', toWallet: btcWallet, toCurrencyCode: 'BTC', - nativeAmount: await arrrWallet.denominationToNative('109', 'ARRR'), + exchangeAmount: '109', quoteFor: 'from' - } - console.log(`fromRequest2:`) - console.log( - JSON.stringify( - { ...fromRequest2, fromWallet: undefined, toWallet: undefined }, - null, - 2 - ) - ) - console.log(`------------`) + }) - // Test a FROM BTC to ARRR quote - const fromRequest3: EdgeSwapRequest = { + await fetchQuote({ fromWallet: btcWallet, fromCurrencyCode: 'BTC', toWallet: arrrWallet, toCurrencyCode: 'ARRR', - nativeAmount: await btcWallet.denominationToNative('0.01', 'BTC'), + exchangeAmount: '0.01', quoteFor: 'from' - } - console.log(`fromRequest3:`) - console.log( - JSON.stringify( - { ...fromRequest3, fromWallet: undefined, toWallet: undefined }, - null, - 2 - ) - ) - console.log(`------------`) + }) // Test a TO quote - const toRequest: EdgeSwapRequest = { + await fetchQuote({ fromWallet: ethWallet, fromCurrencyCode: 'ETH', toWallet: btcWallet, toCurrencyCode: 'BTC', - nativeAmount: await btcWallet.denominationToNative('0.004', 'BTC'), + exchangeAmount: '0.004', quoteFor: 'to' - } - console.log(`toRequest:`) - console.log( - JSON.stringify( - { ...toRequest, fromWallet: undefined, toWallet: undefined }, - null, - 2 - ) - ) + }) - // Test a FROM quote polygon:USDC to ethereum:WBTC - const fromRequest4: EdgeSwapRequest = { + await fetchQuote({ fromWallet: maticWallet, fromCurrencyCode: 'USDC', toWallet: ethWallet, toCurrencyCode: 'WBTC', - nativeAmount: await maticWallet.denominationToNative('4000', 'USDC'), + exchangeAmount: '4000', quoteFor: 'from' - } - console.log(`fromRequest4:`) - console.log( - JSON.stringify( - { ...fromRequest4, fromWallet: undefined, toWallet: undefined }, - null, - 2 - ) - ) - console.log(`------------`) + }) - // Test a FROM quote bitcoincash to ethereum:WBTC - const fromRequest5: EdgeSwapRequest = { + const quote = await fetchQuote({ fromWallet: bchWallet, fromCurrencyCode: 'BCH', toWallet: ethWallet, toCurrencyCode: 'WBTC', - nativeAmount: await bchWallet.denominationToNative('10', 'BCH'), + exchangeAmount: '10', quoteFor: 'from' - } - console.log(`fromRequest5:`) - console.log( - JSON.stringify( - { ...fromRequest5, fromWallet: undefined, toWallet: undefined }, - null, - 2 - ) - ) - - const quote5 = await account.fetchSwapQuote(fromRequest5).catch(e => { - console.log(e) - console.log(e.message) - return null }) - logQuote(quote5) - console.log(`------------`) // Test a TO quote bitcoincash to ethereum:WBTC using 'to' amount from // previous request - const toRequest6: EdgeSwapRequest = { + await fetchQuote({ fromWallet: bchWallet, fromCurrencyCode: 'BCH', toWallet: ethWallet, toCurrencyCode: 'WBTC', - nativeAmount: quote5?.toNativeAmount ?? '0', + nativeAmount: quote?.toNativeAmount ?? '0', // nativeAmount: await ethWallet.denominationToNative('0.15185834', 'WBTC'), quoteFor: 'to' - } - console.log(`toRequest6:`) - console.log( - JSON.stringify( - { ...toRequest6, fromWallet: undefined, toWallet: undefined }, - null, - 2 - ) - ) - - const quote6 = await account.fetchSwapQuote(toRequest6).catch(e => { - console.log(e) - console.log(e.message) - return null }) - logQuote(quote6) - console.log('-------------------------') - // Test a FROM quote bitcoin to bitcoincash - const fromRequest7: EdgeSwapRequest = { + await fetchQuote({ fromWallet: btcWallet, fromCurrencyCode: 'BTC', toWallet: bchWallet, toCurrencyCode: 'BCH', - nativeAmount: await btcWallet.denominationToNative('0.2', 'BTC'), + exchangeAmount: '0.2', quoteFor: 'from' - } - console.log(`fromRequest7:`) - console.log( - JSON.stringify( - { ...fromRequest7, fromWallet: undefined, toWallet: undefined }, - null, - 2 - ) - ) - - const quote7 = await account.fetchSwapQuote(fromRequest7).catch(e => { - console.log(e) - console.log(e.message) - return null }) - logQuote(quote7) - console.log(`------------`) - // Test a FROM quote bitcoincash to bitcoin - const fromRequest8: EdgeSwapRequest = { + await fetchQuote({ fromWallet: bchWallet, fromCurrencyCode: 'BCH', toWallet: btcWallet, toCurrencyCode: 'BTC', - nativeAmount: await bchWallet.denominationToNative('0.2', 'BCH'), + exchangeAmount: '0.2', quoteFor: 'from' - } - console.log(`fromRequest8:`) - console.log( - JSON.stringify( - { ...fromRequest8, fromWallet: undefined, toWallet: undefined }, - null, - 2 - ) - ) - - const quote8 = await account.fetchSwapQuote(fromRequest8).catch(e => { - console.log(e) - console.log(e.message) - return null }) - logQuote(quote8) - console.log(`------------`) process.exit(0) }