forked from many-things/cw-hyperlane
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwarp.ts
178 lines (161 loc) · 4.02 KB
/
warp.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// prevents "TypeError: Reflect.hasOwnMetadata is not a function"
import "reflect-metadata";
import { version } from "../package.json";
import { loadContext } from "../src/load_context";
import { config, getSigningClient } from "../src/config";
import { ContractFetcher } from "./fetch";
import { addPad } from "../src/conv";
import { ExecuteResult } from "@cosmjs/cosmwasm-stargate";
import { Command } from "commander";
const program = new Command();
program.name("Warp CLI").version(version);
program
.command("new")
.argument("<denom>", 'token denom, e.g. "untrn"')
.option(
"--token-mode <token_mode>",
'token mode, e.g. "collateral" or "bridged"',
"collateral"
)
.action(create);
program
.command("set-ism")
.argument("<address>", "address of internal warp route")
.argument("<ismAddress>", "address of ISM")
.action(setIsm);
program
.command("link")
.argument("<address>", "address of internal warp route")
.argument("<domain>", "domain of external chain, e.g. 5 (goerli)")
.argument("<external_route>", "address of external route")
.action(link);
program
.command("transfer")
.argument("<address>", "address of internal warp route")
.argument("<domain>", "domain of external chain, e.g. 5 (goerli)")
.argument("<recipient>", "recipient address")
.argument("<amount>")
.action(transfer);
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])),
}))
);
};
async function create(
denom: string,
{ tokenMode }: { tokenMode: "collateral" | "bridged" }
) {
const client = await getSigningClient(config);
const ctx = loadContext(config.network.id);
const fetcher = new ContractFetcher(ctx, client);
const {
core: { mailbox },
warp,
} = fetcher.getContracts();
switch (tokenMode) {
case "collateral":
const ibc_route = await warp.native.instantiate({
token: {
collateral: {
denom,
},
},
hrp: config.network.hrp,
owner: client.signer,
mailbox: mailbox.address!,
});
console.log("ibc_route", ibc_route);
return;
case "bridged":
throw Error("not implemented");
}
}
async function setIsm(address: string, ism: string) {
const client = await getSigningClient(config);
const resp = await client.wasm.execute(
client.signer,
address,
{
connection: {
set_ism: {
ism,
},
},
},
"auto"
);
console.log(parseWasmEventLog(resp));
console.log(resp.transactionHash);
}
async function link(address: string, domain: string, external_route: string) {
const client = await getSigningClient(config);
const resp = await client.wasm.execute(
client.signer,
address,
{
router: {
set_route: {
set: {
domain: Number(domain),
route: addPad(external_route),
},
},
},
},
"auto"
);
console.log(parseWasmEventLog(resp));
console.log(resp.transactionHash);
}
async function transfer(
address: string,
domain: string,
recipient: string,
amount: string
) {
const client = await getSigningClient(config);
const {
type: {
native: {
fungible: { denom },
},
},
}: {
type: {
native: {
fungible: {
denom: string;
};
};
};
} = await client.wasm.queryContractSmart(address, {
token_default: {
token_type: {},
},
});
const resp = await client.wasm.execute(
client.signer,
address,
{
transfer_remote: {
dest_domain: Number(domain),
recipient: addPad(recipient),
amount,
},
},
"auto",
undefined,
[
{ amount, denom },
{ amount: "100", denom: "untrn" },
]
);
console.log(parseWasmEventLog(resp));
console.log(resp.transactionHash);
}