Nikita Duginets
Nikita Duginets

Reputation: 303

How permit function works

I'm trying to figure out what's purpose of permit function from the Uniswap core

I know that ecrecover function ensures, that messages was signed by some address, but that's not clear for me -- why we can use permit function?

Upvotes: 1

Views: 1140

Answers (2)

Yilmaz
Yilmaz

Reputation: 49321

In Erc20, transferFrom allows decentralized exchange (DEX) to transfer funds from your account. But before calling this function, you have to call approve function, to allow the DEX how much fund it can use. So you are calling 2 functions and each will cost you a gas fee. Those gas fees are high and If you are trading often on DEX, you will be paying a lot of gas fees.

With permit function, you do not need to call the approve function. You are approving the transaction by signing the transaction. This transaction is signed off-chain so you are not paying any gas fees. Fron-end developers handle this part and then they derive the v,r,s of the signature. If you look at the args of the permit function, it expects v,r,s arguments.

permit() function allows anyone to authorize and swap a token in one transaction instead of two transactions. But this does not mean that you are saving half the gas fees. For example, if you were paying 10 wei gas fees for two transactions now it is not going to be 5 wei because permit function has more logic to implement. So the total gas fee will be between 5-10 wei. permit is not about just saving the gas fee but delegating the transaction to another wallet so that the wallet will pay the transaction. that is called gasless transaction.

Upvotes: 1

Petr Hejda
Petr Hejda

Reputation: 43521

The owner can sign a structured message which produces a signature (separated into variables v, r, and s).

Meaning of the message could be described like "I, the owner, am approving the spender to operate value of my LP tokens of this specific Uniswap pair. This approval needs to be recorded onchain before deadline."

The owner then passes the signature and the values offchain to anyone else (e.g. to the spender), who executes the permit() function. By executing the permit() function, the approval becomes valid since now it's recorded onchain.


This is useful in cases when the owner has LP tokens - but doesn't have sufficient native balance to pay for gas fees for their transfer or approval.

Or when the owner is a user of your app and you have some kind of an agreement that your app will pay gas fees for the user. E.g. because they compensate you for the gas fees using another way - stablecoins, monthly fee, etc.

Upvotes: 2

Related Questions