Adding validations to SNTStrategy

This commit is contained in:
Richard Ramos 2018-08-13 11:12:53 -04:00
parent fe7446920c
commit 31afec7806
4 changed files with 32 additions and 11 deletions

View File

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

View File

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

View File

@ -1,18 +1,34 @@
const Strategy = require('./BaseStrategy'); const Strategy = require('./BaseStrategy');
const TransferSNT = "0x916b6511";
class SNTStrategy extends Strategy { class SNTStrategy extends Strategy {
async execute(message){ async execute(message){
const params = this._obtainParametersFunc(message); const params = this._obtainParametersFunc(message);
// Verifying if token is allowed
const token = this.settings.getTokenBySymbol("SNT");
if(token == undefined) return {success: false, message: "Token not allowed"};
if(message.input.functionName == TransferSNT){
const estimatedGas = await this.web3.eth.estimateGas({
data: message.input.payload,
from: this.config.node.blockchain.account,
to: message.input.address
});
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); // Adding 10% - TODO: tune this value
if(balance.lt(requiredGas)){
return {success: false, message: "Address has not enough balance to transfer specified value + fees (" + requiredGas.toString() + ")"};
}
} else {
// TODO: logic is needed for executeGasRelayed. // TODO: logic is needed for executeGasRelayed.
}
// TODO: Transfers are simple and only need to:
// -------- estimate cost of transfer
// -------- check balance is enough to cover transfer + gas estimate
// ------- notify if not enough balance for transfer too.
return { return {
success: true, success: true,

View File

@ -97,7 +97,7 @@ class TransferSNT extends Component {
} }
} }
sendMessage = event => { sendMessage = async event => {
event.preventDefault(); event.preventDefault();
const {web3, kid, skid} = this.props; const {web3, kid, skid} = this.props;
@ -117,6 +117,9 @@ class TransferSNT extends Component {
this.state.gasPrice, this.state.gasPrice,
this.state.signature this.state.signature
]); ]);
const accounts = await web3.eth.getAccounts();
const sendOptions = { const sendOptions = {
ttl: 1000, ttl: 1000,
sig: kid, sig: kid,
@ -126,6 +129,7 @@ class TransferSNT extends Component {
symKeyID: skid, symKeyID: skid,
payload: web3.utils.toHex({ payload: web3.utils.toHex({
'address': SNTController.options.address, 'address': SNTController.options.address,
'wallet': accounts[2],
'encodedFunctionCall': funCall 'encodedFunctionCall': funCall
}) })
}; };