Skip to content

Commit b8d6440

Browse files
author
YukiTsuchida
committed
refactor: standardize contract address formatting and add configuration template
1 parent e8985db commit b8d6440

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

pkg/consts/contract.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
)
66

77
const (
8-
IBCHandlerAddress = "0x727A5648832D2b317925CE043eA9b7fE04B4CD55"
9-
CrossSimpleModuleAddress = "0xAA43D337145E8930D01Cb4E60AbF6595C692921E"
8+
IBCHandlerAddress = "0x727a5648832d2b317925ce043ea9b7fe04b4cd55"
9+
CrossSimpleModuleAddress = "0xaa43d337145e8930d01cb4e60abf6595c692921e"
1010
)
1111

1212
type contractConfig struct{}

scripts/confgen.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env node
2+
const fs = require("fs");
3+
const ejs = require("ejs");
4+
const path = require("path");
5+
const util = require("util");
6+
7+
const renderFile = util.promisify(ejs.renderFile);
8+
9+
const {
10+
CONF_TPL,
11+
IBC_HANDLER,
12+
CROSS_SIMPLE_MODULE,
13+
CHAIN_ID = "5777",
14+
BROADCAST_DIR = "broadcast",
15+
} = process.env;
16+
17+
if (!CONF_TPL) {
18+
console.error("You must set environment variable 'CONF_TPL'");
19+
process.exit(1);
20+
}
21+
22+
function makePairs(arr) {
23+
const pairs = [];
24+
for (let i = 0; i < arr.length; i += 2) {
25+
if (arr[i + 1] === undefined) {
26+
console.error("invalid pair found in CONF_TPL");
27+
process.exit(1);
28+
}
29+
pairs.push([arr[i], arr[i + 1]]);
30+
}
31+
return pairs;
32+
}
33+
34+
function latestJson(name) {
35+
const p = path.join(BROADCAST_DIR, `${name}/${CHAIN_ID}/run-latest.json`);
36+
if (!fs.existsSync(p)) {
37+
throw new Error(`broadcast not found: ${p}`);
38+
}
39+
return JSON.parse(fs.readFileSync(p, "utf8"));
40+
}
41+
42+
function pickAddr(json, contractName) {
43+
const txs = json.transactions || [];
44+
const hit = txs.filter((t) => t.contractName === contractName && t.contractAddress).pop();
45+
return hit && hit.contractAddress;
46+
}
47+
48+
(async () => {
49+
let ibc = IBC_HANDLER;
50+
let cross = CROSS_SIMPLE_MODULE;
51+
52+
if (!ibc || !cross) {
53+
const core = latestJson("001_DeployCore.s.sol");
54+
const app = latestJson("002_DeployApp.s.sol");
55+
ibc = ibc || pickAddr(core, "OwnableIBCHandler");
56+
cross = cross || pickAddr(app, "CrossSimpleModule");
57+
}
58+
59+
if (!ibc || !cross) {
60+
console.error("failed to resolve contract addresses (ENV or broadcast)");
61+
process.exit(1);
62+
}
63+
64+
const targets = makePairs(CONF_TPL.split(":"));
65+
for (const [outPath, tplPath] of targets) {
66+
const str = await renderFile(tplPath, {
67+
IBCHandlerAddress: ibc,
68+
CrossSimpleModuleAddress: cross,
69+
});
70+
fs.writeFileSync(outPath, str);
71+
console.log("generated file", outPath);
72+
}
73+
})().catch((e) => {
74+
console.error(e);
75+
process.exit(1);
76+
});

scripts/setup.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,23 @@ function chain() {
2828
echo "variable network must be set"
2929
exit 1
3030
fi
31+
if [ -z "$CONF_TPL" ]; then
32+
echo "variable CONF_TPL must be set"
33+
exit 1
34+
fi
3135

3236
pushd ./chains && docker compose up -d ${network} && popd
3337
# XXX Wait for the first block to be created
3438
sleep 3
3539
npm run migrate
40+
node ./scripts/confgen.js
3641
}
3742

3843
function development {
3944
before_common
4045

4146
network=development
47+
export CONF_TPL="./pkg/consts/contract.go:./scripts/template/contract.go.tpl"
4248
chain
4349

4450
after_common

scripts/template/contract.go.tpl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package consts
2+
3+
import (
4+
"github.com/ethereum/go-ethereum/common"
5+
)
6+
7+
const (
8+
IBCHandlerAddress = "<%= IBCHandlerAddress; %>"
9+
CrossSimpleModuleAddress = "<%= CrossSimpleModuleAddress; %>"
10+
)
11+
12+
type contractConfig struct{}
13+
14+
var Contract contractConfig
15+
16+
func (contractConfig) GetIBCHandlerAddress() common.Address {
17+
return common.HexToAddress(IBCHandlerAddress)
18+
}
19+
20+
func (contractConfig) GetCrossSimpleModuleAddress() common.Address {
21+
return common.HexToAddress(CrossSimpleModuleAddress)
22+
}

0 commit comments

Comments
 (0)