Skip to content

Commit

Permalink
Augment on-ramp-currencies-lists.json with Sardine data fetched dynam…
Browse files Browse the repository at this point in the history
…ically from API
  • Loading branch information
nvonpentz committed Aug 23, 2023
1 parent f9a3274 commit 6da9315
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
6 changes: 5 additions & 1 deletion scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,13 @@ async function stageOnRampLists(stagingDir) {
rampTokens = await util.addSupportedCoinbaseTokens(rampTokens);
await fsPromises.writeFile(dstRampTokensPath, JSON.stringify(rampTokens, null, 2));

// on-ramp-currency-lists.json
const srcOnRampCurrenciesPath = path.join('data', 'onramps', 'on-ramp-currency-lists.json');
const dstOnRampCurrenciesPath = path.join(stagingDir, 'on-ramp-currency-lists.json');
await fsPromises.copyFile(srcOnRampCurrenciesPath, dstOnRampCurrenciesPath);
const srcOnRampCurrenciesData = await fsPromises.readFile(srcOnRampCurrenciesPath, 'utf-8');
let onRampCurrencies = JSON.parse(srcOnRampCurrenciesData);
onRampCurrencies = await util.addSupportedSardineCurrencies(onRampCurrencies);
await fsPromises.writeFile(dstOnRampCurrenciesPath, JSON.stringify(onRampCurrencies, null, 2));
}

async function stageTokenPackage() {
Expand Down
46 changes: 46 additions & 0 deletions scripts/util.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,51 @@ const addSupportedCoinbaseTokens = async (rampTokens) => {
return rampTokens;
}


const addSupportedSardineCurrencies = async (onRampCurrencies) => {
const sardine = "sardine"
const sardineApiUrl = "https://api.sandbox.sardine.ai/v1/geo-coverage";
const response = await fetch(sardineApiUrl);
const jsonResponse = await response.json();
const currencyCodes = [];

for (let country of jsonResponse.data) {
if (country.isAllowedOnRamp) {
if (!currencyCodes.includes(country.currencyCode)) {
currencyCodes.push(country.currencyCode);
}
}
}

// Create a dictionary for quick lookups
const currencyLookup = {};
onRampCurrencies.currencies.forEach((currency) => {
currencyLookup[currency.currency_code] = currency;
});

for (let currencyCode of currencyCodes) {
const existingCurrency = currencyLookup[currencyCode];

if (existingCurrency) {
// If "sardine" isn't already in the providers array, add it
if (!existingCurrency.providers.includes(sardine)) {
existingCurrency.providers.push(sardine);
}
} else {
// If the currency doesn't exist, create a new entry
const newCurrency = {
currency_code: currencyCode,
// currency_name: "", // TODO - This isn't available.
providers: [sardine],
};
onRampCurrencies.currencies.push(newCurrency);
currencyLookup[currencyCode] = newCurrency;
}
}

return onRampCurrencies
};

const generateChainList = async () => {
const chainListResponse = await fetch('https://chainid.network/chains.json')
if (!chainListResponse.ok) {
Expand Down Expand Up @@ -637,6 +682,7 @@ module.exports = {
generateMainnetTokenList,
generateDappLists,
addSupportedCoinbaseTokens,
addSupportedSardineCurrencies,
generateCoingeckoIds,
generateChainList,
injectCoingeckoIds
Expand Down

0 comments on commit 6da9315

Please sign in to comment.