From ac70ba2b8f8755ffd6cefb03695795e847f64863 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 17 Apr 2018 13:25:25 -0400 Subject: [PATCH] Calculating gas limit --- app/gas-relayer/config/config.json | 4 ++-- app/gas-relayer/src/service.js | 19 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/gas-relayer/config/config.json b/app/gas-relayer/config/config.json index 0e14159..1a2c578 100644 --- a/app/gas-relayer/config/config.json +++ b/app/gas-relayer/config/config.json @@ -42,7 +42,7 @@ "data": "_data", "gasPrice": "_gasPrice", "gasToken": "_gasToken", - "gasMinimal": "_gasMinimal", + "gasLimit": "_gasLimit", "isToken": true, "token": "_baseToken" }, @@ -53,7 +53,7 @@ "data": "_data", "gasPrice": "_gasPrice", "gasToken": "_gasToken", - "gasMinimal": "_gasMinimal", + "gasLimit": "_gasLimit", "isToken": false } ] diff --git a/app/gas-relayer/src/service.js b/app/gas-relayer/src/service.js index 0389482..b7f9ce9 100644 --- a/app/gas-relayer/src/service.js +++ b/app/gas-relayer/src/service.js @@ -158,10 +158,9 @@ const processMessages = async function(error, message, subscription){ } const gasPrice = web3.utils.toBN(params[contract.allowedFunctions[functionName].gasPrice]); - const gasMinimal = web3.utils.toBN(params[contract.allowedFunctions[functionName].gasMinimal]); + const gasLimit = web3.utils.toBN(params[contract.allowedFunctions[functionName].gasLimit]); - - // Determining balances of gasPrice + // Determining balances of token used let balance; if(token.symbol == "ETH") balance = new web3.utils.BN(await web3.eth.getBalance(address)); @@ -171,10 +170,6 @@ const processMessages = async function(error, message, subscription){ balance = new web3.utils.BN(await Token.methods.balanceOf(address).call()); } - if(balance.lt(web3.utils.toBN(gasPrice.mul(gasMinimal)))){ - return reply("Not enough balance", message); - } - // Determine if enough balance for baseToken if(contract.allowedFunctions[functionName].isToken){ const Token = new web3.eth.Contract(erc20ABI); @@ -193,14 +188,18 @@ const processMessages = async function(error, message, subscription){ factor = 1; } - // TODO Determine cost of running function in ether - // TODO Determine if gas price offered is worth at least the minimum + const balanceInETH = balance.div(factor); + const gasLimitInETH = gasLimit.div(factor); + if(balanceInETH.lt(web3.utils.toBN(gasPrice.mul(gasLimit)))){ + return reply("Not enough balance", message); + } web3.eth.sendTransaction({ from: config.blockchain.account, to: address, value: 0, - data: payload + data: payload, + gasLimit: gasLimitInETH }) .then(function(receipt){ return reply("Transaction mined;" + receipt.transactionHash, message);