-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathApp.vue
327 lines (319 loc) · 8.31 KB
/
App.vue
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<template>
<div class="example">
<h2>This is a ts-relayer-demo</h2>
<div class="logger">
<template
v-for="(log, index) in relayerLog"
v-bind:key="'logline' + index"
>
<div>{{ log }}</div>
</template>
</div>
<h3>
Relayer Logger output above.<br />Please open dev-tools/console log for
additional details.
</h3>
<div v-if="step == 1">
<h4>Enter details for chain A</h4>
<input
v-model="config.chainA.endpoint"
placeholder="RPC Endpoint"
/><br />
<input
v-model="config.chainA.addrPrefix"
placeholder="Address Prefix"
/><br />
<input
v-model="config.chainA.gasPrice"
placeholder="Gas Price (e.g. 0.001uatom)"
/><br />
<textarea v-model="config.chainA.mnemonic" placeholder="Mnemonic" /><br />
<button v-on:click="next">Next</button>
</div>
<div v-if="step == 2">
<h4>Enter details for chain B</h4>
<input
v-model="config.chainB.endpoint"
placeholder="RPC Endpoint"
/><br />
<input
v-model="config.chainB.addrPrefix"
placeholder="Address Prefix"
/><br />
<input
v-model="config.chainB.gasPrice"
placeholder="Gas Price (e.g. 0.001uatom)"
/><br />
<textarea v-model="config.chainB.mnemonic" placeholder="Mnemonic" /><br />
<button v-on:click="next">Next</button>
</div>
<div v-if="step == 3">
<button v-on:click="setupRelayer">Connect</button>
</div>
<div v-if="step == 4">
<button v-on:click="runRelayer">Run Relayer</button>
</div>
<div v-if="step == 5">
<button v-on:click="stopRelayer">Stop Relayer</button><br />
<input
v-model="transaction.senderMnemonic"
placeholder="Sender mnemonic on chain A"
/><br />
<input
v-model="transaction.rcptAddress"
placeholder="Recipient address on chain b"
/><br />
<input
v-model="transaction.amount"
placeholder="Amount to transfer (e.g. 1token)"
/><br />
<button v-on:click="sendIBCtransaction">
Send IBC Transfer Transaction
</button>
</div>
</div>
</template>
<script>
import { stringToPath } from "@cosmjs/crypto";
import { IbcClient, Link } from "@confio/relayer/build";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { GasPrice, parseCoins } from "@cosmjs/launchpad";
import { SigningStargateClient } from "@cosmjs/stargate";
import Long from "long";
export default {
name: "App",
data() {
return {
step: 1,
config: {
chainA: {
endpoint: "http://localhost:26657",
addrPrefix: "mars",
gasPrice: "0.0001token",
mnemonic:
"shrug eager kid scale grunt trust slice dose equal jacket speak book arctic spy time trouble output steel provide leave shuffle play wear expand",
},
chainB: {
endpoint: "http://localhost:26659",
addrPrefix: "planet",
gasPrice: "0.0001token",
mnemonic:
"lock play cabin penalty trumpet permit correct dinner exchange usage focus normal net tongue present amused bid plunge hill dutch milk mobile obey diagram",
},
},
transaction: {
senderMnemonic: "",
amount: "",
rcptAddress: "",
},
signerA: null,
signerB: null,
clientA: null,
clientB: null,
link: null,
channels: null,
relayerLog: [],
running: false,
nextRelay: {},
};
},
methods: {
next() {
this.step++;
},
prev() {
this.step > 0 ? (this.step = this.step - 1) : (this.step = 0);
},
logger() {
return {
log: (msg) => {
this.relayerLog.push("LOG: " + msg);
},
info: (msg) => {
this.relayerLog.push("INFO: " + msg);
},
error: (msg) => {
this.relayerLog.push("ERROR: " + msg);
},
warn: (msg) => {
this.relayerLog.push("WARN: " + msg);
},
verbose: (msg) => {
this.relayerLog.push("VERBOSE: " + msg);
},
debug: (msg) => {
this.relayerLog.push("DEBUG: " + msg);
},
};
},
sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
},
async sendIBCtransaction() {
const signer = await DirectSecp256k1HdWallet.fromMnemonic(
this.transaction.senderMnemonic,
{
hdPaths: [stringToPath("m/44'/118'/0'/0/0")],
addrPrefix: this.config.chainA.addrPrefix,
}
);
const client = await SigningStargateClient.connectWithSigner(
this.config.chainA.endpoint,
signer
);
const [fromAccount] = await signer.getAccounts();
const msg = {
typeUrl: "/ibc.applications.transfer.v1.MsgTransfer",
value: {
sourcePort: "transfer",
sourceChannel: this.channels.src.channelId,
sender: fromAccount.address,
receiver: this.transaction.rcptAddress,
timeoutTimestamp: Long.fromString(
new Date().getTime() + 60000 + "000000"
),
token: parseCoins(this.transaction.amount)[0],
},
};
const fee = {
amount: [],
gas: "200000",
};
await client.signAndBroadcast(fromAccount.address, [msg], fee);
},
async relayerLoop(
options = { poll: 5000, maxAgeDest: 86400, maxAgeSrc: 86400 }
) {
while (this.running) {
try {
this.nextRelay = await this.link.checkAndRelayPacketsAndAcks(
this.nextRelay,
2,
6
);
console.group("Next Relay:");
console.log(this.nextRelay);
console.groupEnd("Next Relay:");
await this.link.updateClientIfStale("A", options.maxAgeDest);
await this.link.updateClientIfStale("B", options.maxAgeSrc);
} catch (e) {
console.error(`Caught error: `, e);
}
await this.sleep(options.poll);
}
},
runRelayer() {
this.running = true;
this.relayerLoop();
this.next();
},
stopRelayer() {
this.running = false;
this.prev();
},
async setupRelayer() {
// Create a signer from chain A config
this.signerA = await DirectSecp256k1HdWallet.fromMnemonic(
this.config.chainA.mnemonic,
{
hdPaths: [stringToPath("m/44'/118'/0'/0/0")],
addrPrefix: this.config.chainA.addrPrefix,
}
);
// Create a signer from chain B config
this.signerB = await DirectSecp256k1HdWallet.fromMnemonic(
this.config.chainB.mnemonic,
{
hdPaths: [stringToPath("m/44'/118'/0'/0/0")],
addrPrefix: this.config.chainB.addrPrefix,
}
);
// get addresses
const [accountA] = await this.signerA.getAccounts();
const [accountB] = await this.signerB.getAccounts();
// Create IBC Client for chain A
this.clientA = await IbcClient.connectWithSigner(
this.config.chainA.endpoint,
this.signerA,
accountA.address,
{
prefix: this.config.chainA.addrPrefix,
logger: this.logger(),
gasPrice: GasPrice.fromString(this.config.chainA.gasPrice),
}
);
console.group("IBC Client for chain A");
console.log(this.clientA);
console.groupEnd("IBC Client for chain A");
// Create IBC Client for chain B
this.clientB = await IbcClient.connectWithSigner(
this.config.chainB.endpoint,
this.signerB,
accountB.address,
{
prefix: this.config.chainB.addrPrefix,
logger: this.logger(),
gasPrice: GasPrice.fromString(this.config.chainB.gasPrice),
}
);
console.group("IBC Client for chain B");
console.log(this.clientB);
console.groupEnd("IBC Client for chain B");
// Create new connectiosn for the 2 clients
this.link = await Link.createWithNewConnections(
this.clientA,
this.clientB,
this.logger()
);
console.group("IBC Link Details");
console.log(this.link);
console.groupEnd("IBC Link Details");
// Create a channel for the connections
this.channels = await this.link.createChannel(
"A",
"transfer",
"transfer",
1,
"ics20-1"
);
console.group("IBC Channel Details");
console.log(this.channels);
console.groupEnd("IBC Channel Details");
this.next();
},
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
.example {
width: 720px;
margin: auto;
}
.logger {
width: 720px;
overflow: auto;
height: 400px;
border: 1px solid black;
text-align: left;
padding: 5px;
}
input {
width: 300px;
margin: 10px auto;
padding: 5px;
}
textarea {
width: 300px;
margin: 10px auto;
height: 80px;
padding: 5px;
}
</style>