diff options
author | Fabio Berger <me@fabioberger.com> | 2017-05-30 22:30:29 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-05-30 22:30:29 +0800 |
commit | 141d23f78afc822023f59a4b4dce175a8ba7cffd (patch) | |
tree | b4e03b69e0b47581a23c8c4d2eb2eaa67e6268a8 /src/contract_wrappers/token_wrapper.ts | |
parent | f066b521438f6e2d4a8c32194a569e59c7079c4f (diff) | |
download | dexon-sol-tools-141d23f78afc822023f59a4b4dce175a8ba7cffd.tar dexon-sol-tools-141d23f78afc822023f59a4b4dce175a8ba7cffd.tar.gz dexon-sol-tools-141d23f78afc822023f59a4b4dce175a8ba7cffd.tar.bz2 dexon-sol-tools-141d23f78afc822023f59a4b4dce175a8ba7cffd.tar.lz dexon-sol-tools-141d23f78afc822023f59a4b4dce175a8ba7cffd.tar.xz dexon-sol-tools-141d23f78afc822023f59a4b4dce175a8ba7cffd.tar.zst dexon-sol-tools-141d23f78afc822023f59a4b4dce175a8ba7cffd.zip |
Implement setProxyAllowanceAsync
Diffstat (limited to 'src/contract_wrappers/token_wrapper.ts')
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index 0282d8266..59dfc0667 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -2,11 +2,14 @@ import * as _ from 'lodash'; import * as BigNumber from 'bignumber.js'; import {Web3Wrapper} from '../web3_wrapper'; import {assert} from '../utils/assert'; +import {constants} from '../utils/constants'; import {ContractWrapper} from './contract_wrapper'; import * as TokenArtifacts from '../artifacts/Token.json'; import * as ProxyArtifacts from '../artifacts/Proxy.json'; import {TokenContract, InternalError} from '../types'; +const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 45730; + export class TokenWrapper extends ContractWrapper { private tokenContractsByAddress: {[address: string]: TokenContract}; constructor(web3Wrapper: Web3Wrapper) { @@ -31,7 +34,8 @@ export class TokenWrapper extends ContractWrapper { return balance; } /** - * Retrieves the allowance of an ERC20 token set to the 0x proxy contract by an owner address + * Retrieves the allowance in baseUnits of the ERC20 token set to the 0x proxy contract + * by an owner address */ public async getProxyAllowanceAsync(tokenAddress: string, ownerAddress: string) { assert.isETHAddressHex('ownerAddress', ownerAddress); @@ -39,9 +43,33 @@ export class TokenWrapper extends ContractWrapper { const tokenContract = await this.getTokenContractAsync(tokenAddress); const proxyAddress = await this.getProxyAddressAsync(); - let allowance = await tokenContract.allowance.call(ownerAddress, proxyAddress); - allowance = _.isUndefined(allowance) ? new BigNumber(0) : new BigNumber(allowance); - return allowance; + let allowanceInBaseUnits = await tokenContract.allowance.call(ownerAddress, proxyAddress); + allowanceInBaseUnits = _.isUndefined(allowanceInBaseUnits) ? + new BigNumber(0) : + new BigNumber(allowanceInBaseUnits); + return allowanceInBaseUnits; + } + /** + * Sets the 0x proxy contract's allowance to a specified number of a tokens' baseUnits on behalf + * of an owner address. + */ + public async setProxyAllowanceAsync(tokenAddress: string, ownerAddress: string, + amountInBaseUnits: BigNumber.BigNumber) { + assert.isETHAddressHex('ownerAddress', ownerAddress); + assert.isETHAddressHex('tokenAddress', tokenAddress); + assert.isBigNumber('amountInBaseUnits', amountInBaseUnits); + + const tokenContract = await this.getTokenContractAsync(tokenAddress); + const proxyAddress = await this.getProxyAddressAsync(); + // Hack: for some reason default estimated gas amount causes `base fee exceeds gas limit` exception + // on testrpc. Probably related to https://github.com/ethereumjs/testrpc/issues/294 + // TODO: Debug issue in testrpc and submit a PR, then remove this hack + const networkIdIfExists = await this.web3Wrapper.getNetworkIdIfExistsAsync(); + const gas = networkIdIfExists === constants.TESTRPC_NETWORK_ID ? ALLOWANCE_TO_ZERO_GAS_AMOUNT : undefined; + await tokenContract.approve(proxyAddress, amountInBaseUnits, { + from: ownerAddress, + gas, + }); } private async getTokenContractAsync(tokenAddress: string): Promise<TokenContract> { let tokenContract = this.tokenContractsByAddress[tokenAddress]; |