diff --git a/src/sdk/common/types.ts b/src/sdk/common/types.ts index d34d9d2..7b60348 100644 --- a/src/sdk/common/types.ts +++ b/src/sdk/common/types.ts @@ -48,4 +48,5 @@ export interface TransactionOverrides { type?: number; accessList?: any; customData?: Record; + ccipReadEnabled?: boolean; } diff --git a/src/sdk/v4/NftSwapV4.ts b/src/sdk/v4/NftSwapV4.ts index c0f8184..b1eaac4 100644 --- a/src/sdk/v4/NftSwapV4.ts +++ b/src/sdk/v4/NftSwapV4.ts @@ -66,7 +66,7 @@ import { import { getWrappedNativeToken } from '../../utils/addresses'; import { DIRECTION_MAPPING, OrderStatusV4, TradeDirection } from './enums'; import { CONTRACT_ORDER_VALIDATOR } from './properties'; -import { ETH_ADDRESS_AS_ERC20 } from './constants'; +import { ETH_ADDRESS_AS_ERC20, PRESIGNED_SIGNATURE } from './constants'; import { ZERO_AMOUNT } from '../../utils/eth'; import { arrayify } from '@ethersproject/bytes'; @@ -283,6 +283,52 @@ class NftSwapV4 implements INftSwapV4 { throw new Error('unsupport order'); }; + /** + * Orders can be simultaneously "signed" and listed on-chain using the presignOrder function. + * This is an on-chain action and requires gas. Orders can only be signed by the maker address specified in the order. + * The pre-sign functions emit the entire order as an event, so that the order is easily indexable by subgraphs and thus easily discoverable without the need for an off-chain database. + * If an order has been pre-signed, it can be filled by providing a “null” signature with the PRESIGNED signature type + * If you prefer off-chain order signatures, use the regular signOrder function + * @param order 0x order that requires a signature + * @param txOverrides (optional) Ethers transaction overrides + * @returns An on-chain transaction that returns the signed order after awaiting the tx being mined + */ + presignOrder = async ( + order: NftOrderV4, + txOverrides?: TransactionOverrides + ): Promise<{ signedOrder: SignedNftOrderV4; tx: ContractTransaction }> => { + if ('erc721Token' in order) { + const signed: SignedNftOrderV4 = { + ...order, + signature: PRESIGNED_SIGNATURE, + }; + const presignErc721OrderTx = await this.exchangeProxy.preSignERC721Order( + order, + { + ...txOverrides, + } + ); + return { + tx: presignErc721OrderTx, + signedOrder: signed, + }; + } else if ('erc1155Token' in order) { + const signed: SignedNftOrderV4 = { + ...order, + signature: PRESIGNED_SIGNATURE, + }; + const presignErc1155OrderTx = + await this.exchangeProxy.preSignERC1155Order(order, { + ...txOverrides, + }); + return { + tx: presignErc1155OrderTx, + signedOrder: signed, + }; + } + throw new Error('unsupport order'); + }; + /** * Batch fill NFT sell orders * Can be used by taker to fill multiple NFT sell orders atomically. diff --git a/src/sdk/v4/constants.ts b/src/sdk/v4/constants.ts index 1f9cd96..1ec380b 100644 --- a/src/sdk/v4/constants.ts +++ b/src/sdk/v4/constants.ts @@ -60,3 +60,11 @@ export const ETH_ADDRESS_AS_ERC20 = export const NATIVE_TOKEN_ADDRESS_AS_ERC20 = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'; + +// https://github.com/0xProject/protocol/blob/refactor/nft-orders/contracts/zero-ex/contracts/src/features/libs/LibSignature.sol#L42-L61 +export const PRESIGNED_SIGNATURE = { + signatureType: 4, // Presigned id + v: 0, + r: '0', + s: '0', +};