Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
toBalances.nonFungible.sort();
} else {
// Transfer a fungible.
const _tokenId = tokenId.toString();
_.update(fromBalances.fungible, [_tokenId], balance => balance.minus(tokenAmount));
_.update(toBalances.fungible, [_tokenId], balance =>
(balance || constants.ZERO_AMOUNT).plus(tokenAmount),
);
}
}
}
_.set(this.balances.erc1155, [fromAddress, tokenAddress], fromBalances);
_.set(this.balances.erc1155, [toAddress, tokenAddress], toBalances);
break;
}
case AssetProxyId.MultiAsset: {
// tslint:disable-next-line:no-unused-variable
const [_proxyId, amounts, nestedAssetData] = await this._devUtils
.decodeMultiAssetData(assetData)
.callAsync();
for (const [i, amt] of amounts.entries()) {
const nestedAmount = amount.times(amt);
await this.transferAssetAsync(fromAddress, toAddress, nestedAmount, nestedAssetData[i]);
}
break;
}
case AssetProxyId.StaticCall:
// Do nothing
break;
default:
throw new Error(`Unhandled asset proxy ID: ${assetProxyId}`);
}
async function verifyExchangeV3ConfigsAsync(): Promise {
const exchangeOwner = await exchange.owner().callAsync();
warnIfMismatch(exchangeOwner, governor.address, 'Unexpected Exchange owner');
const registeredERC20Proxy = await exchange.getAssetProxy(AssetProxyId.ERC20).callAsync();
warnIfMismatch(registeredERC20Proxy, erc20Proxy.address, 'Unexpected ERC20Proxy registered in Exchange');
const registeredERC721Proxy = await exchange.getAssetProxy(AssetProxyId.ERC721).callAsync();
warnIfMismatch(registeredERC721Proxy, erc721Proxy.address, 'Unexpected ERC721Proxy registered in Exchange');
const registeredERC1155Proxy = await exchange.getAssetProxy(AssetProxyId.ERC1155).callAsync();
warnIfMismatch(registeredERC1155Proxy, erc1155Proxy.address, 'Unexpected ERC1155Proxy registered in Exchange');
const registeredMultiAssetProxy = await exchange.getAssetProxy(AssetProxyId.MultiAsset).callAsync();
warnIfMismatch(
registeredMultiAssetProxy,
multiAssetProxy.address,
'Unexpected MultiAssetProxy registered in Exchange',
);
const registeredStaticCallProxy = await exchange.getAssetProxy(AssetProxyId.StaticCall).callAsync();
warnIfMismatch(
registeredStaticCallProxy,
addresses.staticCallProxy,
'Unexpected StaticCallProxy registered in Exchange',
);
const registeredERC20BridgeProxy = await exchange.getAssetProxy(AssetProxyId.ERC20Bridge).callAsync();
warnIfMismatch(
registeredERC20BridgeProxy,
warnIfMismatch(exchangeOwner, governor.address, 'Unexpected ExchangeV2 owner');
const registeredERC20Proxy = await exchangeV2.getAssetProxy(AssetProxyId.ERC20).callAsync();
warnIfMismatch(registeredERC20Proxy, erc20Proxy.address, 'Unexpected ERC20Proxy registered in ExchangeV2');
const registeredERC721Proxy = await exchangeV2.getAssetProxy(AssetProxyId.ERC721).callAsync();
warnIfMismatch(registeredERC721Proxy, erc721Proxy.address, 'Unexpected ERC721Proxy registered in ExchangeV2');
const registeredERC1155Proxy = await exchangeV2.getAssetProxy(AssetProxyId.ERC1155).callAsync();
warnIfMismatch(
registeredERC1155Proxy,
erc1155Proxy.address,
'Unexpected ERC1155Proxy registered in ExchangeV2',
);
const registeredMultiAssetProxy = await exchangeV2.getAssetProxy(AssetProxyId.MultiAsset).callAsync();
warnIfMismatch(
registeredMultiAssetProxy,
multiAssetProxy.address,
'Unexpected MultiAssetProxy registered in ExchangeV2',
);
const registeredStaticCallProxy = await exchangeV2.getAssetProxy(AssetProxyId.StaticCall).callAsync();
warnIfMismatch(
registeredStaticCallProxy,
addresses.staticCallProxy,
'Unexpected StaticCallProxy registered in ExchangeV2',
);
}
tokenIds,
tokenValues,
callbackData,
] = assetDataEncoder.getABIDecodedTransactionData<[string, BigNumber[], BigNumber[], string]>(
'ERC1155Assets',
assetData,
);
return {
assetProxyId,
tokenAddress,
tokenIds,
tokenValues,
callbackData,
};
}
case AssetProxyId.MultiAsset: {
const [amounts, nestedAssetData] = assetDataEncoder.getABIDecodedTransactionData<
[BigNumber[], string[]]
>('MultiAsset', assetData);
const multiAssetData: MultiAssetData = {
assetProxyId,
amounts,
nestedAssetData,
};
return multiAssetData;
}
case AssetProxyId.StaticCall:
const [callTarget, staticCallData, callResultHash] = assetDataEncoder.getABIDecodedTransactionData<
[string, string, string]
>('StaticCall', assetData);
return {
assertIsMultiAssetData(assetData: string): void {
if (assetData.length < constants.MULTI_ASSET_DATA_MIN_CHAR_LENGTH_WITH_PREFIX) {
throw new Error(
`Could not decode MultiAsset assetData. Expected length of encoded data to be at least ${
constants.MULTI_ASSET_DATA_MIN_CHAR_LENGTH_WITH_PREFIX
}. Got ${assetData.length}`,
);
}
const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData);
if (assetProxyId !== AssetProxyId.MultiAsset) {
throw new Error(
`Could not decode MultiAsset assetData. Expected assetProxyId to be MultiAsset (${
AssetProxyId.MultiAsset
}), but got ${assetProxyId}`,
);
}
},
/**
(nestedAssetDataElement, index) => {
const decodedNestedAssetDataElement = assetDataUtils.decodeAssetDataOrThrow(nestedAssetDataElement);
if (decodedNestedAssetDataElement.assetProxyId === AssetProxyId.MultiAsset) {
const recursivelyDecodedAssetData = assetDataUtils.decodeMultiAssetDataRecursively(
nestedAssetDataElement,
);
amounts.push(
_.map(recursivelyDecodedAssetData.amounts, amountElement =>
amountElement.times(decodedAssetData.amounts[index]),
),
);
return recursivelyDecodedAssetData.nestedAssetData;
} else {
amounts.push(decodedAssetData.amounts[index]);
return decodedNestedAssetDataElement as SingleAssetData;
}
},
);
const decodedNestedAssetData = decodedAssetData.nestedAssetData.map((nestedAssetDataElement, index) => {
const decodedNestedAssetDataElement = assetDataUtils.decodeAssetDataOrThrow(nestedAssetDataElement);
if (decodedNestedAssetDataElement.assetProxyId === AssetProxyId.MultiAsset) {
const recursivelyDecodedAssetData = assetDataUtils.decodeMultiAssetDataRecursively(
nestedAssetDataElement,
);
amounts.push(
recursivelyDecodedAssetData.amounts.map(amountElement =>
amountElement.times(decodedAssetData.amounts[index]),
),
);
return recursivelyDecodedAssetData.nestedAssetData;
} else {
amounts.push(decodedAssetData.amounts[index]);
return decodedNestedAssetDataElement;
}
});
const flattenedAmounts = _.flattenDeep(amounts);
private _deleteLazyStoreBalance(assetData: string, userAddress: string): void {
const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData);
switch (assetProxyId) {
case AssetProxyId.ERC20:
case AssetProxyId.ERC721:
this._balanceAndProxyAllowanceLazyStore.deleteBalance(assetData, userAddress);
break;
case AssetProxyId.MultiAsset:
const decodedAssetData = assetDataUtils.decodeMultiAssetData(assetData);
_.each(decodedAssetData.nestedAssetData, nestedAssetDataElement =>
this._deleteLazyStoreBalance(nestedAssetDataElement, userAddress),
);
break;
default:
break;
}
}
private _deleteLazyStoreProxyAllowance(assetData: string, userAddress: string): void {
export function convertAssetProxyIdToType(assetProxyId: AssetProxyId): AssetType {
switch (assetProxyId) {
case AssetProxyId.ERC20:
return AssetType.ERC20;
case AssetProxyId.ERC721:
return AssetType.ERC721;
case AssetProxyId.MultiAsset:
return AssetType.MultiAsset;
default:
throw new Error(`${assetProxyId} not a supported assetProxyId`);
}
}