forked from many-things/cw-hyperlane
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmailbox.ts
106 lines (92 loc) · 2.98 KB
/
mailbox.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// prevents "TypeError: Reflect.hasOwnMetadata is not a function"
import "reflect-metadata";
import { ExecuteResult } from "@cosmjs/cosmwasm-stargate";
import { Command } from "commander";
import { version } from "../package.json";
import { config, getSigningClient } from "../src/config";
import {
HplHookMerkle,
HplIgp,
HplIgpOracle,
HplIsmAggregate,
HplMailbox,
} from "../src/contracts";
import { addPad } from "../src/conv";
import { loadContext } from "../src/load_context";
import { ContractFetcher } from "./fetch";
const program = new Command();
program.name("Mailbox CLI").version(version);
program
.command("dispatch")
.argument("<dest_domain>", 'destination domain, e.g. "5"')
.argument("<recipient_addr>", "recipient address in hex")
.argument("<msg_body>", "message body in utf-8")
.action(makeHandler("dispatch"));
program
.command("process")
.argument("<metadata>", "metadata in hex")
.argument("<msg_body>", "message body in hex")
.action(makeHandler("process"));
program.parseAsync(process.argv).catch(console.error);
const parseWasmEventLog = (res: ExecuteResult) => {
return (
res.events
// .filter((v) => v.type.startsWith("wasm"))
.map((v) => ({
"@type": v.type.slice(5),
...Object.fromEntries(v.attributes.map((x) => [x.key, x.value])),
}))
);
};
function makeHandler(
action: "dispatch" | "process"
): (...args: any[]) => void | Promise<void> {
const ctx = loadContext(config.network.id);
const loadDeps = async () => {
const client = await getSigningClient(config);
const fetcher = new ContractFetcher(ctx, client);
const mailbox = fetcher.get(HplMailbox, "hpl_mailbox");
const igp = fetcher.get(HplIgp, "hpl_igp");
const igp_oracle = fetcher.get(HplIgpOracle, "hpl_igp_oracle");
const hook_merkle = fetcher.get(HplHookMerkle, "hpl_hook_merkle");
const hook_aggregate = fetcher.get(HplIsmAggregate, "hpl_hook_aggregate");
return {
client,
mailbox,
igp: { core: igp, oracle: igp_oracle },
hook: { merkle: hook_merkle, aggregate: hook_aggregate },
};
};
switch (action) {
case "dispatch":
return async (
dest_domain: string,
recipient_addr: string,
msg_body: string
) => {
const { mailbox } = await loadDeps();
const res = await mailbox.execute(
{
dispatch: {
dest_domain: Number(dest_domain),
recipient_addr: addPad(recipient_addr),
msg_body: Buffer.from(msg_body, "utf-8").toString("hex"),
},
},
[{ denom: "inj", amount: "2500" }]
);
console.log(parseWasmEventLog(res));
};
case "process":
return async (metadata: string, msg_body: string) => {
const { mailbox } = await loadDeps();
const res = await mailbox.execute({
process: {
metadata,
msg_body,
},
});
console.log(parseWasmEventLog(res));
};
}
}