add toggle approved helper function

This commit is contained in:
Barry Gitarts 2018-08-15 15:11:55 -04:00
parent 30359be72f
commit e2fa2cc604
2 changed files with 15 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import web3 from "Embark/web3"
import ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry';
import TestToken from 'Embark/contracts/TestToken';
import React from 'react';
import { connect } from 'react-redux';
import Hidden from '@material-ui/core/Hidden';

View File

@ -1,3 +1,17 @@
import web3 from "Embark/web3"
import BigNumber from 'bignumber.js';
// By default BigNumber's `toString` method converts to exponential notation if the value has
// more then 20 digits. We want to avoid this behavior, so we set EXPONENTIAL_AT to a high number
BigNumber.config({
EXPONENTIAL_AT: 1000,
});
const unlimitedAllowance = new BigNumber(2).pow(256).sub(1);
export const getDefaultAccount = () => web3.eth.defaultAccount;
export const toggleApproved = (token, spender, approved, onlyApprove = false) => {
const isApproved = !!Number(approved);
if (isApproved && onlyApprove) return;
const { approve } = token.methods;
const amountToApprove = isApproved ? 0 : unlimitedAllowance;
return approve(spender,amountToApprove).send();
}