Skip to content

Commit

Permalink
add include feature (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
fewensa authored Jul 25, 2024
1 parent fa3ac7d commit ea46ffd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
14 changes: 1 addition & 13 deletions examples/bridges.mainnet.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 13 additions & 0 deletions examples/includes/mainnet/arbitrum-polygon.yml
Original file line number Diff line number Diff line change
@@ -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'
56 changes: 51 additions & 5 deletions src/register/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('->');
Expand Down

0 comments on commit ea46ffd

Please sign in to comment.