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
feature/default_network_editable
Daniel 3 years ago committed by GitHub
parent dcffd5fbf8
commit ecceb4d6da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app/scripts/lib/buy-eth-url.js
  2. 7
      app/scripts/lib/buy-eth-url.test.js
  3. 11
      ui/pages/swaps/swaps.util.js
  4. 24
      ui/pages/swaps/swaps.util.test.js

@ -1,12 +1,13 @@
import log from 'loglevel'; 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 { import {
GOERLI_CHAIN_ID, GOERLI_CHAIN_ID,
KOVAN_CHAIN_ID, KOVAN_CHAIN_ID,
MAINNET_CHAIN_ID, MAINNET_CHAIN_ID,
RINKEBY_CHAIN_ID, RINKEBY_CHAIN_ID,
ROPSTEN_CHAIN_ID, ROPSTEN_CHAIN_ID,
MAINNET_NETWORK_ID,
} from '../../../shared/constants/network'; } from '../../../shared/constants/network';
import { SECOND } from '../../../shared/constants/time'; import { SECOND } from '../../../shared/constants/time';
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout'; import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
@ -20,7 +21,7 @@ const fetchWithTimeout = getFetchWithTimeout(SECOND * 30);
* @returns String * @returns String
*/ */
const createWyrePurchaseUrl = async (address) => { 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`; const wyrePurchaseUrlFallback = `https://pay.sendwyre.com/purchase?dest=ethereum:${address}&destCurrency=ETH&accountId=AC-7AG3W4XH4N2&paymentMethod=debit-card`;
try { try {
const response = await fetchWithTimeout(fiatOnRampUrlApi, { const response = await fetchWithTimeout(fiatOnRampUrlApi, {

@ -7,6 +7,7 @@ import {
ROPSTEN_CHAIN_ID, ROPSTEN_CHAIN_ID,
} from '../../../shared/constants/network'; } from '../../../shared/constants/network';
import { TRANSAK_API_KEY } from '../constants/on-ramp'; import { TRANSAK_API_KEY } from '../constants/on-ramp';
import { SWAPS_API_V2_BASE_URL } from '../../../shared/constants/swaps';
import getBuyEthUrl from './buy-eth-url'; import getBuyEthUrl from './buy-eth-url';
const WYRE_ACCOUNT_ID = 'AC-7AG3W4XH4N2'; const WYRE_ACCOUNT_ID = 'AC-7AG3W4XH4N2';
@ -28,8 +29,10 @@ const KOVAN = {
describe('buy-eth-url', function () { describe('buy-eth-url', function () {
it('returns Wyre url with an ETH address for Ethereum mainnet', async function () { it('returns Wyre url with an ETH address for Ethereum mainnet', async function () {
nock('https://api.metaswap.codefi.network') nock(SWAPS_API_V2_BASE_URL)
.get(`/fiatOnRampUrl?serviceName=wyre&destinationAddress=${ETH_ADDRESS}`) .get(
`/networks/1/fiatOnRampUrl?serviceName=wyre&destinationAddress=${ETH_ADDRESS}`,
)
.reply(200, { .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`, 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`,
}); });

@ -275,11 +275,18 @@ export const shouldEnableDirectWrapping = (
sourceToken, sourceToken,
destinationToken, destinationToken,
) => { ) => {
if (!sourceToken || !destinationToken) {
return false;
}
const wrappedToken = SWAPS_WRAPPED_TOKENS_ADDRESSES[chainId]; const wrappedToken = SWAPS_WRAPPED_TOKENS_ADDRESSES[chainId];
const nativeToken = SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.address; const nativeToken = SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.address;
const sourceTokenLowerCase = sourceToken.toLowerCase();
const destinationTokenLowerCase = destinationToken.toLowerCase();
return ( return (
(sourceToken === wrappedToken && destinationToken === nativeToken) || (sourceTokenLowerCase === wrappedToken &&
(sourceToken === nativeToken && destinationToken === wrappedToken) destinationTokenLowerCase === nativeToken) ||
(sourceTokenLowerCase === nativeToken &&
destinationTokenLowerCase === wrappedToken)
); );
}; };

@ -424,6 +424,18 @@ describe('Swaps Util', () => {
).toBe(true); ).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', () => { it('returns true if swapping from WETH to ETH', () => {
expect( expect(
shouldEnableDirectWrapping( shouldEnableDirectWrapping(
@ -434,6 +446,18 @@ describe('Swaps Util', () => {
).toBe(true); ).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', () => { it('returns false if swapping from ETH to a non-WETH token', () => {
expect( expect(
shouldEnableDirectWrapping( shouldEnableDirectWrapping(

Loading…
Cancel
Save