Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
};
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;
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,
);
}
};
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
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
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) {
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,
);
isERC20AssetData(decodedAssetData: SingleAssetData | MultiAssetData): decodedAssetData is ERC20AssetData {
return decodedAssetData.assetProxyId === AssetProxyId.ERC20;
},
/**
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;
},
};
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;