Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds presign function to 0x v4 SDK #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/sdk/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ export interface TransactionOverrides {
type?: number;
accessList?: any;
customData?: Record<string, any>;
ccipReadEnabled?: boolean;
}
48 changes: 47 additions & 1 deletion src/sdk/v4/NftSwapV4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/sdk/v4/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};