diff --git a/examples/bridges.mainnet.yml b/examples/bridges.mainnet.yml index 01475a7..8cf8c57 100644 --- a/examples/bridges.mainnet.yml +++ b/examples/bridges.mainnet.yml @@ -1,18 +1,6 @@ registers: - - bridge: arbitrum->polygon - symbol: usdt - type: lnv3 - contract: '0xbA5D580B18b6436411562981e02c8A9aA1776D10' - sourceTokenAddress: '0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9' - targetTokenAddress: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f' - baseFee: 0.1 - liquidityFeeRate: 0.1 - transferLimit: 2000 - deposit: 30 - approve: 10000000000000 - safeWalletAddress: '0x000000000Bb6a011dB294ce3F3423f00EAc4959e' - sourceSafeWalletUrl: 'https://safe-transaction-arbitrum.safe.global/api' + - include: arbitrum-polygon.yml - bridge: polygon->arbitrum symbol: usdt diff --git a/examples/includes/mainnet/arbitrum-polygon.yml b/examples/includes/mainnet/arbitrum-polygon.yml new file mode 100644 index 0000000..030bcd7 --- /dev/null +++ b/examples/includes/mainnet/arbitrum-polygon.yml @@ -0,0 +1,13 @@ +- bridge: arbitrum->polygon + symbol: usdt + type: lnv3 + contract: '0xbA5D580B18b6436411562981e02c8A9aA1776D10' + sourceTokenAddress: '0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9' + targetTokenAddress: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f' + baseFee: 0.1 + liquidityFeeRate: 0.1 + transferLimit: 2000 + deposit: 30 + approve: 10000000000000 + safeWalletAddress: '0x000000000Bb6a011dB294ce3F3423f00EAc4959e' + sourceSafeWalletUrl: 'https://safe-transaction-arbitrum.safe.global/api' diff --git a/src/register/index.js b/src/register/index.js index 6e26829..9fe2ba4 100644 --- a/src/register/index.js +++ b/src/register/index.js @@ -49,17 +49,63 @@ async function registerWithGroup(options, group) { const registers = bridgeConfig.registers; for (const register of registers) { - console.log(`==> start register [${register.type}] [${register.symbol}] ${register.bridge}`); - await handle({ - ...options, - register, - }); + const patches = await refactorConfig({...options, registers, register, group}); + if (patches.length) { + for (const ir of patches) { + console.log(`==> start register [${ir.type}] [${ir.symbol}] ${ir.bridge}`); + await handle({ + ...options, + register: ir, + }); + } + } else { + console.log(`==> start register [${register.type}] [${register.symbol}] ${register.bridge}`); + await handle({ + ...options, + register, + }); + } console.log('-----------------------') console.log('') console.log('') } } +async function refactorConfig(options) { + const {definition, registers, register, group} = options; + const include = register.include; + if (!include) { + return []; + } + const keys = Object.keys(register); + 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}/${include}`); + if (fs.existsSync(pathOfGroupInclude)) { + includeFileContent = await fs.readFile(pathOfGroupInclude, 'utf8'); + } + const includeConfigs = YAML.parse(includeFileContent); + 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) { + throw new Error(`duplicated config {bridge: ${ic.bridge}, symbol: ${ic.symbol}, type: ${ic.type}}`); + } + } + return includeConfigs; +} + async function handle(options) { const {definition, register} = options; const [sourceChainName, targetChainName] = register.bridge.split('->');