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 | |
parent | 946e6c16442ce434e160fa47a87cd705c5274038 (diff) | |
download | dexon-sol-tools-fe75660e88ed0c37c4f3d461a644bd9305bf6183.tar dexon-sol-tools-fe75660e88ed0c37c4f3d461a644bd9305bf6183.tar.gz dexon-sol-tools-fe75660e88ed0c37c4f3d461a644bd9305bf6183.tar.bz2 dexon-sol-tools-fe75660e88ed0c37c4f3d461a644bd9305bf6183.tar.lz dexon-sol-tools-fe75660e88ed0c37c4f3d461a644bd9305bf6183.tar.xz dexon-sol-tools-fe75660e88ed0c37c4f3d461a644bd9305bf6183.tar.zst dexon-sol-tools-fe75660e88ed0c37c4f3d461a644bd9305bf6183.zip |
Refactor ERC20 and ERC721 wrappers for V2 and introduce the assetWrapper superset
-rw-r--r-- | packages/contracts/src/abstract/abstract_asset_wrapper.ts | 10 | ||||
-rw-r--r-- | packages/contracts/src/utils/asset_wrapper.ts | 32 | ||||
-rw-r--r-- | packages/contracts/src/utils/erc20_wrapper.ts | 27 | ||||
-rw-r--r-- | packages/contracts/src/utils/erc721_wrapper.ts | 35 |
4 files changed, 95 insertions, 9 deletions
diff --git a/packages/contracts/src/abstract/abstract_asset_wrapper.ts b/packages/contracts/src/abstract/abstract_asset_wrapper.ts new file mode 100644 index 000000000..d7ab58fea --- /dev/null +++ b/packages/contracts/src/abstract/abstract_asset_wrapper.ts @@ -0,0 +1,10 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class AbstractAssetWrapper { + public abstract getProxyId(): number; + public abstract async setBalancesAndAllowancesAsync(): Promise<void>; + public abstract async getBalanceAsync(owner: string, assetData: string): Promise<BigNumber>; + public abstract async getProxyAllowanceAsync(owner: string, assetData: string): Promise<BigNumber>; + public abstract getTokenOwnerAddresses(): string[]; + public abstract getTokenAddresses(): string[]; +} diff --git a/packages/contracts/src/utils/asset_wrapper.ts b/packages/contracts/src/utils/asset_wrapper.ts new file mode 100644 index 000000000..4c345aa30 --- /dev/null +++ b/packages/contracts/src/utils/asset_wrapper.ts @@ -0,0 +1,32 @@ +import { assetProxyUtils } from '@0xproject/order-utils'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { AbstractAssetWrapper } from '../abstract/abstract_asset_wrapper'; + +interface ProxyIdToAssetWrappers { + [proxyId: number]: AbstractAssetWrapper; +} + +export class AssetWrapper { + private _proxyIdToAssetWrappers: ProxyIdToAssetWrappers; + constructor(assetWrappers: AbstractAssetWrapper[]) { + this._proxyIdToAssetWrappers = {}; + _.each(assetWrappers, assetWrapper => { + const proxyId = assetWrapper.getProxyId(); + this._proxyIdToAssetWrappers[proxyId] = assetWrapper; + }); + } + public async getBalanceAsync(owner: string, assetData: string): Promise<BigNumber> { + const proxyId = assetProxyUtils.decodeAssetDataId(assetData); + const assetWrapper = this._proxyIdToAssetWrappers[proxyId]; + const balance = await assetWrapper.getBalanceAsync(owner, assetData); + return balance; + } + public async getProxyAllowanceAsync(owner: string, assetData: string): Promise<BigNumber> { + const proxyId = assetProxyUtils.decodeAssetDataId(assetData); + const assetWrapper = this._proxyIdToAssetWrappers[proxyId]; + const balance = await assetWrapper.getProxyAllowanceAsync(owner, assetData); + return balance; + } +} 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(); diff --git a/packages/contracts/src/utils/erc721_wrapper.ts b/packages/contracts/src/utils/erc721_wrapper.ts index b20d9acd2..6a6f10fa7 100644 --- a/packages/contracts/src/utils/erc721_wrapper.ts +++ b/packages/contracts/src/utils/erc721_wrapper.ts @@ -1,4 +1,4 @@ -import { generatePseudoRandomSalt } from '@0xproject/order-utils'; +import { assetProxyUtils, generatePseudoRandomSalt } from '@0xproject/order-utils'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Provider } from 'ethereum-types'; @@ -19,6 +19,7 @@ export class ERC721Wrapper { private _provider: Provider; private _dummyTokenContracts: DummyERC721TokenContract[]; private _proxyContract?: ERC721ProxyContract; + private _proxyIdIfExists?: number; private _initialTokenIdsByOwner: ERC721TokenIdsByOwner = {}; constructor(provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) { this._web3Wrapper = new Web3Wrapper(provider); @@ -47,8 +48,13 @@ export class ERC721Wrapper { 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(); @@ -85,6 +91,33 @@ export class ERC721Wrapper { } } } + public async getBalanceAsync(owner: string, assetData: string): Promise<BigNumber> { + const erc721ProxyData = assetProxyUtils.decodeERC721AssetData(assetData); + const tokenAddress = erc721ProxyData.tokenAddress; + const tokenContractIfExists = _.find(this._dummyTokenContracts, c => c.address === tokenAddress); + if (_.isUndefined(tokenContractIfExists)) { + throw new Error(`Token: ${tokenAddress} was not deployed through ERC20Wrapper`); + } + const tokenId = erc721ProxyData.tokenId; + const tokenOwner = await tokenContractIfExists.ownerOf.callAsync(tokenId); + const balance = tokenOwner === owner ? new BigNumber(1) : new BigNumber(0); + return balance; + } + public async getProxyAllowanceAsync(owner: string, assetData: string): Promise<BigNumber> { + const erc721ProxyData = assetProxyUtils.decodeERC721AssetData(assetData); + const tokenAddress = erc721ProxyData.tokenAddress; + this._validateProxyContractExistsOrThrow(); + const tokenContractIfExists = _.find(this._dummyTokenContracts, c => c.address === tokenAddress); + if (_.isUndefined(tokenContractIfExists)) { + throw new Error(`Token: ${tokenAddress} was not deployed through ERC20Wrapper`); + } + const operator = (this._proxyContract as ERC721ProxyContract).address; + const didApproveAll = await tokenContractIfExists.isApprovedForAll.callAsync(owner, operator); + const tokenId = erc721ProxyData.tokenId; + const allowedAddress = await tokenContractIfExists.getApproved.callAsync(tokenId); + const allowance = allowedAddress === operator || didApproveAll ? new BigNumber(1) : new BigNumber(0); + return allowance; + } public async getBalancesAsync(): Promise<ERC721TokenIdsByOwner> { this._validateDummyTokenContractsExistOrThrow(); this._validateBalancesAndAllowancesSetOrThrow(); |