XSC002 Permit
- An adaptation of EIP-2612 for Xian.
- Allows for a user to grant a spender a certain amount of tokens to spend on their behalf, without having to submit a transaction to the network.
- Once permit is successfully called, the spender is granted an allowance, and can spend the users tokens as normal.
How it works :
The user constructs a message to sign, and the contract verifies the signature.
The message follows consists of the following fields:
owner
: The address of the owner of the permitspender
: The address of the spendervalue
: The amount of tokens to spenddeadline
: The deadline for the permitsignature
: The signature of the messagecontract
: The name of the contract to which the permit is granted
The user / frontend dapp will construct the message like so :
msg = f"{owner}:{spender}:{value}:{deadline}:{contract}"
signature = wallet.sign_msg(msg)
The frontend dapp will then call
permit(owner, spender, value, deadline, signature)
on the contractpermit()
will :- construct the message like so :
msg = f"{owner}:{spender}:{value}:{deadline}:{ctx.this}"
- assert the deadline is greater than the current time.
- construct a
permit_hash
using SHA3 hash ofmsg
. - assert
permit_hash
is None, if not, revert - store
permit_hash
inpermits[permit_hash]
, to prevent replays - call
verify(msg, signature)
- if valid:
- will add the
permit_hash
topermits
- add the allowance to the spender
- will add the
- construct the message like so :
How to test :
- Setup testing harness by following the instructions in the contract dev environment
- Clone this repo to
contracts
- Run
pytest
in the root directory of the repo