-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
235 lines (225 loc) · 7.44 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
<html>
<script>
let pretty = (x) => x;
(async () => {
pretty = (await import('./pretty.mjs')).default;
})();
const version = '0.1.10-rc.6';
let reach;
let acc;
let binname;
let bin;
const get = (id) => document.getElementById(id);
const make = (...args) => document.createElement(...args);
const t = (el, s) => {
el.append(document.createTextNode(s));
return el;
};
const removeAll = (el) => {
while (el.firstChild) { el.removeChild(el.firstChild); }
return el;
};
const replace = (el, children) => {
removeAll(el);
for (const child of children) { el.appendChild(child); }
return el;
};
const set = (el, attrs) => {
for (const k in attrs) { el[k] = attrs[k]; }
return el;
};
const loadReach = () => {
replace(get('body'), [
t(make('div'), 'loading...'),
set(make('script'), {
src: `https://cdn.jsdelivr.net/npm/@reach-sh/stdlib@${version}/dist/browser/reachsdk.min.js`,
onload: () => {
reach = reachsdk.loadStdlib('ALGO');
showSelectConnector();
},
}),
]);
};
const jsonArea = ({id, value, h, w}) => set(make('textarea'), {
id, value,
spellcheck: false,
style: `min-height: ${h}%; min-width: ${w}%;`,
});
const showSelectConnector = () => {
const providerEnv = reach.providerEnvByName('MainNet');
replace(get('body'), [
jsonArea({
id: 'providerEnv', h: 50, w: 100,
value: JSON.stringify(providerEnv, null, 2)
}),
make('br'),
t(make('p'), 'Choose your wallet fallback, using the above connection details.'),
make('br'),
set(t(make('button'), 'No Wallet'), {
onclick: () => { setWFB(); },
}),
set(t(make('button'), 'MyAlgo Wallet'), {
onclick: () => { setWFB('MyAlgoConnect'); },
}),
set(t(make('button'), 'Wallet Connect'), {
onclick: () => { setWFB('WalletConnect'); },
}),
make('br'),
t(make('p'), 'You may need to disable your popup blocker'),
t(make('p'), 'This power tool intended for desktop use; '
+ 'it might not work on ios because ios likes to kill the WalletConnect connection for no good reason...'
),
]);
};
const setWFB = async (wallet) => {
const providerEnv = JSON.parse(get('providerEnv').value);
const wopts = {providerEnv};
if (wallet) {
wopts[wallet] = reachsdk[`ALGO_${wallet}`];
reach.setWalletFallback(reach.walletFallback(wopts));
replace(get('body'), [t(make('p'), 'connecting...')])
acc = await reach.getDefaultAccount();
} else {
reach.setWalletFallback(reach.walletFallback(wopts));
acc = await reach.createAccount();
}
const b = (s) => set(t(make('button'), s), {
onclick: () => loadBin(s),
});
replace(get('body'), [
t(make('span'), 'load bin'),
set(make('span'), {id: 'loadedbin'}),
make('br'),
b('index.net_tok'),
b('index.tok_tok'),
b('staker.main'),
make('br'),
set(make('div'), {id: 'bindiv'}),
]);
};
const loadBin = async (s) => {
binname = s;
bin = await import(`./${version}/${s}.mjs`);
const ctc = acc.contract(bin);
const abi = ctc.getABI();
console.log(JSON.stringify(abi, null, 2));
const b = (sig) => {
const fname = sig.split('(')[0];
const fnameArgs = sig.split(')')[0] + ')';
return set(t(make('button'), fnameArgs), {
onclick: () => go(fname),
// use Staker_withdrawAndHarvest instead
disabled: fname == 'Staker_withdraw',
});
};
const be = (e) => {
return set(t(make('button'), e), {
onclick: () => goe(e),
});
};
t(removeAll(get('loadedbin')), ` (currently loaded: ${s})`);
replace(get('bindiv'), [
t(make('span'), 'appid'),
set(make('input'), { id: 'appid' }),
make('br'),
set(t(make('span'), 'args (json)')),
make('br'),
jsonArea({id: 'args', value: '[]', h: 0, w: 80}),
make('br'),
t(make('span'), 'commands'),
make('br'),
replace(make('div'), abi.sigs.map(b)),
make('br'),
t(make('span'), 'events'),
replace(make('div'), Object.keys(ctc.e).map(be)),
set(make('div'), { id: 'estatus' }),
make('br'),
t(make('span'), 'result'),
make('br'),
set(jsonArea({id: 'output', value: '', h: 50, w: 80})),
make('br'),
t(make('p'), `my addr: ${acc.getAddress()}`),
t(make('p'), `my addr (pretty): ${reach.formatAddress(acc)}`),
t(make('p'), 'Please note that all quantities are in the indivisible unit. e.g. if the token has 6 decimals, the amount is multiplied by 1,000,000'),
]);
};
const getIn = (obj, path) => {
while (obj && path.length > 0) { obj = obj[path.shift()]; }
return obj;
}
const go = async (fname) => {
const appid = get('appid').value;
const ctc = acc.contract(bin, appid);
const args = JSON.parse(get('args').value);
const fpath = fname.split('_');
const f = getIn(ctc, ['views', ...fpath])
|| getIn(ctc, ['apis', ...fpath]);
set(get('output'), {value: `calling ${fname}...`});
try {
const res = await f(...args);
console.log(pretty(res));
set(get('output'), {value: JSON.stringify(pretty(res), null, 2)});
} catch (e) {
console.error(e);
set(get('output'), {value: `${e}`});
}
};
let stop;
let count;
const goe = async (ename) => {
if (stop) stop();
let stopped = false;
const estatus = get('estatus');
stop = () => {
stopped = true;
removeAll(estatus);
};
count = 0;
const appid = get('appid').value;
const ctc = acc.contract(bin, appid);
replace(estatus, [
t(make('span'), 'reading live... '),
set(make('span'), { id: 'ecount' }),
set(t(make('button'), 'stop'), {
onclick: stop,
}),
set(t(make('button'), 'prettify addrs'), {
onclick: () => prettifyAddrs(ename),
}),
]);
const ecount = get('ecount');
const out = get('output');
out.value = '';
while (!stopped) {
const e = await ctc.e[ename].next();
if (stopped) break;
const estr = JSON.stringify(pretty(e));
h = out.value;
if (h) out.value += '\n';
out.value += estr;
ecount.innerHTML = ++count;
}
};
const prettifyAddrs = (ename) => {
const out = get('output');
const obj_strs = out.value.split('\n');
out.value = obj_strs.map((obj_str) => {
const obj = JSON.parse(obj_str);
// TODO: prettify based on inspecting reach type
obj.what[0] = reach.formatAddress(obj.what[0]);
if (ename === 'Harvest') {
obj.what[2] = reach.formatAddress(obj.what[2]);
} else if (ename === 'Withdraw') {
obj.what[3] = reach.formatAddress(obj.what[3]);
}
return JSON.stringify(obj);
}).join('\n');
};
</script>
<body id="body">
<p>This is a simple power tool for calling Reach contract methods</p>
<button id="theButton" onclick="loadReach()">load reach 0.1.10-rc.6 for Algorand</button>
</body>
<script>
</script>
</html>