How to use the @0x/types.MarketOperation.Buy 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 / market_utils.ts View on Github external
function findOrdersThatCoverAssetFillAmount(
    orders: T[],
    assetFillAmount: BigNumber,
    operation: MarketOperation,
    opts?: FindOrdersThatCoverTakerAssetFillAmountOpts | FindOrdersThatCoverMakerAssetFillAmountOpts,
): OrdersAndRemainingTakerFillAmount | OrdersAndRemainingMakerFillAmount {
    const variablePrefix = operation === MarketOperation.Buy ? 'Maker' : 'Taker';
    assert.doesConformToSchema('orders', orders, schemas.ordersSchema);
    assert.isValidBaseUnitAmount('assetFillAmount', assetFillAmount);
    // try to get remainingFillableTakerAssetAmounts from opts, if it's not there, use takerAssetAmount values from orders
    const remainingFillableAssetAmounts = _.get(
        opts,
        `remainingFillable${variablePrefix}AssetAmounts`,
        _.map(orders, order => (operation === MarketOperation.Buy ? order.makerAssetAmount : order.takerAssetAmount)),
    ) as BigNumber[];
    _.forEach(remainingFillableAssetAmounts, (amount, index) =>
        assert.isValidBaseUnitAmount(`remainingFillable${variablePrefix}AssetAmount[${index}]`, amount),
    );
    assert.assert(
        orders.length === remainingFillableAssetAmounts.length,
        `Expected orders.length to equal opts.remainingFillable${variablePrefix}AssetAmounts.length`,
    );
    // try to get slippageBufferAmount from opts, if it's not there, default to 0
github 0xProject / 0x-monorepo / packages / asset-swapper / src / quote_consumers / exchange_swap_quote_consumer.ts View on Github external
public async getSmartContractParamsOrThrowAsync(
        quote: SwapQuote,
        _opts: Partial,
    ): Promise> {
        assert.isValidSwapQuote('quote', quote);

        const { orders } = quote;

        const signatures = _.map(orders, o => o.signature);

        const optimizedOrders = swapQuoteConsumerUtils.optimizeOrdersForMarketExchangeOperation(orders, quote.type);

        let params: ExchangeSmartContractParams;
        let methodName: string;

        if (quote.type === MarketOperation.Buy) {
            const { makerAssetFillAmount } = quote;

            params = {
                orders: optimizedOrders,
                signatures,
                makerAssetFillAmount,
                type: MarketOperation.Buy,
            };

            methodName = 'marketBuyOrders';
        } else {
            const { takerAssetFillAmount } = quote;

            params = {
                orders: optimizedOrders,
                signatures,
github 0xProject / 0x-monorepo / packages / order-utils / src / market_utils.ts View on Github external
        _.map(orders, order => (operation === MarketOperation.Buy ? order.makerAssetAmount : order.takerAssetAmount)),
    ) as BigNumber[];
github 0xProject / 0x-monorepo / packages / asset-swapper / src / quote_consumers / exchange_swap_quote_consumer.ts View on Github external
assert.isETHAddressHex('takerAddress', takerAddress);
        }
        if (gasLimit !== undefined) {
            assert.isNumber('gasLimit', gasLimit);
        }
        if (gasPrice !== undefined) {
            assert.isBigNumber('gasPrice', gasPrice);
        }

        const { orders } = quote;

        const finalTakerAddress = await swapQuoteConsumerUtils.getTakerAddressOrThrowAsync(this.provider, opts);

        try {
            let txHash: string;
            if (quote.type === MarketOperation.Buy) {
                const { makerAssetFillAmount } = quote;
                txHash = await this._contractWrappers.exchange.marketBuyOrdersNoThrow.sendTransactionAsync(
                    orders,
                    makerAssetFillAmount,
                    orders.map(o => o.signature),
                    {
                        from: finalTakerAddress,
                        gas: gasLimit,
                        gasPrice,
                    },
                );
            } else {
                const { takerAssetFillAmount } = quote;
                txHash = await this._contractWrappers.exchange.marketSellOrdersNoThrow.sendTransactionAsync(
                    orders,
                    takerAssetFillAmount,
github 0xProject / 0x-monorepo / packages / order-utils / src / market_utils.ts View on Github external
findOrdersThatCoverMakerAssetFillAmount(
        orders: T[],
        makerAssetFillAmount: BigNumber,
        opts?: FindOrdersThatCoverMakerAssetFillAmountOpts,
    ): OrdersAndRemainingMakerFillAmount {
        return findOrdersThatCoverAssetFillAmount(
            orders,
            makerAssetFillAmount,
            MarketOperation.Buy,
            opts,
        ) as OrdersAndRemainingMakerFillAmount;
    },
    /**
github 0xProject / 0x-monorepo / packages / asset-swapper / src / quote_consumers / exchange_swap_quote_consumer.ts View on Github external
public async getCalldataOrThrowAsync(
        quote: SwapQuote,
        opts: Partial,
    ): Promise {
        assert.isValidSwapQuote('quote', quote);

        const { toAddress, methodAbi, ethAmount, params } = await this.getSmartContractParamsOrThrowAsync(quote, opts);

        const abiEncoder = new AbiEncoder.Method(methodAbi);

        const { orders, signatures } = params;
        let args: any[];
        if (params.type === MarketOperation.Buy) {
            const { makerAssetFillAmount } = params;
            args = [orders, makerAssetFillAmount, signatures];
        } else {
            const { takerAssetFillAmount } = params;
            args = [orders, takerAssetFillAmount, signatures];
        }
        const calldataHexString = abiEncoder.encode(args, { shouldOptimize: true });
        return {
            calldataHexString,
            methodAbi,
            toAddress,
            ethAmount,
        };
    }
github 0xProject / 0x-monorepo / packages / asset-swapper / src / quote_consumers / exchange_swap_quote_consumer.ts View on Github external
const signatures = _.map(orders, o => o.signature);

        const optimizedOrders = swapQuoteConsumerUtils.optimizeOrdersForMarketExchangeOperation(orders, quote.type);

        let params: ExchangeSmartContractParams;
        let methodName: string;

        if (quote.type === MarketOperation.Buy) {
            const { makerAssetFillAmount } = quote;

            params = {
                orders: optimizedOrders,
                signatures,
                makerAssetFillAmount,
                type: MarketOperation.Buy,
            };

            methodName = 'marketBuyOrders';
        } else {
            const { takerAssetFillAmount } = quote;

            params = {
                orders: optimizedOrders,
                signatures,
                takerAssetFillAmount,
                type: MarketOperation.Sell,
            };

            methodName = 'marketSellOrders';
        }