You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.1 KiB
125 lines
4.1 KiB
3 years ago
|
import log from 'loglevel';
|
||
|
|
||
3 years ago
|
import { SWAPS_API_V2_BASE_URL } from '../../../shared/constants/swaps';
|
||
4 years ago
|
import {
|
||
|
GOERLI_CHAIN_ID,
|
||
|
KOVAN_CHAIN_ID,
|
||
|
MAINNET_CHAIN_ID,
|
||
|
RINKEBY_CHAIN_ID,
|
||
|
ROPSTEN_CHAIN_ID,
|
||
3 years ago
|
MAINNET_NETWORK_ID,
|
||
3 years ago
|
BUYABLE_CHAINS_MAP,
|
||
4 years ago
|
} from '../../../shared/constants/network';
|
||
3 years ago
|
import { SECOND } from '../../../shared/constants/time';
|
||
|
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
|
||
4 years ago
|
import { TRANSAK_API_KEY } from '../constants/on-ramp';
|
||
|
|
||
3 years ago
|
const fetchWithTimeout = getFetchWithTimeout(SECOND * 30);
|
||
|
|
||
|
/**
|
||
|
* Create a Wyre purchase URL.
|
||
3 years ago
|
*
|
||
|
* @param {string} address - Ethereum destination address
|
||
3 years ago
|
* @returns String
|
||
|
*/
|
||
|
const createWyrePurchaseUrl = async (address) => {
|
||
3 years ago
|
const fiatOnRampUrlApi = `${SWAPS_API_V2_BASE_URL}/networks/${MAINNET_NETWORK_ID}/fiatOnRampUrl?serviceName=wyre&destinationAddress=${address}`;
|
||
3 years ago
|
const wyrePurchaseUrlFallback = `https://pay.sendwyre.com/purchase?dest=ethereum:${address}&destCurrency=ETH&accountId=AC-7AG3W4XH4N2&paymentMethod=debit-card`;
|
||
|
try {
|
||
|
const response = await fetchWithTimeout(fiatOnRampUrlApi, {
|
||
|
method: 'GET',
|
||
|
headers: {
|
||
3 years ago
|
Accept: 'application/json',
|
||
3 years ago
|
'Content-Type': 'application/json',
|
||
|
},
|
||
|
});
|
||
|
const parsedResponse = await response.json();
|
||
|
if (response.ok && parsedResponse.url) {
|
||
|
return parsedResponse.url;
|
||
|
}
|
||
|
log.warn('Failed to create a Wyre purchase URL', parsedResponse);
|
||
|
} catch (err) {
|
||
|
log.warn('Failed to create a Wyre purchase URL', err);
|
||
|
}
|
||
|
return wyrePurchaseUrlFallback; // In case the API call would fail, we return a fallback URL for Wyre's Checkout.
|
||
|
};
|
||
|
|
||
4 years ago
|
/**
|
||
|
* Create a Transak Checkout URL.
|
||
|
* API docs here: https://www.notion.so/Query-Parameters-9ec523df3b874ec58cef4fa3a906f238
|
||
3 years ago
|
*
|
||
3 years ago
|
* @param {string} walletAddress - Ethereum destination address
|
||
|
* @param {string} chainId - Current chain ID
|
||
4 years ago
|
* @returns String
|
||
|
*/
|
||
3 years ago
|
const createTransakUrl = (walletAddress, chainId) => {
|
||
|
const { transakCurrencies, network } = BUYABLE_CHAINS_MAP[chainId];
|
||
|
|
||
4 years ago
|
const queryParams = new URLSearchParams({
|
||
|
apiKey: TRANSAK_API_KEY,
|
||
|
hostURL: 'https://metamask.io',
|
||
3 years ago
|
cryptoCurrencyList: transakCurrencies.join(','),
|
||
|
defaultCryptoCurrency: transakCurrencies[0],
|
||
|
networks: network,
|
||
|
walletAddress,
|
||
4 years ago
|
});
|
||
3 years ago
|
|
||
4 years ago
|
return `https://global.transak.com/?${queryParams}`;
|
||
|
};
|
||
4 years ago
|
|
||
7 years ago
|
/**
|
||
|
* Gives the caller a url at which the user can acquire eth, depending on the network they are in
|
||
|
*
|
||
5 years ago
|
* @param {Object} opts - Options required to determine the correct url
|
||
4 years ago
|
* @param {string} opts.chainId - The chainId for which to return a url
|
||
|
* @param {string} opts.address - The address the bought ETH should be sent to. Only relevant if chainId === '0x1'.
|
||
3 years ago
|
* @param opts.service
|
||
4 years ago
|
* @returns {string|undefined} The url at which the user can access ETH, while in the given chain. If the passed
|
||
|
* chainId does not match any of the specified cases, or if no chainId is given, returns undefined.
|
||
7 years ago
|
*/
|
||
3 years ago
|
export default async function getBuyUrl({ chainId, address, service }) {
|
||
6 years ago
|
// default service by network if not specified
|
||
5 years ago
|
if (!service) {
|
||
4 years ago
|
// eslint-disable-next-line no-param-reassign
|
||
4 years ago
|
service = getDefaultServiceForChain(chainId);
|
||
5 years ago
|
}
|
||
6 years ago
|
|
||
|
switch (service) {
|
||
6 years ago
|
case 'wyre':
|
||
3 years ago
|
return await createWyrePurchaseUrl(address);
|
||
4 years ago
|
case 'transak':
|
||
3 years ago
|
return createTransakUrl(address, chainId);
|
||
6 years ago
|
case 'metamask-faucet':
|
||
4 years ago
|
return 'https://faucet.metamask.io/';
|
||
6 years ago
|
case 'rinkeby-faucet':
|
||
4 years ago
|
return 'https://www.rinkeby.io/';
|
||
6 years ago
|
case 'kovan-faucet':
|
||
4 years ago
|
return 'https://github.com/kovan-testnet/faucet';
|
||
6 years ago
|
case 'goerli-faucet':
|
||
4 years ago
|
return 'https://goerli-faucet.slock.it/';
|
||
5 years ago
|
default:
|
||
4 years ago
|
throw new Error(
|
||
|
`Unknown cryptocurrency exchange or faucet: "${service}"`,
|
||
|
);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
function getDefaultServiceForChain(chainId) {
|
||
|
switch (chainId) {
|
||
|
case MAINNET_CHAIN_ID:
|
||
4 years ago
|
return 'wyre';
|
||
4 years ago
|
case ROPSTEN_CHAIN_ID:
|
||
4 years ago
|
return 'metamask-faucet';
|
||
4 years ago
|
case RINKEBY_CHAIN_ID:
|
||
4 years ago
|
return 'rinkeby-faucet';
|
||
4 years ago
|
case KOVAN_CHAIN_ID:
|
||
4 years ago
|
return 'kovan-faucet';
|
||
4 years ago
|
case GOERLI_CHAIN_ID:
|
||
4 years ago
|
return 'goerli-faucet';
|
||
5 years ago
|
default:
|
||
4 years ago
|
throw new Error(
|
||
4 years ago
|
`No default cryptocurrency exchange or faucet for chainId: "${chainId}"`,
|
||
4 years ago
|
);
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|