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,
);
): Promise {
if (fromAddress === toAddress) {
return;
}
const assetProxyId = await this._devUtils.decodeAssetProxyId(assetData).callAsync();
switch (assetProxyId) {
case AssetProxyId.ERC20: {
// tslint:disable-next-line:no-unused-variable
const [_proxyId, tokenAddress] = await this._devUtils.decodeERC20AssetData(assetData).callAsync();
_.update(this.balances.erc20, [fromAddress, tokenAddress], balance => balance.minus(amount));
_.update(this.balances.erc20, [toAddress, tokenAddress], balance =>
(balance || constants.ZERO_AMOUNT).plus(amount),
);
break;
}
case AssetProxyId.ERC721: {
// tslint:disable-next-line:no-unused-variable
const [_proxyId, tokenAddress, tokenId] = await this._devUtils
.decodeERC721AssetData(assetData)
.callAsync();
const fromTokens = _.get(this.balances.erc721, [fromAddress, tokenAddress], []);
const toTokens = _.get(this.balances.erc721, [toAddress, tokenAddress], []);
if (amount.gte(1)) {
const tokenIndex = _.findIndex(fromTokens as BigNumber[], t => t.eq(tokenId));
if (tokenIndex !== -1) {
fromTokens.splice(tokenIndex, 1);
toTokens.push(tokenId);
toTokens.sort();
}
}
_.set(this.balances.erc721, [fromAddress, tokenAddress], fromTokens);
_.set(this.balances.erc721, [toAddress, tokenAddress], toTokens);
if (newState && newState.zeroExInstantShowing) {
// We have returned to a history state that expects instant to be rendered.
if (!isInstantRendered()) {
removeInstant = renderInstant(coercedConfig, selector);
}
} else {
// History has changed to a different state.
if (isInstantRendered()) {
removeInstant();
}
}
};
window.onpopstate = onPopStateHandler;
};
export const ERC721_PROXY_ID = AssetProxyId.ERC721;
export const ERC20_PROXY_ID = AssetProxyId.ERC20;
export const assetDataForERC20TokenAddress = (tokenAddress: string): string => {
assert.isETHAddressHex('tokenAddress', tokenAddress);
return assetDataEncoder.ERC20Token(tokenAddress).getABIEncodedTransactionData();
};
export const assetDataForERC721TokenAddress = (tokenAddress: string, tokenId: string | number): string => {
assert.isETHAddressHex('tokenAddress', tokenAddress);
return assetDataEncoder.ERC721Token(tokenAddress, new BigNumber(tokenId)).getABIEncodedTransactionData();
};
export const hasMetaDataForAssetData = (assetData: string): boolean => {
assert.isHexString('assetData', assetData);
return assetMetaDataMap[assetData] !== undefined;
tradeSide,
transferType,
);
}
if (balance.isLessThan(amountInBaseUnits)) {
ExchangeTransferSimulator._throwValidationError(FailureReason.Balance, tradeSide, transferType);
}
if (assetProxyId !== AssetProxyId.ERC1155) {
// No need to decrease allowance for ERC115 because it's all or nothing.
await this._decreaseProxyAllowanceAsync(assetData, from, amountInBaseUnits);
}
await this._decreaseBalanceAsync(assetData, from, amountInBaseUnits);
await this._increaseBalanceAsync(assetData, to, amountInBaseUnits);
break;
}
case AssetProxyId.MultiAsset: {
const decodedAssetData = assetDataUtils.decodeMultiAssetData(assetData);
await this._decreaseBalanceAsync(assetData, from, amountInBaseUnits);
await this._increaseBalanceAsync(assetData, to, amountInBaseUnits);
for (const [index, nestedAssetDataElement] of decodedAssetData.nestedAssetData.entries()) {
const amountsElement = decodedAssetData.amounts[index];
const totalAmount = amountInBaseUnits.times(amountsElement);
await this.transferFromAsync(
nestedAssetDataElement,
from,
to,
totalAmount,
tradeSide,
transferType,
);
}
break;