Adding gas limit estimation

This commit is contained in:
Richard Ramos 2018-04-17 16:40:31 -04:00
parent ac70ba2b8f
commit 967a742bfd
5 changed files with 32 additions and 13 deletions

View File

@ -1 +1 @@
[{"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":"_gasMinimal","type":"uint256"},{"name":"_gasToken","type":"address"},{"name":"_messageSignatures","type":"bytes"}],"name":"approveAndCallGasRelayed","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"},{"name":"_data","type":"bytes"},{"name":"_nonce","type":"uint256"},{"name":"_gasPrice","type":"uint256"},{"name":"_gasMinimal","type":"uint256"},{"name":"_gasToken","type":"address"},{"name":"_messageSignatures","type":"bytes"}],"name":"callGasRelayed","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[],"name":"Debug","type":"event"}]
[{"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":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"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":"callGasRelayed","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[],"name":"Debug","type":"event"}]

View File

@ -1,8 +1,9 @@
{
"blockchain": {
"account": "0x9e14016ba37b23498885864053fded5226161a3a",
"rpcHost": "localhost",
"rpcPort": 8545
"protocol": "ws",
"host": "localhost",
"port": 8545
},
"whisper": {
@ -32,7 +33,7 @@
"contracts":{
"IdentityGasRelay": {
"isIdentity": true,
"factoryAddress": "0x96f0811c6484c59c2674da1f64e725c01d82c1b5",
"factoryAddress": "0x18f71378f4735b35e01eb71afcc66df090eccea7",
"abiFile": "../abi/IdentityGasRelay.json",
"allowedFunctions": [
{

View File

@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"daemonize2": "^0.4.2",
"ganache-cli": "^6.1.0",
"md5": "^2.2.1",
"web3": "^1.0.0-beta.33"
}

View File

@ -2,6 +2,7 @@ const md5 = require('md5');
const Web3 = require('web3');
const config = require('../config/config.json');
const web3 = new Web3(`${config.whisper.protocol}://${config.whisper.host}:${config.whisper.port}`);
var ganache = require("ganache-cli");
const erc20ABI = require('../abi/ERC20.json');
@ -64,7 +65,6 @@ for(let contractName in config.contracts){
}
}
// Setting up Whisper options
const shhOptions = {
ttl: config.whisper.ttl,
@ -169,7 +169,7 @@ const processMessages = async function(error, message, subscription){
Token.options.address = params[contract.allowedFunctions[functionName].gasToken];
balance = new web3.utils.BN(await Token.methods.balanceOf(address).call());
}
// Determine if enough balance for baseToken
if(contract.allowedFunctions[functionName].isToken){
const Token = new web3.eth.Contract(erc20ABI);
@ -179,21 +179,38 @@ const processMessages = async function(error, message, subscription){
return reply("Not enough balance", message);
}
}
// Obtain factor
let factor;
if(contract.allowedFunctions[functionName].isToken){
factor = config.tokens[tokenAddress].pricePlugin.getFactor();
factor =web3.utils.toBN(config.tokens[tokenAddress].pricePlugin.getFactor());
} else {
factor = 1;
factor = web3.utils.toBN(1);
}
const balanceInETH = balance.div(factor);
const gasLimitInETH = gasLimit.div(factor);
if(balanceInETH.lt(web3.utils.toBN(gasPrice.mul(gasLimit)))){
if(balanceInETH.lt(web3.utils.toBN(gasPrice.mul(gasLimit)))) {
return reply("Not enough balance", message);
}
// Estimate costs
const web3Sim = new Web3(ganache.provider({fork: `${config.blockchain.protocol}://${config.blockchain.host}:${config.blockchain.port}`}));
const simAccounts = await web3.eth.getAccounts();
let simulatedReceipt = await web3.eth.sendTransaction({
from: simAccounts[0],
to: address,
value: 0,
data: payload
});
const estimatedGas = web3.utils.toBN(simulatedReceipt.gasUsed);
if(gasLimit.lt(estimatedGas)) {
return reply("Gas limit below estimated gas", message);
}
web3.eth.sendTransaction({
from: config.blockchain.account,
to: address,

View File

@ -10,7 +10,7 @@ contract TestIdentityGasRelay {
bytes _data,
uint _nonce,
uint _gasPrice,
uint _gasMinimal,
uint _gasLimit,
address _gasToken,
bytes _messageSignatures
) external {
@ -23,7 +23,7 @@ contract TestIdentityGasRelay {
bytes _data,
uint _nonce,
uint _gasPrice,
uint _gasMinimal,
uint _gasLimit,
address _gasToken,
bytes _messageSignatures
) external {