Skip to content

Commit

Permalink
chore: add config key validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehoang92 committed Dec 26, 2024
1 parent 82c8618 commit 3762bb7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
7 changes: 1 addition & 6 deletions configs/devnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,5 @@
"label_denom": "dai",
"target_denom" : "dai"
}
],
"disabled_transfer_banner_config": {
"unsupported_tokens": [],
"temp_disabled_transfer_tokens": {},
"temp_disabled_bridges": {}
}
]
}
7 changes: 1 addition & 6 deletions configs/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
}
]
}
7 changes: 1 addition & 6 deletions configs/testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,5 @@
"label_denom": "dai",
"target_denom" : "dai"
}
],
"disabled_transfer_banner_config": {
"unsupported_tokens": [],
"temp_disabled_transfer_tokens": {},
"temp_disabled_bridges": {}
}
]
}
46 changes: 41 additions & 5 deletions scripts/check_configs.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 3762bb7

Please sign in to comment.