Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,474 changes: 4 additions & 1,470 deletions packages/caip/src/adapters/onramper/index.ts

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions src/components/Modals/FiatRamps/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ import {
getMtPelerinAssets,
getMtPelerinFiatCurrencies,
} from './fiatRampProviders/mtpelerin'
import {
createOnRamperUrl,
getOnRamperAssets,
getSupportedOnRamperFiatCurrencies,
} from './fiatRampProviders/onramper'
import { createOnRamperUrl, getOnRamperAssets } from './fiatRampProviders/onramper'
import { SUPPORTED_ONRAMPER_FIAT_CURRENCIES } from './fiatRampProviders/onramper/constants'
import type { CreateUrlProps } from './types'

import banxaLogo from '@/assets/banxa.png'
Expand Down Expand Up @@ -97,7 +94,7 @@ export const supportedFiatRamps: SupportedFiatRamp = {
)
return [filteredBuyAndSellAssetIds, filteredBuyAndSellAssetIds]
},
getSupportedFiatList: () => getSupportedOnRamperFiatCurrencies(),
getSupportedFiatList: () => SUPPORTED_ONRAMPER_FIAT_CURRENCIES,
onSubmit: async props => {
try {
const onRamperCheckoutUrl = await createOnRamperUrl(props)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import type { ChainId } from '@shapeshiftoss/caip'
import {
arbitrumChainId,
avalancheChainId,
baseChainId,
bchChainId,
bscChainId,
btcChainId,
cosmosChainId,
dogeChainId,
ethChainId,
gnosisChainId,
ltcChainId,
optimismChainId,
polygonChainId,
solanaChainId,
thorchainChainId,
} from '@shapeshiftoss/caip'

import type { CommonFiatCurrencies } from '../../config'

export const SUPPORTED_ONRAMPER_FIAT_CURRENCIES: CommonFiatCurrencies[] = [
'AOA',
'AUD',
'BBD',
'BZD',
'BMD',
'BRL',
'GBP',
'BND',
'BGN',
'CAD',
'XAF',
'CLP',
'CNY',
'COP',
'KMF',
'CRC',
'HRK',
'CZK',
'DKK',
'DJF',
'DOP',
'XCD',
'EGP',
'EUR',
'FKP',
'FJD',
'GEL',
'GHS',
'GIP',
'GTQ',
'HNL',
'HKD',
'HUF',
'ISK',
'IDR',
'ILS',
'JMD',
'JPY',
'JOD',
'KZT',
'KES',
'KWD',
'KGS',
'MGA',
'MWK',
'MYR',
'MRU',
'MXN',
'MDL',
'MAD',
'MZN',
'TWD',
'NZD',
'NGN',
'NOK',
'OMR',
'PKR',
'PGK',
'PYG',
'PEN',
'PHP',
'PLN',
'RON',
'RWF',
'STN',
'SCR',
'SGD',
'SBD',
'ZAR',
'KRW',
'LKR',
'SRD',
'SZL',
'SEK',
'CHF',
'TJS',
'TZS',
'THB',
'TOP',
'TRY',
'TMT',
'UGX',
'USD',
'UYU',
'VND',
]

// Base mapping from ChainId to Onramper network names
export const CHAIN_ID_TO_ONRAMPER_NETWORK: Record<ChainId, string> = {
[btcChainId]: 'bitcoin',
[bchChainId]: 'bitcoincash',
[dogeChainId]: 'dogecoin',
[ltcChainId]: 'litecoin',
[ethChainId]: 'ethereum',
[avalancheChainId]: 'avaxc',
[optimismChainId]: 'optimism',
[bscChainId]: 'bsc',
[polygonChainId]: 'polygon',
[gnosisChainId]: 'gnosis',
[arbitrumChainId]: 'arbitrum',
[baseChainId]: 'base',
[cosmosChainId]: 'cosmos',
[thorchainChainId]: 'thorchain',
[solanaChainId]: 'solana',
} as const

export const ONRAMPER_NETWORK_TO_CHAIN_ID: Record<string, ChainId> = Object.fromEntries(
Object.entries(CHAIN_ID_TO_ONRAMPER_NETWORK).map(([chainId, network]) => [network, chainId]),
)

export const getChainIdFromOnramperNetwork = (network: string): ChainId | undefined => {
return ONRAMPER_NETWORK_TO_CHAIN_ID[network]
}
Original file line number Diff line number Diff line change
@@ -1,138 +1,20 @@
import type { AssetId } from '@shapeshiftoss/caip'
import { adapters } from '@shapeshiftoss/caip'
import axios from 'axios'
import head from 'lodash/head'

import type { CommonFiatCurrencies } from '../config'
import { FiatRampAction } from '../FiatRampsCommon'
import type { CreateUrlProps } from '../types'
import { FiatRampAction } from '../../FiatRampsCommon'
import type { CreateUrlProps } from '../../types'
import type { OnRamperGatewaysResponse } from './types'
import {
findAssetIdByOnramperCrypto,
findOnramperTokenIdByAssetId,
getSupportedOnramperCurrencies,
} from './utils'

import { getConfig } from '@/config'

// Non-exhaustive required types definition. Full reference: https://github.com/onramper/widget/blob/master/package/src/ApiContext/api/types/gateways.ts
type Crypto = {
id: string
code: string
name: string
symbol: string
network: string
icon: string
}

type OnRamperGatewaysResponse = {
message: {
crypto: Crypto[]
}
}

const getGatewayData = async () => {
try {
const baseUrl = getConfig().VITE_ONRAMPER_API_URL
const apiKey = getConfig().VITE_ONRAMPER_API_KEY
return (
await axios.get<OnRamperGatewaysResponse>(`${baseUrl}supported`, {
headers: {
Authorization: apiKey,
},
})
).data
} catch (e) {
console.error(e)
}
}

export const getSupportedOnRamperFiatCurrencies = (): CommonFiatCurrencies[] => {
return [
'AOA',
'AUD',
'BBD',
'BZD',
'BMD',
'BRL',
'GBP',
'BND',
'BGN',
'CAD',
'XAF',
'CLP',
'CNY',
'COP',
'KMF',
'CRC',
'HRK',
'CZK',
'DKK',
'DJF',
'DOP',
'XCD',
'EGP',
'EUR',
'FKP',
'FJD',
'GEL',
'GHS',
'GIP',
'GTQ',
'HNL',
'HKD',
'HUF',
'ISK',
'IDR',
'ILS',
'JMD',
'JPY',
'JOD',
'KZT',
'KES',
'KWD',
'KGS',
'MGA',
'MWK',
'MYR',
'MRU',
'MXN',
'MDL',
'MAD',
'MZN',
'TWD',
'NZD',
'NGN',
'NOK',
'OMR',
'PKR',
'PGK',
'PYG',
'PEN',
'PHP',
'PLN',
'RON',
'RWF',
'STN',
'SCR',
'SGD',
'SBD',
'ZAR',
'KRW',
'LKR',
'SRD',
'SZL',
'SEK',
'CHF',
'TJS',
'TZS',
'THB',
'TOP',
'TRY',
'TMT',
'UGX',
'USD',
'UYU',
'VND',
]
}

export const getOnRamperAssets = async (): Promise<AssetId[]> => {
const data = await getGatewayData()
const data = await getSupportedOnramperCurrencies()
if (!data) return []
return convertOnRamperDataToFiatRampAsset(data)
}
Expand All @@ -141,7 +23,7 @@ const convertOnRamperDataToFiatRampAsset = (response: OnRamperGatewaysResponse):
return Array.from(
new Set(
response.message.crypto
.map(currency => adapters.onRamperTokenIdToAssetId(currency.id))
.map(currency => findAssetIdByOnramperCrypto(currency))
.filter((assetId): assetId is AssetId => Boolean(assetId)),
),
)
Expand Down Expand Up @@ -178,8 +60,23 @@ export const createOnRamperUrl = async ({
fiatCurrency,
options: { language, mode, currentUrl },
}: CreateUrlProps): Promise<string> => {
const onRamperSymbols = adapters.assetIdToOnRamperTokenList(assetId)
if (!onRamperSymbols) throw new Error('Asset not supported by OnRamper')
const supportedCurrencies = await getSupportedOnramperCurrencies()
if (!supportedCurrencies) throw new Error('Failed to get supported currencies from Onramper')

const onRamperSymbols = (() => {
const maybeMappingSymbols = adapters.assetIdToOnRamperTokenList(assetId)
if (maybeMappingSymbols?.length) return maybeMappingSymbols

const tokenId = findOnramperTokenIdByAssetId(assetId, supportedCurrencies)

if (!tokenId) return

return [tokenId]
})()

if (!onRamperSymbols?.length) {
throw new Error('Asset not supported by OnRamper')
}

const baseUrl = getConfig().VITE_ONRAMPER_WIDGET_URL
const apiKey = getConfig().VITE_ONRAMPER_API_KEY
Expand Down
Loading