Skip to content

Commit

Permalink
feat: Add CoinGecko API for add coinGeckoIds
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Gamja committed Nov 18, 2024
1 parent 8866d3f commit 447b15d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/evm_okx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
echo "OKX_API_KEY=${{ secrets.OKX_API_KEY }}" >> $GITHUB_ENV
echo "OKX_SECRET_KEY=${{ secrets.OKX_SECRET_KEY }}" >> $GITHUB_ENV
echo "OKX_PASSPHRASE=${{ secrets.OKX_PASSPHRASE }}" >> $GITHUB_ENV
echo "COINGECKO_API_KEY=${{ secrets.COINGECKO_API_KEY }}" >> $GITHUB_ENV
echo "FRONT_API_KEY=${{ secrets.FRONT_API_KEY }}" >> $GITHUB_ENV
- name: Run code modification script
run: |
Expand All @@ -39,6 +41,8 @@ jobs:
API_KEY: ${{ secrets.OKX_API_KEY }}
SECRET_KEY: ${{ secrets.OKX_SECRET_KEY }}
PASSPHRASE: ${{ secrets.OKX_PASSPHRASE }}
COINGECKO_API_KEY: ${{ secrets.COINGECKO_API_KEY }}
FRONT_API_KEY: ${{ secrets.FRONT_API_KEY }}

- name: Create dynamic branch name
id: vars
Expand Down
98 changes: 85 additions & 13 deletions _script/evm_okx/evm_okx.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ import cryptoJS from "crypto-js";

const nativeCoinContractAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";

function isEqualsIgnoringCase(a, b) {
return (
typeof a === "string" &&
typeof b === "string" &&
a.toLowerCase() === b.toLowerCase()
);
}

async function main() {
try {
const apiKey = process.env.OKX_API_KEY;
const secretKey = process.env.OKX_SECRET_KEY;
const passphrase = process.env.OKX_PASSPHRASE;
const coinGeckoApiKey = process.env.COINGECKO_API_KEY;
const frontApiKey = process.env.FRONT_API_KEY;

const date = new Date();
const timestamp = date.toISOString();
Expand All @@ -23,6 +33,37 @@ async function main() {
const fileName = `./chain/${chain}/erc20_2.json`;
const currentAssets = JSON.parse(readFileSync(fileName, "utf-8"));

const coingeckoActiveCoinsData = await fetch(
`https://api.coingecko.com/api/v3/coins/list?include_platform=true&status=active`,
{
headers: {
"x-cg-pro-api-key": coinGeckoApiKey,
},
}
);

const activeGeckoCoinsDataResponse = await coingeckoActiveCoinsData.json();

const assetPlatformsResponse = await fetch(
`https://api.coingecko.com/api/v3/asset_platforms`,
{
headers: {
"x-cg-pro-api-key": coinGeckoApiKey,
},
}
);

const assetPlatformsData = await assetPlatformsResponse.json();

// NOTE chain_identifier only supports EVM chainids
const assetPlatformId = assetPlatformsData.find(
(item) => String(item.chain_identifier) === String(chainId)
).id;

const filteredCoinGeckoIdsByChain = activeGeckoCoinsDataResponse.filter(
(item) => !!item.platforms[assetPlatformId]
);

const response = await fetch(
`https://www.okx.com/api/v5/dex/aggregator/all-tokens?chainId=${chainId}`,
{
Expand Down Expand Up @@ -60,19 +101,50 @@ async function main() {
nativeCoinContractAddress.toLowerCase()
);
})
.map((asset) => ({
type: "erc20",
contract: asset.tokenContractAddress,
name: asset.tokenName,
symbol: asset.tokenSymbol,
description: asset.tokenSymbol, // NOTE: Temporary
decimals:
typeof asset.decimals === "string"
? Number(asset.decimals)
: asset.decimals,
image: asset?.tokenLogoUrl,
coinGeckoId: asset?.coingeckoId || "",
}));
.map((asset) => {
const coinGeckoId =
filteredCoinGeckoIdsByChain.find((item) =>
isEqualsIgnoringCase(
item.platforms[assetPlatformId],
asset.tokenContractAddress
)
)?.id || "";

return {
type: "erc20",
contract: asset.tokenContractAddress,
name: asset.tokenName,
symbol: asset.tokenSymbol,
description: asset.tokenSymbol, // NOTE: Temporary
decimals:
typeof asset.decimals === "string"
? Number(asset.decimals)
: asset.decimals,
image: asset?.tokenLogoUrl,
coinGeckoId: coinGeckoId || "",
};
});

const newCoinGeckoIds = assetsToAdd
.filter((item) => !!item.coinGeckoId)
.map((item) => item.coinGeckoId);

console.log("Coin ID List for comparison");
console.log("🚀 ~ newCoinGeckoIds:", JSON.stringify(newCoinGeckoIds));
console.log("🚀 ~ newCoinGeckoIds length:", newCoinGeckoIds.length);

try {
await fetch("https://front.api.mintscan.io/v10/utils/market/register", {
method: "POST",
headers: {
"x-authorization": frontApiKey,
"Content-Type": "application/json",
},
body: JSON.stringify({ coingecko_id: newCoinGeckoIds }),
});
} catch (error) {
throw new Error("Front API Error: " + error);
}

const mergedAssets = [...currentAssets, ...assetsToAdd];

Expand Down

0 comments on commit 447b15d

Please sign in to comment.