Skip to content

Commit ba2de00

Browse files
committed
feat: close position accounts (#186)
1 parent 798e103 commit ba2de00

7 files changed

Lines changed: 165 additions & 8 deletions

File tree

idl/drip.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,37 @@ export type Drip = {
839839
}
840840
],
841841
"args": []
842+
},
843+
{
844+
"name": "adminClosePositionAccount",
845+
"accounts": [
846+
{
847+
"name": "admin",
848+
"isMut": false,
849+
"isSigner": true
850+
},
851+
{
852+
"name": "vaultProtoConfig",
853+
"isMut": false,
854+
"isSigner": false
855+
},
856+
{
857+
"name": "vault",
858+
"isMut": false,
859+
"isSigner": false
860+
},
861+
{
862+
"name": "position",
863+
"isMut": true,
864+
"isSigner": false
865+
},
866+
{
867+
"name": "solDestination",
868+
"isMut": true,
869+
"isSigner": false
870+
}
871+
],
872+
"args": []
842873
}
843874
],
844875
"accounts": [
@@ -1286,6 +1317,11 @@ export type Drip = {
12861317
"code": 6028,
12871318
"name": "InvalidSolDestination",
12881319
"msg": "Invalid sol_destination"
1320+
},
1321+
{
1322+
"code": 6029,
1323+
"name": "PositionIsNotClosed",
1324+
"msg": "Position is not closed"
12891325
}
12901326
]
12911327
};
@@ -2131,6 +2167,37 @@ export const IDL: Drip = {
21312167
}
21322168
],
21332169
"args": []
2170+
},
2171+
{
2172+
"name": "adminClosePositionAccount",
2173+
"accounts": [
2174+
{
2175+
"name": "admin",
2176+
"isMut": false,
2177+
"isSigner": true
2178+
},
2179+
{
2180+
"name": "vaultProtoConfig",
2181+
"isMut": false,
2182+
"isSigner": false
2183+
},
2184+
{
2185+
"name": "vault",
2186+
"isMut": false,
2187+
"isSigner": false
2188+
},
2189+
{
2190+
"name": "position",
2191+
"isMut": true,
2192+
"isSigner": false
2193+
},
2194+
{
2195+
"name": "solDestination",
2196+
"isMut": true,
2197+
"isSigner": false
2198+
}
2199+
],
2200+
"args": []
21342201
}
21352202
],
21362203
"accounts": [
@@ -2578,6 +2645,11 @@ export const IDL: Drip = {
25782645
"code": 6028,
25792646
"name": "InvalidSolDestination",
25802647
"msg": "Invalid sol_destination"
2648+
},
2649+
{
2650+
"code": 6029,
2651+
"name": "PositionIsNotClosed",
2652+
"msg": "Position is not closed"
25812653
}
25822654
]
25832655
};

idl/idl.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,37 @@
839839
}
840840
],
841841
"args": []
842+
},
843+
{
844+
"name": "adminClosePositionAccount",
845+
"accounts": [
846+
{
847+
"name": "admin",
848+
"isMut": false,
849+
"isSigner": true
850+
},
851+
{
852+
"name": "vaultProtoConfig",
853+
"isMut": false,
854+
"isSigner": false
855+
},
856+
{
857+
"name": "vault",
858+
"isMut": false,
859+
"isSigner": false
860+
},
861+
{
862+
"name": "position",
863+
"isMut": true,
864+
"isSigner": false
865+
},
866+
{
867+
"name": "solDestination",
868+
"isMut": true,
869+
"isSigner": false
870+
}
871+
],
872+
"args": []
842873
}
843874
],
844875
"accounts": [
@@ -1286,6 +1317,11 @@
12861317
"code": 6028,
12871318
"name": "InvalidSolDestination",
12881319
"msg": "Invalid sol_destination"
1320+
},
1321+
{
1322+
"code": 6029,
1323+
"name": "PositionIsNotClosed",
1324+
"msg": "Position is not closed"
12891325
}
12901326
]
12911327
}

programs/drip/src/actions/admin.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::errors::DripError;
2-
use crate::instruction_accounts::{InitializeVaultAccountsBumps, WithdrawAAccounts};
2+
use crate::instruction_accounts::{
3+
ClosePositionAccountAccounts, InitializeVaultAccountsBumps, WithdrawAAccounts,
4+
};
35
use crate::interactions::executor::CpiExecutor;
46
use crate::interactions::transfer_token::TransferToken;
57
use crate::state::{
@@ -29,6 +31,9 @@ pub enum Admin<'a, 'info> {
2931
WithdrawA {
3032
accounts: &'a mut WithdrawAAccounts<'info>,
3133
},
34+
ClosePositionAccount {
35+
accounts: &'a mut ClosePositionAccountAccounts<'info>,
36+
},
3237
}
3338

3439
impl<'a, 'info> Validatable for Admin<'a, 'info> {
@@ -122,6 +127,24 @@ impl<'a, 'info> Validatable for Admin<'a, 'info> {
122127
DripError::VaultTokenAAccountIsEmpty
123128
);
124129
}
130+
Admin::ClosePositionAccount { accounts } => {
131+
validate!(
132+
accounts.admin.key() == accounts.vault_proto_config.admin,
133+
DripError::SignerIsNotAdmin
134+
);
135+
136+
validate!(
137+
accounts.vault_proto_config.key() == accounts.vault.proto_config,
138+
DripError::InvalidVaultProtoConfigReference
139+
);
140+
141+
validate!(
142+
accounts.vault.key() == accounts.position.vault.key(),
143+
DripError::InvalidVaultReference
144+
);
145+
146+
validate!(accounts.position.is_closed, DripError::PositionIsNotClosed);
147+
}
125148
}
126149

127150
Ok(())
@@ -168,6 +191,11 @@ impl<'a, 'info> Executable for Admin<'a, 'info> {
168191
let signer: &Vault = &accounts.vault;
169192
cpi_executor.execute_all(vec![&Some(&transfer_a_to_admin)], signer)?;
170193
}
194+
Admin::ClosePositionAccount { accounts } => {
195+
accounts
196+
.position
197+
.close(accounts.sol_destination.to_account_info())?;
198+
}
171199
}
172200

173201
Ok(())

programs/drip/src/errors/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ pub enum DripError {
6161
VaultTokenAAccountIsEmpty,
6262
#[msg("Invalid sol_destination")]
6363
InvalidSolDestination,
64+
#[msg("Position is not closed")]
65+
PositionIsNotClosed,
6466
}

programs/drip/src/instruction_accounts/admin.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::state::{Vault, VaultProtoConfig};
1+
use crate::state::{Position, Vault, VaultProtoConfig};
22
use anchor_lang::prelude::*;
33
use anchor_spl::associated_token::AssociatedToken;
44
use anchor_spl::token::{Mint, Token, TokenAccount};
@@ -104,3 +104,19 @@ pub struct WithdrawAAccounts<'info> {
104104

105105
pub token_program: Program<'info, Token>,
106106
}
107+
108+
#[derive(Accounts)]
109+
pub struct ClosePositionAccountAccounts<'info> {
110+
pub admin: Signer<'info>,
111+
112+
pub vault_proto_config: Account<'info, VaultProtoConfig>,
113+
114+
pub vault: Account<'info, Vault>,
115+
116+
#[account(mut)]
117+
pub position: Account<'info, Position>,
118+
119+
#[account(mut)]
120+
/// CHECK: We don't care what this account is
121+
pub sol_destination: AccountInfo<'info>,
122+
}

programs/drip/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ pub mod drip {
113113
accounts: ctx.accounts,
114114
})
115115
}
116+
117+
pub fn admin_close_position_account(ctx: Context<ClosePositionAccountAccounts>) -> Result<()> {
118+
handle_action(Admin::ClosePositionAccount {
119+
accounts: ctx.accounts,
120+
})
121+
}
116122
}
117123

118124
fn handle_action(action: impl Validatable + Executable) -> Result<()> {

programs/drip/src/state/position.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ pub struct Position {
2121
pub number_of_swaps: u64, // 8
2222
// deposit_amount_token_a / number_of_swaps
2323
pub periodic_drip_amount: u64, // 8
24-
pub is_closed: bool, // 1
25-
pub bump: u8, // 1
24+
// DEPRECATED FIELD: Position accounts are closed now instead of being marked closed
25+
pub is_closed: bool, // 1
26+
pub bump: u8, // 1
2627
}
2728

2829
impl Position {
@@ -63,10 +64,6 @@ impl Position {
6364
pub fn increase_withdrawn_amount(&mut self, amount: u64) {
6465
self.withdrawn_token_b_amount = self.withdrawn_token_b_amount.checked_add(amount).unwrap();
6566
}
66-
67-
pub fn close(&mut self) {
68-
self.is_closed = true;
69-
}
7067
}
7168

7269
test_account_size!(Position);

0 commit comments

Comments
 (0)