From 8c078589ad5d71766414f10d0b9c83d99dd160cb Mon Sep 17 00:00:00 2001 From: fewensa <37804932+fewensa@users.noreply.github.com> Date: Thu, 25 Jul 2024 13:12:27 +0800 Subject: [PATCH] add include feature for generate configure (#12) --- examples/bridges.mainnet.yml | 12 +---- .../mainnet/configures/arbitrum-polygon.yml | 13 +++++ .../{ => registers}/arbitrum-polygon.yml | 0 src/ecosys/arg.js | 2 +- src/generator/generate_configure.js | 48 +++++++++++++++++++ src/register/index.js | 19 ++++++-- 6 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 examples/includes/mainnet/configures/arbitrum-polygon.yml rename examples/includes/mainnet/{ => registers}/arbitrum-polygon.yml (100%) diff --git a/examples/bridges.mainnet.yml b/examples/bridges.mainnet.yml index 8cf8c57..170e344 100644 --- a/examples/bridges.mainnet.yml +++ b/examples/bridges.mainnet.yml @@ -27,17 +27,7 @@ configure: - name: polygon rpc: https://polygon-rpc.com bridges: - - direction: arbitrum->polygon - feeLimit: 100 - reorgThreshold: 10 - bridgeType: lnv3 - safeWalletAddress: '0x000000000Bb6a011dB294ce3F3423f00EAc4959e' - safeWalletUrl: 'https://safe-transaction-polygon.safe.global/api' - safeWalletRole: signer - tokens: - - symbol: usdt->usdt - swapRate: 2300 - withdrawLiquidityCountThreshold: 10 + - include: arbitrum-polygon.yml - direction: polygon->arbitrum feeLimit: 150 diff --git a/examples/includes/mainnet/configures/arbitrum-polygon.yml b/examples/includes/mainnet/configures/arbitrum-polygon.yml new file mode 100644 index 0000000..95ae696 --- /dev/null +++ b/examples/includes/mainnet/configures/arbitrum-polygon.yml @@ -0,0 +1,13 @@ + +- direction: arbitrum->polygon + feeLimit: 100 + reorgThreshold: 10 + bridgeType: lnv3 + safeWalletAddress: '0x000000000Bb6a011dB294ce3F3423f00EAc4959e' + safeWalletUrl: 'https://safe-transaction-polygon.safe.global/api' + safeWalletRole: signer + tokens: + - symbol: usdt->usdt + swapRate: 2300 + withdrawLiquidityCountThreshold: 10 + diff --git a/examples/includes/mainnet/arbitrum-polygon.yml b/examples/includes/mainnet/registers/arbitrum-polygon.yml similarity index 100% rename from examples/includes/mainnet/arbitrum-polygon.yml rename to examples/includes/mainnet/registers/arbitrum-polygon.yml diff --git a/src/ecosys/arg.js b/src/ecosys/arg.js index 3e81d03..dd29754 100644 --- a/src/ecosys/arg.js +++ b/src/ecosys/arg.js @@ -38,6 +38,6 @@ export function datadir() { } export function datapath(file) { - return `${datadir()}${file}` + return `${datadir()}/${file}` } diff --git a/src/generator/generate_configure.js b/src/generator/generate_configure.js index c33129d..dbeecc0 100644 --- a/src/generator/generate_configure.js +++ b/src/generator/generate_configure.js @@ -29,6 +29,7 @@ async function generateWithGroup(options, group) { const bridgeConfigRaw = await fs.readFile(arg.datapath(`/bridges.${group}.yml`), 'utf8'); const bridgeConfig = YAML.parse(bridgeConfigRaw); const configure = bridgeConfig.configure; + await refactorConfig({configure, group}); const CONFIGURE_PATH = arg.datapath('/configure'); const storeFile = `${CONFIGURE_PATH}/configure.${group}.json`; @@ -50,6 +51,53 @@ async function generateWithGroup(options, group) { console.log(''); } +async function refactorConfig(options) { + const {configure, group} = options; + const nbdgs = []; + for (const bridge of configure.bridges) { + const include = bridge.include; + if (!include) { + nbdgs.push(bridge); + continue; + } + + const keys = Object.keys(bridge); + if (keys.length > 1) { + throw new Error(`include mode please do not add other fields: [${keys.join(', ')}]`) + } + + let includeFileContent; + if (fs.existsSync(include)) { + includeFileContent = await fs.readFile(include, 'utf8'); + } + // check path from datapath + const pathOfIncludeFromDataPath = arg.datapath(include); + if (fs.existsSync(pathOfIncludeFromDataPath)) { + includeFileContent = await fs.readFile(pathOfIncludeFromDataPath, 'utf8'); + } + // check group file + const pathOfGroupInclude = arg.datapath(`/includes/${group}/configures/${include}`); + if (fs.existsSync(pathOfGroupInclude)) { + includeFileContent = await fs.readFile(pathOfGroupInclude, 'utf8'); + } + if (!includeFileContent) { + throw new Error(`include file ${include} not found, please check your path`); + } + const includeConfigs = YAML.parse(includeFileContent); + if (!includeConfigs) { + continue; + } + // check include configs + for (const ic of includeConfigs) { + const ickey = `${ic.direction}${ic.bridgeType}`.toUpperCase(); + if (configure.bridges.findIndex(item => `${item.direction}${item.bridgeType}`.toUpperCase() === ickey) > -1) { + throw new Error(`duplicated config {direction: ${ic.direction}, bridgeType: ${ic.bridgeType}}`); + } + } + nbdgs.push(...includeConfigs); + } + configure.bridges = nbdgs; +} async function _fillEncryptedPrivateKey(options) { const {configure} = options; diff --git a/src/register/index.js b/src/register/index.js index 9fe2ba4..d4d3b6e 100644 --- a/src/register/index.js +++ b/src/register/index.js @@ -92,14 +92,20 @@ async function refactorConfig(options) { includeFileContent = await fs.readFile(pathOfIncludeFromDataPath, 'utf8'); } // check group file - const pathOfGroupInclude = arg.datapath(`/includes/${group}/${include}`); + const pathOfGroupInclude = arg.datapath(`/includes/${group}/registers/${include}`); if (fs.existsSync(pathOfGroupInclude)) { includeFileContent = await fs.readFile(pathOfGroupInclude, 'utf8'); } + if (!includeFileContent) { + throw new Error(`include file ${include} not found, please check your path`); + } const includeConfigs = YAML.parse(includeFileContent); + if (!includeConfigs) { + return []; + } for (const ic of includeConfigs) { - const ickey = `${ic.bridge}${ic.symbol}${ic.type}`; - if (registers.findIndex(item => `${item.bridge}${item.symbol}${item.type}` === ickey) > -1) { + const ickey = `${ic.bridge}${ic.symbol}${ic.type}`.toUpperCase(); + if (registers.findIndex(item => `${item.bridge}${item.symbol}${item.type}`.toUpperCase() === ickey) > -1) { throw new Error(`duplicated config {bridge: ${ic.bridge}, symbol: ${ic.symbol}, type: ${ic.type}}`); } } @@ -165,7 +171,12 @@ async function hashRegister(register) { keys.sort(); let merged = ''; for (const key of keys) { - merged += register[key]; + const rv = register[key]; + if (typeof rv === 'object') { + merged += JSON.stringify(rv); + } else { + merged += rv; + } } const hash = await $`echo "${merged}" | sha256sum | cut -d ' ' -f1`; return {