Added approveAndCallGasRelayed function

This commit is contained in:
Richard Ramos 2018-09-01 07:42:44 -04:00
parent 1f14d25dec
commit f3557762cd
2 changed files with 164 additions and 24 deletions

View File

@ -1,5 +1,5 @@
import React, {Component} from 'react'; import React, {Component} from 'react';
import StatusGasRelayer, {Contracts} from '../status-gas-relayer'; import StatusGasRelayer, {Contracts, Functions} from '../status-gas-relayer';
import Button from '@material-ui/core/Button'; import Button from '@material-ui/core/Button';
import Card from '@material-ui/core/Card'; import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions'; import CardActions from '@material-ui/core/CardActions';
@ -78,6 +78,7 @@ class CallGasRelayed extends Component {
try { try {
const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount) const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount)
.setContractFunction(Functions.Identity.call)
.setTransaction(this.state.to, this.state.value, this.state.data) .setTransaction(this.state.to, this.state.value, this.state.data)
.setGas(this.state.gasToken, this.state.gasPrice, this.state.gasLimit); .setGas(this.state.gasToken, this.state.gasPrice, this.state.gasLimit);
@ -133,6 +134,7 @@ class CallGasRelayed extends Component {
try { try {
const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount) const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount)
.setContractFunction(Functions.Identity.call)
.setTransaction(this.state.to, this.state.value, this.state.data) .setTransaction(this.state.to, this.state.value, this.state.data)
.setGas(this.state.gasToken, this.state.gasPrice, this.state.gasLimit) .setGas(this.state.gasToken, this.state.gasPrice, this.state.gasLimit)
.setRelayer(relayer) .setRelayer(relayer)

View File

@ -143,6 +143,11 @@ class IdentityGasRelayedAction extends Action {
return this; return this;
} }
setContractFunction = contractFunction => {
this.contractFunction = contractFunction;
return this;
}
setTransaction = (to, value, data) => { setTransaction = (to, value, data) => {
this.to = to; this.to = to;
this.value = value; this.value = value;
@ -151,6 +156,11 @@ class IdentityGasRelayedAction extends Action {
return this; return this;
} }
setBaseToken = baseToken => {
this.baseToken = baseToken;
return this;
}
_nonce = async(contract) => { _nonce = async(contract) => {
const nonce = await contract.methods.nonce().call(); const nonce = await contract.methods.nonce().call();
return nonce; return nonce;
@ -165,18 +175,34 @@ class IdentityGasRelayedAction extends Action {
sign = async web3 => { sign = async web3 => {
const contract = new web3.eth.Contract(identityGasRelayABI, this.contractAddress); const contract = new web3.eth.Contract(identityGasRelayABI, this.contractAddress);
const nonce = await this._nonce(contract); const nonce = await this._nonce(contract);
let hashedMessage;
// TODO: this depends of the operation to execute switch(this.contractFunction){
const hashedMessage = await contract.methods.callGasRelayHash( case Functions.Identity.call:
this.to, hashedMessage = await contract.methods.callGasRelayHash(
this.value, this.to,
web3.utils.soliditySha3({t: 'bytes', v: this.data}), this.value,
nonce, web3.utils.soliditySha3({t: 'bytes', v: this.data}),
this.gasPrice, nonce,
this.gasLimit, this.gasPrice,
this.gasToken this.gasLimit,
).call(); this.gasToken
).call();
break;
case Functions.Identity.approveAndCall:
hashedMessage = await contract.methods.deployGasRelayHash(
this.value,
web3.utils.soliditySha3({t: 'bytes', v: this.data}),
nonce,
this.gasPrice,
this.gasLimit,
this.gasToken
).call();
break;
default:
throw new Error("Function not allowed");
}
const signedMessage = await web3.eth.sign(hashedMessage, this.accountAddress); const signedMessage = await web3.eth.sign(hashedMessage, this.accountAddress);
return signedMessage; return signedMessage;
@ -191,18 +217,38 @@ class IdentityGasRelayedAction extends Action {
} }
_getMessage = web3 => { _getMessage = web3 => {
// TODO: this depends on the operation to execute let jsonAbi = identityGasRelayABI.find(x => x.name == this.contractFunction);
let jsonAbi = identityGasRelayABI.find(x => x.name == "callGasRelayed"); let funCall;
let funCall = web3.eth.abi.encodeFunctionCall(jsonAbi, [
this.to, switch(this.contractFunction){
this.value, case Functions.Identity.call:
this.data, funCall = web3.eth.abi.encodeFunctionCall(jsonAbi, [
this.nonce, this.to,
this.gasPrice, this.value,
this.gasLimit, this.data,
this.gasToken, this.nonce,
this.signature this.gasPrice,
]); this.gasLimit,
this.gasToken,
this.signature
]);
break;
case Functions.Identity.approveAndCall:
funCall = web3.eth.abi.encodeFunctionCall(jsonAbi, [
this.baseToken,
this.to,
this.value,
this.data,
this.nonce,
this.gasPrice,
this.gasLimit,
this.gasToken,
this.signature
]);
break;
default:
throw new Error("Function not allowed");
}
return { return {
'contract': this.contractAddress, 'contract': this.contractAddress,
@ -349,6 +395,98 @@ const identityGasRelayABI = [
"stateMutability": "nonpayable", "stateMutability": "nonpayable",
"type": "function", "type": "function",
"signature": "0xfd0dded5" "signature": "0xfd0dded5"
},
{
"constant": false,
"inputs": [
{
"name": "_baseToken",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
},
{
"name": "_data",
"type": "bytes"
},
{
"name": "_nonce",
"type": "uint256"
},
{
"name": "_gasPrice",
"type": "uint256"
},
{
"name": "_gasLimit",
"type": "uint256"
},
{
"name": "_gasToken",
"type": "address"
},
{
"name": "_messageSignatures",
"type": "bytes"
}
],
"name": "approveAndCallGasRelayed",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0x59f4ac61"
},
{
"constant": true,
"inputs": [
{
"name": "_value",
"type": "uint256"
},
{
"name": "_dataHash",
"type": "bytes32"
},
{
"name": "_nonce",
"type": "uint256"
},
{
"name": "_gasPrice",
"type": "uint256"
},
{
"name": "_gasLimit",
"type": "uint256"
},
{
"name": "_gasToken",
"type": "address"
}
],
"name": "deployGasRelayHash",
"outputs": [
{
"name": "_callGasRelayHash",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0x86962d85"
} }
]; ];