From 3762bb7985fd0cb5ab2041658098a8c1989c6638 Mon Sep 17 00:00:00 2001 From: kylehoang92 Date: Thu, 26 Dec 2024 15:32:54 +0700 Subject: [PATCH] chore: add config key validation --- configs/devnet.json | 7 +----- configs/mainnet.json | 7 +----- configs/testnet.json | 7 +----- scripts/check_configs.ts | 46 +++++++++++++++++++++++++++++++++++----- 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/configs/devnet.json b/configs/devnet.json index e771cf7..6e151c1 100644 --- a/configs/devnet.json +++ b/configs/devnet.json @@ -70,10 +70,5 @@ "label_denom": "dai", "target_denom" : "dai" } - ], - "disabled_transfer_banner_config": { - "unsupported_tokens": [], - "temp_disabled_transfer_tokens": {}, - "temp_disabled_bridges": {} - } + ] } \ No newline at end of file diff --git a/configs/mainnet.json b/configs/mainnet.json index ee6196a..862465d 100644 --- a/configs/mainnet.json +++ b/configs/mainnet.json @@ -507,10 +507,5 @@ "label_denom": "brdg/a02afc2c1edf77cc023eefa25fc036c184612faf9365cda9c1daa3b1675ebf8f", "target_denom": "brdg/a02afc2c1edf77cc023eefa25fc036c184612faf9365cda9c1daa3b1675ebf8f" } - ], - "disabled_transfer_banner_config": { - "unsupported_tokens": [], - "temp_disabled_transfer_tokens": {}, - "temp_disabled_bridges": {} - } + ] } diff --git a/configs/testnet.json b/configs/testnet.json index 2d4af77..cc830b8 100644 --- a/configs/testnet.json +++ b/configs/testnet.json @@ -84,10 +84,5 @@ "label_denom": "dai", "target_denom" : "dai" } - ], - "disabled_transfer_banner_config": { - "unsupported_tokens": [], - "temp_disabled_transfer_tokens": {}, - "temp_disabled_bridges": {} - } + ] } diff --git a/scripts/check_configs.ts b/scripts/check_configs.ts index 5a77d26..e54df1c 100644 --- a/scripts/check_configs.ts +++ b/scripts/check_configs.ts @@ -1,5 +1,6 @@ -import { BlockchainUtils, CarbonSDK } from "carbon-js-sdk"; +import { CarbonSDK } from "carbon-js-sdk"; import { PageRequest } from "carbon-js-sdk/lib/codec/cosmos/base/query/v1beta1/pagination"; +import { BridgeMap } from "carbon-js-sdk/lib/util/blockchain"; import * as fs from "fs"; import Long from "long"; @@ -466,11 +467,29 @@ function isValidMarketPromo(marketPromo: {[marketId: string]: MarketPromo}, netw return true; } -function isValidTransferBanner(transferBanner: DisabledTransferBannerConfig, network: CarbonSDK.Network): boolean { - const { temp_disabled_transfer_tokens = {}, temp_disabled_bridges = {} } = transferBanner; - const disabledTokenKeys = Object.keys(temp_disabled_transfer_tokens) +function isValidDisabledTransferBannerConfig(transferBanner: DisabledTransferBannerConfig, denoms: string[], bridges: string[], network: CarbonSDK.Network): boolean { + const { unsupported_tokens = [], temp_disabled_transfer_tokens = {}, temp_disabled_bridges = {} } = transferBanner; + + if (unsupported_tokens.length > 0) { + const validUnsupportedTokensOutcome = checkValidEntries(unsupported_tokens, denoms); + if (validUnsupportedTokensOutcome.status && isErrorOutcome(validUnsupportedTokensOutcome)) { + const invalidUnsupportedTokensStr = joinEntriesIntoStr(validUnsupportedTokensOutcome.entry!); + console.error(`[ERROR] disabled_transfer_banner_config.unsupported_tokens of ${network}.json has the following invalid token denoms: ${invalidUnsupportedTokensStr}. Please make sure to input only valid token denoms.`); + return false + } + } + + const disabledTokenKeys = Object.keys(temp_disabled_transfer_tokens) if (disabledTokenKeys.length > 0) { + const validDisabledTknsOutcome = checkValidEntries(disabledTokenKeys, denoms); + + if (validDisabledTknsOutcome.status && isErrorOutcome(validDisabledTknsOutcome)) { + const invalidDissabedTokensStr = joinEntriesIntoStr(validDisabledTknsOutcome.entry!); + console.error(`[ERROR] disabled_transfer_banner_config.temp_disabled_transfer_tokens of ${network}.json has the following invalid token denoms: ${invalidDissabedTokensStr}. Please make sure to input only valid token denoms.`); + return false + } + disabledTokenKeys.map((key) => { const { start, end } = temp_disabled_transfer_tokens[key]; if (end && start) { @@ -486,6 +505,13 @@ function isValidTransferBanner(transferBanner: DisabledTransferBannerConfig, net const disabledBridgeKeys = Object.keys(temp_disabled_bridges) if (disabledBridgeKeys.length > 0) { + const validDisableBridgesOutcome = checkValidEntries(disabledBridgeKeys, bridges); + if (validDisableBridgesOutcome.status && isErrorOutcome(validDisableBridgesOutcome)) { + const invalidDissabedBridgesStr = joinEntriesIntoStr(validDisableBridgesOutcome.entry!); + console.error(`[ERROR] disabled_transfer_banner_config.temp_disabled_bridges of ${network}.json has the following invalid bridge addreses: ${invalidDissabedBridgesStr}. Please make sure to input only valid bridge addresses.`); + return false + } + disabledBridgeKeys.map((key) => { const { start, end } = temp_disabled_bridges[key]; if (start && end) { @@ -649,6 +675,16 @@ async function main() { outcomeMap[network] = false; } + // query all bridges + const bridgesMap: BridgeMap | undefined = sdk?.token?.bridges + let bridgesArr: string[] = [] + + if (bridgesMap && bridgesMap.ibc.length && bridgesMap.polynetwork.length) { + const polynetworkBridges = bridgesMap.polynetwork.map((bridge) => bridge.bridgeAddresses).flat() + const ibcBridges = bridgesMap.ibc.map((bridge) => bridge.bridgeAddresses).flat() + bridgesArr = polynetworkBridges.concat(ibcBridges) + } + // transfer disabled tokens object check const isTransferDisabledTokensValid = isValidTransferDisabledTokens(jsonData.transfer_disabled_tokens, tokens, network); if (!isTransferDisabledTokensValid) outcomeMap[network] = false; @@ -889,7 +925,7 @@ async function main() { } // transfer banner check - if (jsonData.disabled_transfer_banner_config && !isValidTransferBanner(jsonData.disabled_transfer_banner_config, network)) { + if (jsonData.disabled_transfer_banner_config && !isValidDisabledTransferBannerConfig(jsonData.disabled_transfer_banner_config, tokens, bridgesArr, network)) { outcomeMap[network] = false; }