Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function signOrder(
provider: Provider,
zeroExOrder: Order
): Promise {
const orderHash = orderHashUtils.getOrderHashHex({
...zeroExOrder,
expirationTimeSeconds: new BigNumber(zeroExOrder.expirationTimeSeconds),
makerFee: new BigNumber(zeroExOrder.makerFee),
makerAssetAmount: new BigNumber(zeroExOrder.makerAssetAmount),
salt: new BigNumber(zeroExOrder.salt),
takerFee: new BigNumber(zeroExOrder.takerFee),
takerAssetAmount: new BigNumber(zeroExOrder.takerAssetAmount)
});
const signature = await signatureUtils.ecSignHashAsync(
provider,
orderHash,
zeroExOrder.makerAddress
);
// Append signature to order
const signedOrder = {
private async _validateFillOrderFireAndForgetAsync(orderJSON: string): Promise {
let orderJSONErrMsg = '';
let parsedOrder: PortalOrder;
let orderHash: string;
try {
const order = orderParser.parseJsonString(orderJSON);
const validationResult = validator.validate(order, portalOrderSchema);
if (validationResult.errors.length > 0) {
orderJSONErrMsg = 'Submitted order JSON is not a valid order';
logUtils.log(`Unexpected order JSON validation error: ${validationResult.errors.join(', ')}`);
return;
}
parsedOrder = order;
const signedOrder = parsedOrder.signedOrder;
orderHash = orderHashUtils.getOrderHashHex(signedOrder);
const exchangeContractAddr = this.props.blockchain.getExchangeContractAddressIfExists();
const signature = signedOrder.signature;
const isSignatureValid = await this.props.blockchain.isValidSignatureAsync(
orderHash,
signature,
signedOrder.makerAddress,
);
if (exchangeContractAddr !== signedOrder.exchangeAddress) {
orderJSONErrMsg = 'This order was made on another network or using a deprecated Exchange contract';
parsedOrder = undefined;
} else if (!isSignatureValid) {
orderJSONErrMsg = 'Order signature is invalid';
parsedOrder = undefined;
} else {
// Update user supplied order cache so that if they navigate away from fill view
// e.g to set a token allowance, when they come back, the fill order persists
const balanceAllowanceStore = new BalanceAndProxyAllowanceLazyStore(balanceAllowanceFetcher);
const exchangeTradeSimulator = new ExchangeTransferSimulator(balanceAllowanceStore);
const filledCancelledFetcher = new OrderFilledCancelledFetcher(this, BlockParamLiteral.Latest);
let fillableTakerAssetAmount;
const shouldValidateRemainingOrderAmountIsFillable =
opts.validateRemainingOrderAmountIsFillable === undefined
? true
: opts.validateRemainingOrderAmountIsFillable;
if (opts.expectedFillTakerTokenAmount) {
// If the caller has specified a taker fill amount, we use this for all validation
fillableTakerAssetAmount = opts.expectedFillTakerTokenAmount;
} else if (shouldValidateRemainingOrderAmountIsFillable) {
// Default behaviour is to validate the amount left on the order.
const filledTakerTokenAmount = await this.getFilledTakerAssetAmountAsync(
orderHashUtils.getOrderHashHex(signedOrder),
);
fillableTakerAssetAmount = signedOrder.takerAssetAmount.minus(filledTakerTokenAmount);
} else {
const orderStateUtils = new OrderStateUtils(balanceAllowanceStore, filledCancelledFetcher);
// Calculate the taker amount fillable given the maker balance and allowance
const orderRelevantState = await orderStateUtils.getOpenOrderRelevantStateAsync(signedOrder);
fillableTakerAssetAmount = orderRelevantState.remainingFillableTakerAssetAmount;
}
const orderValidationUtils = new OrderValidationUtils(filledCancelledFetcher, this._web3Wrapper.getProvider());
await orderValidationUtils.validateOrderFillableOrThrowAsync(
exchangeTradeSimulator,
signedOrder,
this.getZRXAssetData(),
fillableTakerAssetAmount,
);
private _removeFromMakerDependentOrderhashes(signedOrder: SignedOrder): void {
const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
this._orderHashesByMakerAddress[signedOrder.makerAddress].delete(orderHash);
if (_.isEmpty(this._orderHashesByMakerAddress[signedOrder.makerAddress])) {
delete this._orderHashesByMakerAddress[signedOrder.makerAddress];
}
}
}
private _addToERC721DependentOrderHashes(
signedOrder: SignedOrder,
erc721TokenAddress: string,
tokenId: BigNumber,
): void {
const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
if (_.isUndefined(this._orderHashesByERC721AddressByTokenIdByMakerAddress[signedOrder.makerAddress])) {
this._orderHashesByERC721AddressByTokenIdByMakerAddress[signedOrder.makerAddress] = {};
}
if (
_.isUndefined(
this._orderHashesByERC721AddressByTokenIdByMakerAddress[signedOrder.makerAddress][erc721TokenAddress],
)
) {
this._orderHashesByERC721AddressByTokenIdByMakerAddress[signedOrder.makerAddress][erc721TokenAddress] = {};
}
if (
_.isUndefined(
this._orderHashesByERC721AddressByTokenIdByMakerAddress[signedOrder.makerAddress][erc721TokenAddress][
tokenId.toString()
private _addToMakerDependentOrderHashes(signedOrder: SignedOrder): void {
const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
if (_.isUndefined(this._orderHashesByMakerAddress[signedOrder.makerAddress])) {
this._orderHashesByMakerAddress[signedOrder.makerAddress] = new Set();
}
this._orderHashesByMakerAddress[signedOrder.makerAddress].add(orderHash);
}
private _removeFromERC20DependentOrderhashes(signedOrder: SignedOrder, erc20TokenAddress: string): void {
private _addToERC20DependentOrderHashes(signedOrder: SignedOrder, erc20TokenAddress: string): void {
const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
if (_.isUndefined(this._orderHashesByERC20ByMakerAddress[signedOrder.makerAddress])) {
this._orderHashesByERC20ByMakerAddress[signedOrder.makerAddress] = {};
}
if (_.isUndefined(this._orderHashesByERC20ByMakerAddress[signedOrder.makerAddress][erc20TokenAddress])) {
this._orderHashesByERC20ByMakerAddress[signedOrder.makerAddress][erc20TokenAddress] = new Set();
}
this._orderHashesByERC20ByMakerAddress[signedOrder.makerAddress][erc20TokenAddress].add(orderHash);
}
private _addToERC721DependentOrderHashes(
const order: Order = {
senderAddress: constants.NULL_ADDRESS,
exchangeAddress,
expirationTimeSeconds: hashData.orderExpiryTimestamp,
feeRecipientAddress: hashData.feeRecipientAddress,
makerAddress: _.isEmpty(hashData.orderMakerAddress) ? constants.NULL_ADDRESS : hashData.orderMakerAddress,
makerFee: hashData.makerFee,
makerAssetData,
makerAssetAmount: hashData.depositAmount,
salt: hashData.orderSalt,
takerAddress: hashData.orderTakerAddress,
takerFee: hashData.takerFee,
takerAssetData,
takerAssetAmount: hashData.receiveAmount,
};
const orderHash = orderHashUtils.getOrderHashHex(order);
return orderHash;
}
}
private _removeFromERC721DependentOrderhashes(
signedOrder: SignedOrder,
erc721TokenAddress: string,
tokenId: BigNumber,
): void {
const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
this._orderHashesByERC721AddressByTokenIdByMakerAddress[signedOrder.makerAddress][erc721TokenAddress][
tokenId.toString()
].delete(orderHash);
if (
_.isEmpty(
this._orderHashesByERC721AddressByTokenIdByMakerAddress[signedOrder.makerAddress][erc721TokenAddress][
tokenId.toString()
],
)
) {
delete this._orderHashesByERC721AddressByTokenIdByMakerAddress[signedOrder.makerAddress][
erc721TokenAddress
][tokenId.toString()];
}