Smart contract interactions

Kiln DeFi Vault smart contracts respect the ERC4626 vault, which is very straightforward to integrate. When the user deposits tokens in the vault, it receives shares representing its position. For better UX, we recommend to abstract any assets <> shares conversion (if needed) from the user and always show the asset amounts on the UI and not the shares amounts.

Here is the ABI of the Vault contract:

1. Depositing

Before depositing X assets, you must make sure the user approved the vault to spend X on its behalf on the asset contract.

To check the allowance, call the allowance(address owner, address spender) → uint256 function on the ERC20 asset contract.

To increase the allowance to the amount the user wants to deposit, use approve(address spender, uint256 amount) → bool function on the ERC20 asset contract.

⚠ī¸ ONLY MAKE THE USER APPROVE THE AMOUNT IT WANTS TO DEPOSIT TO THE VAULT

To deposit an amount of the vault's ERC20, transaction sender as to call the deposit(uint256 assets, address receiver) function where assets is the amount of ERC20 to deposit in the strategy and receiver the address that should own the deposited position.

(optional) If you need to compute the amount of shares the user will receive before doing a deposit, you can preview the amount by calling previewDeposit(uint256 assets) where assets is the amount of ERC20 to deposit.

2. Get the balance in assets

Using the reporting API
curl https://api.kiln.fi/v1/defi/v1/stakes?users=$ADDRESS1&vaults=$VAULT1,$VAULT2 \
   -H "Authorization: Bearer $API_TOKEN"

200
{
  "stakes": [
    {
      "current_balance": 1880000,
      "asset": "0x94a9d9ac8a22534e3faca9f4e7f2e2cf85d5e4c8",
      "total_rewards": 42000,
      "asset_symbol": "USDC",
      "total_deposited_amount": 1980000,
      "total_withdrawn_amount": 100000,
      "vault": "0x9aB5F9101a3C1B868e2c422E294cc2ee685551D5",
      "chain": "ethereum"
    },
    {
      "current_balance": 1880000,
      "asset": "0x94a9d9ac8a22534e3faca9f4e7f2e2cf85d5e4c8",
      "total_rewards": 1330,
      "asset_symbol": "USDC",
      "total_deposited_amount": 1980000,
      "total_withdrawn_amount": 100000,
      "vault": "0x8aB6F9101a3C1B868e2c422E294cc2ee686662Ab",
      "chain": "ethereum"
    }
  ]
}
Using contract view functions
  1. Call the balanceOf(address owner) function where owner is the address of the position owner. This will return the position balance in shares.

  2. Call the previewRedeem(uint256 shares) function where shares is the amount of shares to redeem for ERC20. This will return the amount of ERC20 the position is worth.

3. Exiting a position

Case 1: exit part of a position

In this case we expect the user to input the amount of ERC20 it wants to withdraw and then call the withdraw(uint256 assets, address receiver, address owner) where assets is the amount of ERC20 to withdraw, receiver the address that will receive the redeemed ERC20 and owner the address of the position sender (the same as the tx sender).

Case 2: exit a position fully

To exit all the position, the best way is to get the shares amount of the user position using:

  • the balanceOf(address owner) function where owner is the address of the position owner.

  • the redeem(uint256 shares, address receiver, address owner) where shares is the shares amount, receiver the address that will receive the redeemed ERC20 and owner the address of the position sender (the same as the tx sender).

Using the withdraw instead of the redeem function for this may cause the user to not exit all its position (as shares <> assets rate might change between the input and the actual block the transaction is included in).

Last updated