c1177416f5
* add LibERC721Order.sol * Add ERC721 interface to vendor/ * Add ERC721OrdersFeature interface * Storage lib for ERC721 orders feature * Implement basic functionality for ERC721 orders (buy, sell, cancel, etc) * Add isValidERC721OrderSignature to interface * implement onERC721Received * Implement batchBuyERC721s * left/right orders -> sell/buy orders * Add missing @return comments * Implement matching functions * Use SafeMath where necessary * add rich errors for ERC721OrdersFeature * Add comments * Add presign support for ERC721 orders * Cancel using just the order nonce * Add IERC721OrdersFeature to IZeroEx * Add taker callback * Assembly optimizations in LibERC721Order * Add ERC721Orders TS class * create zero-ex/contracts/test/integration/ and tokens/ directories * TestMintableERC721Token * tmp * address feedback from original PR (#391) * address feedback from original PR * Update contracts/zero-ex/contracts/src/features/ERC721OrdersFeature.sol Co-authored-by: Kim Persson <kimpers@users.noreply.github.com> * address review feedback and improve order parameter naming * Add batchCancel function * Emit order fields in preSign * Fix tests Co-authored-by: Lawrence Forman <me@merklejerk.com> Co-authored-by: Kim Persson <kimpers@users.noreply.github.com> Co-authored-by: Michael Zhu <mchl.zhu.96@gmail.com> * Remove revertIfIncomplete from batchMatch * Sanity check maker address in preSign * ERC1155OrdersFeature contracts * Commence refactor, abstract base contract * ERC721OrdersFeature inherits from NFTOrders * Refactor ERC1155OrdersFeature to inherit from NFTOrders * Fix order hashing * Fix ERC721OrdersFeature tests * Typos * Remove maker address from preSigned mapping * disable dex sampler tests * Refactor TS tooling * Address PR feedback * Rearrange event fields to better align with struct fields * Update comments * update AbiEncoder.create params * Add ERC1155Order to protocol-utils * Add ERC1155OrdersFeeature tests * Bump package versions and regenerate contract wrappers * Add ERC165Feature * NFT orders: address audit findings (#417) * CVF-1: use pragma solidity ^0.6 instead of ^0.6.5 * CVF-11: fix inaccurate comment * CVF-16: Enable taker callbacks for batchBuyERC1155s * CVF-17: use internal call if revertIfIncomplete is true * CVF-21: avoid duplicate SLOAD * CVF-23: merge if statements * CVF-24: Reorder status checks to be consistent with ERC721OrdersFeature * CVF-25: Update unclear comment (canonical hash -> EIP-712 hash) * CVF-31: Document keys of orderState mapping * CVF-45: DRY up fees/properties hashing * CVF-47, CVF-50, CVF-57: calculate properties.length once; hash propertyStructHashArray in-place using assembly * CVF-56: More descriptive names for assembly variables * CVF-71: Update confusing comment about rounding in _payFees * CVF-72: Move ETH assertions outside of loop in _payFees * CVF-74: Move property validation loop to else branch * CVF-82: Update inaccurate comment * CVF-86: Enable taker callbacks for batchBuyERC721s * CVF-87: use internal call if revertIfIncomplete is true * CVF-89: Perform token mismatch checks before stateful operations * CVF-90, CVF-91: Defer ERC20 token mismatch check * CVF-93: Add inline comments for _payFees parameters in matchERC721Orders * CVF-94: Fix comment (Step 7 -> Step 5) * CVF-98: Use binary & operator instead of mod * CVF-99: Update unclear comment (canonical hash -> EIP-712 hash) * CVF-65, CVF-66, CVF-67: Copy params.ethAvailable into local variable; check that ethSpent does not exceed ethAvailable; remove ethAvailable < erc20FillAmount check * CVF-52, CVF-55, CVF-59: calculate fees.length once; hash feeStructHashArray in-place using assembly * CVF-14, CVF-32: OrderState struct; separate storage mapping for 1155 cancellations so orders can be cancelled by nonce * Update changelogs, IZeroEx artifact/wrapper Co-authored-by: Lawrence Forman <lawrence@0xproject.com> Co-authored-by: Lawrence Forman <me@merklejerk.com> Co-authored-by: Kim Persson <kimpers@users.noreply.github.com>
426 lines
15 KiB
TypeScript
426 lines
15 KiB
TypeScript
import { getContractAddressesForChainOrThrow } from '@0x/contract-addresses';
|
|
import { SupportedProvider } from '@0x/subproviders';
|
|
import { EIP712TypedData } from '@0x/types';
|
|
import { BigNumber, hexUtils, NULL_ADDRESS } from '@0x/utils';
|
|
|
|
import { ZERO } from './constants';
|
|
import {
|
|
createExchangeProxyEIP712Domain,
|
|
EIP712_DOMAIN_PARAMETERS,
|
|
getExchangeProxyEIP712Hash,
|
|
getTypeHash,
|
|
} from './eip712_utils';
|
|
import {
|
|
eip712SignHashWithKey,
|
|
eip712SignTypedDataWithProviderAsync,
|
|
ethSignHashWithKey,
|
|
ethSignHashWithProviderAsync,
|
|
Signature,
|
|
SignatureType,
|
|
} from './signature_utils';
|
|
|
|
// tslint:disable:enum-naming
|
|
export enum TradeDirection {
|
|
SellNFT = 0,
|
|
BuyNFT = 1,
|
|
}
|
|
// tslint:enable:enum-naming
|
|
|
|
export enum OrderStatus {
|
|
Invalid = 0,
|
|
Fillable = 1,
|
|
Unfillable = 2,
|
|
Expired = 3,
|
|
}
|
|
|
|
interface Fee {
|
|
recipient: string;
|
|
amount: BigNumber;
|
|
feeData: string;
|
|
}
|
|
|
|
interface Property {
|
|
propertyValidator: string;
|
|
propertyData: string;
|
|
}
|
|
|
|
const NFT_ORDER_DEFAULT_VALUES = {
|
|
direction: TradeDirection.SellNFT,
|
|
maker: NULL_ADDRESS,
|
|
taker: NULL_ADDRESS,
|
|
expiry: ZERO,
|
|
nonce: ZERO,
|
|
erc20Token: NULL_ADDRESS,
|
|
erc20TokenAmount: ZERO,
|
|
fees: [] as Fee[],
|
|
chainId: 1,
|
|
verifyingContract: getContractAddressesForChainOrThrow(1).exchangeProxy,
|
|
};
|
|
|
|
const ERC721_ORDER_DEFAULT_VALUES = {
|
|
direction: TradeDirection.SellNFT,
|
|
maker: NULL_ADDRESS,
|
|
taker: NULL_ADDRESS,
|
|
expiry: ZERO,
|
|
nonce: ZERO,
|
|
erc20Token: NULL_ADDRESS,
|
|
erc20TokenAmount: ZERO,
|
|
fees: [] as Fee[],
|
|
erc721Token: NULL_ADDRESS,
|
|
erc721TokenId: ZERO,
|
|
erc721TokenProperties: [] as Property[],
|
|
chainId: 1,
|
|
verifyingContract: getContractAddressesForChainOrThrow(1).exchangeProxy,
|
|
};
|
|
|
|
const ERC1155_ORDER_DEFAULT_VALUES = {
|
|
direction: TradeDirection.SellNFT,
|
|
maker: NULL_ADDRESS,
|
|
taker: NULL_ADDRESS,
|
|
expiry: ZERO,
|
|
nonce: ZERO,
|
|
erc20Token: NULL_ADDRESS,
|
|
erc20TokenAmount: ZERO,
|
|
fees: [] as Fee[],
|
|
erc1155Token: NULL_ADDRESS,
|
|
erc1155TokenId: ZERO,
|
|
erc1155TokenProperties: [] as Property[],
|
|
erc1155TokenAmount: ZERO,
|
|
chainId: 1,
|
|
verifyingContract: getContractAddressesForChainOrThrow(1).exchangeProxy,
|
|
};
|
|
|
|
type CommonNFTOrderFields = typeof NFT_ORDER_DEFAULT_VALUES;
|
|
type ERC721OrderFields = typeof ERC721_ORDER_DEFAULT_VALUES;
|
|
type ERC1155OrderFields = typeof ERC1155_ORDER_DEFAULT_VALUES;
|
|
|
|
export abstract class NFTOrder {
|
|
public static readonly FEE_ABI = [
|
|
{ type: 'address', name: 'recipient' },
|
|
{ type: 'uint256', name: 'amount' },
|
|
{ type: 'bytes', name: 'feeData' },
|
|
];
|
|
public static readonly PROPERTY_ABI = [
|
|
{ type: 'address', name: 'propertyValidator' },
|
|
{ type: 'bytes', name: 'propertyData' },
|
|
];
|
|
public static readonly FEE_TYPE_HASH = getTypeHash('Fee', NFTOrder.FEE_ABI);
|
|
public static readonly PROPERTY_TYPE_HASH = getTypeHash('Property', NFTOrder.PROPERTY_ABI);
|
|
|
|
public direction: TradeDirection;
|
|
public maker: string;
|
|
public taker: string;
|
|
public expiry: BigNumber;
|
|
public nonce: BigNumber;
|
|
public erc20Token: string;
|
|
public erc20TokenAmount: BigNumber;
|
|
public fees: Fee[];
|
|
public chainId: number;
|
|
public verifyingContract: string;
|
|
|
|
protected constructor(fields: Partial<CommonNFTOrderFields> = {}) {
|
|
const _fields = { ...NFT_ORDER_DEFAULT_VALUES, ...fields };
|
|
this.direction = _fields.direction;
|
|
this.maker = _fields.maker;
|
|
this.taker = _fields.taker;
|
|
this.expiry = _fields.expiry;
|
|
this.nonce = _fields.nonce;
|
|
this.erc20Token = _fields.erc20Token;
|
|
this.erc20TokenAmount = _fields.erc20TokenAmount;
|
|
this.fees = _fields.fees;
|
|
this.chainId = _fields.chainId;
|
|
this.verifyingContract = _fields.verifyingContract;
|
|
}
|
|
|
|
public abstract getStructHash(): string;
|
|
public abstract getEIP712TypedData(): EIP712TypedData;
|
|
protected abstract _getProperties(): Property[];
|
|
|
|
public willExpire(secondsFromNow: number = 0): boolean {
|
|
const millisecondsInSecond = 1000;
|
|
const currentUnixTimestampSec = new BigNumber(Date.now() / millisecondsInSecond).integerValue();
|
|
return this.expiry.isLessThan(currentUnixTimestampSec.plus(secondsFromNow));
|
|
}
|
|
|
|
public getHash(): string {
|
|
return getExchangeProxyEIP712Hash(this.getStructHash(), this.chainId, this.verifyingContract);
|
|
}
|
|
|
|
public async getSignatureWithProviderAsync(
|
|
provider: SupportedProvider,
|
|
type: SignatureType = SignatureType.EthSign,
|
|
signer: string = this.maker,
|
|
): Promise<Signature> {
|
|
switch (type) {
|
|
case SignatureType.EIP712:
|
|
return eip712SignTypedDataWithProviderAsync(this.getEIP712TypedData(), signer, provider);
|
|
case SignatureType.EthSign:
|
|
return ethSignHashWithProviderAsync(this.getHash(), signer, provider);
|
|
default:
|
|
throw new Error(`Cannot sign with signature type: ${type}`);
|
|
}
|
|
}
|
|
|
|
public getSignatureWithKey(key: string, type: SignatureType = SignatureType.EthSign): Signature {
|
|
switch (type) {
|
|
case SignatureType.EIP712:
|
|
return eip712SignHashWithKey(this.getHash(), key);
|
|
case SignatureType.EthSign:
|
|
return ethSignHashWithKey(this.getHash(), key);
|
|
default:
|
|
throw new Error(`Cannot sign with signature type: ${type}`);
|
|
}
|
|
}
|
|
|
|
protected _getPropertiesHash(): string {
|
|
return hexUtils.hash(
|
|
hexUtils.concat(
|
|
...this._getProperties().map(property =>
|
|
hexUtils.hash(
|
|
hexUtils.concat(
|
|
hexUtils.leftPad(NFTOrder.PROPERTY_TYPE_HASH),
|
|
hexUtils.leftPad(property.propertyValidator),
|
|
hexUtils.hash(property.propertyData),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
protected _getFeesHash(): string {
|
|
return hexUtils.hash(
|
|
hexUtils.concat(
|
|
...this.fees.map(fee =>
|
|
hexUtils.hash(
|
|
hexUtils.concat(
|
|
hexUtils.leftPad(NFTOrder.FEE_TYPE_HASH),
|
|
hexUtils.leftPad(fee.recipient),
|
|
hexUtils.leftPad(fee.amount),
|
|
hexUtils.hash(fee.feeData),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
export class ERC721Order extends NFTOrder {
|
|
public static readonly STRUCT_NAME = 'ERC721Order';
|
|
public static readonly STRUCT_ABI = [
|
|
{ type: 'uint8', name: 'direction' },
|
|
{ type: 'address', name: 'maker' },
|
|
{ type: 'address', name: 'taker' },
|
|
{ type: 'uint256', name: 'expiry' },
|
|
{ type: 'uint256', name: 'nonce' },
|
|
{ type: 'address', name: 'erc20Token' },
|
|
{ type: 'uint256', name: 'erc20TokenAmount' },
|
|
{ type: 'Fee[]', name: 'fees' },
|
|
{ type: 'address', name: 'erc721Token' },
|
|
{ type: 'uint256', name: 'erc721TokenId' },
|
|
{ type: 'Property[]', name: 'erc721TokenProperties' },
|
|
];
|
|
|
|
public static readonly TYPE_HASH = getTypeHash(ERC721Order.STRUCT_NAME, ERC721Order.STRUCT_ABI, {
|
|
['Fee']: NFTOrder.FEE_ABI,
|
|
['Property']: NFTOrder.PROPERTY_ABI,
|
|
});
|
|
|
|
public erc721Token: string;
|
|
public erc721TokenId: BigNumber;
|
|
public erc721TokenProperties: Property[];
|
|
|
|
constructor(fields: Partial<ERC721OrderFields> = {}) {
|
|
const _fields = { ...ERC721_ORDER_DEFAULT_VALUES, ...fields };
|
|
super(_fields);
|
|
this.erc721Token = _fields.erc721Token;
|
|
this.erc721TokenId = _fields.erc721TokenId;
|
|
this.erc721TokenProperties = _fields.erc721TokenProperties;
|
|
}
|
|
|
|
public clone(fields: Partial<ERC721OrderFields> = {}): ERC721Order {
|
|
return new ERC721Order({
|
|
direction: this.direction,
|
|
maker: this.maker,
|
|
taker: this.taker,
|
|
expiry: this.expiry,
|
|
nonce: this.nonce,
|
|
erc20Token: this.erc20Token,
|
|
erc20TokenAmount: this.erc20TokenAmount,
|
|
fees: this.fees,
|
|
erc721Token: this.erc721Token,
|
|
erc721TokenId: this.erc721TokenId,
|
|
erc721TokenProperties: this.erc721TokenProperties,
|
|
chainId: this.chainId,
|
|
verifyingContract: this.verifyingContract,
|
|
...fields,
|
|
});
|
|
}
|
|
|
|
public getStructHash(): string {
|
|
return hexUtils.hash(
|
|
hexUtils.concat(
|
|
hexUtils.leftPad(ERC721Order.TYPE_HASH),
|
|
hexUtils.leftPad(this.direction),
|
|
hexUtils.leftPad(this.maker),
|
|
hexUtils.leftPad(this.taker),
|
|
hexUtils.leftPad(this.expiry),
|
|
hexUtils.leftPad(this.nonce),
|
|
hexUtils.leftPad(this.erc20Token),
|
|
hexUtils.leftPad(this.erc20TokenAmount),
|
|
this._getFeesHash(),
|
|
hexUtils.leftPad(this.erc721Token),
|
|
hexUtils.leftPad(this.erc721TokenId),
|
|
this._getPropertiesHash(),
|
|
),
|
|
);
|
|
}
|
|
|
|
public getEIP712TypedData(): EIP712TypedData {
|
|
return {
|
|
types: {
|
|
EIP712Domain: EIP712_DOMAIN_PARAMETERS,
|
|
[ERC721Order.STRUCT_NAME]: ERC721Order.STRUCT_ABI,
|
|
['Fee']: NFTOrder.FEE_ABI,
|
|
['Property']: NFTOrder.PROPERTY_ABI,
|
|
},
|
|
domain: createExchangeProxyEIP712Domain(this.chainId, this.verifyingContract) as any,
|
|
primaryType: ERC721Order.STRUCT_NAME,
|
|
message: {
|
|
direction: this.direction,
|
|
maker: this.maker,
|
|
taker: this.taker,
|
|
expiry: this.expiry.toString(10),
|
|
nonce: this.nonce.toString(10),
|
|
erc20Token: this.erc20Token,
|
|
erc20TokenAmount: this.erc20TokenAmount.toString(10),
|
|
fees: this.fees.map(fee => ({
|
|
recipient: fee.recipient,
|
|
amount: fee.amount.toString(10),
|
|
feeData: fee.feeData,
|
|
})) as any,
|
|
erc721Token: this.erc721Token,
|
|
erc721TokenId: this.erc721TokenId.toString(10),
|
|
erc721TokenProperties: this.erc721TokenProperties as any,
|
|
},
|
|
};
|
|
}
|
|
|
|
protected _getProperties(): Property[] {
|
|
return this.erc721TokenProperties;
|
|
}
|
|
}
|
|
|
|
export class ERC1155Order extends NFTOrder {
|
|
public static readonly STRUCT_NAME = 'ERC1155Order';
|
|
public static readonly STRUCT_ABI = [
|
|
{ type: 'uint8', name: 'direction' },
|
|
{ type: 'address', name: 'maker' },
|
|
{ type: 'address', name: 'taker' },
|
|
{ type: 'uint256', name: 'expiry' },
|
|
{ type: 'uint256', name: 'nonce' },
|
|
{ type: 'address', name: 'erc20Token' },
|
|
{ type: 'uint256', name: 'erc20TokenAmount' },
|
|
{ type: 'Fee[]', name: 'fees' },
|
|
{ type: 'address', name: 'erc1155Token' },
|
|
{ type: 'uint256', name: 'erc1155TokenId' },
|
|
{ type: 'Property[]', name: 'erc1155TokenProperties' },
|
|
{ type: 'uint128', name: 'erc1155TokenAmount' },
|
|
];
|
|
|
|
public static readonly TYPE_HASH = getTypeHash(ERC1155Order.STRUCT_NAME, ERC1155Order.STRUCT_ABI, {
|
|
['Fee']: NFTOrder.FEE_ABI,
|
|
['Property']: NFTOrder.PROPERTY_ABI,
|
|
});
|
|
|
|
public erc1155Token: string;
|
|
public erc1155TokenId: BigNumber;
|
|
public erc1155TokenProperties: Property[];
|
|
public erc1155TokenAmount: BigNumber;
|
|
|
|
constructor(fields: Partial<ERC1155OrderFields> = {}) {
|
|
const _fields = { ...ERC1155_ORDER_DEFAULT_VALUES, ...fields };
|
|
super(_fields);
|
|
this.erc1155Token = _fields.erc1155Token;
|
|
this.erc1155TokenId = _fields.erc1155TokenId;
|
|
this.erc1155TokenProperties = _fields.erc1155TokenProperties;
|
|
this.erc1155TokenAmount = _fields.erc1155TokenAmount;
|
|
}
|
|
|
|
public clone(fields: Partial<ERC1155OrderFields> = {}): ERC1155Order {
|
|
return new ERC1155Order({
|
|
direction: this.direction,
|
|
maker: this.maker,
|
|
taker: this.taker,
|
|
expiry: this.expiry,
|
|
nonce: this.nonce,
|
|
erc20Token: this.erc20Token,
|
|
erc20TokenAmount: this.erc20TokenAmount,
|
|
fees: this.fees,
|
|
erc1155Token: this.erc1155Token,
|
|
erc1155TokenId: this.erc1155TokenId,
|
|
erc1155TokenProperties: this.erc1155TokenProperties,
|
|
erc1155TokenAmount: this.erc1155TokenAmount,
|
|
chainId: this.chainId,
|
|
verifyingContract: this.verifyingContract,
|
|
...fields,
|
|
});
|
|
}
|
|
|
|
public getStructHash(): string {
|
|
return hexUtils.hash(
|
|
hexUtils.concat(
|
|
hexUtils.leftPad(ERC1155Order.TYPE_HASH),
|
|
hexUtils.leftPad(this.direction),
|
|
hexUtils.leftPad(this.maker),
|
|
hexUtils.leftPad(this.taker),
|
|
hexUtils.leftPad(this.expiry),
|
|
hexUtils.leftPad(this.nonce),
|
|
hexUtils.leftPad(this.erc20Token),
|
|
hexUtils.leftPad(this.erc20TokenAmount),
|
|
this._getFeesHash(),
|
|
hexUtils.leftPad(this.erc1155Token),
|
|
hexUtils.leftPad(this.erc1155TokenId),
|
|
this._getPropertiesHash(),
|
|
hexUtils.leftPad(this.erc1155TokenAmount),
|
|
),
|
|
);
|
|
}
|
|
|
|
public getEIP712TypedData(): EIP712TypedData {
|
|
return {
|
|
types: {
|
|
EIP712Domain: EIP712_DOMAIN_PARAMETERS,
|
|
[ERC1155Order.STRUCT_NAME]: ERC1155Order.STRUCT_ABI,
|
|
['Fee']: NFTOrder.FEE_ABI,
|
|
['Property']: NFTOrder.PROPERTY_ABI,
|
|
},
|
|
domain: createExchangeProxyEIP712Domain(this.chainId, this.verifyingContract) as any,
|
|
primaryType: ERC1155Order.STRUCT_NAME,
|
|
message: {
|
|
direction: this.direction,
|
|
maker: this.maker,
|
|
taker: this.taker,
|
|
expiry: this.expiry.toString(10),
|
|
nonce: this.nonce.toString(10),
|
|
erc20Token: this.erc20Token,
|
|
erc20TokenAmount: this.erc20TokenAmount.toString(10),
|
|
fees: this.fees.map(fee => ({
|
|
recipient: fee.recipient,
|
|
amount: fee.amount.toString(10),
|
|
feeData: fee.feeData,
|
|
})) as any,
|
|
erc1155Token: this.erc1155Token,
|
|
erc1155TokenId: this.erc1155TokenId.toString(10),
|
|
erc1155TokenProperties: this.erc1155TokenProperties as any,
|
|
erc1155TokenAmount: this.erc1155TokenAmount.toString(10),
|
|
},
|
|
};
|
|
}
|
|
|
|
protected _getProperties(): Property[] {
|
|
return this.erc1155TokenProperties;
|
|
}
|
|
}
|