Skip to content

Commit 8c07858

Browse files
authored
add include feature for generate configure (#12)
1 parent ea46ffd commit 8c07858

File tree

6 files changed

+78
-16
lines changed

6 files changed

+78
-16
lines changed

examples/bridges.mainnet.yml

+1-11
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,7 @@ configure:
2727
- name: polygon
2828
rpc: https://polygon-rpc.com
2929
bridges:
30-
- direction: arbitrum->polygon
31-
feeLimit: 100
32-
reorgThreshold: 10
33-
bridgeType: lnv3
34-
safeWalletAddress: '0x000000000Bb6a011dB294ce3F3423f00EAc4959e'
35-
safeWalletUrl: 'https://safe-transaction-polygon.safe.global/api'
36-
safeWalletRole: signer
37-
tokens:
38-
- symbol: usdt->usdt
39-
swapRate: 2300
40-
withdrawLiquidityCountThreshold: 10
30+
- include: arbitrum-polygon.yml
4131

4232
- direction: polygon->arbitrum
4333
feeLimit: 150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
- direction: arbitrum->polygon
3+
feeLimit: 100
4+
reorgThreshold: 10
5+
bridgeType: lnv3
6+
safeWalletAddress: '0x000000000Bb6a011dB294ce3F3423f00EAc4959e'
7+
safeWalletUrl: 'https://safe-transaction-polygon.safe.global/api'
8+
safeWalletRole: signer
9+
tokens:
10+
- symbol: usdt->usdt
11+
swapRate: 2300
12+
withdrawLiquidityCountThreshold: 10
13+

src/ecosys/arg.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ export function datadir() {
3838
}
3939

4040
export function datapath(file) {
41-
return `${datadir()}${file}`
41+
return `${datadir()}/${file}`
4242
}
4343

src/generator/generate_configure.js

+48
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ async function generateWithGroup(options, group) {
2929
const bridgeConfigRaw = await fs.readFile(arg.datapath(`/bridges.${group}.yml`), 'utf8');
3030
const bridgeConfig = YAML.parse(bridgeConfigRaw);
3131
const configure = bridgeConfig.configure;
32+
await refactorConfig({configure, group});
3233

3334
const CONFIGURE_PATH = arg.datapath('/configure');
3435
const storeFile = `${CONFIGURE_PATH}/configure.${group}.json`;
@@ -50,6 +51,53 @@ async function generateWithGroup(options, group) {
5051
console.log('');
5152
}
5253

54+
async function refactorConfig(options) {
55+
const {configure, group} = options;
56+
const nbdgs = [];
57+
for (const bridge of configure.bridges) {
58+
const include = bridge.include;
59+
if (!include) {
60+
nbdgs.push(bridge);
61+
continue;
62+
}
63+
64+
const keys = Object.keys(bridge);
65+
if (keys.length > 1) {
66+
throw new Error(`include mode please do not add other fields: [${keys.join(', ')}]`)
67+
}
68+
69+
let includeFileContent;
70+
if (fs.existsSync(include)) {
71+
includeFileContent = await fs.readFile(include, 'utf8');
72+
}
73+
// check path from datapath
74+
const pathOfIncludeFromDataPath = arg.datapath(include);
75+
if (fs.existsSync(pathOfIncludeFromDataPath)) {
76+
includeFileContent = await fs.readFile(pathOfIncludeFromDataPath, 'utf8');
77+
}
78+
// check group file
79+
const pathOfGroupInclude = arg.datapath(`/includes/${group}/configures/${include}`);
80+
if (fs.existsSync(pathOfGroupInclude)) {
81+
includeFileContent = await fs.readFile(pathOfGroupInclude, 'utf8');
82+
}
83+
if (!includeFileContent) {
84+
throw new Error(`include file ${include} not found, please check your path`);
85+
}
86+
const includeConfigs = YAML.parse(includeFileContent);
87+
if (!includeConfigs) {
88+
continue;
89+
}
90+
// check include configs
91+
for (const ic of includeConfigs) {
92+
const ickey = `${ic.direction}${ic.bridgeType}`.toUpperCase();
93+
if (configure.bridges.findIndex(item => `${item.direction}${item.bridgeType}`.toUpperCase() === ickey) > -1) {
94+
throw new Error(`duplicated config {direction: ${ic.direction}, bridgeType: ${ic.bridgeType}}`);
95+
}
96+
}
97+
nbdgs.push(...includeConfigs);
98+
}
99+
configure.bridges = nbdgs;
100+
}
53101

54102
async function _fillEncryptedPrivateKey(options) {
55103
const {configure} = options;

src/register/index.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,20 @@ async function refactorConfig(options) {
9292
includeFileContent = await fs.readFile(pathOfIncludeFromDataPath, 'utf8');
9393
}
9494
// check group file
95-
const pathOfGroupInclude = arg.datapath(`/includes/${group}/${include}`);
95+
const pathOfGroupInclude = arg.datapath(`/includes/${group}/registers/${include}`);
9696
if (fs.existsSync(pathOfGroupInclude)) {
9797
includeFileContent = await fs.readFile(pathOfGroupInclude, 'utf8');
9898
}
99+
if (!includeFileContent) {
100+
throw new Error(`include file ${include} not found, please check your path`);
101+
}
99102
const includeConfigs = YAML.parse(includeFileContent);
103+
if (!includeConfigs) {
104+
return [];
105+
}
100106
for (const ic of includeConfigs) {
101-
const ickey = `${ic.bridge}${ic.symbol}${ic.type}`;
102-
if (registers.findIndex(item => `${item.bridge}${item.symbol}${item.type}` === ickey) > -1) {
107+
const ickey = `${ic.bridge}${ic.symbol}${ic.type}`.toUpperCase();
108+
if (registers.findIndex(item => `${item.bridge}${item.symbol}${item.type}`.toUpperCase() === ickey) > -1) {
103109
throw new Error(`duplicated config {bridge: ${ic.bridge}, symbol: ${ic.symbol}, type: ${ic.type}}`);
104110
}
105111
}
@@ -165,7 +171,12 @@ async function hashRegister(register) {
165171
keys.sort();
166172
let merged = '';
167173
for (const key of keys) {
168-
merged += register[key];
174+
const rv = register[key];
175+
if (typeof rv === 'object') {
176+
merged += JSON.stringify(rv);
177+
} else {
178+
merged += rv;
179+
}
169180
}
170181
const hash = await $`echo "${merged}" | sha256sum | cut -d ' ' -f1`;
171182
return {

0 commit comments

Comments
 (0)