Adding validations to SNTStrategy
This commit is contained in:
parent
fe7446920c
commit
31afec7806
|
@ -10,14 +10,15 @@ class BaseStrategy {
|
|||
this.config = config;
|
||||
}
|
||||
|
||||
async getBalance(token, message, gasToken){
|
||||
async getBalance(address, token, message, gasToken){
|
||||
// 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(message.input.address));
|
||||
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;
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ class IdentityStrategy extends Strategy {
|
|||
|
||||
// gasPrice * limit calculation
|
||||
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)))) {
|
||||
return {success: false, message: "Identity has not enough tokens for gasPrice*gasLimit"};
|
||||
}
|
||||
|
|
|
@ -1,18 +1,34 @@
|
|||
const Strategy = require('./BaseStrategy');
|
||||
|
||||
const TransferSNT = "0x916b6511";
|
||||
|
||||
class SNTStrategy extends Strategy {
|
||||
|
||||
async execute(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: 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 {
|
||||
success: true,
|
||||
|
|
|
@ -97,7 +97,7 @@ class TransferSNT extends Component {
|
|||
}
|
||||
}
|
||||
|
||||
sendMessage = event => {
|
||||
sendMessage = async event => {
|
||||
event.preventDefault();
|
||||
|
||||
const {web3, kid, skid} = this.props;
|
||||
|
@ -117,6 +117,9 @@ class TransferSNT extends Component {
|
|||
this.state.gasPrice,
|
||||
this.state.signature
|
||||
]);
|
||||
|
||||
const accounts = await web3.eth.getAccounts();
|
||||
|
||||
const sendOptions = {
|
||||
ttl: 1000,
|
||||
sig: kid,
|
||||
|
@ -126,6 +129,7 @@ class TransferSNT extends Component {
|
|||
symKeyID: skid,
|
||||
payload: web3.utils.toHex({
|
||||
'address': SNTController.options.address,
|
||||
'wallet': accounts[2],
|
||||
'encodedFunctionCall': funCall
|
||||
})
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue