diff --git a/app/gas-relayer/config/config.json b/app/gas-relayer/config/config.json index db52f50..d41111c 100644 --- a/app/gas-relayer/config/config.json +++ b/app/gas-relayer/config/config.json @@ -15,10 +15,24 @@ "powTime": 1000 }, + "tokens": { + "0x0": { + "minRelayFactor": 1, + "name": "Ethereum", + "symbol": "ETH" + }, + "0x0ABEEF": { + "minRelayFactor": 1000000000000, + "name": "RandomToken", + "symbol": "RDN", + "pricePlugin": "plugins/token-utils" + } + }, + "contracts":{ "IdentityGasRelay": { "isIdentity": true, - "factoryAddress": "0x24cb6a5b6c81e8ce3edb059ab97984c3d3ea2e3e", + "factoryAddress": "0x96f0811c6484c59c2674da1f64e725c01d82c1b5", "abiFile": "../abi/IdentityGasRelay.json", "allowedFunctions": [ { @@ -26,6 +40,7 @@ "to": "_to", "value": "_value", "data": "_data", + "gasPrice": "_gasPrice", "isToken": true, "token": "_baseToken" }, @@ -34,6 +49,7 @@ "to": "_to", "value": "_value", "data": "_data", + "gasPrice": "_gasPrice", "isToken": false } ] diff --git a/app/gas-relayer/plugins/token-utils.js b/app/gas-relayer/plugins/token-utils.js new file mode 100644 index 0000000..6d210f6 --- /dev/null +++ b/app/gas-relayer/plugins/token-utils.js @@ -0,0 +1,15 @@ +class TokenUtils { + constructor(tokenConfig){ + this.name = tokenConfig.name || ""; + this.symbol = tokenConfig.symbol || ""; + this.minRelayFactor = tokenConfig.minRelayFactor || 1; + } + + getFactor(){ + // TODO get price from somewhere + return 100000; + } +} + + +module.exports = TokenUtils; \ No newline at end of file diff --git a/app/gas-relayer/src/service.js b/app/gas-relayer/src/service.js index 22e6adc..59a7420 100644 --- a/app/gas-relayer/src/service.js +++ b/app/gas-relayer/src/service.js @@ -136,12 +136,34 @@ const processMessages = async function(error, message, subscription){ return reply('Invalid contract code', message); // TODO Log this } - // Determining balances + const params = web3.eth.abi.decodeParameters(contract.allowedFunctions[functionName].inputs, functionParameters); + const tokenAddress = contract.allowedFunctions[functionName].isToken ? params[contract.allowedFunctions[functionName].token] : "0x0"; + + // Determine if gas price offered is worth at least the minimum + const gasPrice = params[contract.allowedFunctions[functionName].gasPrice]; + if(gasPrice < config.tokens[tokenAddress].minRelayFactor){ + return reply("_gasPrice less than minimum: ", config.tokens[tokenAddress.minRelayFactor]); + } + + // Obtain factor + let factor; + if(contract.allowedFunctions[functionName].isToken){ + const PricePlugin = require(config.tokens[tokenAddress].pricePlugin); + const pricePlg = new PricePlugin(config.tokens) + factor = pricePlg.getFactor(); + } else { + factor = 1; + } + + console.log(factor); + + /* + // Determining balances let balance; if(contract.isIdentity){ if(contract.allowedFunctions[functionName].isToken){ - const Token = new web3.eth.Contract(erc20ABI, params[contracts.allowedFunctions[functionName].token]); + const Token = new web3.eth.Contract(erc20ABI, params[contract.allowedFunctions[functionName].token]); balance = new web3.utils.BN(await Token.methods.balanceOf(address).call()); } else { balance = new web3.utils.BN(await web3.eth.getBalance(address)); @@ -149,14 +171,15 @@ const processMessages = async function(error, message, subscription){ } else { // TODO SNT Controller } + */ - // Estimating gas - let estimatedGas = new web3.utils.BN(await web3.eth.estimateGas({ - to: address, - data: params[contracts.allowedFunctions[functionName].data] - })); - + // Estimating gas + /*let estimatedGas = new web3.utils.BN(await web3.eth.estimateGas({ + to: address, + data: params[contract.allowedFunctions[functionName].data] + }));*/ + // TODO determine if balance is enough web3.eth.sendTransaction({