diff options
author | Fabio Berger <me@fabioberger.com> | 2018-06-13 22:01:01 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-06-13 22:01:01 +0800 |
commit | fe75660e88ed0c37c4f3d461a644bd9305bf6183 (patch) | |
tree | 5c4ec25bc6ef5c0b5573cf4e4f9fe34428394a49 /packages/contracts/src/utils/erc20_wrapper.ts | |
parent | 946e6c16442ce434e160fa47a87cd705c5274038 (diff) | |
download | dexon-sol-tools-fe75660e88ed0c37c4f3d461a644bd9305bf6183.tar.gz dexon-sol-tools-fe75660e88ed0c37c4f3d461a644bd9305bf6183.tar.zst dexon-sol-tools-fe75660e88ed0c37c4f3d461a644bd9305bf6183.zip |
Refactor ERC20 and ERC721 wrappers for V2 and introduce the assetWrapper superset
Diffstat (limited to 'packages/contracts/src/utils/erc20_wrapper.ts')
-rw-r--r-- | packages/contracts/src/utils/erc20_wrapper.ts | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/packages/contracts/src/utils/erc20_wrapper.ts b/packages/contracts/src/utils/erc20_wrapper.ts index fc42fbff2..2d367ae1c 100644 --- a/packages/contracts/src/utils/erc20_wrapper.ts +++ b/packages/contracts/src/utils/erc20_wrapper.ts @@ -1,3 +1,4 @@ +import { assetProxyUtils } from '@0xproject/order-utils'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Provider } from 'ethereum-types'; @@ -18,6 +19,7 @@ export class ERC20Wrapper { private _provider: Provider; private _dummyTokenContracts: DummyERC20TokenContract[]; private _proxyContract?: ERC20ProxyContract; + private _proxyIdIfExists?: number; constructor(provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) { this._dummyTokenContracts = []; this._web3Wrapper = new Web3Wrapper(provider); @@ -50,8 +52,13 @@ export class ERC20Wrapper { this._provider, txDefaults, ); + this._proxyIdIfExists = await this._proxyContract.getProxyId.callAsync(); return this._proxyContract; } + public getProxyId(): number { + this._validateProxyContractExistsOrThrow(); + return this._proxyIdIfExists as number; + } public async setBalancesAndAllowancesAsync(): Promise<void> { this._validateDummyTokenContractsExistOrThrow(); this._validateProxyContractExistsOrThrow(); @@ -76,24 +83,28 @@ export class ERC20Wrapper { } } } - public async getBalanceAsync(owner: string, token: string): Promise<BigNumber> { - const tokenContractIfExists = _.find(this._dummyTokenContracts, c => c.address === token); + public async getBalanceAsync(owner: string, assetData: string): Promise<BigNumber> { + const erc20ProxyData = assetProxyUtils.decodeERC20AssetData(assetData); + const tokenAddress = erc20ProxyData.tokenAddress; + const tokenContractIfExists = _.find(this._dummyTokenContracts, c => c.address === tokenAddress); if (_.isUndefined(tokenContractIfExists)) { - throw new Error(`Token: ${token} was not deployed through ERC20Wrapper`); + throw new Error(`Token: ${tokenAddress} was not deployed through ERC20Wrapper`); } const balance = new BigNumber(await tokenContractIfExists.balanceOf.callAsync(owner)); return balance; } - public async getProxyAllowanceAsync(owner: string, token: string): Promise<BigNumber> { + public async getProxyAllowanceAsync(owner: string, assetData: string): Promise<BigNumber> { + const erc20ProxyData = assetProxyUtils.decodeERC20AssetData(assetData); + const tokenAddress = erc20ProxyData.tokenAddress; this._validateProxyContractExistsOrThrow(); - const tokenContractIfExists = _.find(this._dummyTokenContracts, c => c.address === token); + const tokenContractIfExists = _.find(this._dummyTokenContracts, c => c.address === tokenAddress); if (_.isUndefined(tokenContractIfExists)) { - throw new Error(`Token: ${token} was not deployed through ERC20Wrapper`); + throw new Error(`Token: ${tokenAddress} was not deployed through ERC20Wrapper`); } - const balance = new BigNumber( + const allowance = new BigNumber( await tokenContractIfExists.allowance.callAsync(owner, (this._proxyContract as ERC20ProxyContract).address), ); - return balance; + return allowance; } public async getBalancesAsync(): Promise<ERC20BalancesByOwner> { this._validateDummyTokenContractsExistOrThrow(); |