diff --git a/README.md b/README.md index f4afbd4..8a60b55 100644 --- a/README.md +++ b/README.md @@ -83,11 +83,11 @@ export const getNetworkConfigurations = async () => { export const nonCirculatingSupplyAddressesConfig = [ { - Name: "Deployer", - Address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", - TokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - JsonRpcUrl: "https://nd-770-685-838.p2pify.com/e30d3ea257d1588823179ce4d5811a61", - ChainId: "1" + name: "Deployer", + address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", + tokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", + jsonRpcUrl: "https://nd-770-685-838.p2pify.com/e30d3ea257d1588823179ce4d5811a61", + chainId: "1" }, // Add more addresses here... ]; diff --git a/build/config.js b/build/config.js index 75f9115..4ec130b 100644 --- a/build/config.js +++ b/build/config.js @@ -8,8 +8,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", { value: true }); -exports.nonCirculatingSupplyAddressesConfig = exports.getNetworkConfigurations = exports.chainIdToNetworkMap = void 0; +exports.getNonCirculatingSupplyAddressConfigurations = exports.getNetworkConfigurations = exports.chainIdToNetworkMap = void 0; +// src/config.ts +const node_fetch_1 = __importDefault(require("node-fetch")); const API_URL = 'https://api-leaderboard.dev.svcs.ferrumnetwork.io/api/v1/currencies/token/data'; const tokenContractAddress = "0xa719b8ab7ea7af0ddb4358719a34631bb79d15dc"; const chainId = 56; @@ -38,7 +43,7 @@ exports.chainIdToNetworkMap = { function getNetworkConfigurations() { return __awaiter(this, void 0, void 0, function* () { const url = `${API_URL}?tokenContractAddress=${tokenContractAddress}&chainId=${chainId}&offset=0`; - const response = yield fetch(url); + const response = yield (0, node_fetch_1.default)(url); const data = yield response.json(); const networks = {}; for (const item of data.body.currencyAddressesByNetworks) { @@ -46,7 +51,8 @@ function getNetworkConfigurations() { if (network) { networks[network.name] = { jsonRpcUrl: network.jsonRpcUrl, - tokenContractAddress: item.tokenContractAddress + tokenContractAddress: item.tokenContractAddress, + chainId: item.network.chainId }; } } @@ -54,76 +60,76 @@ function getNetworkConfigurations() { }); } exports.getNetworkConfigurations = getNetworkConfigurations; -const nonCirculatingSupplyAddressesConfig = [ +const nonCirculatingSupplyAddressesConfigInput = [ { - Name: "Deployer", - Address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", - TokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - JsonRpcUrl: "https://nd-770-685-838.p2pify.com/e30d3ea257d1588823179ce4d5811a61", - ChainId: "1" + name: "Deployer", + address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", + chainId: "1" }, { - Name: "Treasury", - Address: "0x517873ca1edaaa0f6403a0dab2cb0162433de9d1", - TokenContractAddress: "0xa719b8ab7ea7af0ddb4358719a34631bb79d15dc", - JsonRpcUrl: "https://nd-605-906-592.p2pify.com/df9065025f5e18317e708040b1f2ab13", - ChainId: "56" + name: "Treasury", + address: "0x517873ca1edaaa0f6403a0dab2cb0162433de9d1", + chainId: "56" }, { - Name: "Deployer", - Address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", - TokenContractAddress: "0xd99bafe5031cc8b345cb2e8c80135991f12d7130", - JsonRpcUrl: "https://nd-662-671-431.p2pify.com/72aea5a70bbd5482f9a498540072b1e1", - ChainId: "137" + name: "Deployer", + address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", + chainId: "137" }, { - Name: "Deployer", - Address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", - TokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - JsonRpcUrl: "https://nd-900-134-973.p2pify.com/0a4e07e77ebc245f0bf7839745b4803b/ext/bc/C/rpc", - ChainId: "43114" + name: "Deployer", + address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", + chainId: "43114" }, // { - // Name: "Deployer", - // Address: "bnb1um8ntkgwle8yrdk0yn5hwdf7hckjpyjjg29k2p", - // TokenContractAddress: "FRM-DE7", - // JsonRpcUrl: "https://dex.binance.org", - // ChainId: "bnbBeaconChain" + // name: "Deployer", + // address: "bnb1um8ntkgwle8yrdk0yn5hwdf7hckjpyjjg29k2p", + // chainId: "bnbBeaconChain" // }, { - Name: "Treasury", - Address: "0xe42b80dA58ccEAbe0A6ECe8e3311AE939Ef6b96c", - TokenContractAddress: "0x9f6abbf0ba6b5bfa27f4deb6597cc6ec20573fda", - JsonRpcUrl: "https://nd-674-145-610.p2pify.com/bc9acaa6f1386224186fb1e794c40c14", - ChainId: "42161" + name: "Treasury", + address: "0xe42b80dA58ccEAbe0A6ECe8e3311AE939Ef6b96c", + chainId: "42161" }, { - Name: "Bridge Pool", - Address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", - TokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - JsonRpcUrl: "https://nd-770-685-838.p2pify.com/e30d3ea257d1588823179ce4d5811a61", - ChainId: "1" + name: "Bridge Pool", + address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", + chainId: "1" }, { - Name: "Bridge Pool", - Address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", - TokenContractAddress: "0xa719b8ab7ea7af0ddb4358719a34631bb79d15dc", - JsonRpcUrl: "https://nd-605-906-592.p2pify.com/df9065025f5e18317e708040b1f2ab13", - ChainId: "56" + name: "Bridge Pool", + address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", + chainId: "56" }, { - Name: "Bridge Pool", - Address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", - TokenContractAddress: "0xd99bafe5031cc8b345cb2e8c80135991f12d7130", - JsonRpcUrl: "https://nd-662-671-431.p2pify.com/72aea5a70bbd5482f9a498540072b1e1", - ChainId: "137" + name: "Bridge Pool", + address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", + chainId: "137" }, { - Name: "Bridge Pool", - Address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", - TokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - JsonRpcUrl: "https://nd-900-134-973.p2pify.com/0a4e07e77ebc245f0bf7839745b4803b/ext/bc/C/rpc", - ChainId: "43114" + name: "Bridge Pool", + address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", + chainId: "43114" } ]; -exports.nonCirculatingSupplyAddressesConfig = nonCirculatingSupplyAddressesConfig; +function getNonCirculatingSupplyAddressConfigurations() { + return __awaiter(this, void 0, void 0, function* () { + const url = `${API_URL}?tokenContractAddress=${tokenContractAddress}&chainId=${chainId}&offset=0`; + const response = yield (0, node_fetch_1.default)(url); + const data = yield response.json(); + const nonCirculatingSupplyAddresses = []; + for (const item of nonCirculatingSupplyAddressesConfigInput) { + const network = exports.chainIdToNetworkMap[item.chainId]; + let networkItemFromGatewayConfig = data.body.currencyAddressesByNetworks.find(i => i.network.chainId === item.chainId); + nonCirculatingSupplyAddresses.push({ + name: item.name, + address: item.address, + tokenContractAddress: networkItemFromGatewayConfig === null || networkItemFromGatewayConfig === void 0 ? void 0 : networkItemFromGatewayConfig.tokenContractAddress, + jsonRpcUrl: network.jsonRpcUrl, + chainId: item.chainId + }); + } + return nonCirculatingSupplyAddresses; + }); +} +exports.getNonCirculatingSupplyAddressConfigurations = getNonCirculatingSupplyAddressConfigurations; diff --git a/build/getSupplyAcrossNetworks.js b/build/getSupplyAcrossNetworks.js index dc45f20..b9ea973 100644 --- a/build/getSupplyAcrossNetworks.js +++ b/build/getSupplyAcrossNetworks.js @@ -18,31 +18,31 @@ const web3_1 = __importDefault(require("web3")); const config_1 = require("./config"); const utils_1 = require("./utils"); // export interface NonCirculatingSupplyBalance { -// ChainId: string; -// Address: string; -// TokenContractAddress: string; -// Name: string; +// chainId: string; +// address: string; +// tokenContractAddress: string; +// name: string; // balance: BigNumber; // } function getNonCirculatingSupplyBalances() { return __awaiter(this, void 0, void 0, function* () { const nonCirculatingSupplyBalances = []; let total = new bignumber_js_1.default(0); - for (const { ChainId, Address, JsonRpcUrl, TokenContractAddress, Name } of config_1.nonCirculatingSupplyAddressesConfig) { + for (const { chainId, address, jsonRpcUrl, tokenContractAddress, name } of yield (0, config_1.getNonCirculatingSupplyAddressConfigurations)()) { let balance; - if (ChainId === "bnbBeaconChain") { - balance = new bignumber_js_1.default(yield (0, utils_1.getBep2TokenBalance)(Address, JsonRpcUrl, TokenContractAddress)); + if (chainId === "bnbBeaconChain") { + balance = new bignumber_js_1.default(yield (0, utils_1.getBep2TokenBalance)(address, jsonRpcUrl, tokenContractAddress)); } else { - balance = new bignumber_js_1.default(yield (0, utils_1.getErc20TokenBalance)(Address, JsonRpcUrl, TokenContractAddress)); + balance = new bignumber_js_1.default(yield (0, utils_1.getErc20TokenBalance)(address, jsonRpcUrl, tokenContractAddress)); } total = total.plus(balance); nonCirculatingSupplyBalances.push({ - ChainId, - Address, - TokenContractAddress, - Name, - Balance: balance + chainId, + address, + tokenContractAddress, + name, + balance: balance }); } return { diff --git a/build/server.js b/build/server.js index 5337fc7..1294401 100644 --- a/build/server.js +++ b/build/server.js @@ -42,9 +42,10 @@ app.get('/totalSupply', (req, res) => __awaiter(void 0, void 0, void 0, function res.status(500).send('Error getting total supply'); } })); -app.get("/nonCirculatingSupplyAddresses", (req, res) => { - res.json(config_1.nonCirculatingSupplyAddressesConfig); -}); +app.get("/nonCirculatingSupplyAddresses", (req, res) => __awaiter(void 0, void 0, void 0, function* () { + const nonCirculatingSupplyAddressConfigurations = yield (0, config_1.getNonCirculatingSupplyAddressConfigurations)(); + res.json(nonCirculatingSupplyAddressConfigurations); +})); app.get('/nonCirculatingSupplyBalancesByAddress', (req, res) => __awaiter(void 0, void 0, void 0, function* () { try { const nonCirculatingSupplyBalances = yield (0, getSupplyAcrossNetworks_1.getNonCirculatingSupplyBalances)(); @@ -58,7 +59,7 @@ app.get('/nonCirculatingSupplyBalancesByAddress', (req, res) => __awaiter(void 0 app.get('/nonCirculatingSupplyBalance', (req, res) => __awaiter(void 0, void 0, void 0, function* () { try { const { balances } = yield (0, getSupplyAcrossNetworks_1.getNonCirculatingSupplyBalances)(); - const totalBalance = balances.reduce((sum, balance) => sum.plus(balance.Balance), new bignumber_js_1.default(0)); + const totalBalance = balances.reduce((sum, balance) => sum.plus(balance.balance), new bignumber_js_1.default(0)); res.send(totalBalance.toString()); } catch (error) { @@ -72,7 +73,7 @@ app.get('/circulatingSupplyBalance', (req, res) => __awaiter(void 0, void 0, voi const totalSupplyData = yield (0, getSupplyAcrossNetworks_1.getTotalSupplyAcrossNetworks)(networks); const totalSupply = new bignumber_js_1.default(totalSupplyData.total); const { balances } = yield (0, getSupplyAcrossNetworks_1.getNonCirculatingSupplyBalances)(); - const nonCirculatingSupply = balances.reduce((sum, balance) => sum.plus(balance.Balance), new bignumber_js_1.default(0)); + const nonCirculatingSupply = balances.reduce((sum, balance) => sum.plus(balance.balance), new bignumber_js_1.default(0)); const circulatingSupply = totalSupply.minus(nonCirculatingSupply); res.send(circulatingSupply.toString()); } diff --git a/package-lock.json b/package-lock.json index 6e25e4d..bf8f71f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "@types/axios": "^0.14.0", "@types/bignumber.js": "^5.0.0", "@types/node": "^18.15.5", + "@types/node-fetch": "^2.6.3", "@types/web3": "^1.2.2", "nodemon": "^2.0.22", "ts-node": "^10.9.1", @@ -612,6 +613,30 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.5.tgz", "integrity": "sha512-Ark2WDjjZO7GmvsyFFf81MXuGTA/d6oP38anyxWOL6EREyBKAxKoFHwBhaZxCfLRLpO8JgVXwqOwSwa7jRcjew==" }, + "node_modules/@types/node-fetch": { + "version": "2.6.3", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.3.tgz", + "integrity": "sha512-ETTL1mOEdq/sxUtgtOhKjyB2Irra4cjxksvcMUR5Zr4n+PxVhsCD9WS46oPbHL3et9Zde7CNRr+WUNlcHvsX+w==", + "dev": true, + "dependencies": { + "@types/node": "*", + "form-data": "^3.0.0" + } + }, + "node_modules/@types/node-fetch/node_modules/form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", + "dev": true, + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/@types/pbkdf2": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/@types/pbkdf2/-/pbkdf2-3.1.0.tgz", @@ -4636,6 +4661,29 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.5.tgz", "integrity": "sha512-Ark2WDjjZO7GmvsyFFf81MXuGTA/d6oP38anyxWOL6EREyBKAxKoFHwBhaZxCfLRLpO8JgVXwqOwSwa7jRcjew==" }, + "@types/node-fetch": { + "version": "2.6.3", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.3.tgz", + "integrity": "sha512-ETTL1mOEdq/sxUtgtOhKjyB2Irra4cjxksvcMUR5Zr4n+PxVhsCD9WS46oPbHL3et9Zde7CNRr+WUNlcHvsX+w==", + "dev": true, + "requires": { + "@types/node": "*", + "form-data": "^3.0.0" + }, + "dependencies": { + "form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", + "dev": true, + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + } + } + }, "@types/pbkdf2": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/@types/pbkdf2/-/pbkdf2-3.1.0.tgz", diff --git a/package.json b/package.json index 3f10f29..a22a6e4 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@types/axios": "^0.14.0", "@types/bignumber.js": "^5.0.0", "@types/node": "^18.15.5", + "@types/node-fetch": "^2.6.3", "@types/web3": "^1.2.2", "nodemon": "^2.0.22", "ts-node": "^10.9.1", diff --git a/src/config-example.ts b/src/config-example.ts deleted file mode 100644 index 67988ce..0000000 --- a/src/config-example.ts +++ /dev/null @@ -1,58 +0,0 @@ -// src/config.ts -import { NetworkConfigurations } from "./getSupplyAcrossNetworks"; - -const networks: NetworkConfigurations = { - Ethereum: { - jsonRpcUrl: "ADD_YOUR_ETHEREUM_RPC_ENDPOINT", - tokenContractAddress: "ADD_YOUR_ETHEREUM_TOKEN_SMART_CONTRACT_ADDRESS_IN_LOWER_CASE", - }, - BSC: { - jsonRpcUrl: "ADD_YOUR_BSC_RPC_ENDPOINT", - tokenContractAddress: "ADD_YOUR_BSC_TOKEN_SMART_CONTRACT_ADDRESS_IN_LOWER_CASE", - }, -/* -Create a file in src folder called config.ts, then add as many networks as your token is deployed on, -just provide a network object with the key of network name such as Polygon, Arbitrum etc -with the jsonRpcUrl of your choice, this can be a public RPC Url (Not recommended due to rate limiting) -or a paid one through Chainstack or other providers. Also provide the token smart contract address in lower -case. See example - Arbitrum: { - jsonRpcUrl: "https://arb1.arbitrum.io/rpc", - tokenContractAddress: "0x9f6abbf0ba6b5bfa27f4deb6597cc6ec20573fda", - }, - Avalanche: { - jsonRpcUrl: "https://api.avax.network/ext/bc/C/rpc", - tokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - } - - The finished file should look something like this if your token is deployed on Ethereum, Polygon, and BSC - - // src/config.ts - import { NetworkConfigurations } from "./getSupplyAcrossNetworks"; - - const networks: NetworkConfigurations = { - Ethereum: { - jsonRpcUrl: "https://rpc.ankr.com/eth", - tokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - }, - BSC: { - jsonRpcUrl: "https://rpc.ankr.com/bsc", - tokenContractAddress: "0xa719b8ab7ea7af0ddb4358719a34631bb79d15dc", - }, - Polygon: { - jsonRpcUrl: "https://polygon-rpc.com/", - tokenContractAddress: "0xd99bafe5031cc8b345cb2e8c80135991f12d7130", - } - }; - - export { networks }; -*/ - - - -}; - -export { networks }; - - - diff --git a/src/config.ts b/src/config.ts index 6cf4db7..57aa647 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,6 @@ // src/config.ts -import { NetworkConfigurations } from "./getSupplyAcrossNetworks"; +import fetch from "node-fetch"; +import { AddressConfiguration, AddressConfigurationInput, NetworkConfigurations } from "./types"; import Web3 from "web3"; import { AbiItem } from "web3-utils"; import erc20Abi from "./erc20Abi.json"; // Make sure you have the ERC20 ABI JSON file in your project @@ -63,95 +64,90 @@ async function getNetworkConfigurations(): Promise { if (network) { networks[network.name] = { jsonRpcUrl: network.jsonRpcUrl, - tokenContractAddress: item.tokenContractAddress + tokenContractAddress: item.tokenContractAddress, + chainId: item.network.chainId }; } } - return networks; } -interface AddressConfiguration { - Name: string; - Address: string; - TokenContractAddress: string; - JsonRpcUrl: string; - ChainId: string; -} - -const nonCirculatingSupplyAddressesConfig: AddressConfiguration[] = [ +const nonCirculatingSupplyAddressesConfigInput: AddressConfigurationInput[] = [ { - Name: "Deployer", - Address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", - TokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - JsonRpcUrl: "https://nd-770-685-838.p2pify.com/e30d3ea257d1588823179ce4d5811a61", - ChainId: "1" + name: "Deployer", + address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", + chainId: "1" }, { - Name: "Treasury", - Address: "0x517873ca1edaaa0f6403a0dab2cb0162433de9d1", - TokenContractAddress: "0xa719b8ab7ea7af0ddb4358719a34631bb79d15dc", - JsonRpcUrl: "https://nd-605-906-592.p2pify.com/df9065025f5e18317e708040b1f2ab13", - ChainId: "56" + name: "Treasury", + address: "0x517873ca1edaaa0f6403a0dab2cb0162433de9d1", + chainId: "56" }, { - Name: "Deployer", - Address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", - TokenContractAddress: "0xd99bafe5031cc8b345cb2e8c80135991f12d7130", - JsonRpcUrl: "https://nd-662-671-431.p2pify.com/72aea5a70bbd5482f9a498540072b1e1", - ChainId: "137" + name: "Deployer", + address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", + chainId: "137" }, { - Name: "Deployer", - Address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", - TokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - JsonRpcUrl: "https://nd-900-134-973.p2pify.com/0a4e07e77ebc245f0bf7839745b4803b/ext/bc/C/rpc", - ChainId: "43114" + name: "Deployer", + address: "0xc2fdcb728170192c72ada2c08957f2e9390076b7", + chainId: "43114" }, // { - // Name: "Deployer", - // Address: "bnb1um8ntkgwle8yrdk0yn5hwdf7hckjpyjjg29k2p", - // TokenContractAddress: "FRM-DE7", - // JsonRpcUrl: "https://dex.binance.org", - // ChainId: "bnbBeaconChain" + // name: "Deployer", + // address: "bnb1um8ntkgwle8yrdk0yn5hwdf7hckjpyjjg29k2p", + // chainId: "bnbBeaconChain" // }, { - Name: "Treasury", - Address: "0xe42b80dA58ccEAbe0A6ECe8e3311AE939Ef6b96c", - TokenContractAddress: "0x9f6abbf0ba6b5bfa27f4deb6597cc6ec20573fda", - JsonRpcUrl: "https://nd-674-145-610.p2pify.com/bc9acaa6f1386224186fb1e794c40c14", - ChainId: "42161" + name: "Treasury", + address: "0xe42b80dA58ccEAbe0A6ECe8e3311AE939Ef6b96c", + chainId: "42161" }, { - Name: "Bridge Pool", - Address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", - TokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - JsonRpcUrl: "https://nd-770-685-838.p2pify.com/e30d3ea257d1588823179ce4d5811a61", - ChainId: "1" + name: "Bridge Pool", + address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", + chainId: "1" }, { - Name: "Bridge Pool", - Address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", - TokenContractAddress: "0xa719b8ab7ea7af0ddb4358719a34631bb79d15dc", - JsonRpcUrl: "https://nd-605-906-592.p2pify.com/df9065025f5e18317e708040b1f2ab13", - ChainId: "56" + name: "Bridge Pool", + address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", + chainId: "56" }, { - Name: "Bridge Pool", - Address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", - TokenContractAddress: "0xd99bafe5031cc8b345cb2e8c80135991f12d7130", - JsonRpcUrl: "https://nd-662-671-431.p2pify.com/72aea5a70bbd5482f9a498540072b1e1", - ChainId: "137" + name: "Bridge Pool", + address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", + chainId: "137" }, { - Name: "Bridge Pool", - Address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", - TokenContractAddress: "0xe5caef4af8780e59df925470b050fb23c43ca68c", - JsonRpcUrl: "https://nd-900-134-973.p2pify.com/0a4e07e77ebc245f0bf7839745b4803b/ext/bc/C/rpc", - ChainId: "43114" + name: "Bridge Pool", + address: "0x8e01cc26d6dd73581347c4370573ce9e59e74802", + chainId: "43114" } ]; - - -export { getNetworkConfigurations, nonCirculatingSupplyAddressesConfig }; \ No newline at end of file +async function getNonCirculatingSupplyAddressConfigurations(): Promise { + const url = `${API_URL}?tokenContractAddress=${tokenContractAddress}&chainId=${chainId}&offset=0`; + const response = await fetch(url); + const data: ApiResponse = await response.json(); + + const nonCirculatingSupplyAddresses: AddressConfiguration[] = []; + + for (const item of nonCirculatingSupplyAddressesConfigInput) { + const network = chainIdToNetworkMap[item.chainId] + let networkItemFromGatewayConfig = data.body.currencyAddressesByNetworks.find(i => i.network.chainId === item.chainId); + nonCirculatingSupplyAddresses.push( + { + name: item.name, + address: item.address, + tokenContractAddress: networkItemFromGatewayConfig?.tokenContractAddress as string, + jsonRpcUrl: network.jsonRpcUrl, + chainId: item.chainId + } + ) + } + + return nonCirculatingSupplyAddresses; +} + + +export { getNetworkConfigurations, getNonCirculatingSupplyAddressConfigurations }; \ No newline at end of file diff --git a/src/getSupplyAcrossNetworks.ts b/src/getSupplyAcrossNetworks.ts index c515bad..6dff502 100644 --- a/src/getSupplyAcrossNetworks.ts +++ b/src/getSupplyAcrossNetworks.ts @@ -4,36 +4,37 @@ import BigNumber from "bignumber.js"; import Web3 from "web3"; import erc20Abi from "./erc20Abi.json"; import axios, { AxiosResponse } from "axios"; -import { chainIdToNetworkMap, getNetworkConfigurations, nonCirculatingSupplyAddressesConfig } from "./config"; +import { chainIdToNetworkMap, getNetworkConfigurations, getNonCirculatingSupplyAddressConfigurations } from "./config"; import { NonCirculatingSupplyBalance } from './types'; import { getBep2TokenBalance, getErc20TokenBalance } from './utils'; +import { NetworkConfigurations } from "./types"; // export interface NonCirculatingSupplyBalance { -// ChainId: string; -// Address: string; -// TokenContractAddress: string; -// Name: string; +// chainId: string; +// address: string; +// tokenContractAddress: string; +// name: string; // balance: BigNumber; // } export async function getNonCirculatingSupplyBalances(): Promise<{balances: NonCirculatingSupplyBalance[], total: BigNumber}> { const nonCirculatingSupplyBalances: NonCirculatingSupplyBalance[] = []; - let total = new BigNumber(0); + let total = new BigNumber(0); - for (const { ChainId, Address, JsonRpcUrl, TokenContractAddress, Name } of nonCirculatingSupplyAddressesConfig) { + for (const { chainId, address, jsonRpcUrl, tokenContractAddress, name } of await getNonCirculatingSupplyAddressConfigurations()) { let balance: BigNumber; - if (ChainId === "bnbBeaconChain") { - balance = new BigNumber(await getBep2TokenBalance(Address, JsonRpcUrl, TokenContractAddress)); + if (chainId === "bnbBeaconChain") { + balance = new BigNumber(await getBep2TokenBalance(address, jsonRpcUrl, tokenContractAddress)); } else { - balance = new BigNumber(await getErc20TokenBalance(Address, JsonRpcUrl, TokenContractAddress)); + balance = new BigNumber(await getErc20TokenBalance(address, jsonRpcUrl, tokenContractAddress)); } total = total.plus(balance); nonCirculatingSupplyBalances.push({ - ChainId, - Address, - TokenContractAddress, - Name, - Balance: balance + chainId, + address, + tokenContractAddress, + name, + balance: balance }); } @@ -43,16 +44,6 @@ export async function getNonCirculatingSupplyBalances(): Promise<{balances: NonC }; } - -interface NetworkConfiguration { - jsonRpcUrl: string; - tokenContractAddress: string; -} - -export type NetworkConfigurations = { - [network: string]: NetworkConfiguration; -}; - export const getTotalSupplyAcrossNetworks = async ( networks: NetworkConfigurations ): Promise<{ [network: string]: string; total: string }> => { diff --git a/src/server.ts b/src/server.ts index fdd88fe..2c3aaa7 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3,7 +3,7 @@ import express from "express"; import bodyParser from 'body-parser'; import BigNumber from "bignumber.js"; import { getTotalSupplyAcrossNetworks, getNonCirculatingSupplyBalances } from "./getSupplyAcrossNetworks"; -import { getNetworkConfigurations, nonCirculatingSupplyAddressesConfig } from "./config"; +import { getNetworkConfigurations, getNonCirculatingSupplyAddressConfigurations } from "./config"; import { NonCirculatingSupplyBalance } from './types'; @@ -34,8 +34,9 @@ app.get('/totalSupply', async (req, res) => { } }); -app.get("/nonCirculatingSupplyAddresses", (req, res) => { - res.json(nonCirculatingSupplyAddressesConfig); +app.get("/nonCirculatingSupplyAddresses", async (req, res) => { + const nonCirculatingSupplyAddressConfigurations = await getNonCirculatingSupplyAddressConfigurations(); + res.json(nonCirculatingSupplyAddressConfigurations); }); app.get('/nonCirculatingSupplyBalancesByAddress', async (req, res) => { @@ -51,7 +52,7 @@ app.get('/nonCirculatingSupplyBalancesByAddress', async (req, res) => { app.get('/nonCirculatingSupplyBalance', async (req, res) => { try { const { balances }: { balances: NonCirculatingSupplyBalance[] } = await getNonCirculatingSupplyBalances(); - const totalBalance = balances.reduce((sum, balance) => sum.plus(balance.Balance), new BigNumber(0)); + const totalBalance = balances.reduce((sum, balance) => sum.plus(balance.balance), new BigNumber(0)); res.send(totalBalance.toString()); } catch (error) { console.error('Error fetching non-circulating supply balances:', error); @@ -66,7 +67,7 @@ app.get('/circulatingSupplyBalance', async (req, res) => { const totalSupply = new BigNumber(totalSupplyData.total); const { balances }: { balances: NonCirculatingSupplyBalance[] } = await getNonCirculatingSupplyBalances(); - const nonCirculatingSupply = balances.reduce((sum, balance) => sum.plus(balance.Balance), new BigNumber(0)); + const nonCirculatingSupply = balances.reduce((sum, balance) => sum.plus(balance.balance), new BigNumber(0)); const circulatingSupply = totalSupply.minus(nonCirculatingSupply); res.send(circulatingSupply.toString()); diff --git a/src/types.ts b/src/types.ts index 2f0b917..509b8d9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,10 +3,34 @@ import BigNumber from "bignumber.js"; export interface NonCirculatingSupplyBalance { - ChainId: string; - Address: string; - TokenContractAddress: string; - Name: string; - Balance: BigNumber; + chainId: string; + address: string; + tokenContractAddress: string; + name: string; + balance: BigNumber; + } + + interface NetworkConfiguration { + jsonRpcUrl: string; + tokenContractAddress: string; + chainId: string; + } + + export type NetworkConfigurations = { + [network: string]: NetworkConfiguration; + }; + + export interface AddressConfigurationInput { + name: string; + address: string; + chainId: string; + } + + export interface AddressConfiguration { + name: string; + address: string; + tokenContractAddress: string; + jsonRpcUrl: string; + chainId: string; } \ No newline at end of file