How to use the @0x/types.AssetProxyId.MultiAsset function in @0x/types

To help you get started, we’ve selected a few @0x/types 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 / order-utils / src / exchange_transfer_simulator.ts View on Github external
tradeSide,
                        transferType,
                    );
                }
                if (balance.isLessThan(amountInBaseUnits)) {
                    ExchangeTransferSimulator._throwValidationError(FailureReason.Balance, tradeSide, transferType);
                }
                if (assetProxyId !== AssetProxyId.ERC1155) {
                    // No need to decrease allowance for ERC115 because it's all or nothing.
                    await this._decreaseProxyAllowanceAsync(assetData, from, amountInBaseUnits);
                }
                await this._decreaseBalanceAsync(assetData, from, amountInBaseUnits);
                await this._increaseBalanceAsync(assetData, to, amountInBaseUnits);
                break;
            }
            case AssetProxyId.MultiAsset: {
                const decodedAssetData = assetDataUtils.decodeMultiAssetData(assetData);
                await this._decreaseBalanceAsync(assetData, from, amountInBaseUnits);
                await this._increaseBalanceAsync(assetData, to, amountInBaseUnits);
                for (const [index, nestedAssetDataElement] of decodedAssetData.nestedAssetData.entries()) {
                    const amountsElement = decodedAssetData.amounts[index];
                    const totalAmount = amountInBaseUnits.times(amountsElement);
                    await this.transferFromAsync(
                        nestedAssetDataElement,
                        from,
                        to,
                        totalAmount,
                        tradeSide,
                        transferType,
                    );
                }
                break;
github 0xProject / 0x-monorepo / contracts / exchange / src / balance_stores / local_balance_store.ts View on Github external
toBalances.nonFungible.sort();
                        } else {
                            // Transfer a fungible.
                            const _tokenId = tokenId.toString();
                            _.update(fromBalances.fungible, [_tokenId], balance => balance.minus(tokenAmount));
                            _.update(toBalances.fungible, [_tokenId], balance =>
                                (balance || constants.ZERO_AMOUNT).plus(tokenAmount),
                            );
                        }
                    }
                }
                _.set(this.balances.erc1155, [fromAddress, tokenAddress], fromBalances);
                _.set(this.balances.erc1155, [toAddress, tokenAddress], toBalances);
                break;
            }
            case AssetProxyId.MultiAsset: {
                // tslint:disable-next-line:no-unused-variable
                const [_proxyId, amounts, nestedAssetData] = await this._devUtils
                    .decodeMultiAssetData(assetData)
                    .callAsync();
                for (const [i, amt] of amounts.entries()) {
                    const nestedAmount = amount.times(amt);
                    await this.transferAssetAsync(fromAddress, toAddress, nestedAmount, nestedAssetData[i]);
                }
                break;
            }
            case AssetProxyId.StaticCall:
                // Do nothing
                break;
            default:
                throw new Error(`Unhandled asset proxy ID: ${assetProxyId}`);
        }
github 0xProject / 0x-monorepo / packages / migrations / src / test_contract_configs.ts View on Github external
async function verifyExchangeV3ConfigsAsync(): Promise {
        const exchangeOwner = await exchange.owner().callAsync();
        warnIfMismatch(exchangeOwner, governor.address, 'Unexpected Exchange owner');

        const registeredERC20Proxy = await exchange.getAssetProxy(AssetProxyId.ERC20).callAsync();
        warnIfMismatch(registeredERC20Proxy, erc20Proxy.address, 'Unexpected ERC20Proxy registered in Exchange');

        const registeredERC721Proxy = await exchange.getAssetProxy(AssetProxyId.ERC721).callAsync();
        warnIfMismatch(registeredERC721Proxy, erc721Proxy.address, 'Unexpected ERC721Proxy registered in Exchange');

        const registeredERC1155Proxy = await exchange.getAssetProxy(AssetProxyId.ERC1155).callAsync();
        warnIfMismatch(registeredERC1155Proxy, erc1155Proxy.address, 'Unexpected ERC1155Proxy registered in Exchange');

        const registeredMultiAssetProxy = await exchange.getAssetProxy(AssetProxyId.MultiAsset).callAsync();
        warnIfMismatch(
            registeredMultiAssetProxy,
            multiAssetProxy.address,
            'Unexpected MultiAssetProxy registered in Exchange',
        );

        const registeredStaticCallProxy = await exchange.getAssetProxy(AssetProxyId.StaticCall).callAsync();
        warnIfMismatch(
            registeredStaticCallProxy,
            addresses.staticCallProxy,
            'Unexpected StaticCallProxy registered in Exchange',
        );

        const registeredERC20BridgeProxy = await exchange.getAssetProxy(AssetProxyId.ERC20Bridge).callAsync();
        warnIfMismatch(
            registeredERC20BridgeProxy,
github 0xProject / 0x-monorepo / packages / migrations / src / test_contract_configs.ts View on Github external
warnIfMismatch(exchangeOwner, governor.address, 'Unexpected ExchangeV2 owner');

        const registeredERC20Proxy = await exchangeV2.getAssetProxy(AssetProxyId.ERC20).callAsync();
        warnIfMismatch(registeredERC20Proxy, erc20Proxy.address, 'Unexpected ERC20Proxy registered in ExchangeV2');

        const registeredERC721Proxy = await exchangeV2.getAssetProxy(AssetProxyId.ERC721).callAsync();
        warnIfMismatch(registeredERC721Proxy, erc721Proxy.address, 'Unexpected ERC721Proxy registered in ExchangeV2');

        const registeredERC1155Proxy = await exchangeV2.getAssetProxy(AssetProxyId.ERC1155).callAsync();
        warnIfMismatch(
            registeredERC1155Proxy,
            erc1155Proxy.address,
            'Unexpected ERC1155Proxy registered in ExchangeV2',
        );

        const registeredMultiAssetProxy = await exchangeV2.getAssetProxy(AssetProxyId.MultiAsset).callAsync();
        warnIfMismatch(
            registeredMultiAssetProxy,
            multiAssetProxy.address,
            'Unexpected MultiAssetProxy registered in ExchangeV2',
        );

        const registeredStaticCallProxy = await exchangeV2.getAssetProxy(AssetProxyId.StaticCall).callAsync();
        warnIfMismatch(
            registeredStaticCallProxy,
            addresses.staticCallProxy,
            'Unexpected StaticCallProxy registered in ExchangeV2',
        );
    }
github 0xProject / 0x-monorepo / packages / order-utils / src / asset_data_utils.ts View on Github external
tokenIds,
                    tokenValues,
                    callbackData,
                ] = assetDataEncoder.getABIDecodedTransactionData<[string, BigNumber[], BigNumber[], string]>(
                    'ERC1155Assets',
                    assetData,
                );
                return {
                    assetProxyId,
                    tokenAddress,
                    tokenIds,
                    tokenValues,
                    callbackData,
                };
            }
            case AssetProxyId.MultiAsset: {
                const [amounts, nestedAssetData] = assetDataEncoder.getABIDecodedTransactionData<
                    [BigNumber[], string[]]
                >('MultiAsset', assetData);

                const multiAssetData: MultiAssetData = {
                    assetProxyId,
                    amounts,
                    nestedAssetData,
                };
                return multiAssetData;
            }
            case AssetProxyId.StaticCall:
                const [callTarget, staticCallData, callResultHash] = assetDataEncoder.getABIDecodedTransactionData<
                    [string, string, string]
                >('StaticCall', assetData);
                return {
github 0xProject / 0x-monorepo / packages / order-utils / src / asset_data_utils.ts View on Github external
assertIsMultiAssetData(assetData: string): void {
        if (assetData.length < constants.MULTI_ASSET_DATA_MIN_CHAR_LENGTH_WITH_PREFIX) {
            throw new Error(
                `Could not decode MultiAsset assetData. Expected length of encoded data to be at least ${
                    constants.MULTI_ASSET_DATA_MIN_CHAR_LENGTH_WITH_PREFIX
                }. Got ${assetData.length}`,
            );
        }
        const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData);
        if (assetProxyId !== AssetProxyId.MultiAsset) {
            throw new Error(
                `Could not decode MultiAsset assetData. Expected assetProxyId to be MultiAsset (${
                    AssetProxyId.MultiAsset
                }), but got ${assetProxyId}`,
            );
        }
    },
    /**
github 0xProject / 0x-monorepo / packages / order-utils / src / asset_data_utils.ts View on Github external
(nestedAssetDataElement, index) => {
                const decodedNestedAssetDataElement = assetDataUtils.decodeAssetDataOrThrow(nestedAssetDataElement);
                if (decodedNestedAssetDataElement.assetProxyId === AssetProxyId.MultiAsset) {
                    const recursivelyDecodedAssetData = assetDataUtils.decodeMultiAssetDataRecursively(
                        nestedAssetDataElement,
                    );
                    amounts.push(
                        _.map(recursivelyDecodedAssetData.amounts, amountElement =>
                            amountElement.times(decodedAssetData.amounts[index]),
                        ),
                    );
                    return recursivelyDecodedAssetData.nestedAssetData;
                } else {
                    amounts.push(decodedAssetData.amounts[index]);
                    return decodedNestedAssetDataElement as SingleAssetData;
                }
            },
        );
github 0xProject / 0x-monorepo / packages / order-utils / src / asset_data_utils.ts View on Github external
const decodedNestedAssetData = decodedAssetData.nestedAssetData.map((nestedAssetDataElement, index) => {
            const decodedNestedAssetDataElement = assetDataUtils.decodeAssetDataOrThrow(nestedAssetDataElement);
            if (decodedNestedAssetDataElement.assetProxyId === AssetProxyId.MultiAsset) {
                const recursivelyDecodedAssetData = assetDataUtils.decodeMultiAssetDataRecursively(
                    nestedAssetDataElement,
                );
                amounts.push(
                    recursivelyDecodedAssetData.amounts.map(amountElement =>
                        amountElement.times(decodedAssetData.amounts[index]),
                    ),
                );
                return recursivelyDecodedAssetData.nestedAssetData;
            } else {
                amounts.push(decodedAssetData.amounts[index]);
                return decodedNestedAssetDataElement;
            }
        });
        const flattenedAmounts = _.flattenDeep(amounts);
github 0xProject / 0x-monorepo / packages / order-watcher / src / order_watcher / order_watcher.ts View on Github external
private _deleteLazyStoreBalance(assetData: string, userAddress: string): void {
        const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData);
        switch (assetProxyId) {
            case AssetProxyId.ERC20:
            case AssetProxyId.ERC721:
                this._balanceAndProxyAllowanceLazyStore.deleteBalance(assetData, userAddress);
                break;
            case AssetProxyId.MultiAsset:
                const decodedAssetData = assetDataUtils.decodeMultiAssetData(assetData);
                _.each(decodedAssetData.nestedAssetData, nestedAssetDataElement =>
                    this._deleteLazyStoreBalance(nestedAssetDataElement, userAddress),
                );
                break;
            default:
                break;
        }
    }
    private _deleteLazyStoreProxyAllowance(assetData: string, userAddress: string): void {
github 0xProject / 0x-monorepo / packages / pipeline / src / utils / transformers / asset_proxy_id_types.ts View on Github external
export function convertAssetProxyIdToType(assetProxyId: AssetProxyId): AssetType {
    switch (assetProxyId) {
        case AssetProxyId.ERC20:
            return AssetType.ERC20;
        case AssetProxyId.ERC721:
            return AssetType.ERC721;
        case AssetProxyId.MultiAsset:
            return AssetType.MultiAsset;
        default:
            throw new Error(`${assetProxyId} not a supported assetProxyId`);
    }
}