Fixing estimated gas verification

This commit is contained in:
Richard Ramos 2018-05-03 09:36:44 -04:00
parent c39d765af5
commit 293c585d87
2 changed files with 28 additions and 40 deletions

View File

@ -22,7 +22,7 @@
"symbol": "ETH"
},
"0x04936bA0aFC438b653E312298D11c7750f4b74cF": {
"minRelayFactor": 1000,
"minRelayFactor": 10000,
"name": "Random Test Token",
"symbol": "RDN",
"pricePlugin": "../plugins/token-utils.js"

View File

@ -12,7 +12,6 @@ class MessageProcessor {
this.kId = kId;
}
_reply(text, message){
if(message.sig !== undefined){
this.web3.shh.post({
@ -27,7 +26,6 @@ class MessageProcessor {
}
}
async _validateInput(message, input){
const contract = this.settings.getContractByTopic(message.topic);
@ -53,12 +51,10 @@ class MessageProcessor {
this._reply('Invalid contract code', message);
return false;
}
}
}
return true;
}
async _validateInstance(message, input){
const contract = this.settings.getContractByTopic(message.topic);
const kernelVerifSignature = this.web3.utils.sha3(contract.kernelVerification).slice(0, 10);
@ -68,7 +64,6 @@ class MessageProcessor {
data: kernelVerifSignature + "000000000000000000000000" + input.address.slice(2)});
}
_extractInput(message){
return {
address: message.payload.slice(0, 42),
@ -78,7 +73,6 @@ class MessageProcessor {
}
}
_obtainParametersFunc(contract, input){
const parameterList = this.web3.eth.abi.decodeParameters(contract.allowedFunctions[input.functionName].inputs, input.functionParameters);
return function(parameterName){
@ -86,7 +80,6 @@ class MessageProcessor {
}
}
_getFactor(input, contract, gasToken){
if(contract.allowedFunctions[input.functionName].isToken){
return this.web3.utils.toBN(this.settings.getToken(gasToken).pricePlugin.getFactor());
@ -95,7 +88,6 @@ class MessageProcessor {
}
}
async getBalance(token, input, gasToken){
// Determining balances of token used
if(token.symbol == "ETH"){
@ -107,7 +99,6 @@ class MessageProcessor {
}
}
async _estimateGas(input){
const web3Sim = new Web3(ganache.provider({fork: `${this.config.node.protocol}://${this.config.node.host}:${this.config.node.port}`}));
const simAccounts = await web3Sim.eth.getAccounts();
@ -120,9 +111,7 @@ class MessageProcessor {
return web3Sim.utils.toBN(simulatedReceipt.gasUsed);
}
async process(error, message){
if(error){
console.error(error);
} else {
@ -135,8 +124,8 @@ class MessageProcessor {
if(!await this._validateInput(message, input)) return; // TODO Log
if(contract.isIdentity)
if(!this._validateInstance(message, input)) return;
if(contract.isIdentity && !this._validateInstance(message, input))
return this._reply("Invalid identity instance", message);
const params = this._obtainParametersFunc(contract, input);
@ -147,37 +136,38 @@ class MessageProcessor {
const gasPrice = this.web3.utils.toBN(params('gasPrice'));
const gasLimit = this.web3.utils.toBN(params('gasLimit'));
// Determine if enough balance for baseToken
if(contract.allowedFunctions[input.functionName].isToken){
const Token = new this.web3.eth.Contract(erc20ABI);
Token.options.address = params('token');
const baseToken = new this.web3.utils.BN(await Token.methods.balanceOf(input.address).call());
if(balance.lt(this.web3.utils.BN(params('value')))){
this._reply("Not enough balance", message);
this._reply("Identity has not enough balance for specified value", message);
return;
}
}
const gasToken = params('gasToken');
const balance = await this.getBalance(token, input, gasToken);
const factor = this._getFactor(input, contract, gasToken);
const balanceInETH = balance.div(factor);
const gasLimitInETH = gasLimit.div(factor);
if(balanceInETH.lt(this.web3.utils.toBN(gasPrice.mul(gasLimitInETH)))) {
this._reply("Not enough balance", message);
if(balance.lt(this.web3.utils.toBN(gasPrice.mul(gasLimit)))) {
this._reply("Identity has not enough tokens for gasPrice*gasLimit", message);
return;
}
const factor = this._getFactor(input, contract, gasToken);
const balanceInETH = balance.div(factor);
const gasPriceInETH = gasPrice.div(factor);
const gasLimitInETH = gasLimit.div(factor);
const estimatedGas = this._estimateGas(input);
if(gasLimitInETH.lt(estimatedGas)) {
return this._reply("Gas limit below estimated gas", message);
}
if(estimatedGas < token.minRelayFactor || gasLimitInETH.lt(token.minRelayFactor)){
return this._reply("Estimated gas below minimum", message);
const estimatedGasInToken = estimatedGas.mul(factor);
if(estimatedGasInToken.mul(gasPrice) < token.minRelayFactor){
return this._reply("Token gasPrice*gasLimit below accepted minimum", message);
}
let p = {
@ -185,24 +175,22 @@ class MessageProcessor {
to: input.address,
value: 0,
data: input.payload,
gas: gasLimitInETH.toString(),
gasPrice: this.config.node.blockchain.gasPrice
gas: gasLimitInETH,
gasPrice: gasPriceInETH
};
this.web3.eth.sendTransaction(p)
.then((receipt) => {
return this._reply("Transaction mined;" + receipt.transactionHash + ';' + JSON.stringify(receipt), message);
}).catch((err) => {
this._reply("Couldn't mine transaction: " + err.message, message);
// TODO log this?
console.error(err);
});
.then((receipt) => {
return this._reply("Transaction mined;"
+ receipt.transactionHash
+ ';'
+ JSON.stringify(receipt.events)
, message);
}).catch((err) => {
this._reply("Couldn't mine transaction: " + err.message, message);
// TODO log this?
console.error(err);
});
}
}
}