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

WIP: unlendable deposits #907

Open
wants to merge 24 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 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
199 changes: 194 additions & 5 deletions mango_v4.json
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,75 @@
}
]
},
{
"name": "tokenCreatePosition",
"accounts": [
{
"name": "group",
"isMut": false,
"isSigner": false
},
{
"name": "account",
"isMut": true,
"isSigner": false,
"relations": [
"group"
]
},
{
"name": "owner",
"isMut": false,
"isSigner": true
},
{
"name": "bank",
"isMut": true,
"isSigner": false,
"relations": [
"group"
]
}
],
"args": [
{
"name": "allowLending",
"type": "bool"
}
]
},
{
"name": "tokenClosePosition",
"accounts": [
{
"name": "group",
"isMut": false,
"isSigner": false
},
{
"name": "account",
"isMut": true,
"isSigner": false,
"relations": [
"group"
]
},
{
"name": "owner",
"isMut": false,
"isSigner": true
},
{
"name": "bank",
"isMut": true,
"isSigner": false,
"relations": [
"group"
]
}
],
"args": []
},
{
"name": "tokenDeposit",
"accounts": [
Expand Down Expand Up @@ -7711,12 +7780,28 @@
],
"type": "f32"
},
{
"name": "padding2",
"type": {
"array": [
"u8",
4
]
}
},
{
"name": "unlendableDeposits",
"docs": [
"The sum of native tokens in unlendable token positions"
],
"type": "u64"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
1900
1888
]
}
}
Expand Down Expand Up @@ -9218,7 +9303,8 @@
{
"name": "indexedPosition",
"docs": [
"The deposit_index (if positive) or borrow_index (if negative) scaled position"
"The token position, scaled with the deposit_index (if positive) or borrow_index (if negative)",
"to get the lendable/borrowed native token amount"
],
"type": {
"defined": "I80F48"
Expand All @@ -9227,7 +9313,7 @@
{
"name": "tokenIndex",
"docs": [
"index into Group.tokens"
"index the token is registered with, same as in Bank and MintInfo"
],
"type": "u16"
},
Expand All @@ -9238,12 +9324,25 @@
],
"type": "u16"
},
{
"name": "disableLending",
"docs": [
"set to 1 when these deposits may not be lent out",
"",
"This has consequences:",
"- only deposits possible, no borrows (also implying no perps with that settle token)",
"- not accounted for in Bank.indexed_deposits,",
"- instead tracked in Bank.unlendable_deposits (to ensure the vault always has them)",
"- indexed_position stays 0, instead use unlendable_deposits a straight native token amount"
],
"type": "u8"
},
{
"name": "padding",
"type": {
"array": [
"u8",
4
3
]
}
},
Expand All @@ -9261,12 +9360,21 @@
"name": "cumulativeBorrowInterest",
"type": "f64"
},
{
"name": "unlendableDeposits",
"docs": [
"deposited unlendable native token amount",
"",
"When this is set, indexed_position is always zero"
],
"type": "u64"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
128
120
]
}
}
Expand Down Expand Up @@ -11090,6 +11198,12 @@
},
{
"name": "HealthCheck"
},
{
"name": "TokenCreatePosition"
},
{
"name": "TokenClosePosition"
}
]
}
Expand Down Expand Up @@ -11540,6 +11654,51 @@
}
]
},
{
"name": "TokenBalanceLogV2",
"fields": [
{
"name": "mangoGroup",
"type": "publicKey",
"index": false
},
{
"name": "mangoAccount",
"type": "publicKey",
"index": false
},
{
"name": "tokenIndex",
"type": "u16",
"index": false
},
{
"name": "indexedPosition",
"type": "i128",
"index": false
},
{
"name": "depositIndex",
"type": "i128",
"index": false
},
{
"name": "borrowIndex",
"type": "i128",
"index": false
},
{
"name": "nativePosition",
"type": "u64",
"index": false
},
{
"name": "allowLending",
"type": "bool",
"index": false
}
]
},
{
"name": "FlashLoanLog",
"fields": [
Expand Down Expand Up @@ -14447,6 +14606,36 @@
"code": 6072,
"name": "InvalidHealth",
"msg": "invalid health"
},
{
"code": 6073,
"name": "PerpSettleTokenPositionMustSupportBorrows",
"msg": "perp settle token position does not support borrows"
},
{
"code": 6074,
"name": "TokenPositionIsInUse",
"msg": "the token position is still in use by another position"
},
{
"code": 6075,
"name": "TokenPositionBalanceNotZero",
"msg": "the token position has a non-zero balance"
},
{
"code": 6076,
"name": "TokenPositionWithDifferentSettingAlreadyExists",
"msg": "cannot have a lending and no-lending token position at the same time"
},
{
"code": 6077,
"name": "UnlendableTokenPositionCannotBeNegative",
"msg": "cannot borrow from unlendable token position"
},
{
"code": 6078,
"name": "TokenConditionalSwapUnsupportedUnlendablePosition",
"msg": "token conditional swaps currently don't support unlendable positions"
}
]
}
2 changes: 2 additions & 0 deletions programs/mango-v4/src/accounts_ix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub use token_conditional_swap_cancel::*;
pub use token_conditional_swap_create::*;
pub use token_conditional_swap_start::*;
pub use token_conditional_swap_trigger::*;
pub use token_create_or_close_position::*;
pub use token_deposit::*;
pub use token_deregister::*;
pub use token_edit::*;
Expand Down Expand Up @@ -146,6 +147,7 @@ mod token_conditional_swap_cancel;
mod token_conditional_swap_create;
mod token_conditional_swap_start;
mod token_conditional_swap_trigger;
mod token_create_or_close_position;
mod token_deposit;
mod token_deregister;
mod token_edit;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use anchor_lang::prelude::*;

use crate::error::*;
use crate::state::*;

#[derive(Accounts)]
pub struct TokenCreateOrClosePosition<'info> {
// ix gate checking happens in instruction code
pub group: AccountLoader<'info, Group>,

#[account(
mut,
has_one = group,
constraint = account.load()?.is_operational() @ MangoError::AccountIsFrozen,
constraint = account.load()?.is_owner_or_delegate(owner.key()),
)]
pub account: AccountLoader<'info, MangoAccountFixed>,
pub owner: Signer<'info>,

#[account(
mut,
has_one = group,
)]
pub bank: AccountLoader<'info, Bank>,
}
12 changes: 12 additions & 0 deletions programs/mango-v4/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ pub enum MangoError {
InvalidSequenceNumber,
#[msg("invalid health")]
InvalidHealth,
#[msg("perp settle token position does not support borrows")]
PerpSettleTokenPositionMustSupportBorrows,
#[msg("the token position is still in use by another position")]
TokenPositionIsInUse,
#[msg("the token position has a non-zero balance")]
TokenPositionBalanceNotZero,
#[msg("cannot have a lending and no-lending token position at the same time")]
TokenPositionWithDifferentSettingAlreadyExists,
#[msg("cannot borrow from unlendable token position")]
UnlendableTokenPositionCannotBeNegative,
#[msg("token conditional swaps currently don't support unlendable positions")]
TokenConditionalSwapUnsupportedUnlendablePosition,
}

impl MangoError {
Expand Down
Loading
Loading