How to use the @0x/order-utils.assetDataUtils.decodeAssetDataOrThrow function in @0x/order-utils

To help you get started, we’ve selected a few @0x/order-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 0xProject / 0x-monorepo / packages / contract-wrappers / src / fetchers / asset_balance_and_proxy_allowance_fetcher.ts View on Github external
public async getBalanceAsync(assetData: string, userAddress: string): Promise {
        const decodedAssetData = assetDataUtils.decodeAssetDataOrThrow(assetData);
        let balance: BigNumber | undefined;
        if (assetDataUtils.isERC20AssetData(decodedAssetData)) {
            balance = await this._erc20Token.getBalanceAsync(decodedAssetData.tokenAddress, userAddress, {
                defaultBlock: this._stateLayer,
            });
        } else if (assetDataUtils.isERC721AssetData(decodedAssetData)) {
            const tokenOwner = await this._erc721Token.getOwnerOfAsync(
                decodedAssetData.tokenAddress,
                decodedAssetData.tokenId,
                {
                    defaultBlock: this._stateLayer,
                },
            );
            balance = tokenOwner === userAddress ? new BigNumber(1) : new BigNumber(0);
        } else if (assetDataUtils.isMultiAssetData(decodedAssetData)) {
            // The `balance` for MultiAssetData is the total units of the entire `assetData` that are held by the `userAddress`.
github 0xProject / 0x-monorepo / packages / pipeline / src / parsers / sra_orders / index.ts View on Github external
export function _convertToEntity(apiOrder: APIOrder): SraOrder {
    // TODO(albrow): refactor out common asset data decoding code.
    const makerAssetData = assetDataUtils.decodeAssetDataOrThrow(apiOrder.order.makerAssetData);
    const makerAssetType = makerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721';
    const takerAssetData = assetDataUtils.decodeAssetDataOrThrow(apiOrder.order.takerAssetData);
    const takerAssetType = takerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721';

    const sraOrder = new SraOrder();
    sraOrder.exchangeAddress = apiOrder.order.exchangeAddress;
    sraOrder.orderHashHex = orderHashUtils.getOrderHashHex(apiOrder.order);

    sraOrder.makerAddress = apiOrder.order.makerAddress;
    sraOrder.takerAddress = apiOrder.order.takerAddress;
    sraOrder.feeRecipientAddress = apiOrder.order.feeRecipientAddress;
    sraOrder.senderAddress = apiOrder.order.senderAddress;
    sraOrder.makerAssetAmount = apiOrder.order.makerAssetAmount;
    sraOrder.takerAssetAmount = apiOrder.order.takerAssetAmount;
    sraOrder.makerFee = apiOrder.order.makerFee;
    sraOrder.takerFee = apiOrder.order.takerFee;
    sraOrder.expirationTimeSeconds = apiOrder.order.expirationTimeSeconds;
    sraOrder.salt = apiOrder.order.salt;
github 0xProject / 0x-monorepo / packages / pipeline / src / parsers / sra_orders / index.ts View on Github external
export function _convertToEntity(apiOrder: APIOrder): SraOrder {
    // TODO(albrow): refactor out common asset data decoding code.
    const makerAssetData = assetDataUtils.decodeAssetDataOrThrow(apiOrder.order.makerAssetData);
    const makerAssetType = makerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721';
    const takerAssetData = assetDataUtils.decodeAssetDataOrThrow(apiOrder.order.takerAssetData);
    const takerAssetType = takerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721';

    const sraOrder = new SraOrder();
    sraOrder.exchangeAddress = apiOrder.order.exchangeAddress;
    sraOrder.orderHashHex = orderHashUtils.getOrderHashHex(apiOrder.order);

    sraOrder.makerAddress = apiOrder.order.makerAddress;
    sraOrder.takerAddress = apiOrder.order.takerAddress;
    sraOrder.feeRecipientAddress = apiOrder.order.feeRecipientAddress;
    sraOrder.senderAddress = apiOrder.order.senderAddress;
    sraOrder.makerAssetAmount = apiOrder.order.makerAssetAmount;
    sraOrder.takerAssetAmount = apiOrder.order.takerAssetAmount;
    sraOrder.makerFee = apiOrder.order.makerFee;
    sraOrder.takerFee = apiOrder.order.takerFee;
github 0xProject / 0x-monorepo / packages / asset-swapper / src / swap_quoter.ts View on Github external
private async _getSignedOrdersAsync(makerAssetData: string, takerAssetData: string): Promise {
        assert.isString('makerAssetData', makerAssetData);
        assert.isString('takerAssetData', takerAssetData);
        assetDataUtils.decodeAssetDataOrThrow(takerAssetData);
        assetDataUtils.decodeAssetDataOrThrow(makerAssetData);
        // get orders
        const apiOrders = await this.orderbook.getOrdersAsync(makerAssetData, takerAssetData);
        const orders = _.map(apiOrders, o => o.order);
        const prunedOrders = orderPrunerUtils.pruneForUsableSignedOrders(
            orders,
            this.permittedOrderFeeTypes,
            this.expiryBufferMs,
        );
        const sortedPrunedOrders = sortingUtils.sortOrders(prunedOrders);
        return sortedPrunedOrders;
    }
github 0xProject / 0x-monorepo / packages / asset-swapper / src / swap_quoter.ts View on Github external
private async _getSignedOrdersAsync(makerAssetData: string, takerAssetData: string): Promise {
        assert.isString('makerAssetData', makerAssetData);
        assert.isString('takerAssetData', takerAssetData);
        assetDataUtils.decodeAssetDataOrThrow(takerAssetData);
        assetDataUtils.decodeAssetDataOrThrow(makerAssetData);
        // get orders
        const apiOrders = await this.orderbook.getOrdersAsync(makerAssetData, takerAssetData);
        const orders = _.map(apiOrders, o => o.order);
        const prunedOrders = orderPrunerUtils.pruneForUsableSignedOrders(
            orders,
            this.permittedOrderFeeTypes,
            this.expiryBufferMs,
        );
        const sortedPrunedOrders = sortingUtils.sortOrders(prunedOrders);
        return sortedPrunedOrders;
    }
github 0xProject / 0x-monorepo / packages / asset-swapper / src / swap_quoter.ts View on Github external
public async isTakerMakerAssetDataPairAvailableAsync(
        makerAssetData: string,
        takerAssetData: string,
    ): Promise {
        assert.isString('makerAssetData', makerAssetData);
        assert.isString('takerAssetData', takerAssetData);
        assetDataUtils.decodeAssetDataOrThrow(takerAssetData);
        assetDataUtils.decodeAssetDataOrThrow(makerAssetData);
        const availableMakerAssetDatas = await this.getAvailableMakerAssetDatasAsync(takerAssetData);
        return _.includes(availableMakerAssetDatas, makerAssetData);
    }
github 0xProject / 0x-monorepo / packages / fill-scenarios / src / fill_scenarios.ts View on Github external
private async _increaseBalanceAndAllowanceWithAssetDataAsync(
        assetData: string,
        userAddress: string,
        amount: BigNumber,
    ): Promise {
        const decodedAssetData = assetDataUtils.decodeAssetDataOrThrow(assetData);
        if (assetDataUtils.isERC20AssetData(decodedAssetData)) {
            await this._increaseERC20BalanceAndAllowanceAsync(decodedAssetData.tokenAddress, userAddress, amount);
        } else if (assetDataUtils.isERC721AssetData(decodedAssetData)) {
            await this._increaseERC721BalanceAndAllowanceAsync(
                decodedAssetData.tokenAddress,
                userAddress,
                decodedAssetData.tokenId,
            );
        } else if (assetDataUtils.isMultiAssetData(decodedAssetData)) {
            for (const [index, nestedAssetDataElement] of decodedAssetData.nestedAssetData.entries()) {
                const amountsElement = decodedAssetData.amounts[index];
                const totalAmount = amount.times(amountsElement);
                await this._increaseBalanceAndAllowanceWithAssetDataAsync(
                    nestedAssetDataElement,
                    userAddress,
                    totalAmount,
github 0xProject / 0x-monorepo / packages / asset-swapper / src / swap_quoter.ts View on Github external
public async getAvailableTakerAssetDatasAsync(makerAssetData: string): Promise {
        assert.isString('makerAssetData', makerAssetData);
        assetDataUtils.decodeAssetDataOrThrow(makerAssetData);
        const allAssetPairs = await this.orderbook.getAvailableAssetDatasAsync();
        const assetPairs = allAssetPairs
            .filter(pair => pair.assetDataA.assetData === makerAssetData)
            .map(pair => pair.assetDataB.assetData);
        return assetPairs;
    }