From ecceb4d6da86017cde7357fbc0c9b91a25bb0d71 Mon Sep 17 00:00:00 2001 From: Daniel <80175477+dan437@users.noreply.github.com> Date: Wed, 17 Nov 2021 18:04:50 +0100 Subject: [PATCH] Use v2 API for fiat onboarding URL creation, fix wrapping / unwrapping (#12729) * Use a Swaps v2 API to get a fiat onboarding URL * Fix an issue with wrapping / unwrapping if an address contained uppercase chars * Rename a constant * Use a constant in a test --- app/scripts/lib/buy-eth-url.js | 5 +++-- app/scripts/lib/buy-eth-url.test.js | 7 +++++-- ui/pages/swaps/swaps.util.js | 11 +++++++++-- ui/pages/swaps/swaps.util.test.js | 24 ++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/app/scripts/lib/buy-eth-url.js b/app/scripts/lib/buy-eth-url.js index f9f15e33b..bda772693 100644 --- a/app/scripts/lib/buy-eth-url.js +++ b/app/scripts/lib/buy-eth-url.js @@ -1,12 +1,13 @@ import log from 'loglevel'; -import { METASWAP_CHAINID_API_HOST_MAP } from '../../../shared/constants/swaps'; +import { SWAPS_API_V2_BASE_URL } from '../../../shared/constants/swaps'; import { GOERLI_CHAIN_ID, KOVAN_CHAIN_ID, MAINNET_CHAIN_ID, RINKEBY_CHAIN_ID, ROPSTEN_CHAIN_ID, + MAINNET_NETWORK_ID, } from '../../../shared/constants/network'; import { SECOND } from '../../../shared/constants/time'; import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout'; @@ -20,7 +21,7 @@ const fetchWithTimeout = getFetchWithTimeout(SECOND * 30); * @returns String */ const createWyrePurchaseUrl = async (address) => { - const fiatOnRampUrlApi = `${METASWAP_CHAINID_API_HOST_MAP[MAINNET_CHAIN_ID]}/fiatOnRampUrl?serviceName=wyre&destinationAddress=${address}`; + const fiatOnRampUrlApi = `${SWAPS_API_V2_BASE_URL}/networks/${MAINNET_NETWORK_ID}/fiatOnRampUrl?serviceName=wyre&destinationAddress=${address}`; const wyrePurchaseUrlFallback = `https://pay.sendwyre.com/purchase?dest=ethereum:${address}&destCurrency=ETH&accountId=AC-7AG3W4XH4N2&paymentMethod=debit-card`; try { const response = await fetchWithTimeout(fiatOnRampUrlApi, { diff --git a/app/scripts/lib/buy-eth-url.test.js b/app/scripts/lib/buy-eth-url.test.js index d15af3e5d..b240f55b8 100644 --- a/app/scripts/lib/buy-eth-url.test.js +++ b/app/scripts/lib/buy-eth-url.test.js @@ -7,6 +7,7 @@ import { ROPSTEN_CHAIN_ID, } from '../../../shared/constants/network'; import { TRANSAK_API_KEY } from '../constants/on-ramp'; +import { SWAPS_API_V2_BASE_URL } from '../../../shared/constants/swaps'; import getBuyEthUrl from './buy-eth-url'; const WYRE_ACCOUNT_ID = 'AC-7AG3W4XH4N2'; @@ -28,8 +29,10 @@ const KOVAN = { describe('buy-eth-url', function () { it('returns Wyre url with an ETH address for Ethereum mainnet', async function () { - nock('https://api.metaswap.codefi.network') - .get(`/fiatOnRampUrl?serviceName=wyre&destinationAddress=${ETH_ADDRESS}`) + nock(SWAPS_API_V2_BASE_URL) + .get( + `/networks/1/fiatOnRampUrl?serviceName=wyre&destinationAddress=${ETH_ADDRESS}`, + ) .reply(200, { url: `https://pay.sendwyre.com/purchase?accountId=${WYRE_ACCOUNT_ID}&utm_campaign=${WYRE_ACCOUNT_ID}&destCurrency=ETH&utm_medium=widget&paymentMethod=debit-card&reservation=MLZVUF8FMXZUMARJC23B&dest=ethereum%3A${ETH_ADDRESS}&utm_source=checkout`, }); diff --git a/ui/pages/swaps/swaps.util.js b/ui/pages/swaps/swaps.util.js index 74203b411..3089d485d 100644 --- a/ui/pages/swaps/swaps.util.js +++ b/ui/pages/swaps/swaps.util.js @@ -275,11 +275,18 @@ export const shouldEnableDirectWrapping = ( sourceToken, destinationToken, ) => { + if (!sourceToken || !destinationToken) { + return false; + } const wrappedToken = SWAPS_WRAPPED_TOKENS_ADDRESSES[chainId]; const nativeToken = SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.address; + const sourceTokenLowerCase = sourceToken.toLowerCase(); + const destinationTokenLowerCase = destinationToken.toLowerCase(); return ( - (sourceToken === wrappedToken && destinationToken === nativeToken) || - (sourceToken === nativeToken && destinationToken === wrappedToken) + (sourceTokenLowerCase === wrappedToken && + destinationTokenLowerCase === nativeToken) || + (sourceTokenLowerCase === nativeToken && + destinationTokenLowerCase === wrappedToken) ); }; diff --git a/ui/pages/swaps/swaps.util.test.js b/ui/pages/swaps/swaps.util.test.js index 347086d0c..a721c585c 100644 --- a/ui/pages/swaps/swaps.util.test.js +++ b/ui/pages/swaps/swaps.util.test.js @@ -424,6 +424,18 @@ describe('Swaps Util', () => { ).toBe(true); }); + it('returns true if swapping from ETH with uppercase chars to WETH', () => { + const ethAddressWithUpperCaseChars = + '0X0000000000000000000000000000000000000000'; + expect( + shouldEnableDirectWrapping( + MAINNET_CHAIN_ID, + ethAddressWithUpperCaseChars, + WETH_CONTRACT_ADDRESS, + ), + ).toBe(true); + }); + it('returns true if swapping from WETH to ETH', () => { expect( shouldEnableDirectWrapping( @@ -434,6 +446,18 @@ describe('Swaps Util', () => { ).toBe(true); }); + it('returns true if swapping from WETH with uppercase chars to ETH', () => { + const wethContractAddressWithUpperCaseChars = + '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; + expect( + shouldEnableDirectWrapping( + MAINNET_CHAIN_ID, + wethContractAddressWithUpperCaseChars, + SWAPS_CHAINID_DEFAULT_TOKEN_MAP[MAINNET_CHAIN_ID]?.address, + ), + ).toBe(true); + }); + it('returns false if swapping from ETH to a non-WETH token', () => { expect( shouldEnableDirectWrapping(