The Aave protocol is migrating from its native LEND token to the AAVE token, as detailed in Aavenomics. The AAVE token is an ERC-20 compatible token with the addition of a snapshot feature (used in governance balance tracking) and integrates EIP 2612 permit function, allowing gas-less transactions and one transaction approval/transfer.
The source code for the AAVE token and migration can be found on Github here.
permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external
使用EIP-2612协议的permit实现了无需花费gas的转账功能
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external {
require(owner != address(0), "INVALID_OWNER");
//solium-disable-next-line
require(block.timestamp <= deadline, "INVALID_EXPIRATION");
uint256 currentValidNonce = _nonces[owner];
bytes32 digest = keccak256(
abi.encodePacked(
"\\x19\\x01",
DOMAIN_SEPARATOR,
keccak256(
abi.encode(PERMIT_TYPEHASH, owner, spender, value, currentValidNonce, deadline))
)
);
require(owner == ecrecover(digest, v, r, s), "INVALID_SIGNATURE");
_nonces[owner] = currentValidNonce.add(1);
_approve(owner, spender, value);
}
import { signTypedData_v4 } from 'eth-sig-util'
import { fromRpcSig } from 'ethereumjs-util'
// ... other imports
import AaveTokenAbi from "./AaveTokenAbi.json"
// ... setup your web3 provider
const aaveTokenAddress = "AAVE_TOKEN_ADDRESS"
const aaveTokenContract = new web3.eth.Contract(AaveTokenAbi, aaveTokenAddress)
const privateKey = "YOUR_PRIVATE_KEY_WITHOUT_0x"
const chainId = 1
const owner = "OWNER_ADDRESS"
const spender = "SPENDER_ADDRESS"
const value = 100 // Amount the spender is permitted
const nonce = 1 // The next valid nonce, use `_nonces()`
const deadline = 1600093162
const permitParams = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
Permit: [
{ name: "owner", type: "address" },
{ name: "spender", type: "address" },
{ name: "value", type: "uint256" },
{ name: "nonce", type: "uint256" },
{ name: "deadline", type: "uint256" },
],
},
primaryType: "Permit",
domain: {
name: "Aave Token",
version: "1",
chainId: chainId,
verifyingContract: aaveTokenAddress,
},
message: {
owner,
spender,
value,
nonce,
deadline,
},
}
const signature = signTypedData_v4(
Buffer.from(privateKey, "hex"),
{ data: permitParams }
)
const { v, r, s } = fromRpcSig(signature)
await aaveTokenContract.methods
.permit({
owner,
spender,
value,
deadline,
v,
r,
s
})
.send()
.catch((e) => {
throw Error(`Error permitting: ${e.message}`)
})
_nonces()
Returns the next valid nonce to submit when calling permit()