aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src/utils/erc721_wrapper.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-06-13 22:01:01 +0800
committerFabio Berger <me@fabioberger.com>2018-06-13 22:01:01 +0800
commitfe75660e88ed0c37c4f3d461a644bd9305bf6183 (patch)
tree5c4ec25bc6ef5c0b5573cf4e4f9fe34428394a49 /packages/contracts/src/utils/erc721_wrapper.ts
parent946e6c16442ce434e160fa47a87cd705c5274038 (diff)
downloaddexon-0x-contracts-fe75660e88ed0c37c4f3d461a644bd9305bf6183.tar.gz
dexon-0x-contracts-fe75660e88ed0c37c4f3d461a644bd9305bf6183.tar.zst
dexon-0x-contracts-fe75660e88ed0c37c4f3d461a644bd9305bf6183.zip
Refactor ERC20 and ERC721 wrappers for V2 and introduce the assetWrapper superset
Diffstat (limited to 'packages/contracts/src/utils/erc721_wrapper.ts')
-rw-r--r--packages/contracts/src/utils/erc721_wrapper.ts35
1 files changed, 34 insertions, 1 deletions
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();