-
Notifications
You must be signed in to change notification settings - Fork 64
/
App.js
424 lines (368 loc) · 13.4 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import React, { useState, useEffect } from "react";
import "./App.css";
import Button from "@material-ui/core/Button";
import {
NotificationContainer,
NotificationManager
} from "react-notifications";
import "react-notifications/lib/notifications.css";
import Web3 from "web3";
import { makeStyles } from '@material-ui/core/styles';
import Link from '@material-ui/core/Link';
import Typography from '@material-ui/core/Typography';
import { helperAttributes,
getDomainSeperator,
getDataToSignForPersonalSign,
getDataToSignForEIP712,
buildForwardTxRequest,
getBiconomyForwarderConfig,
getTokenGasPrice,
getDaiPermit} from "./erc20ForwarderHelpers";
import { Box } from "@material-ui/core";
let sigUtil = require("eth-sig-util");
const { config } = require("./config");
const abi = require("ethereumjs-abi");
let web3;
let contract;
let provider;
let ethersProvider;
const useStyles = makeStyles((theme) => ({
root: {
'& > * + *': {
marginLeft: theme.spacing(2),
},
},
link: {
marginLeft: "5px"
}
}));
//todo
//add cost calculation and access to fee manager, fee proxy and oracle aggregator read variables (like biconomy forwarder)
//review and easy permit flow
function App() {
const classes = useStyles();
const preventDefault = (event) => event.preventDefault();
const [quote, setQuote] = useState("This is a default quote");
const [owner, setOwner] = useState("Default Owner Address");
const [newQuote, setNewQuote] = useState("");
const [selectedAddress, setSelectedAddress] = useState("");
const [metaTxEnabled, setMetaTxEnabled] = useState(true);
const [transactionHash, setTransactionHash] = useState("");
useEffect(() => {
async function init() {
if (
typeof window.ethereum !== "undefined" &&
window.ethereum.isMetaMask
) {
// Ethereum user detected. You can now use the provider.
provider = window["ethereum"];
await provider.enable();
//const biconomy = new Biconomy(provider,{apiKey: "du75BkKO6.941bfec1-660f-4894-9743-5cdfe93c6209", debug: true});
web3 = new Web3(provider);
contract = new web3.eth.Contract(
config.contract.abi,
config.contract.address
);
setSelectedAddress(provider.selectedAddress);
getQuoteFromNetwork();
provider.on("accountsChanged", function(accounts) {
setSelectedAddress(accounts[0]);
});
} else {
showErrorMessage("Metamask not installed");
}
}
init();
}, []);
const onQuoteChange = event => {
setNewQuote(event.target.value);
};
/* this app does not need to use @biconomy/mexa */
/* this example demonstrates paying DAI tokens as gas fees*/
const onERCForwardWithEIP712Signature = async event => {
if (newQuote != "" && contract) {
setTransactionHash("");
if (metaTxEnabled) {
console.log("Sending meta transaction");
let userAddress = selectedAddress;
let data = contract.methods.setQuote(newQuote).encodeABI();
let txGas = await contract.methods.setQuote(newQuote).estimateGas({from: userAddress});
let networkId = 42;
const daiPermitOptions = {
spender: config.erc20ForwarderAddress,
expiry: Math.floor(Date.now() / 1000 + 3600),
allowed: true,
networkId: 42
};
//await getDaiPermit(provider,userAddress,daiPermitOptions);
let forwarder = await getBiconomyForwarderConfig(42);
let forwarderContract = new web3.eth.Contract(
forwarder.abi,
forwarder.address
);
const batchNonce = await forwarderContract.methods.getNonce(userAddress,0).call();
//const batchId = await biconomyForwarder.methods.getBatch(userAddress).call();
console.log(batchNonce);
const tokGasPrice = await getTokenGasPrice(networkId,config.tokenAddress);
let request = await buildForwardTxRequest(networkId,{account:userAddress,to:config.contract.address,gasLimitNum:Number(txGas),batchId:0,batchNonce,tokenGasPrice:tokGasPrice,data,tokenAddress:config.tokenAddress});
const req = request.request;
console.log(req);
const cost = request.cost;
console.log(`Your user will be paying ${cost} amount of tokens`);
const domainSeparator = getDomainSeperator(networkId);
console.log(domainSeparator);
const dataToSign = getDataToSignForEIP712(req,networkId);
const promi = new Promise(async function(resolve, reject) {
await web3.currentProvider.send(
{
jsonrpc: "2.0",
id: 999999999999,
method: "eth_signTypedData_v4",
params: [userAddress, dataToSign]
}, function(error, res){
if(error) {
reject(error);
} else {
resolve(res.result);
}
});
});
promi.then(async function(sig){
console.log('signature ' + sig);
console.log(req);
sendTransaction({userAddress, req, domainSeparator, sig, signatureType:"EIP712_SIGN"});
}).catch(function(error) {
console.log('could not get signature error ' + error);
showErrorMessage("Could not get user signature");
});
} else {
showErrorMessage("Meta Transaction disabled");
}
} else {
showErrorMessage("Error while sending");
}
};
const onERCForwardWithPersonalSignature = async event => {
if (newQuote != "" && contract) {
setTransactionHash("");
/**
* create an instance of BiconomyForwarder <= ABI, Address
* create functionSignature
* create txGas param which is gas estimation of his function call
* get nonce from biconomyForwarder instance
* get gasPrice and tokenGasPrice (based on token address you wish to transact as gas)
* get token address based on symbol? // pass the token address accoridng to provider's network id
* create a forwarder request
* create dataToSign as per signature scheme used (personal signature)
* get the signature from user
* create the domain separator
* Now call the meta tx API with two parameters request and signature
* */
if (metaTxEnabled) {
console.log("Sending meta transaction");
let userAddress = selectedAddress;
let data = contract.methods.setQuote(newQuote).encodeABI();
let txGas = await contract.methods.setQuote(newQuote).estimateGas({from: userAddress});
let networkId = 42;
const daiPermitOptions = {
spender: config.erc20ForwarderAddress,
expiry: Math.floor(Date.now() / 1000 + 3600),
allowed: true,
networkId: networkId
};
await getDaiPermit(provider,userAddress,daiPermitOptions);
let forwarder = await getBiconomyForwarderConfig(42);
let forwarderContract = new web3.eth.Contract(
forwarder.abi,
forwarder.address
);
const batchNonce = await forwarderContract.methods.getNonce(userAddress,0).call();
//const batchId = await biconomyForwarder.methods.getBatch(userAddress).call();
console.log(batchNonce);
const tokGasPrice = await getTokenGasPrice(networkId,config.tokenAddress);
let request = await buildForwardTxRequest(networkId,{account:userAddress,to:config.contract.address,gasLimitNum:Number(txGas),batchId:0,batchNonce,tokenGasPrice:tokGasPrice,data,tokenAddress:config.tokenAddress});
const req = request.request;
console.log(req);
const cost = request.cost;
console.log(cost);
const hashToSign = getDataToSignForPersonalSign(req);
const sig = await web3.eth.personal.sign("0x" + hashToSign.toString("hex"), userAddress);
console.log('signature ' + sig);
sendTransaction({userAddress, req, sig, signatureType:"PERSONAL_SIGN"});
} else {
showErrorMessage("Meta Transaction disabled");
}
} else {
showErrorMessage("Error while sending");
}
};
/*const signMessage = async (addr, data) => {
let signature;
await web3.currentProvider.sendAsync(
{
jsonrpc: "2.0",
id: 999999999999,
method: "eth_signTypedData_v4",
params: [addr, data]
},
function(error, response) {
console.info(`User signature is ${response.result}`);
if (error || (response && response.error)) {
showErrorMessage("Could not get user signature");
} else if (response && response.result) {
signature = response.result;
return signature;
}
}
);
}*/
const sendTransaction = ({userAddress, req, sig, domainSeparator, signatureType}) => {
if (web3 && contract) {
let params;
if(domainSeparator) {
params = [req, domainSeparator, sig]
} else {
params = [req, sig]
}
try {
fetch(`https://test-api.biconomy.io/api/v2/meta-tx/native`, {
method: "POST",
headers: {
"x-api-key" : "m60yDrUs7.5c3b23fa-0b93-46ac-86f9-79e998d8f361",
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify({
"to": config.contract.address,
"apiId": "1d5f7951-ee45-4042-976e-90aac1d7d42e",
"params": params,
"from": userAddress,
"signatureType": signatureType
})
})
.then(response=>response.json())
.then(function(result) {
console.log(result);
showInfoMessage(`Transaction sent by relayer with hash ${result.txHash}`);
return result.txHash;
})
.then(function(hash){
const receipt = fetchMinedTransactionReceipt(hash);
return receipt;
}).then(function(receipt){
if(receipt)
{
console.log(receipt);
setTransactionHash(receipt.transactionHash);
showSuccessMessage("Transaction confirmed on chain");
getQuoteFromNetwork();
}
})
.catch(function(error) {
console.log(error)
});
} catch (error) {
console.log(error);
}
}
};
const fetchMinedTransactionReceipt = (transactionHash) => {
return new Promise((resolve, reject) => {
var timer = setInterval(()=> {
web3.eth.getTransactionReceipt(transactionHash, (err, receipt)=> {
if(!err && receipt){
clearInterval(timer);
resolve(receipt);
}
else if(err)
{
reject(err);
}
});
}, 2000)
})
}
const getQuoteFromNetwork = () => {
if (web3 && contract) {
contract.methods
.getQuote()
.call()
.then(function(result) {
console.log(result);
if (
result &&
result.currentQuote != undefined &&
result.currentOwner != undefined
) {
if (result.currentQuote == "") {
showErrorMessage("No quotes set on blockchain yet");
} else {
setQuote(result.currentQuote);
setOwner(result.currentOwner);
}
} else {
showErrorMessage("Not able to get quote information from Network");
}
});
}
};
const showErrorMessage = message => {
NotificationManager.error(message, "Error", 5000);
};
const showSuccessMessage = message => {
NotificationManager.success(message, "Message", 3000);
};
const showInfoMessage = message => {
NotificationManager.info(message, "Info", 3000);
};
return (
<div className="App">
<section className="main">
<div className="mb-wrap mb-style-2">
<blockquote cite="http://www.gutenberg.org/ebboks/11">
<p>{quote}</p>
</blockquote>
</div>
<div className="mb-attribution">
<p className="mb-author">{owner}</p>
{selectedAddress.toLowerCase() === owner.toLowerCase() && (
<cite className="owner">You are the owner of the quote</cite>
)}
{selectedAddress.toLowerCase() !== owner.toLowerCase() && (
<cite>You are not the owner of the quote</cite>
)}
</div>
</section>
<section>
{transactionHash !== "" && <Box className={classes.root} mt={2} p={2}>
<Typography>
Check your transaction hash
<Link href={`https://kovan.etherscan.io/tx/${transactionHash}/internal_transactions`} target="_blank"
className={classes.link}>
here
</Link>
</Typography>
</Box>}
</section>
<section>
<div className="submit-container">
<div className="submit-row">
<input
type="text"
placeholder="Enter your quote"
onChange={onQuoteChange}
value={newQuote}
/>
<Button variant="contained" color="primary" onClick={onERCForwardWithEIP712Signature}>
Submit with ERC20 Forwarder (EIP712 Sig)
</Button>
<Button variant="contained" color="primary" onClick={onERCForwardWithPersonalSignature}>
Submit with ERC20 Forwarder (Personal Sig)
</Button>
</div>
</div>
</section>
<NotificationContainer />
</div>
);
}
export default App;