Lending Core
The core entry of NAVI Protocol.
Use lending_core::incentive_v3 for the deposits, withdrawals, loans, repayments, liquidations and reward claims below.
Entry Functions
entry_deposit
This function allows a user to deposit a specified amount of a given coin type into a lending pool. The deposit amount is added to the pool's balance, and a deposit event is emitted.
public entry fun entry_deposit<CoinType>(
clock: &Clock,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
deposit_coin: Coin<CoinType>,
amount: u64,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
ctx: &mut TxContext
) {}entry_withdraw_v2
This function allows a user to withdraw a specified amount from a lending pool. The withdrawal amount is deducted from the pool's balance, and a withdrawal event is emitted.
public entry fun entry_withdraw_v2<CoinType>(
clock: &Clock,
oracle: &PriceOracle,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
amount: u64,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
system_state: &mut SuiSystemState,
ctx: &mut TxContext
) {}entry_borrow_v2
This function allows a user to borrow a specified amount from a lending pool. The borrowed amount is deducted from the pool's balance, and a borrow event is emitted.
public entry fun entry_borrow_v2<CoinType>(
clock: &Clock,
oracle: &PriceOracle,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
amount: u64,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
system_state: &mut SuiSystemState,
ctx: &mut TxContext
) {}entry_repay
This function allows a user to repay a specified loan amount. The repayment amount is added to the pool's balance, and a repay event is emitted.
public entry fun entry_repay<CoinType>(
clock: &Clock,
oracle: &PriceOracle,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
repay_coin: Coin<CoinType>,
amount: u64,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
ctx: &mut TxContext
) {}claim_reward_entry
This function allows users to claim rewards in a specified reward coin. Rewards are distributed from the bonus pool and a claim reward event is emitted.
public entry fun claim_reward_entry<RewardCoinType>(
clock: &Clock,
incentive: &mut Incentive,
storage: &mut Storage,
reward_fund: &mut RewardFund<RewardCoinType>,
coin_types: vector<String>,
rule_ids: vector<address>,
ctx: &mut TxContext
) {}entry_liquidation_v2
This function is used to liquidate a loan when a borrower's collateral is insufficient to cover their debt. The liquidation process repays the debt, returning any excess amount to the sender. A liquidation call event is emitted.
public entry fun entry_liquidation_v2<DebtCoinType, CollateralCoinType>(
clock: &Clock,
oracle: &PriceOracle,
storage: &mut Storage,
debt_asset: u8,
debt_pool: &mut Pool<DebtCoinType>,
debt_coin: Coin<DebtCoinType>,
collateral_asset: u8,
collateral_pool: &mut Pool<CollateralCoinType>,
liquidate_user: address,
liquidate_amount: u64,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
system_state: &mut SuiSystemState,
ctx: &mut TxContext
) {}Non-entry Functions
deposit_with_account_cap
This function is similar to a standard deposit but includes an account cap limitation. The deposit amount is added to the pool's balance, and a deposit event is emitted.
public fun deposit_with_account_cap<CoinType>(
clock: &Clock,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
deposit_coin: Coin<CoinType>,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
account_cap: &AccountCap
) {}withdraw_v2
This function allows other contracts to withdraw a specified amount from a lending pool. The balance object is returned, and a withdrawal event is emitted.
public fun withdraw_v2<CoinType>(
clock: &Clock,
oracle: &PriceOracle,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
amount: u64,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
system_state: &mut SuiSystemState,
ctx: &mut TxContext
): Balance<CoinType> {}withdraw_with_account_cap_v2
This function is similar to a standard withdraw but includes an account cap limitation. The balance object is returned, and a withdrawal event is emitted.
public fun withdraw_with_account_cap_v2<CoinType>(
clock: &Clock,
oracle: &PriceOracle,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
amount: u64,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
account_cap: &AccountCap,
system_state: &mut SuiSystemState,
ctx: &mut TxContext
): Balance<CoinType> {}borrow_v2
This function allows other contracts to borrow a specified amount from a lending pool. The balance object is returned, and a borrow event is emitted.
public fun borrow_v2<CoinType>(
clock: &Clock,
oracle: &PriceOracle,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
amount: u64,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
system_state: &mut SuiSystemState,
ctx: &mut TxContext
): Balance<CoinType> {}borrow_with_account_cap_v2
This function is similar to a standard borrow but includes an account cap limitation. The balance object is returned, and the borrow event is emitted.
public fun borrow_with_account_cap_v2<CoinType>(
clock: &Clock,
oracle: &PriceOracle,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
amount: u64,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
account_cap: &AccountCap,
system_state: &mut SuiSystemState,
ctx: &mut TxContext
): Balance<CoinType> {}repay
This function allows other contracts to repay a specified amount of a loan. The balance object is returned, and a repay event is emitted.
public fun repay<CoinType>(
clock: &Clock,
oracle: &PriceOracle,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
repay_coin: Coin<CoinType>,
amount: u64,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
ctx: &mut TxContext
): Balance<CoinType> {}repay_with_account_cap
Pass the repayment coin for the cap's position. Its value sets the repayment amount. Handle the returned surplus balance.
public fun repay_with_account_cap<CoinType>(
clock: &Clock,
oracle: &PriceOracle,
storage: &mut Storage,
pool: &mut Pool<CoinType>,
asset: u8,
repay_coin: Coin<CoinType>,
incentive_v2: &mut IncentiveV2,
incentive_v3: &mut Incentive,
account_cap: &AccountCap
): Balance<CoinType> {}claim_reward
Quick Reward Claiming Using the SDK
We recommend using the SDK to claim rewards because the SDK will automatically claim all available rewards and perform off-chain indexing to optimize gas usage.
SDK Benefits:
- Off-chain indexing: Retrieves relevant rules without incurring on-chain gas costs.
- Optimized filtering: Selects only the necessary rule IDs before executing on-chain transactions.
For users who want to implement customized and native reward claiming, we provide the following instructions:
Step-by-Step Process
- Retrieve Claimable Rewards
- Use
get_user_claimable_rewardsto fetch claimable reward information. - Parse the response using
parse_claimable_rewardsto identify the reward coin, asset coin types and rule IDs.
get_user_claimable_rewards
This function retrieves the claimable rewards for a user, returning detailed reward information.
struct ClaimableReward has copy, drop {
asset_coin_type: String,
reward_coin_type: String,
user_claimable_reward: u256,
user_claimed_reward: u256,
rule_ids: vector<address>,
}
public fun get_user_claimable_rewards(
clock: &Clock,
storage: &mut Storage,
incentive: &Incentive,
user: address
): vector<ClaimableReward>parse_claimable_rewards
This function parses the claimable rewards to return the asset coin types, reward coin types, user total rewards, user claimed rewards, and rule ids.
public fun parse_claimable_rewards(claimable_rewards: vector<ClaimableReward>): (
vector<String>, // asset coin types
vector<String>, // reward coin types
vector<u256>, // user total rewards
vector<u256>, // user claimed rewards
vector<vector<address>> // rule ids
)- The
coin_typesare derived from the asset tokens involved. - The
rule_idsare extracted from the parsed claimable rewards.
Use the same market for Storage, Incentive and RewardFund<RewardCoinType>. Resolve the fund by reward coin type in that market's configuration.
import { getConfig, normalizeCoinType } from '@naviprotocol/lending';
async function getRewardObjects(market: string, rewardCoinType: string) {
const config = await getConfig({ market });
const rewardFund = config.rewardFunds[normalizeCoinType(rewardCoinType)];
if (!rewardFund) throw new Error('No reward fund for this coin in the selected market');
return { storage: config.storage, incentive: config.incentiveV3, rewardFund };
}claim_reward
Use the returned balance in another Move call or convert it to a coin. The entry variant transfers the reward to the sender.
public fun claim_reward<RewardCoinType>(
clock: &Clock,
incentive: &mut Incentive,
storage: &mut Storage,
reward_fund: &mut RewardFund<RewardCoinType>,
coin_types: vector<String>,
rule_ids: vector<address>,
ctx: &mut TxContext
): Balance<RewardCoinType>claim_reward_with_account_cap
This function is similar to a standard claim rewards but includes an account cap limitation. Rewards are distributed from the bonus pool, and a claim reward event is emitted.
public fun claim_reward_with_account_cap<RewardCoinType>(
clock: &Clock,
incentive: &mut Incentive,
storage: &mut Storage,
reward_fund: &mut RewardFund<RewardCoinType>,
coin_types: vector<String>,
rule_ids: vector<address>,
account_cap: &AccountCap
): Balance<RewardCoinType>Full Claim Contract Demo
The example claims rewards in one coin for the sender. Each rule ID needs a matching asset coin type, even when several rules apply to the same asset.
use lending_core::incentive_v3::{Self, Incentive, RewardFund};
use lending_core::storage::Storage;
use std::ascii::String;
use std::type_name;
use sui::balance::Balance;
use sui::clock::Clock;
use sui::tx_context::{Self, TxContext};
public fun claim_for_sender<RewardCoinType>(
clock: &Clock,
storage: &mut Storage,
incentive: &mut Incentive,
reward_fund: &mut RewardFund<RewardCoinType>,
ctx: &mut TxContext,
): Balance<RewardCoinType> {
let rewards = incentive_v3::get_user_claimable_rewards(
clock, storage, incentive, tx_context::sender(ctx),
);
let (assets, coins, totals, claimed, rules) =
incentive_v3::parse_claimable_rewards(rewards);
let target = type_name::into_string(type_name::get<RewardCoinType>());
let mut asset_types = vector::empty<String>();
let mut rule_ids = vector::empty<address>();
let mut i = 0;
while (i < vector::length(&assets)) {
if (vector::borrow(&coins, i) == &target &&
*vector::borrow(&totals, i) > *vector::borrow(&claimed, i)) {
let asset = *vector::borrow(&assets, i);
let asset_rules = vector::borrow(&rules, i);
let mut j = 0;
while (j < vector::length(asset_rules)) {
vector::push_back(&mut asset_types, asset);
vector::push_back(&mut rule_ids, *vector::borrow(asset_rules, j));
j = j + 1;
};
};
i = i + 1;
};
incentive_v3::claim_reward<RewardCoinType>(
clock, incentive, storage, reward_fund, asset_types, rule_ids, ctx,
)
}