-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
326 lines (301 loc) · 9.86 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Sample dApp</title>
</head>
<body style="max-width: 40rem; margin: auto">
<h1>Sample dApp</h1>
<p>
This sample dApp allows you to interact with the extension either by clicking the button below or by using one of
the following commands in the console
</p>
<code>
<pre style="background-color: #ececec; padding: 1rem">
await vega.connectWallet()
await vega.disconnectWallet()
await vega.getChainId()
await vega.listKeys()
await vega.sendTransaction({ publicKey, transaction, sendingMode })</pre
>
</code>
<main id="app"></main>
<script src="https://unpkg.com/mithril/mithril.js"></script>
<script>
const App = {
connected: false,
busy: false,
chainId: null,
ondisconnect() {
App.connected = false
App.chainId = null
m.redraw()
},
getChainId() {
if (App.connected === false) return
App.busy = true
vega
.getChainId()
.then(({ chainID }) => {
App.chainId = chainID
})
.finally(() => {
App.busy = false
m.redraw()
})
},
view() {
if (window.vega == null) {
return [
m('h1', 'No browser extension detected'),
m('p', [
'No installation of the Vega Wallet browser extension was found. Follow the installation instructions to download and install the latest release.',
m('br'),
m(
'a',
{
href: 'https://github.com/vegaprotocol/vegawallet-browser#readme',
target: '_blank'
},
'Installation instructions'
),
m('br'),
m('button', { onclick() {} }, 'Retry')
])
]
}
return [
App.connected === false
? m(
'button',
{
disabled: App.busy,
onclick() {
App.busy = true
vega
.connectWallet({
chainId: 'vega-fairground-202305051805'
})
.then(() => {
App.connected = true
window.vega.on('client.disconnected', App.ondisconnect)
})
.catch(console.error)
.finally(() => {
App.busy = false
m.redraw()
App.getChainId()
})
}
},
'Connect wallet'
)
: m(
'button',
{
disabled: App.busy,
onclick() {
App.busy = true
vega
.disconnectWallet()
.then(() => {
App.connected = false
App.chainId = null
window.vega.off('client.disconnected', App.ondisconnect)
})
.finally(() => {
App.busy = false
m.redraw()
})
}
},
'Disconnect wallet'
),
App.chainId == null ? null : m('span', [' Connected to: ', m('code', App.chainId)]),
App.connected === false ? null : m(Transfer)
]
}
}
const Transfer = {
keys: [],
loading: true,
selectedKey: null,
error: null,
txHash: null,
oninit() {
Transfer.refreshKeys()
},
refreshKeys() {
Transfer.error = null
Transfer.loading = true
Transfer.keys = []
Transfer.selectedKey = null
vega
.listKeys()
.then(({ keys }) => {
Transfer.keys = keys
Transfer.selectedKey = keys[0]
})
.catch(console.error)
.finally(() => {
Transfer.loading = false
m.redraw()
})
},
sendTransaction() {
Transfer.error = null
Transfer.txHash = null
Transfer.loading = true
vega
.sendTransaction({
publicKey: Transfer.selectedKey.publicKey,
sendingMode: 'TYPE_SYNC',
transaction: {
transfer: {
fromAccountType: 'ACCOUNT_TYPE_GENERAL',
toAccountType: 'ACCOUNT_TYPE_GENERAL',
asset: 'fc7fd956078fb1fc9db5c19b88f0874c4299b2a7639ad05a47a28c0aef291b55',
amount: '1',
to: Transfer.selectedKey.publicKey,
kind: {
oneOff: {}
}
}
}
})
.then((res) => {
Transfer.txHash = res.txHash
})
.catch((err) => {
Transfer.error = err
})
.finally(() => {
Transfer.loading = false
m.redraw()
})
},
view() {
const hasKeys = Transfer.keys.length > 0
let keyOptions
if (!hasKeys && Transfer.loading) {
keyOptions = [m('option', 'Loading keys...')]
} else if (!hasKeys) {
keyOptions = [m('option', 'No keys found')]
} else {
keyOptions = Transfer.keys.map((k) => {
const shortKey = k.publicKey.slice(0, 6) + '...' + k.publicKey.slice(-6)
const label = `${k.name} (${shortKey})`
return m(
'option',
{
value: k.publicKey,
selected: k.publicKey === Transfer.selectedKey
},
label
)
})
}
return [
m('h1', 'Transfer'),
m('p', 'This sample makes a transfer for the VEGA assert from the selected key to itself'),
m(
'select',
{
disabled: !hasKeys,
onchange(ev) {
Transfer.selectedKey = ev.target.value
}
},
keyOptions
),
m('button', { style: { margin: '0 0.5rem' }, onclick: Transfer.refreshKeys }, 'Refresh keys'),
m('p', 'Proposed transaction'),
Transfer.selectedKey == null
? null
: m(
'pre',
{ style: 'background-color: #ececec; padding: 1rem;' },
`{
"publicKey": "${Transfer.selectedKey.publicKey}",
"sendingMode": "TYPE_SYNC",
"transaction": {
"transfer": {
"fromAccountType": "ACCOUNT_TYPE_GENERAL",
"toAccountType": "ACCOUNT_TYPE_GENERAL",
"asset": "fc7fd956078fb1fc9db5c19b88f0874c4299b2a7639ad05a47a28c0aef291b55",
"amount": "1",
"to": "${Transfer.selectedKey.publicKey}",
"kind": {
"oneOff": {}
}
}
}
}`
),
m('br'),
m(
'button',
{
disabled: Transfer.loading || Transfer.selectedKey == null,
style: { fontSize: '1.5rem', margin: '0.8rem 0' },
onclick: Transfer.sendTransaction
},
'Transfer'
),
Transfer.error == null
? null
: m(
'p',
{ style: { color: 'red' } },
`Error: ${Transfer.error.message} (${Transfer.error.data?.code ?? ''}) - ${
Transfer.error.data?.message ?? ''
}`
),
Transfer.txHash == null
? null
: [
m('p', [
'Transaction hash ',
m('code', Transfer.txHash),
'. Find your transaction on one of the block explorers (depending on your chosen network):'
]),
m(
'a',
{
target: '_blank',
href: 'https://explorer.fairground.wtf/txs/' + Transfer.txHash
},
'View on Fairground'
),
m('br'),
m(
'a',
{
target: '_blank',
href: 'https://explorer.stagnet1.vega.xyz/txs/' + Transfer.txHash
},
'View on Stagnet1'
),
m('br'),
m(
'a',
{
target: '_blank',
href: 'https://explorer.stagnet3.vega.xyz/txs/' + Transfer.txHash
},
'View on Stagnet3'
),
m('br')
]
]
}
}
const root = document.getElementById('app')
window.onload = () => {
m.mount(root, App)
}
</script>
</body>
</html>