aTokens
aTokens are yield-generating tokens that are minted and burned upon deposit and withdraw. The aTokens' value is pegged to the value of the corresponding deposited asset at a 1:1 ratio, and can be safely stored, transferred or traded. All interest collected by the aTokens reserves are distributed to aTokens holders directly by continuously increasing their wallet balance.
The source code can be found on Github here.
For all minting and burning actions, see Deposit() and Withdraw() methods in the LendingPool contract.

EIP20 Methods

All standard EIP20 methods are implemented, such as balanceOf(), transfer(), transferFrom(), approve(), totalSupply(), etc.
balanceOf() will always return the most up to date balance of the user, which includes their principal balance + the interest generated by the principal balance.

EIP2612 Methods

permit()

function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
Allows a user to permit another account (or contract) to use their funds using a signed message. This enables gas-less transactions and single approval/transfer transactions.
Parameter
Type
Description
owner
address
The owner of the funds
spender
address
The spender for the funds
value
uint256
The amount the spender is permitted to use
deadline
uint256
The deadline timestamp that the permit is valid. Use type(uint).max for no deadline.
v
uint8
Signature parameter
r
bytes32
Signature parameter
s
bytes32
Signature parameter
Web3.js
1
import { signTypedData_v4 } from 'eth-sig-util'
2
import { fromRpcSig } from 'ethereumjs-util'
3
4
// ... other imports
5
6
import aTokenAbi from "./aTokenAbi.json"
7
8
// ... setup your web3 provider
9
10
const aTokenAddress = "ATOKEN_ADDRESS"
11
const aTokenContract = new web3.eth.Contract(aTokenAbi, aTokenAddress)
12
13
const privateKey = "YOUR_PRIVATE_KEY_WITHOUT_0x"
14
const chainId = 1
15
const owner = "OWNER_ADDRESS"
16
const spender = "SPENDER_ADDRESS"
17
const value = 100 // Amount the spender is permitted
18
const nonce = 1 // The next valid nonce, use `_nonces()`
19
const deadline = 1600093162
20
21
const permitParams = {
22
types: {
23
EIP712Domain: [
24
{ name: "name", type: "string" },
25
{ name: "version", type: "string" },
26
{ name: "chainId", type: "uint256" },
27
{ name: "verifyingContract", type: "address" },
28
],
29
Permit: [
30
{ name: "owner", type: "address" },
31
{ name: "spender", type: "address" },
32
{ name: "value", type: "uint256" },
33
{ name: "nonce", type: "uint256" },
34
{ name: "deadline", type: "uint256" },
35
],
36
},
37
primaryType: "Permit",
38
domain: {
39
name: "aTOKEN_NAME",
40
version: "1",
41
chainId: chainId,
42
verifyingContract: aTokenAddress,
43
},
44
message: {
45
owner,
46
spender,
47
value,
48
nonce,
49
deadline,
50
},
51
}
52
53
const signature = signTypedData_v4(
54
Buffer.from(privateKey, "hex"),
55
{ data: permitParams }
56
)
57
58
// The signature can now be used to execute the transaction
59
60
const { v, r, s } = fromRpcSig(signature)
61
62
await aTokenContract.methods
63
.permit({
64
owner,
65
spender,
66
value,
67
deadline,
68
v,
69
r,
70
s
71
})
72
.send()
73
.catch((e) => {
74
throw Error(`Error permitting: ${e.message}`)
75
})
Copied!

_nonces()

function _nonces(address owner) public
Returns the next valid nonce to submit when calling permit()

Methods

UNDERLYING_ASSET_ADDRESS()

function UNDERLYING_ASSET_ADDRESS()
Returns the underlying asset of the aToken.

RESERVE_TREASURY_ADDRESS()

function RESERVE_TREASURY_ADDRESS()
Returns the address of the aTokens reserve treasury.

POOL()

function POOL()
Returns the address of the associated LendingPool for the aToken.

scaledBalanceOf()

function scaledBalanceOf(address user)
Returns the scaled balance of user as a uint256.
The scaled balance is the balance of the underlying asset of the user (amount deposited), divided by the current liquidity index at the moment of the update.
I.e.
scaledBalance=amountDeposited/currentLiquidityIndexscaledBalance = amount Deposited / currentLiquidityIndex
This essentially 'marks' when a user has deposited in the reserve pool, and can be used to calculate the users current compounded aToken balance.
Example:
  • User A deposits 1000 DAI at the liquidity index of 1.1
  • User B deposits another amount into the same pool
  • The liquidity index is now 1.2.
  • Therefore to calculate User A's current compounded aToken balance, the reverse operation should be performed:
    aTokenBalance=scaledBalancecurrentLiquidityIndexaTokenBalance = scaledBalance * currentLiquidityIndex

getScaledUserBalanceAndSupply()

function getScaledUserBalanceAndSupply(address user)
Returns the scaled balance of userand the principal total supply.

Return values

Type
Description
uint256
scaled balance of user
uint256
principal total supply

scaledTotalSupply()

function scaledTotalSupply()
Returns the scaled total supply of the aToken as uint256.
The scaled total supply is the sum of all the updated stored balances, divided by the reserve index at the moment of the update.
function isTransferAllowed(address user, uint256 amount)
Returns true if a transfer is allowed.
Specifically, a transfer will fail if the resulting Health Factor of user will end up being below 1.