Simplified getBalance()

This commit is contained in:
Richard Ramos 2018-08-15 13:53:09 -04:00
parent c527ab4ef1
commit 9badb2f3fe
3 changed files with 5 additions and 7 deletions

View File

@ -10,14 +10,13 @@ class BaseStrategy {
this.config = config;
}
async getBalance(address, token, message, gasToken){
async getBalance(address, token){
// Determining balances of token used
// TODO: probably token and gasToken can be unified
if(token.symbol == "ETH"){
return new this.web3.utils.BN(await this.web3.eth.getBalance(address));
} else {
const Token = new this.web3.eth.Contract(erc20ABI.abi);
Token.options.address = gasToken;
Token.options.address = token.address;
return new this.web3.utils.BN(await Token.methods.balanceOf(address).call());
}
}

View File

@ -42,8 +42,7 @@ class IdentityStrategy extends Strategy {
}
// gasPrice * limit calculation
const gasToken = params('_gasToken');
const balance = await this.getBalance(message.input.address, token, message, gasToken);
const balance = await this.getBalance(message.input.address, token);
if(balance.lt(this.web3.utils.toBN(gasPrice.mul(gasLimit)))) {
return {success: false, message: "Identity has not enough tokens for gasPrice*gasLimit"};
}

View File

@ -13,6 +13,8 @@ class SNTStrategy extends Strategy {
const token = this.settings.getTokenBySymbol("SNT");
if(token == undefined) return {success: false, message: "Token not allowed"};
const balance = await this.getBalance(message.input.wallet, token);
if(message.input.functionName == TransferSNT){
const estimatedGas = await this.web3.eth.estimateGas({
data: message.input.payload,
@ -21,7 +23,6 @@ class SNTStrategy extends Strategy {
});
const gas = this.web3.utils.toBN(estimatedGas);
const balance = await this.getBalance(message.input.wallet, token, message, token.address);
const value = this.web3.utils.toBN(params('_amount'));
const requiredGas = value.add(gas);
@ -37,7 +38,6 @@ class SNTStrategy extends Strategy {
if(exc.message.indexOf("revert") > -1) return {success: false, message: "Transaction will revert"};
}
const balance = await this.getBalance(message.input.wallet, token, message, token.address);
if(balance.lt(estimatedGas)){
return {success: false, message: "Address has not enough balance to execute the transaction (" + estimatedGas.toString() + ")"};
}