Skip to content

Commit

Permalink
add include feature for generate configure (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
fewensa authored Jul 25, 2024
1 parent ea46ffd commit 8c07858
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
12 changes: 1 addition & 11 deletions examples/bridges.mainnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions examples/includes/mainnet/configures/arbitrum-polygon.yml
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion src/ecosys/arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export function datadir() {
}

export function datapath(file) {
return `${datadir()}${file}`
return `${datadir()}/${file}`
}

48 changes: 48 additions & 0 deletions src/generator/generate_configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand All @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions src/register/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}`);
}
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8c07858

Please sign in to comment.