Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/create loan offer #54

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions programs/enso-lending/src/common/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub const LEND_OFFER_ACCOUNT_SEED: &[u8] = b"lend_offer";
pub const LOAN_OFFER_ACCOUNT_SEED: &[u8] = b"loan_offer";

pub const OPERATE_SYSTEM_PUBKEY: &str = "opty8HWBKX3wW8c9qMPkmB4xnrCpMWWmQwqq7yGzmr4";
pub const HOT_WALLET_PUBKEY: &str = "Hot7zcvBTa3NybAnKrKtjcW1yJcoDWao39ZAoBn4mfPu";

pub const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds

Expand Down
4 changes: 3 additions & 1 deletion programs/enso-lending/src/common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum LoanOfferError {
HealthRatioLimit,
#[msg("Loan offer expired")]
LoanOfferExpired,
#[msg("Invalid hot wallet account")]
InvalidHotWallet,
#[msg("Invalid operator system account")]
InvalidSystem,
#[msg("Invalid borrower")]
Expand All @@ -73,7 +75,7 @@ pub enum RepayOfferError {
#[msg("Not enough assets")]
NotEnoughAmount,
#[msg("Loan offer is not available")]
LoanOfferIsNotAvailable,
LoanOfferIsNotAvailable,
#[msg("Invalid lend amount")]
InvalidLendAmount,
#[msg("Loan offer not belong to borrower")]
Expand Down
10 changes: 2 additions & 8 deletions programs/enso-lending/src/contexts/create_loan_offer_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ pub struct CreateLoanOfferNative<'info> {
bump = setting_account.bump
)]
pub setting_account: Account<'info, SettingAccount>,
/// CHECK: This is the account used to receive the collateral amount
#[account(mut)]
pub receiver: AccountInfo<'info>,
pub token_program: Program<'info, Token>,
pub system_program: Program<'info, System>,
}
Expand All @@ -85,9 +82,6 @@ impl<'info> CreateLoanOfferNative<'info> {
collateral_amount: u64
) -> Result<()> {
self.validate_initialize_loan_offer(collateral_amount)?;
if self.receiver.key() != self.setting_account.receiver.key() {
return err!(LoanOfferError::InvalidReceiver);
}

self.deposit_collateral(collateral_amount)?;

Expand Down Expand Up @@ -166,15 +160,15 @@ impl<'info> CreateLoanOfferNative<'info> {
fn deposit_collateral(&self, collateral_amount: u64) -> Result<()> {
let transfer_instruction = system_instruction::transfer(
&self.borrower.key(),
&self.receiver.key(),
&self.loan_offer.key(),
collateral_amount
);

invoke_signed(
&transfer_instruction,
&[
self.borrower.to_account_info(),
self.receiver.to_account_info(),
self.loan_offer.to_account_info(),
self.system_program.to_account_info()
],
&[],
Expand Down
17 changes: 15 additions & 2 deletions programs/enso-lending/src/contexts/liquidate_collateral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ use std::str::FromStr;

use anchor_lang::prelude::*;

use crate::{common::constant::{ENSO_SEED, LOAN_OFFER_ACCOUNT_SEED, OPERATE_SYSTEM_PUBKEY}, LiquidatingCollateralEvent, LoanOfferAccount, LoanOfferError, LoanOfferStatus};
use crate::{
common::constant::{ENSO_SEED, LOAN_OFFER_ACCOUNT_SEED, OPERATE_SYSTEM_PUBKEY},
LiquidatingCollateralEvent, LoanOfferAccount, LoanOfferError, LoanOfferStatus, HOT_WALLET_PUBKEY
};

#[derive(Accounts)]
#[instruction(offer_id: String)]
pub struct LiquidateCollateral<'info> {
#[account(mut)]
pub system: Signer<'info>,
/// CHECK: This is the account used to make a seeds
pub borrower: AccountInfo<'info>,
pando-dreamer marked this conversation as resolved.
Show resolved Hide resolved
pub borrower: UncheckedAccount<'info>,
#[account(
mut,
seeds = [
Expand All @@ -23,6 +26,9 @@ pub struct LiquidateCollateral<'info> {
bump
)]
pub loan_offer: Account<'info, LoanOfferAccount>,
#[account(mut)]
/// CHECK: This is the account used to received the collateral for liquidate
pub hot_wallet: UncheckedAccount<'info>
}

impl<'info> LiquidateCollateral<'info> {
Expand All @@ -31,6 +37,10 @@ impl<'info> LiquidateCollateral<'info> {
return err!(LoanOfferError::InvalidSystem);
}

if self.hot_wallet.key() != Pubkey::from_str(HOT_WALLET_PUBKEY).unwrap() {
pando-dreamer marked this conversation as resolved.
Show resolved Hide resolved
return err!(LoanOfferError::InvalidHotWallet);
}

let loan_offer = &mut self.loan_offer;
if loan_offer.status != LoanOfferStatus::FundTransferred {
return err!(LoanOfferError::InvalidOfferStatus);
Expand All @@ -40,6 +50,9 @@ impl<'info> LiquidateCollateral<'info> {
loan_offer.liquidating_at = Some(liquidating_at);
loan_offer.status = LoanOfferStatus::Liquidating;

self.loan_offer.sub_lamports(self.loan_offer.collateral_amount)?;
self.hot_wallet.add_lamports(self.loan_offer.collateral_amount)?;

Ok(())
}

Expand Down
26 changes: 14 additions & 12 deletions programs/enso-lending/src/contexts/repay_loan_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,28 @@ impl<'info> RepayLoanOffer<'info> {
}

self.deposit(total_amount)?;
self.loan_offer.status = LoanOfferStatus::Repay;

self.loan_offer.sub_lamports(self.loan_offer.collateral_amount)?;
self.borrower.add_lamports(self.loan_offer.collateral_amount)?;
self.loan_offer.status = LoanOfferStatus::BorrowerPaid;

self.emit_event_repay_loan_offer( "repay_loan_offer".to_string(), self.loan_offer.offer_id.clone(), total_amount)?;

Ok(())
}

pub fn deposit(&mut self, repay_amount: u64) -> Result<()> {
let cpi_accounts = TransferChecked {
from: self.loan_ata_asset.to_account_info(),
mint: self.mint_asset.to_account_info(),
to: self.hot_wallet_ata.to_account_info(),
authority: self.borrower.to_account_info(),
};

let cpi_ctx = CpiContext::new(self.token_program.to_account_info(), cpi_accounts);

transfer_checked(
self.into_deposit_context(),
cpi_ctx,
repay_amount,
self.mint_asset.decimals,
)
Expand All @@ -107,16 +119,6 @@ impl<'info> RepayLoanOffer<'info> {
Ok(())
}

fn into_deposit_context(&self) -> CpiContext<'_, '_, '_, 'info, TransferChecked<'info>> {
let cpi_accounts = TransferChecked {
from: self.loan_ata_asset.to_account_info(),
mint: self.mint_asset.to_account_info(),
to: self.hot_wallet_ata.to_account_info(),
authority: self.borrower.to_account_info(),
};
CpiContext::new(self.token_program.to_account_info(), cpi_accounts)
}

pub fn emit_event_repay_loan_offer(&mut self, label: String, loan_offer_id: String, repay_amount: u64) -> Result<()> {
emit!(RepayLoanOfferEvent {
borrower: self.borrower.key(),
Expand Down
Loading