How to use the @0x/types.AssetProxyId.ERC20 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 / instant / src / redux / reducer.ts View on Github external
const doesSwapQuoteMatchState = (swapQuote: MarketBuySwapQuote, state: State): boolean => {
    const selectedAssetIfExists = state.selectedAsset;
    const selectedAssetUnitAmountIfExists = state.selectedAssetUnitAmount;
    // if no selectedAsset or selectedAssetAmount exists on the current state, return false
    if (selectedAssetIfExists === undefined || selectedAssetUnitAmountIfExists === undefined) {
        return false;
    }
    // if swapQuote's assetData does not match that of the current selected asset, return false
    if (selectedAssetIfExists.assetData !== swapQuote.makerAssetData) {
        return false;
    }
    // if ERC20 and swapQuote's makerAssetFillAmount does not match selectedAssetAmount, return false
    // if ERC721, return true
    const selectedAssetMetaData = selectedAssetIfExists.metaData;
    if (selectedAssetMetaData.assetProxyId === AssetProxyId.ERC20) {
        const selectedAssetAmountBaseUnits = Web3Wrapper.toBaseUnitAmount(
            selectedAssetUnitAmountIfExists,
            selectedAssetMetaData.decimals,
        );
        const doesAssetAmountMatch = selectedAssetAmountBaseUnits.eq(swapQuote.makerAssetFillAmount);

        return doesAssetAmountMatch;
    } else {
        return true;
    }
};
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;
    sraOrder.signature = apiOrder.order.signature;
github 0xProject / 0x-monorepo / packages / instant / src / util / buy_quote_fetcher.ts View on Github external
export const updateBuyQuoteOrFlashErrorAsyncForState = async (state: State, dispatch: Dispatch) => {
    const { selectedAsset, selectedAssetAmount, affiliateInfo } = state;
    const assetBuyer = state.providerState.assetBuyer;

    if (selectedAsset && selectedAssetAmount && selectedAsset.metaData.assetProxyId === AssetProxyId.ERC20) {
        // TODO: maybe dont do in the case of an error showing
        updateBuyQuoteOrFlashErrorAsync(
            assetBuyer,
            selectedAsset as ERC20Asset, // TODO: better way to do this?
            selectedAssetAmount,
            dispatch,
            affiliateInfo,
        );
    }
};
github 0xProject / 0x-monorepo / packages / pipeline / src / parsers / events / exchange_events.ts View on Github external
export function _convertToExchangeCancelEvent(
    eventLog: LogWithDecodedArgs,
): ExchangeCancelEvent {
    const makerAssetData = assetDataUtils.decodeAssetDataOrThrow(eventLog.args.makerAssetData);
    const makerAssetType = makerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721';
    const takerAssetData = assetDataUtils.decodeAssetDataOrThrow(eventLog.args.takerAssetData);
    const takerAssetType = takerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721';
    const exchangeCancelEvent = new ExchangeCancelEvent();
    exchangeCancelEvent.contractAddress = eventLog.address as string;
    exchangeCancelEvent.blockNumber = eventLog.blockNumber as number;
    exchangeCancelEvent.logIndex = eventLog.logIndex as number;
    exchangeCancelEvent.rawData = eventLog.data as string;
    exchangeCancelEvent.transactionHash = eventLog.transactionHash;
    exchangeCancelEvent.makerAddress = eventLog.args.makerAddress;
    exchangeCancelEvent.takerAddress = eventLog.args.takerAddress;
    exchangeCancelEvent.feeRecipientAddress = eventLog.args.feeRecipientAddress;
    exchangeCancelEvent.senderAddress = eventLog.args.senderAddress;
    exchangeCancelEvent.orderHash = eventLog.args.orderHash;
    exchangeCancelEvent.rawMakerAssetData = eventLog.args.makerAssetData;
    exchangeCancelEvent.makerAssetType = makerAssetType;
    exchangeCancelEvent.makerAssetProxyId = makerAssetData.assetProxyId;
    exchangeCancelEvent.makerTokenAddress = makerAssetData.tokenAddress;
    // tslint:disable-next-line:no-unnecessary-type-assertion
github 0xProject / 0x-monorepo / packages / instant / src / components / instant_heading.tsx View on Github external
private _renderAssetHeadingContent(): React.ReactNode {
        const { selectedAsset } = this.props;
        if (selectedAsset === undefined) {
            // TODO: Only the ERC20 flow supports selecting assets.
            return this._renderERC20AssetHeading();
        }
        if (selectedAsset.metaData.assetProxyId === AssetProxyId.ERC20) {
            return this._renderERC20AssetHeading();
        } else if (selectedAsset.metaData.assetProxyId === AssetProxyId.ERC721) {
            return this._renderERC721AssetHeading(selectedAsset as ERC721Asset);
        }
        return null;
    }
    // tslint:disable-next-line:prefer-function-over-method
github 0xProject / 0x-monorepo / packages / instant / src / util / swap_quote_updater.ts View on Github external
dispatchErrors: boolean;
        },
    ): Promise => {
        // get a new swap quote.
        const baseUnitValue =
            asset.metaData.assetProxyId === AssetProxyId.ERC20
                ? Web3Wrapper.toBaseUnitAmount(assetUnitAmount, asset.metaData.decimals)
                : assetUnitAmount;
        if (options.setPending) {
            // mark quote as pending
            dispatch(actions.setQuoteRequestStatePending());
        }
        const wethAssetData = await swapQuoter.getEtherTokenAssetDataOrThrowAsync();
        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) {
github 0xProject / 0x-monorepo / packages / order-utils / src / exchange_transfer_simulator.ts View on Github external
public async transferFromAsync(
        assetData: string,
        from: string,
        to: string,
        amountInBaseUnits: BigNumber,
        tradeSide: TradeSide,
        transferType: TransferType,
    ): Promise {
        const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData);
        switch (assetProxyId) {
            case AssetProxyId.ERC1155:
            case AssetProxyId.ERC20:
            case AssetProxyId.ERC721: {
                // HACK: When simulating an open order (e.g taker is NULL_ADDRESS), we don't want to adjust balances/
                // allowances for the taker. We do however, want to increase the balance of the maker since the maker
                // might be relying on those funds to fill subsequent orders or pay the order's fees.
                if (from === constants.NULL_ADDRESS && tradeSide === TradeSide.Taker) {
                    await this._increaseBalanceAsync(assetData, to, amountInBaseUnits);
                    return;
                }
                const balance = await this._store.getBalanceAsync(assetData, from);
                const proxyAllowance = await this._store.getProxyAllowanceAsync(assetData, from);
                if (proxyAllowance.isLessThan(amountInBaseUnits)) {
                    ExchangeTransferSimulator._throwValidationError(
                        FailureReason.ProxyAllowance,
                        tradeSide,
                        transferType,
                    );
github 0xProject / 0x-monorepo / packages / order-utils / src / asset_data_utils.ts View on Github external
isERC20AssetData(decodedAssetData: SingleAssetData | MultiAssetData): decodedAssetData is ERC20AssetData {
        return decodedAssetData.assetProxyId === AssetProxyId.ERC20;
    },
    /**
github 0xProject / 0x-monorepo / packages / instant / src / util / asset_data.ts View on Github external
bestNameForAsset: (assetData: string | undefined, defaultString: string) => {
        if (_.isUndefined(assetData)) {
            return defaultString;
        }
        const metaData = assetMetaData[assetData];
        if (_.isUndefined(metaData)) {
            return defaultString;
        }
        if (metaData.assetProxyId === AssetProxyId.ERC20) {
            return metaData.symbol.toUpperCase();
        }
        return defaultString;
    },
};
github 0xProject / 0x-monorepo / packages / pipeline / src / parsers / events / exchange_events.ts View on Github external
export function _convertToExchangeCancelEvent(
    eventLog: LogWithDecodedArgs,
): ExchangeCancelEvent {
    const makerAssetData = assetDataUtils.decodeAssetDataOrThrow(eventLog.args.makerAssetData);
    const makerAssetType = makerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721';
    const takerAssetData = assetDataUtils.decodeAssetDataOrThrow(eventLog.args.takerAssetData);
    const takerAssetType = takerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721';
    const exchangeCancelEvent = new ExchangeCancelEvent();
    exchangeCancelEvent.contractAddress = eventLog.address as string;
    exchangeCancelEvent.blockNumber = eventLog.blockNumber as number;
    exchangeCancelEvent.logIndex = eventLog.logIndex as number;
    exchangeCancelEvent.rawData = eventLog.data as string;
    exchangeCancelEvent.transactionHash = eventLog.transactionHash;
    exchangeCancelEvent.makerAddress = eventLog.args.makerAddress;
    exchangeCancelEvent.takerAddress = eventLog.args.takerAddress;
    exchangeCancelEvent.feeRecipientAddress = eventLog.args.feeRecipientAddress;
    exchangeCancelEvent.senderAddress = eventLog.args.senderAddress;
    exchangeCancelEvent.orderHash = eventLog.args.orderHash;
    exchangeCancelEvent.rawMakerAssetData = eventLog.args.makerAssetData;
    exchangeCancelEvent.makerAssetType = makerAssetType;
    exchangeCancelEvent.makerAssetProxyId = makerAssetData.assetProxyId;