aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contract-wrappers/src/fetchers
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-09-05 01:53:18 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-09-05 01:53:18 +0800
commite61dbbb6cf395d3bf807340cac8f99f224ec616e (patch)
tree04cbe061658c4062423aa6ac40c0968c5b250c90 /packages/contract-wrappers/src/fetchers
parent11df29fa8e4f355ec438966fa9e40cb09fe97380 (diff)
parent6a619a4084188d1c48d8030d0fe2a1ba777c55e3 (diff)
downloaddexon-sol-tools-e61dbbb6cf395d3bf807340cac8f99f224ec616e.tar
dexon-sol-tools-e61dbbb6cf395d3bf807340cac8f99f224ec616e.tar.gz
dexon-sol-tools-e61dbbb6cf395d3bf807340cac8f99f224ec616e.tar.bz2
dexon-sol-tools-e61dbbb6cf395d3bf807340cac8f99f224ec616e.tar.lz
dexon-sol-tools-e61dbbb6cf395d3bf807340cac8f99f224ec616e.tar.xz
dexon-sol-tools-e61dbbb6cf395d3bf807340cac8f99f224ec616e.tar.zst
dexon-sol-tools-e61dbbb6cf395d3bf807340cac8f99f224ec616e.zip
Merge https://github.com/0xProject/0x-monorepo into feature/website/update-portal-v2
Diffstat (limited to 'packages/contract-wrappers/src/fetchers')
-rw-r--r--packages/contract-wrappers/src/fetchers/asset_balance_and_proxy_allowance_fetcher.ts77
-rw-r--r--packages/contract-wrappers/src/fetchers/order_filled_cancelled_fetcher.ts30
2 files changed, 107 insertions, 0 deletions
diff --git a/packages/contract-wrappers/src/fetchers/asset_balance_and_proxy_allowance_fetcher.ts b/packages/contract-wrappers/src/fetchers/asset_balance_and_proxy_allowance_fetcher.ts
new file mode 100644
index 000000000..023cd5ac3
--- /dev/null
+++ b/packages/contract-wrappers/src/fetchers/asset_balance_and_proxy_allowance_fetcher.ts
@@ -0,0 +1,77 @@
+// tslint:disable:no-unnecessary-type-assertion
+import { AbstractBalanceAndProxyAllowanceFetcher, assetDataUtils } from '@0xproject/order-utils';
+import { AssetProxyId, ERC20AssetData, ERC721AssetData } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
+import { BlockParamLiteral } from 'ethereum-types';
+
+import { ERC20TokenWrapper } from '../contract_wrappers/erc20_token_wrapper';
+import { ERC721TokenWrapper } from '../contract_wrappers/erc721_token_wrapper';
+
+export class AssetBalanceAndProxyAllowanceFetcher implements AbstractBalanceAndProxyAllowanceFetcher {
+ private readonly _erc20Token: ERC20TokenWrapper;
+ private readonly _erc721Token: ERC721TokenWrapper;
+ private readonly _stateLayer: BlockParamLiteral;
+ constructor(erc20Token: ERC20TokenWrapper, erc721Token: ERC721TokenWrapper, stateLayer: BlockParamLiteral) {
+ this._erc20Token = erc20Token;
+ this._erc721Token = erc721Token;
+ this._stateLayer = stateLayer;
+ }
+ public async getBalanceAsync(assetData: string, userAddress: string): Promise<BigNumber> {
+ const decodedAssetData = assetDataUtils.decodeAssetDataOrThrow(assetData);
+ if (decodedAssetData.assetProxyId === AssetProxyId.ERC20) {
+ const decodedERC20AssetData = decodedAssetData as ERC20AssetData;
+ const balance = await this._erc20Token.getBalanceAsync(decodedERC20AssetData.tokenAddress, userAddress, {
+ defaultBlock: this._stateLayer,
+ });
+ return balance;
+ } else {
+ const decodedERC721AssetData = decodedAssetData as ERC721AssetData;
+ const tokenOwner = await this._erc721Token.getOwnerOfAsync(
+ decodedERC721AssetData.tokenAddress,
+ decodedERC721AssetData.tokenId,
+ {
+ defaultBlock: this._stateLayer,
+ },
+ );
+ const balance = tokenOwner === userAddress ? new BigNumber(1) : new BigNumber(0);
+ return balance;
+ }
+ }
+ public async getProxyAllowanceAsync(assetData: string, userAddress: string): Promise<BigNumber> {
+ const decodedAssetData = assetDataUtils.decodeAssetDataOrThrow(assetData);
+ if (decodedAssetData.assetProxyId === AssetProxyId.ERC20) {
+ const decodedERC20AssetData = decodedAssetData as ERC20AssetData;
+ const proxyAllowance = await this._erc20Token.getProxyAllowanceAsync(
+ decodedERC20AssetData.tokenAddress,
+ userAddress,
+ {
+ defaultBlock: this._stateLayer,
+ },
+ );
+ return proxyAllowance;
+ } else {
+ const decodedERC721AssetData = decodedAssetData as ERC721AssetData;
+
+ const isApprovedForAll = await this._erc721Token.isProxyApprovedForAllAsync(
+ decodedERC721AssetData.tokenAddress,
+ userAddress,
+ {
+ defaultBlock: this._stateLayer,
+ },
+ );
+ if (isApprovedForAll) {
+ return new BigNumber(this._erc20Token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS);
+ } else {
+ const isApproved = await this._erc721Token.isProxyApprovedAsync(
+ decodedERC721AssetData.tokenAddress,
+ decodedERC721AssetData.tokenId,
+ {
+ defaultBlock: this._stateLayer,
+ },
+ );
+ const proxyAllowance = isApproved ? new BigNumber(1) : new BigNumber(0);
+ return proxyAllowance;
+ }
+ }
+ }
+}
diff --git a/packages/contract-wrappers/src/fetchers/order_filled_cancelled_fetcher.ts b/packages/contract-wrappers/src/fetchers/order_filled_cancelled_fetcher.ts
new file mode 100644
index 000000000..ba6f5fb5e
--- /dev/null
+++ b/packages/contract-wrappers/src/fetchers/order_filled_cancelled_fetcher.ts
@@ -0,0 +1,30 @@
+// tslint:disable:no-unnecessary-type-assertion
+import { AbstractOrderFilledCancelledFetcher } from '@0xproject/order-utils';
+import { BigNumber } from '@0xproject/utils';
+import { BlockParamLiteral } from 'ethereum-types';
+
+import { ERC20TokenWrapper } from '../contract_wrappers/erc20_token_wrapper';
+import { ExchangeWrapper } from '../contract_wrappers/exchange_wrapper';
+
+export class OrderFilledCancelledFetcher implements AbstractOrderFilledCancelledFetcher {
+ private readonly _exchange: ExchangeWrapper;
+ private readonly _stateLayer: BlockParamLiteral;
+ constructor(exchange: ExchangeWrapper, stateLayer: BlockParamLiteral) {
+ this._exchange = exchange;
+ this._stateLayer = stateLayer;
+ }
+ public async getFilledTakerAmountAsync(orderHash: string): Promise<BigNumber> {
+ const filledTakerAmount = this._exchange.getFilledTakerAssetAmountAsync(orderHash, {
+ defaultBlock: this._stateLayer,
+ });
+ return filledTakerAmount;
+ }
+ public async isOrderCancelledAsync(orderHash: string): Promise<boolean> {
+ const isCancelled = await this._exchange.isCancelledAsync(orderHash);
+ return isCancelled;
+ }
+ public getZRXAssetData(): string {
+ const zrxAssetData = this._exchange.getZRXAssetData();
+ return zrxAssetData;
+ }
+}