Skip to content

Permit2

0xA4Bf80b2CFBd80C00cB0Cc3d74C8762Ff4762770

Permit2 handles signature-based transfers in SignatureTransfer and allowance-based transfers in AllowanceTransfer.

Users must approve Permit2 before calling any of the transfer functions.

State Variables

allowance

Maps users to tokens to spender addresses and information about the approval on the token

Indexed in the order of token owner address, token address, spender address

The stored word saves the allowed amount, expiration on the allowance, and nonce

solidity
mapping(address => mapping(address => mapping(address => PackedAllowance))) public allowance;

nonceBitmap

A map from token owner address and a caller specified word index to a bitmap. Used to set bits in the bitmap to prevent against signature replay protection

Uses unordered nonces so that permit messages do not need to be spent in a certain order

solidity
mapping(address => mapping(uint256 => uint256)) public nonceBitmap;

Functions

permitTransferFrom

Transfers a token using a signed permit message

Reverts if the requested amount is greater than the permitted signed amount

solidity
function permitTransferFrom(
    PermitTransferFrom memory permit,
    SignatureTransferDetails calldata transferDetails,
    address owner,
    bytes calldata signature
) external;

Parameters

NameTypeDescription
permitPermitTransferFromThe permit data signed over by the owner
transferDetailsSignatureTransferDetailsThe spender's requested transfer details for the permitted token
owneraddressThe owner of the tokens to transfer
signaturebytesThe signature to verify

permitWitnessTransferFrom

Transfers a token using a signed permit message

The witness type string must follow EIP712 ordering of nested structs and must include the TokenPermissions type definition

solidity
function permitWitnessTransferFrom(
    PermitTransferFrom memory permit,
    SignatureTransferDetails calldata transferDetails,
    address owner,
    bytes32 witness,
    string calldata witnessTypeString,
    bytes calldata signature
) external;

Parameters

NameTypeDescription
permitPermitTransferFromThe permit data signed over by the owner
transferDetailsSignatureTransferDetailsThe spender's requested transfer details for the permitted token
owneraddressThe owner of the tokens to transfer
witnessbytes32Extra data to include when checking the user signature
witnessTypeStringstringThe EIP-712 type definition for remaining string stub of the typehash
signaturebytesThe signature to verify

_permitTransferFrom

Transfers a token using a signed permit message.

solidity
function _permitTransferFrom(
    PermitTransferFrom memory permit,
    SignatureTransferDetails calldata transferDetails,
    address owner,
    bytes32 dataHash,
    bytes calldata signature
) private;

Parameters

NameTypeDescription
permitPermitTransferFromThe permit data signed over by the owner
transferDetailsSignatureTransferDetailsThe spender's requested transfer details for the permitted token
owneraddressThe owner of the tokens to transfer
dataHashbytes32The EIP-712 hash of permit data to include when checking signature
signaturebytesThe signature to verify

permitTransferFrom

Transfers a token using a signed permit message

Reverts if the requested amount is greater than the permitted signed amount

solidity
function permitTransferFrom(
    PermitBatchTransferFrom memory permit,
    SignatureTransferDetails[] calldata transferDetails,
    address owner,
    bytes calldata signature
) external;

Parameters

NameTypeDescription
permitPermitBatchTransferFromThe permit data signed over by the owner
transferDetailsSignatureTransferDetails[]The spender's requested transfer details for the permitted token
owneraddressThe owner of the tokens to transfer
signaturebytesThe signature to verify

permitWitnessTransferFrom

Transfers a token using a signed permit message

The witness type string must follow EIP712 ordering of nested structs and must include the TokenPermissions type definition

solidity
function permitWitnessTransferFrom(
    PermitBatchTransferFrom memory permit,
    SignatureTransferDetails[] calldata transferDetails,
    address owner,
    bytes32 witness,
    string calldata witnessTypeString,
    bytes calldata signature
) external;

Parameters

NameTypeDescription
permitPermitBatchTransferFromThe permit data signed over by the owner
transferDetailsSignatureTransferDetails[]The spender's requested transfer details for the permitted token
owneraddressThe owner of the tokens to transfer
witnessbytes32Extra data to include when checking the user signature
witnessTypeStringstringThe EIP-712 type definition for remaining string stub of the typehash
signaturebytesThe signature to verify

_permitTransferFrom

Transfers tokens using a signed permit messages

solidity
function _permitTransferFrom(
    PermitBatchTransferFrom memory permit,
    SignatureTransferDetails[] calldata transferDetails,
    address owner,
    bytes32 dataHash,
    bytes calldata signature
) private;

Parameters

NameTypeDescription
permitPermitBatchTransferFromThe permit data signed over by the owner
transferDetailsSignatureTransferDetails[]
owneraddressThe owner of the tokens to transfer
dataHashbytes32The EIP-712 hash of permit data to include when checking signature
signaturebytesThe signature to verify

invalidateUnorderedNonces

Invalidates the bits specified in mask for the bitmap at the word position

The wordPos is maxed at type(uint248).max

solidity
function invalidateUnorderedNonces(uint256 wordPos, uint256 mask) external;

Parameters

NameTypeDescription
wordPosuint256A number to index the nonceBitmap at
maskuint256A bitmap masked against msg.sender's current bitmap at the word position

bitmapPositions

Returns the index of the bitmap and the bit position within the bitmap. Used for unordered nonces

The first 248 bits of the nonce value is the index of the desired bitmap

The last 8 bits of the nonce value is the position of the bit in the bitmap

solidity
function bitmapPositions(uint256 nonce) private pure returns (uint256 wordPos, uint256 bitPos);

Parameters

NameTypeDescription
nonceuint256The nonce to get the associated word and bit positions

Returns

NameTypeDescription
wordPosuint256The word position or index into the nonceBitmap
bitPosuint256The bit position

_useUnorderedNonce

Checks whether a nonce is taken and sets the bit at the bit position in the bitmap at the word position

solidity
function _useUnorderedNonce(address from, uint256 nonce) internal;

Parameters

NameTypeDescription
fromaddressThe address to use the nonce at
nonceuint256The nonce to spend

approve

Approves the spender to use up to amount of the specified token up until the expiration

The packed allowance also holds a nonce, which will stay unchanged in approve

solidity
function approve(address token, address spender, uint160 amount, uint48 expiration) external;

Parameters

NameTypeDescription
tokenaddressThe token to approve
spenderaddressThe spender address to approve
amountuint160The approved amount of the token
expirationuint48The timestamp at which the approval is no longer valid

permit

Permit a spender to a given amount of the owners token via the owner's EIP-712 signature

May fail if the owner's nonce was invalidated in-flight by invalidateNonce

solidity
function permit(address owner, PermitSingle memory permitSingle, bytes calldata signature)
    external;

Parameters

NameTypeDescription
owneraddressThe owner of the tokens being approved
permitSinglePermitSingleData signed over by the owner specifying the terms of approval
signaturebytesThe owner's signature over the permit data

permit

Permit a spender to a given amount of the owners token via the owner's EIP-712 signature

May fail if the owner's nonce was invalidated in-flight by invalidateNonce

solidity
function permit(address owner, PermitBatch memory permitBatch, bytes calldata signature) external;

Parameters

NameTypeDescription
owneraddressThe owner of the tokens being approved
permitBatchPermitBatch
signaturebytesThe owner's signature over the permit data

transferFrom

Transfer approved tokens from one address to another

Requires the from address to have approved at least the desired amount of tokens to msg.sender.

solidity
function transferFrom(address from, address to, uint160 amount, address token) external;

Parameters

NameTypeDescription
fromaddressThe address to transfer from
toaddressThe address of the recipient
amountuint160The amount of the token to transfer
tokenaddressThe token address to transfer

transferFrom

Transfer approved tokens from one address to another

Requires the from address to have approved at least the desired amount of tokens to msg.sender.

solidity
function transferFrom(AllowanceTransferDetails[] calldata transferDetails) external;

Parameters

NameTypeDescription
transferDetailsAllowanceTransferDetails[]

_transfer

Internal function for transferring tokens using stored allowances

Will fail if the allowed timeframe has passed

solidity
function _transfer(address from, address to, uint160 amount, address token) private;

lockdown

Enables performing a "lockdown" of the sender's Permit2 identity by batch revoking approvals

solidity
function lockdown(TokenSpenderPair[] calldata approvals) external;

Parameters

NameTypeDescription
approvalsTokenSpenderPair[]Array of approvals to revoke.

invalidateNonces

Invalidate nonces for a given (token, spender) pair

Can't invalidate more than 2**16 nonces per transaction.

solidity
function invalidateNonces(address token, address spender, uint48 newNonce) external;

Parameters

NameTypeDescription
tokenaddressThe token to invalidate nonces for
spenderaddressThe spender to invalidate nonces for
newNonceuint48The new nonce to set. Invalidates all nonces less than it.

_updateApproval

Sets the new values for amount, expiration, and nonce.

Will check that the signed nonce is equal to the current nonce and then incrememnt the nonce value by 1.

Emits a Permit event.

solidity
function _updateApproval(PermitDetails memory details, address owner, address spender) private;