How to use @0x/asset-swapper - 10 common examples

To help you get started, we’ve selected a few @0x/asset-swapper 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 / instant / src / util / swap_quote_updater.ts View on Github external
let newSwapQuote: MarketBuySwapQuote | undefined;
        const slippagePercentage =
            asset.metaData.assetProxyId === AssetProxyId.ERC20
                ? ERC20_SWAP_QUOTE_SLIPPAGE_PERCENTAGE
                : ERC721_SWAP_QUOTE_SLIPPAGE_PERCENTAGE;
        try {
            const gasInfo = await gasPriceEstimator.getGasInfoAsync();
            newSwapQuote = await swapQuoter.getMarketBuySwapQuoteForAssetDataAsync(
                asset.assetData,
                wethAssetData,
                baseUnitValue,
                {
                    slippagePercentage,
                    gasPrice: gasInfo.gasPriceInWei,
                    // Only use native orders
                    excludedSources: [ERC20BridgeSource.Eth2Dai, ERC20BridgeSource.Kyber, ERC20BridgeSource.Uniswap],
                },
            );
        } catch (error) {
            const errorMessage = assetUtils.swapQuoterErrorMessage(asset, error);

            errorReporter.report(error);
            analytics.trackQuoteError(error.message ? error.message : 'other', baseUnitValue, fetchOrigin);

            if (options.dispatchErrors) {
                dispatch(actions.setQuoteRequestStateFailure());
                errorFlasher.flashNewErrorMessage(dispatch, errorMessage || 'Error fetching price, please try again');
            }
            return;
        }
        // We have a successful new swap quote
        errorFlasher.clearError(dispatch);
github 0xProject / 0x-monorepo / packages / instant / src / index.umd.ts View on Github external
export const assetDataForERC721TokenAddress = (tokenAddress: string, tokenId: string | number): string => {
    assert.isETHAddressHex('tokenAddress', tokenAddress);
    return assetDataEncoder.ERC721Token(tokenAddress, new BigNumber(tokenId)).getABIEncodedTransactionData();
};
github 0xProject / 0x-monorepo / packages / instant / src / index.umd.ts View on Github external
let provider = supportedProvider;
    if (provider !== undefined) {
        provider = providerUtils.standardizeOrThrow(provider);
    }

    const bestProvider: ZeroExProvider = provider || providerFactory.getFallbackNoSigningProvider(chainId);

    const swapQuoterOptions = { chainId };

    const swapQuoter = _.isString(orderSource)
        ? SwapQuoter.getSwapQuoterForStandardRelayerAPIUrl(bestProvider, orderSource, swapQuoterOptions)
        : SwapQuoter.getSwapQuoterForProvidedOrders(bestProvider, orderSource, swapQuoterOptions);

    const wethAssetData = await swapQuoter.getEtherTokenAssetDataOrThrowAsync();
    const liquidity = await swapQuoter.getLiquidityForMakerTakerAssetDataPairAsync(wethAssetData, takerAssetData);
    return liquidity.makerAssetAvailableInBaseUnits.gt(new BigNumber(0));
};
github 0xProject / 0x-monorepo / packages / instant / src / util / asset_swapper_factory.ts View on Github external
getSwapQuoter: (supportedProvider: SupportedProvider, orderSource: OrderSource, network: Network): SwapQuoter => {
        const swapQuoterOpts: Partial = {
            chainId: network,
        };
        const swapQuoter = _.isString(orderSource)
            ? SwapQuoter.getSwapQuoterForStandardRelayerAPIUrl(supportedProvider, orderSource, swapQuoterOpts)
            : SwapQuoter.getSwapQuoterForProvidedOrders(supportedProvider, orderSource, swapQuoterOpts);
        return swapQuoter;
    },
    getSwapQuoteConsumer: (supportedProvider: SupportedProvider, network: Network): SwapQuoteConsumer => {
github 0xProject / 0x-monorepo / packages / instant / src / index.umd.ts View on Github external
assert.isHexString('takerAssetData', takerAssetData);
    assert.isValidOrderSource('orderSource', orderSource);
    assert.isNumber('chainId', chainId);

    let provider = supportedProvider;
    if (provider !== undefined) {
        provider = providerUtils.standardizeOrThrow(provider);
    }

    const bestProvider: ZeroExProvider = provider || providerFactory.getFallbackNoSigningProvider(chainId);

    const swapQuoterOptions = { chainId };

    const swapQuoter = _.isString(orderSource)
        ? SwapQuoter.getSwapQuoterForStandardRelayerAPIUrl(bestProvider, orderSource, swapQuoterOptions)
        : SwapQuoter.getSwapQuoterForProvidedOrders(bestProvider, orderSource, swapQuoterOptions);

    const wethAssetData = await swapQuoter.getEtherTokenAssetDataOrThrowAsync();
    const liquidity = await swapQuoter.getLiquidityForMakerTakerAssetDataPairAsync(wethAssetData, takerAssetData);
    return liquidity.makerAssetAvailableInBaseUnits.gt(new BigNumber(0));
};
github 0xProject / 0x-monorepo / packages / instant / src / util / asset_swapper_factory.ts View on Github external
getSwapQuoter: (supportedProvider: SupportedProvider, orderSource: OrderSource, network: Network): SwapQuoter => {
        const swapQuoterOpts: Partial = {
            chainId: network,
        };
        const swapQuoter = _.isString(orderSource)
            ? SwapQuoter.getSwapQuoterForStandardRelayerAPIUrl(supportedProvider, orderSource, swapQuoterOpts)
            : SwapQuoter.getSwapQuoterForProvidedOrders(supportedProvider, orderSource, swapQuoterOpts);
        return swapQuoter;
    },
    getSwapQuoteConsumer: (supportedProvider: SupportedProvider, network: Network): SwapQuoteConsumer => {
github 0xProject / 0x-monorepo / packages / instant / src / index.umd.ts View on Github external
): Promise => {
    assert.isHexString('takerAssetData', takerAssetData);
    assert.isValidOrderSource('orderSource', orderSource);
    assert.isNumber('chainId', chainId);

    let provider = supportedProvider;
    if (provider !== undefined) {
        provider = providerUtils.standardizeOrThrow(provider);
    }

    const bestProvider: ZeroExProvider = provider || providerFactory.getFallbackNoSigningProvider(chainId);

    const swapQuoterOptions = { chainId };

    const swapQuoter = _.isString(orderSource)
        ? SwapQuoter.getSwapQuoterForStandardRelayerAPIUrl(bestProvider, orderSource, swapQuoterOptions)
        : SwapQuoter.getSwapQuoterForProvidedOrders(bestProvider, orderSource, swapQuoterOptions);

    const wethAssetData = await swapQuoter.getEtherTokenAssetDataOrThrowAsync();
    const liquidity = await swapQuoter.getLiquidityForMakerTakerAssetDataPairAsync(wethAssetData, takerAssetData);
    return liquidity.makerAssetAvailableInBaseUnits.gt(new BigNumber(0));
};
github 0xProject / 0x-monorepo / packages / instant / src / util / asset_swapper_factory.ts View on Github external
getSwapQuoteConsumer: (supportedProvider: SupportedProvider, network: Network): SwapQuoteConsumer => {
        const swapQuoteConsumerOptions: Partial = {
            chainId: network,
        };
        return new SwapQuoteConsumer(supportedProvider, swapQuoteConsumerOptions);
    },
};
github 0xProject / 0x-monorepo / packages / instant / src / util / asset.ts View on Github external
? Web3Wrapper.toUnitAmount(error.amountAvailableToFill, asset.metaData.decimals)
                        : error.amountAvailableToFill;
                const roundedUnitAmountAvailableToFill = unitAmountAvailableToFill.decimalPlaces(
                    2,
                    BigNumber.ROUND_DOWN,
                );

                if (roundedUnitAmountAvailableToFill.isGreaterThan(BIG_NUMBER_ZERO)) {
                    return `There are only ${roundedUnitAmountAvailableToFill} ${assetName} available to buy`;
                }
            }

            return `Not enough ${assetName} available`;
        } else if (
            error.message === SwapQuoterError.StandardRelayerApiError ||
            error.message.startsWith(SwapQuoterError.AssetUnavailable)
        ) {
            const assetName = assetUtils.bestNameForAsset(asset, 'This asset');
            return `${assetName} is currently unavailable`;
        }

        return undefined;
    },
};
github 0xProject / 0x-monorepo / packages / instant / src / util / asset.ts View on Github external
swapQuoterErrorMessage: (asset: Asset, error: Error): string | undefined => {
        if (error.message === SwapQuoterError.InsufficientAssetLiquidity) {
            const assetName = assetUtils.bestNameForAsset(asset, 'of this asset');
            if (
                error instanceof InsufficientAssetLiquidityError &&
                error.amountAvailableToFill.isGreaterThan(BIG_NUMBER_ZERO)
            ) {
                const unitAmountAvailableToFill =
                    asset.metaData.assetProxyId === AssetProxyId.ERC20
                        ? Web3Wrapper.toUnitAmount(error.amountAvailableToFill, asset.metaData.decimals)
                        : error.amountAvailableToFill;
                const roundedUnitAmountAvailableToFill = unitAmountAvailableToFill.decimalPlaces(
                    2,
                    BigNumber.ROUND_DOWN,
                );

                if (roundedUnitAmountAvailableToFill.isGreaterThan(BIG_NUMBER_ZERO)) {
                    return `There are only ${roundedUnitAmountAvailableToFill} ${assetName} available to buy`;