aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-watcher/src/fetchers/asset_balance_and_proxy_allowance_fetcher.ts
blob: a1de22a5ec630ed0b0cea4b991c8d2060f2b9db3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// tslint:disable:no-unnecessary-type-assertion
import { BlockParamLiteral, ERC20TokenWrapper, ERC721TokenWrapper } from '@0xproject/contract-wrappers';
import { AbstractBalanceAndProxyAllowanceFetcher, assetDataUtils } from '@0xproject/order-utils';
import { AssetProxyId, ERC20AssetData, ERC721AssetData } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';

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;
            }
        }
    }
}