1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00

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
This commit is contained in:
Daniel 2021-11-17 18:04:50 +01:00 committed by GitHub
parent dcffd5fbf8
commit ecceb4d6da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 6 deletions

View File

@ -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, {

View File

@ -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`,
});

View File

@ -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)
);
};

View File

@ -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(