aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-05-30 22:30:29 +0800
committerFabio Berger <me@fabioberger.com>2017-05-30 22:30:29 +0800
commit141d23f78afc822023f59a4b4dce175a8ba7cffd (patch)
treeb4e03b69e0b47581a23c8c4d2eb2eaa67e6268a8
parentf066b521438f6e2d4a8c32194a569e59c7079c4f (diff)
downloaddexon-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
-rw-r--r--src/contract_wrappers/token_wrapper.ts36
-rw-r--r--src/types.ts1
-rw-r--r--src/utils/constants.ts1
3 files changed, 34 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];
diff --git a/src/types.ts b/src/types.ts
index c2a048e8a..bf0a1d8b4 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -42,6 +42,7 @@ export interface TokenContract {
allowance: {
call: (ownerAddress: string, allowedAddress: string) => Promise<BigNumber.BigNumber>;
};
+ approve: (proxyAddress: string, amountInBaseUnits: BigNumber.BigNumber, opts: any) => void;
}
export interface TokenRegistryContract {
diff --git a/src/utils/constants.ts b/src/utils/constants.ts
index ec2fe744a..5a5ba0e0a 100644
--- a/src/utils/constants.ts
+++ b/src/utils/constants.ts
@@ -1,3 +1,4 @@
export const constants = {
NULL_ADDRESS: '0x0000000000000000000000000000000000000000',
+ TESTRPC_NETWORK_ID: 50,
};