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,9 +175,11 @@ 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:
hashedMessage = await contract.methods.callGasRelayHash(
this.to, this.to,
this.value, this.value,
web3.utils.soliditySha3({t: 'bytes', v: this.data}), web3.utils.soliditySha3({t: 'bytes', v: this.data}),
@ -176,6 +188,20 @@ class IdentityGasRelayedAction extends Action {
this.gasLimit, this.gasLimit,
this.gasToken this.gasToken
).call(); ).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);
@ -191,9 +217,12 @@ 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, [
switch(this.contractFunction){
case Functions.Identity.call:
funCall = web3.eth.abi.encodeFunctionCall(jsonAbi, [
this.to, this.to,
this.value, this.value,
this.data, this.data,
@ -203,6 +232,23 @@ class IdentityGasRelayedAction extends Action {
this.gasToken, this.gasToken,
this.signature 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"
} }
]; ];