-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
386 lines (358 loc) · 13.5 KB
/
App.js
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import React, { Component } from 'react';
import { Navbar, Nav, Form, FormControl, Button, } from 'react-bootstrap';
import { Container, Row, Col } from 'react-bootstrap';
import { Web3Modal } from '@web3modal/standalone'
const WCApp = require( './components/WCApp.js');
const config = require( './config.json');
const transactions = require( './transactions.json');
const c_projectID = config.projectID;
const c_starkProvider = config.starkProvider;
const c_ethProvider = config.ethProvider;
let g_exampleCommands = {}
g_exampleCommands['eth.transfer'] = {
service:"eth",
type:"run",
command:"signAndSend",
args:{
to: "31349e0c9d36f3d11b980df145a1abc871399b8a",
value: "0.001",
gasPrice: "2000000000",
gasLimit: "21000",
type:1,
chainId: 5,
}
}
transactions.forEach((t)=>{
if (['spot','perpetual','spot_signed','perpetual_signed'].includes( t.test_type))
{
t.systemId = "sys_0";
let command = "send";
if (t.test_type.includes("_signed"))
command = "signAndSend";
g_exampleCommands[t.gui_label] = {
service:"starkex",
type:"request",
command:command,
args: t
}
}
})
g_exampleCommands['stark.sign_message'] = {
service:"starkex",
type:"request",
command:"signAndSend",
args: {"type":"TransferRequest",
systemId: "0",
amount: '1000',
nonce: 1519522183,
senderPublicKey: '0x59a543d42bcc9475917247fa7f136298bb385a6388c3df7309955fcb39b8dd4',
senderVaultId: 1,
token: '0x3003a65651d3b9fb2eff934a4416db301afd112a8492aaf8d7297fc87dcd9f4',
receiverPublicKey: '0x5fa3383597691ea9d827a79e1a4f0f7949435ced18ca9619de8ab97e661020',
receiverVaultId: 1,
expirationTimestamp: 438953}
}
class App extends Component {
constructor(props)
{
super(props);
this.projectId = c_projectID;
this.namespaces = {
eip155: { methods: ['generate_account',
'expose_account',
'sendTransaction',
'sign_message',
'set_gateway',
'getFirstUnusedTxId',
'set_admin_account',
'select_account',
'generate_stark_account_from_private_key',
'signTransaction'],
chains: ['eip155:1'],
events: ['accountsChanged'] }
}
this.web3Modal = new Web3Modal({ projectId:this.projectId, standaloneChains: this.namespaces.eip155.chains })
this.app = new WCApp();
this.userInfo = {};
this.selectedOption = "";
this.exampleCommands = g_exampleCommands;
}
state = {
stateInfoArea: "",
commands: [],
selectedQuery: ""
};
handleSelectChange = (event) => {
this.selectedOption = event.target.value;
this.setState({ selectedQuery:JSON.stringify(this.exampleCommands[this.selectedOption],null,2) });
};
handleQuery = async () => {
let query = JSON.parse(this.state.selectedQuery)
if (query.command == "signAndSend")
return this.signAndSend(query);
if (query.command == "send")
return this.send(query);
if (query.type == "request")
return await this.request(query.command,query.service,query.args);
if (query.type == "run")
return await this.run(query.command,query.service,query.args);
}
/* signAndSend - *Example compound transaction*
Sign and send both signs a local ETH transaction, it also sends the signed transaction
Mostly, however, this is an example of how to create a compound transaction within
the App.js. You can use this function as inspiration when developing your own
multi-step queries
*/
signAndSend = async (query) => {
if (query.service == "eth")
{
if (this.userInfo.ethPrivateAccount == undefined)
{
alert("Please generate an ETH account first")
return;
}
let signedEthTransaction = await this.run("signTransaction","eth",query.args);
query.args = {"hex":signedEthTransaction};
let sentEthTransaction = await this.run("sendTransaction","ethgate",query.args);
return {signedEthTransaction, sentEthTransaction};
}
if (query.service == "starkex")
{
if (this.userInfo.starkProvider == undefined)
{
alert("Please set up a stark account with the above button")
return;
}
let req = query.args;
req['nonce'] = Math.floor(Math.random() * 900000) + 100000;
let sig = await this.request( "sign_message", "starkex", query.args);
req['signature'] = sig;
if (req.test_type.includes("spot"))
await this.run( "set_gateway", "starkexgate", {"providerUrl":"https://gw.playground-v2.starkex.co",
"getFirstUnusedTxIdUrl":"/v2/gateway/testing/get_first_unused_tx_id"});
else
await this.run( "set_gateway", "starkexgate", {"providerUrl":"https://perpetual-playground-v2.starkex.co",
"getFirstUnusedTxIdUrl":"/get_first_unused_tx_id"});
let sent = await this.run("sendTransaction","starkexgate",req);
return {req, sent};
}
}
send = async (query) => {
if (query.service == "eth")
{
if (this.userInfo.ethPrivateAccount == undefined)
{
alert("Please generate an ETH account first")
return;
}
let sentEthTransaction = await this.run("sendTransaction","ethgate",query.args);
return {sentEthTransaction};
}
if (query.service == "starkex")
{
if (this.userInfo.starkProvider == undefined)
{
alert("Please set up a stark account with the above button")
return;
}
let req = query.args;
req['nonce'] = Math.floor(Math.random() * 900000) + 100000;
await this.run( "set_gateway", "starkexgate", {"providerUrl":this.userInfo.starkProvider,
"getFirstUnusedTxIdUrl":"/v2/gateway/testing/get_first_unused_tx_id"});
let sent = await this.run("sendTransaction","starkexgate",req);
return {sent};
}
}
sign = async (query) => {
if (query.service == "eth")
{
if (this.userInfo.ethPrivateAccount == undefined)
{
alert("Please generate an ETH account first")
return;
}
let signedEthTransaction = await this.run("signTransaction","eth",query.args);
return {signedEthTransaction};
}
if (query.service == "stark")
{
return await this.request( "sign_message", "stark", query.args);
}
}
handleConnect = async () => {
try {
const { deep_link, approval } = await this.app.doConnect(this.namespaces,this.projectId);
let uri = deep_link;
if (uri) {
this.web3Modal.openModal({ uri })
let res = await approval;
this.web3Modal.closeModal()
let sessionApproval = res;
this.userInfo.connected = true;
this.setInfo(JSON.stringify(this.userInfo,null,2));
}
} catch (err) {
console.error(err)
}
}
/**
* With admin approval, lets actually expose keys. The Wallet will have to voluntairly send this data over.
* (Go ahead and try to say Y and then N to these requests in the /wallet/index.js application)
*/
handleStarkGenerateAccount = async () => {
let results = {}
if (this.userInfo.ethPrivateAccount == undefined)
{
alert("Please generate an ETH account first")
return;
}
if (this.userInfo.starkResponse != undefined)
{
alert("You have already set up a stark account")
return;
}
results.starkProvider = c_starkProvider;
let starkKeyData = await this.request("generate_stark_account_from_private_key","starkex",{'privateKey':this.userInfo.ethPrivateAccount.privateKey});
results.starkKey = starkKeyData.starkKey;
results.starkAccount = starkKeyData.account;
results.starkSelected = await this.request("select_account","starkex",{starkKey:results.starkKey});
this.userInfo = { ...this.userInfo,...results}
this.setInfo(JSON.stringify(this.userInfo,null,2));
}
handleEthGenerateAccount = async () => {
let results = {}
if (this.userInfo.ethPrivateAccount != undefined)
{
alert("You already have an account setup")
return;
}
results.ethProvider = c_ethProvider;
let ethAcct = await this.run("generate_account","eth",{});
await this.run("set_gateway","ethgate", {"providerUrl":results.ethProvider});
results.ethPrivateAccount = await this.run("expose_account","eth",{publicKey: ethAcct.publicKey});
this.userInfo = { ...this.userInfo,...results}
this.setInfo(JSON.stringify(this.userInfo,null,2));
}
addToCommands = command => {
this.setState({ commands: [ command,...this.state.commands] });
};
setInfo = info => {
this.setState({ stateInfoArea: info });
};
request = async (command,service,args) =>{
let resStr = [];
resStr.push("Command:"+command);
resStr.push("service:"+service);
let results = await this.app.request( command, service, args);
resStr.push("REQUESTING---------------");
resStr.push(JSON.stringify(args,null,2));
resStr.push("------");
resStr.push(JSON.stringify(results,null,2));
resStr.push("---------------------");
this.addToCommands(resStr.join("\n"));
return results;
}
run = async (command,service,args) =>{
let resStr = [];
resStr.push("Command:"+command);
resStr.push("service:"+service);
let results = await this.app.run( command, service, args);
resStr.push("RUNNING---------------");
resStr.push(JSON.stringify(args,null,2));
resStr.push("------");
resStr.push(JSON.stringify(results,null,2));
resStr.push("---------------------");
this.addToCommands(resStr.join("\n"));
return results;
}
render() {
return (
<div>
<Navbar bg="light" expand="lg">
<Navbar.Brand href="#home">dApp Example</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="#home">Home</Nav.Link>
<Nav.Link href="#about">About</Nav.Link>
</Nav>
<Nav.Link href="#user-sign-in">
<i className="fas fa-user-circle" />
</Nav.Link>
</Navbar.Collapse>
</Navbar>
<div className="container">
<Container>
<Row>
<Col xs={12} md={4}>
<Form xs={12} md={4}>
<Form.Group>
<Form.Control
type="button"
value="1. create browser eth keys"
onClick={this.handleEthGenerateAccount}
/>
</Form.Group>
<Form.Group>
<Form.Control
type="button"
value="2. connect to stark-wallet"
onClick={this.handleConnect}
/>
</Form.Group>
<Form.Group>
<Form.Control
type="button"
value="3. create stark-wallet key"
onClick={this.handleStarkGenerateAccount}
/>
</Form.Group>
<Form.Group controlId="transactionSelect">
<Form.Control
as="select"
value={this.selectedOption}
onChange={this.handleSelectChange}
>
<option value="">Select...</option>
{Object.keys(this.exampleCommands).map((key) => (
<option key={key} value={key}>
{key}
</option>
))}
</Form.Control>
<Form.Control as="textarea" rows="7"
defaultValue={this.state.selectedQuery}
onChange={(event) => this.setState({ selectedQuery: event.target.value })}
/>
<Form.Control
type="button"
value="4. run example query"
onClick={this.handleQuery}
/>
</Form.Group>
</Form>
</Col>
<Col xs={12} md={8}>
<div style={{ display: "flex", flexDirection: "column" }}>
<div style={{ marginBottom: "1em" }}>
<h5>Account Info:</h5>
<pre><code>{this.state.stateInfoArea}</code></pre>
</div>
<div>
<h5>Previous Commands:</h5>
<ul>
{this.state.commands.map((command, i) => (
<li key={i}><pre>{command}</pre></li>
))}
</ul>
</div>
</div>
</Col>
</Row>
</Container>
</div>
</div>
);
}
}
export default App;