2021-06-25 16:52:29 +02:00
|
|
|
import log from 'loglevel';
|
|
|
|
|
2021-11-17 18:04:50 +01:00
|
|
|
import { SWAPS_API_V2_BASE_URL } from '../../../shared/constants/swaps';
|
2021-03-12 23:23:26 +01:00
|
|
|
import {
|
|
|
|
GOERLI_CHAIN_ID,
|
|
|
|
KOVAN_CHAIN_ID,
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
RINKEBY_CHAIN_ID,
|
|
|
|
ROPSTEN_CHAIN_ID,
|
2021-11-17 18:04:50 +01:00
|
|
|
MAINNET_NETWORK_ID,
|
2021-03-12 23:23:26 +01:00
|
|
|
} from '../../../shared/constants/network';
|
2021-06-25 16:52:29 +02:00
|
|
|
import { SECOND } from '../../../shared/constants/time';
|
|
|
|
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
|
2021-06-25 15:10:24 +02:00
|
|
|
import { TRANSAK_API_KEY } from '../constants/on-ramp';
|
|
|
|
|
2021-06-25 16:52:29 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout(SECOND * 30);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Wyre purchase URL.
|
|
|
|
* @param {String} address Ethereum destination address
|
|
|
|
* @returns String
|
|
|
|
*/
|
|
|
|
const createWyrePurchaseUrl = async (address) => {
|
2021-11-17 18:04:50 +01:00
|
|
|
const fiatOnRampUrlApi = `${SWAPS_API_V2_BASE_URL}/networks/${MAINNET_NETWORK_ID}/fiatOnRampUrl?serviceName=wyre&destinationAddress=${address}`;
|
2021-06-25 16:52:29 +02:00
|
|
|
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: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'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.
|
|
|
|
};
|
|
|
|
|
2021-06-25 15:10:24 +02:00
|
|
|
/**
|
|
|
|
* Create a Transak Checkout URL.
|
|
|
|
* API docs here: https://www.notion.so/Query-Parameters-9ec523df3b874ec58cef4fa3a906f238
|
|
|
|
* @param {String} address Ethereum destination address
|
|
|
|
* @returns String
|
|
|
|
*/
|
|
|
|
const createTransakUrl = (address) => {
|
|
|
|
const queryParams = new URLSearchParams({
|
|
|
|
apiKey: TRANSAK_API_KEY,
|
|
|
|
hostURL: 'https://metamask.io',
|
|
|
|
defaultCryptoCurrency: 'ETH',
|
|
|
|
walletAddress: address,
|
|
|
|
});
|
|
|
|
return `https://global.transak.com/?${queryParams}`;
|
|
|
|
};
|
2021-03-12 23:23:26 +01:00
|
|
|
|
2018-04-16 19:08:04 +02:00
|
|
|
/**
|
|
|
|
* Gives the caller a url at which the user can acquire eth, depending on the network they are in
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Object} opts - Options required to determine the correct url
|
2021-03-12 23:23:26 +01:00
|
|
|
* @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'.
|
|
|
|
* @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.
|
2018-04-16 19:08:04 +02:00
|
|
|
*
|
|
|
|
*/
|
2021-06-25 16:52:29 +02:00
|
|
|
export default async function getBuyEthUrl({ chainId, address, service }) {
|
2019-03-29 03:03:35 +01:00
|
|
|
// default service by network if not specified
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!service) {
|
2020-08-15 13:58:11 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2021-03-12 23:23:26 +01:00
|
|
|
service = getDefaultServiceForChain(chainId);
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2019-03-29 03:03:35 +01:00
|
|
|
|
|
|
|
switch (service) {
|
2019-03-29 03:20:19 +01:00
|
|
|
case 'wyre':
|
2021-06-25 16:52:29 +02:00
|
|
|
return await createWyrePurchaseUrl(address);
|
2021-06-25 15:10:24 +02:00
|
|
|
case 'transak':
|
|
|
|
return createTransakUrl(address);
|
2019-03-29 03:03:35 +01:00
|
|
|
case 'metamask-faucet':
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'https://faucet.metamask.io/';
|
2019-03-29 03:03:35 +01:00
|
|
|
case 'rinkeby-faucet':
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'https://www.rinkeby.io/';
|
2019-03-29 03:03:35 +01:00
|
|
|
case 'kovan-faucet':
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'https://github.com/kovan-testnet/faucet';
|
2019-04-17 19:34:49 +02:00
|
|
|
case 'goerli-faucet':
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'https://goerli-faucet.slock.it/';
|
2019-11-27 17:08:35 +01:00
|
|
|
default:
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(
|
|
|
|
`Unknown cryptocurrency exchange or faucet: "${service}"`,
|
|
|
|
);
|
2019-03-29 03:03:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 23:23:26 +01:00
|
|
|
function getDefaultServiceForChain(chainId) {
|
|
|
|
switch (chainId) {
|
|
|
|
case MAINNET_CHAIN_ID:
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'wyre';
|
2021-03-12 23:23:26 +01:00
|
|
|
case ROPSTEN_CHAIN_ID:
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'metamask-faucet';
|
2021-03-12 23:23:26 +01:00
|
|
|
case RINKEBY_CHAIN_ID:
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'rinkeby-faucet';
|
2021-03-12 23:23:26 +01:00
|
|
|
case KOVAN_CHAIN_ID:
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'kovan-faucet';
|
2021-03-12 23:23:26 +01:00
|
|
|
case GOERLI_CHAIN_ID:
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'goerli-faucet';
|
2019-11-27 17:08:35 +01:00
|
|
|
default:
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
2021-03-12 23:23:26 +01:00
|
|
|
`No default cryptocurrency exchange or faucet for chainId: "${chainId}"`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2017-04-05 03:23:46 +02:00
|
|
|
}
|
2017-04-25 23:39:01 +02:00
|
|
|
}
|