Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
export const assetDataForERC721TokenAddress = (tokenAddress: string, tokenId: string | number): string => {
assert.isETHAddressHex('tokenAddress', tokenAddress);
return assetDataEncoder.ERC721Token(tokenAddress, new BigNumber(tokenId)).getABIEncodedTransactionData();
};
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));
};
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 => {
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));
};
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 => {
): 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));
};
getSwapQuoteConsumer: (supportedProvider: SupportedProvider, network: Network): SwapQuoteConsumer => {
const swapQuoteConsumerOptions: Partial = {
chainId: network,
};
return new SwapQuoteConsumer(supportedProvider, swapQuoteConsumerOptions);
},
};
? 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;
},
};
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`;