From 30851e230bf0a3fed726307478fb552b1f576799 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Mon, 5 Feb 2024 18:02:35 +0400 Subject: [PATCH] types --- data/addresses.mainnet.json | 48 ++++++++++++------------ data/addresses.testnet.json | 58 ++++++++++++++--------------- lib/addresses.ts | 3 +- lib/index.ts | 1 + lib/types.ts | 4 ++ package.json | 2 +- scripts/generate_addresses.sh | 6 ++- scripts/generate_addresses_types.ts | 42 +++++++++++++++++++++ 8 files changed, 108 insertions(+), 56 deletions(-) create mode 100644 lib/types.ts create mode 100644 scripts/generate_addresses_types.ts diff --git a/data/addresses.mainnet.json b/data/addresses.mainnet.json index 7cbd4aa2..b352fb0b 100644 --- a/data/addresses.mainnet.json +++ b/data/addresses.mainnet.json @@ -161,45 +161,45 @@ "type": "fungibleModule" }, { - "address": "0x0000028a2eB8346cd5c0267856aB7594B7a55308", + "address": "0xf091867EC603A6628eD83D274E835539D82e9cc8", "category": "messaging", - "chain_id": "56", - "chain_name": "bsc_mainnet", + "chain_id": "1", + "chain_name": "eth_mainnet", "type": "zetaToken" }, { - "address": "0x000063A6e758D9e2f438d430108377564cf4077D", + "address": "0x000007Cf399229b2f5A4D043F20E90C9C98B7C6a", "category": "messaging", - "chain_id": "56", - "chain_name": "bsc_mainnet", + "chain_id": "1", + "chain_name": "eth_mainnet", "type": "connector" }, { - "address": "0x00000fF8fA992424957F97688015814e707A0115", + "address": "0x0000030Ec64DF25301d8414eE5a29588C4B0dE10", "category": "omnichain", - "chain_id": "56", - "chain_name": "bsc_mainnet", + "chain_id": "1", + "chain_name": "eth_mainnet", "type": "erc20Custody" }, { - "address": "0xf091867EC603A6628eD83D274E835539D82e9cc8", + "address": "0x0000028a2eB8346cd5c0267856aB7594B7a55308", "category": "messaging", - "chain_id": "1", - "chain_name": "eth_mainnet", + "chain_id": "56", + "chain_name": "bsc_mainnet", "type": "zetaToken" }, { - "address": "0x000007Cf399229b2f5A4D043F20E90C9C98B7C6a", + "address": "0x000063A6e758D9e2f438d430108377564cf4077D", "category": "messaging", - "chain_id": "1", - "chain_name": "eth_mainnet", + "chain_id": "56", + "chain_name": "bsc_mainnet", "type": "connector" }, { - "address": "0x0000030Ec64DF25301d8414eE5a29588C4B0dE10", + "address": "0x00000fF8fA992424957F97688015814e707A0115", "category": "omnichain", - "chain_id": "1", - "chain_name": "eth_mainnet", + "chain_id": "56", + "chain_name": "bsc_mainnet", "type": "erc20Custody" }, { @@ -217,17 +217,17 @@ "type": "tssUpdater" }, { - "address": "0xaf28a257D292e7f0E531073f70a175b57E0261a8", + "address": "0xaeB6dDB7708467814D557e340283248be8E43124", "category": "messaging", - "chain_id": "56", - "chain_name": "bsc_mainnet", + "chain_id": "1", + "chain_name": "eth_mainnet", "type": "pauser" }, { - "address": "0xaeB6dDB7708467814D557e340283248be8E43124", + "address": "0xaf28a257D292e7f0E531073f70a175b57E0261a8", "category": "messaging", - "chain_id": "1", - "chain_name": "eth_mainnet", + "chain_id": "56", + "chain_name": "bsc_mainnet", "type": "pauser" } ] diff --git a/data/addresses.testnet.json b/data/addresses.testnet.json index 575ff9bd..2975e138 100644 --- a/data/addresses.testnet.json +++ b/data/addresses.testnet.json @@ -160,27 +160,6 @@ "address": "0x735b14BB79463307AAcBED86DAf3322B1e6226aB", "type": "fungibleModule" }, - { - "address": "0x0000c9ec4042283e8139c74f4c64bcd1e0b9b54f", - "category": "messaging", - "chain_id": "80001", - "chain_name": "mumbai_testnet", - "type": "zetaToken" - }, - { - "address": "0x0000ecb8cdd25a18f12daa23f6422e07fbf8b9e1", - "category": "messaging", - "chain_id": "80001", - "chain_name": "mumbai_testnet", - "type": "connector" - }, - { - "address": "0x0000a7db254145767262c6a81a7ee1650684258e", - "category": "omnichain", - "chain_id": "80001", - "chain_name": "mumbai_testnet", - "type": "erc20Custody" - }, { "address": "0x0000c304d2934c00db1d51995b9f6996affd17c0", "category": "messaging", @@ -224,17 +203,31 @@ "type": "erc20Custody" }, { - "address": "0x55122f7590164Ac222504436943FAB17B62F5d7d", + "address": "0x0000c9ec4042283e8139c74f4c64bcd1e0b9b54f", + "category": "messaging", + "chain_id": "80001", + "chain_name": "mumbai_testnet", + "type": "zetaToken" + }, + { + "address": "0x0000ecb8cdd25a18f12daa23f6422e07fbf8b9e1", + "category": "messaging", + "chain_id": "80001", + "chain_name": "mumbai_testnet", + "type": "connector" + }, + { + "address": "0x0000a7db254145767262c6a81a7ee1650684258e", "category": "omnichain", "chain_id": "80001", "chain_name": "mumbai_testnet", - "type": "tssUpdater" + "type": "erc20Custody" }, { "address": "0x55122f7590164Ac222504436943FAB17B62F5d7d", "category": "omnichain", - "chain_id": "97", - "chain_name": "bsc_testnet", + "chain_id": "80001", + "chain_name": "mumbai_testnet", "type": "tssUpdater" }, { @@ -246,10 +239,10 @@ }, { "address": "0x55122f7590164Ac222504436943FAB17B62F5d7d", - "category": "messaging", - "chain_id": "80001", - "chain_name": "mumbai_testnet", - "type": "pauser" + "category": "omnichain", + "chain_id": "97", + "chain_name": "bsc_testnet", + "type": "tssUpdater" }, { "address": "0x55122f7590164Ac222504436943FAB17B62F5d7d", @@ -258,6 +251,13 @@ "chain_name": "goerli_testnet", "type": "pauser" }, + { + "address": "0x55122f7590164Ac222504436943FAB17B62F5d7d", + "category": "messaging", + "chain_id": "80001", + "chain_name": "mumbai_testnet", + "type": "pauser" + }, { "address": "0x55122f7590164Ac222504436943FAB17B62F5d7d", "category": "messaging", diff --git a/lib/addresses.ts b/lib/addresses.ts index 2d374e16..c429f1b7 100644 --- a/lib/addresses.ts +++ b/lib/addresses.ts @@ -1,8 +1,9 @@ import testnet from "../data/addresses.testnet.json"; import mainnet from "../data/addresses.mainnet.json"; import { getChainId } from "@zetachain/networks"; +import { ParamChainName, ParamSymbol, ParamType } from "./types"; -export const getAddress = (type: any, network: any, symbol?: any) => { +export const getAddress = (type: ParamType, network: ParamChainName, symbol?: ParamSymbol) => { const networks = [...testnet, ...mainnet]; let address; if (type === "zrc20" && !symbol) { diff --git a/lib/index.ts b/lib/index.ts index 2538dee6..a30f92d8 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,5 +1,6 @@ export * from "./address.tools"; export * from "./addresses"; +export * from "./types"; export { default as testnet } from "../data/addresses.testnet.json"; export { default as mainnet } from "../data/addresses.mainnet.json"; diff --git a/lib/types.ts b/lib/types.ts new file mode 100644 index 00000000..bbcd0395 --- /dev/null +++ b/lib/types.ts @@ -0,0 +1,4 @@ +export declare type ParamSymbol = "USDC.BSC" | "USDC.ETH" | "BTC.BTC" | "BNB.BSC" | "USDT.ETH" | "USDT.BSC" | "ETH.ETH" | "USDC" | "gETH" | "tMATIC" | "tBTC" | "tBNB"; +export declare type ParamChainName = "eth_mainnet" | "bsc_mainnet" | "btc_mainnet" | "zeta_mainnet" | "zeta_testnet" | "goerli_testnet" | "bsc_testnet" | "mumbai_testnet" | "btc_testnet"; +export declare type ParamType = "tss" | "systemContract" | "connector" | "zrc20" | "uniswapv2Factory" | "wZetaContract" | "uniswapv2Router02" | "fungibleModule" | "zetaToken" | "erc20Custody" | "tssUpdater" | "pauser"; + diff --git a/package.json b/package.json index 61c6e323..546fdcde 100644 --- a/package.json +++ b/package.json @@ -79,4 +79,4 @@ }, "types": "./dist/lib/index.d.ts", "version": "0.0.8" -} +} \ No newline at end of file diff --git a/scripts/generate_addresses.sh b/scripts/generate_addresses.sh index d7146033..62e81086 100755 --- a/scripts/generate_addresses.sh +++ b/scripts/generate_addresses.sh @@ -3,4 +3,8 @@ echo "Generating protocol addresses..." npx hardhat addresses --network zeta_testnet > ./data/addresses.testnet.json -npx hardhat addresses --network zeta_mainnet > ./data/addresses.mainnet.json \ No newline at end of file +npx hardhat addresses --network zeta_mainnet > ./data/addresses.mainnet.json + +echo "Generating protocol addresses types..." + +npx ts-node scripts/generate_addresses_types.ts > ./lib/types.d.ts \ No newline at end of file diff --git a/scripts/generate_addresses_types.ts b/scripts/generate_addresses_types.ts new file mode 100644 index 00000000..70909d01 --- /dev/null +++ b/scripts/generate_addresses_types.ts @@ -0,0 +1,42 @@ +import testnet from "../data/addresses.testnet.json"; +import mainnet from "../data/addresses.mainnet.json"; + +const extractUniqueValues = (data: any[], key: string): string[] => { + const allValues = data.filter((item) => item[key] !== undefined).map((item) => item[key].toString()); + return [...new Set(allValues)]; +}; + +const generateTypesForKeys = (keys: string[]) => { + const networks = [...mainnet, ...testnet]; + + let typeDefs = ""; + + keys.forEach((key) => { + const uniqueValues = extractUniqueValues(networks, key); + if (uniqueValues.length > 0) { + const isNumeric = uniqueValues.every((value) => !isNaN(Number(value))); + const formattedValues = isNumeric ? uniqueValues.join(" | ") : `"${uniqueValues.join('" | "')}"`; + + typeDefs += `export type Param${toCamelCase(key, true)} = ${formattedValues};\n`; + } + }); + + console.log(typeDefs); +}; + +// Modify this function to handle underscores and capitalize each word +const toCamelCase = (string: string, capitalizeFirst: boolean = false) => { + return string + .split("_") + .map((word, index) => { + if (index === 0) { + return capitalizeFirst ? word.charAt(0).toUpperCase() + word.slice(1) : word; + } + return word.charAt(0).toUpperCase() + word.slice(1); + }) + .join(""); +}; + +const keysToGenerate = ["symbol", "chain_name", "type"]; + +generateTypesForKeys(keysToGenerate);