Updated approveAndCall section

This commit is contained in:
Richard Ramos 2018-09-01 07:47:43 -04:00
parent f3557762cd
commit 4b97ac8704
2 changed files with 33 additions and 75 deletions

View File

@ -72,8 +72,6 @@ class IdentityStrategy extends Strategy {
estimatedGas = await this._estimateGas(input);
} else {
const tmp = Math.floor(parseInt((await this._estimateGas(input)).toString(10), 10) * 1.05);
console.log(tmp);
estimatedGas = this.web3.utils.toBN(tmp); // TODO: tune this
}

View File

@ -1,4 +1,5 @@
import React, {Component} from 'react';
import StatusGasRelayer, {Contracts, Functions} from '../status-gas-relayer';
import Button from '@material-ui/core/Button';
import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions';
@ -15,7 +16,7 @@ import TextField from '@material-ui/core/TextField';
import config from '../config';
import web3 from 'Embark/web3';
import {withStyles} from '@material-ui/core/styles';
import { Typography } from '@material-ui/core';
const styles = theme => ({
root: {
width: '100%',
@ -63,7 +64,7 @@ class ApproveAndCallGasRelayed extends Component {
});
};
sign = (event) => {
sign = async (event) => {
if(event) event.preventDefault();
this.setState({
@ -74,27 +75,21 @@ class ApproveAndCallGasRelayed extends Component {
IdentityGasRelay.options.address = this.props.identityAddress;
try {
IdentityGasRelay.methods.deployGasRelayHash(
this.state.value,
web3.utils.soliditySha3({t: 'bytes', v: this.state.data}),
this.props.nonce,
this.state.gasPrice,
this.state.gasLimit,
this.state.gasToken
)
.call()
.then(message => {
return web3.eth.sign(message, web3.eth.defaultAccount);
})
.then(signature => {
this.setState({signature});
});
const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount)
.setContractFunction(Functions.Identity.approveAndCall)
.setTransaction(this.state.to, this.state.value, this.state.data)
.setBaseToken(this.state.baseToken)
.setGas(this.state.gasToken, this.state.gasPrice, this.state.gasLimit);
const signature = await s.sign(web3);
this.setState({signature});
} catch(error){
this.setState({transactionError: error.message});
}
}
obtainRelayers = event => {
obtainRelayers = async event => {
event.preventDefault();
const {web3, kid, skid} = this.props;
@ -106,34 +101,20 @@ class ApproveAndCallGasRelayed extends Component {
this.props.clearMessages();
try {
const sendOptions = {
ttl: 1000,
sig: kid,
powTarget: 1,
powTime: 20,
topic: this.state.topic,
symKeyID: skid,
payload: web3.utils.toHex({
'contract': this.props.identityAddress,
'address': web3.eth.defaultAccount,
'action': 'availability',
'gasToken': this.state.gasToken,
'gasPrice': this.state.gasPrice
})
};
web3.shh.post(sendOptions)
.then(() => {
this.setState({submitting: false});
console.log("Message sent");
return true;
});
const s = new StatusGasRelayer.AvailableRelayers(Contracts.Identity, this.props.identityAddress, web3.eth.defaultAccount)
.setRelayersSymKeyID(skid)
.setAsymmetricKeyID(kid)
.setGas(this.state.gasToken, this.state.gasPrice);
await s.post(web3);
console.log("Message sent");
this.setState({submitting: false});
} catch(error){
this.setState({messagingError: error.message, submitting: false});
}
}
sendTransaction = event => {
sendTransaction = async event => {
event.preventDefault();
const {web3, kid} = this.props;
@ -151,39 +132,18 @@ class ApproveAndCallGasRelayed extends Component {
try {
let jsonAbi = IdentityGasRelay._jsonInterface.filter(x => x.name == "approveAndCallGasRelayed")[0];
let funCall = web3.eth.abi.encodeFunctionCall(jsonAbi, [
this.state.baseToken,
this.state.to,
this.state.value,
this.state.data,
this.props.nonce,
this.state.gasPrice,
this.state.gasLimit,
this.state.gasToken,
this.state.signature
]);
const sendOptions = {
ttl: 1000,
sig: kid,
powTarget: 1,
powTime: 20,
topic: this.state.topic,
pubKey: relayer,
payload: web3.utils.toHex({
'contract': this.props.identityAddress,
'address': web3.eth.defaultAccount,
'action': 'transaction',
'encodedFunctionCall': funCall
})
};
const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount)
.setContractFunction(Functions.Identity.approveAndCall)
.setTransaction(this.state.to, this.state.value, this.state.data)
.setBaseToken(this.state.baseToken)
.setGas(this.state.gasToken, this.state.gasPrice, this.state.gasLimit)
.setRelayer(relayer)
.setAsymmetricKeyID(kid);
web3.shh.post(sendOptions)
.then(() => {
this.setState({submitting: false});
console.log("Message sent");
return true;
});
await s.post(this.state.signature, web3);
this.setState({submitting: false});
console.log("Message sent");
} catch(error){
this.setState({messagingError: error.message, submitting: false});
}