Skip to content

Commit

Permalink
werd
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalu committed Aug 2, 2024
1 parent 060d22d commit 08e60b5
Show file tree
Hide file tree
Showing 10 changed files with 352 additions and 49 deletions.
47 changes: 45 additions & 2 deletions integration_tests/tests/fixtures/vault_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct VaultRoot {
pub vault_admin: Keypair,
}

#[derive(Debug)]
pub struct VaultStakerWithdrawTicketRoot {
pub base: Pubkey,
}
Expand Down Expand Up @@ -89,14 +90,35 @@ impl VaultProgramClient {

pub async fn get_vault_delegation_list(
&mut self,
account: &Pubkey,
vault_pubkey: &Pubkey,
) -> Result<VaultDelegationList, BanksClientError> {
let account = self.banks_client.get_account(*account).await?.unwrap();
let account =
VaultDelegationList::find_program_address(&jito_vault_program::id(), &vault_pubkey).0;
let account = self.banks_client.get_account(account).await?.unwrap();
Ok(VaultDelegationList::deserialize(
&mut account.data.as_slice(),
)?)
}

pub async fn get_vault_staker_withdraw_ticket(
&mut self,
vault: &Pubkey,
staker: &Pubkey,
base: &Pubkey,
) -> Result<VaultStakerWithdrawTicket, BanksClientError> {
let account = VaultStakerWithdrawTicket::find_program_address(
&jito_vault_program::id(),
vault,
staker,
base,
)
.0;
let account = self.banks_client.get_account(account).await?.unwrap();
Ok(VaultStakerWithdrawTicket::deserialize(
&mut account.data.as_slice(),
)?)
}

pub async fn get_vault_avs_slasher_ticket(
&mut self,
vault: &Pubkey,
Expand Down Expand Up @@ -634,6 +656,27 @@ impl VaultProgramClient {
})
}

pub async fn do_update_vault(&mut self, vault_pubkey: &Pubkey) -> Result<(), BanksClientError> {
let blockhash = self.banks_client.get_latest_blockhash().await?;

let vault = self.get_vault(vault_pubkey).await?;

self._process_transaction(&Transaction::new_signed_with_payer(
&[jito_vault_sdk::update_vault(
&jito_vault_program::id(),
&Config::find_program_address(&jito_vault_program::id()).0,
vault_pubkey,
&VaultDelegationList::find_program_address(&jito_vault_program::id(), vault_pubkey)
.0,
&get_associated_token_address(vault_pubkey, &vault.supported_mint()),
)],
Some(&self.payer.pubkey()),
&[&self.payer],
blockhash,
))
.await
}

pub async fn enqueue_withdraw(
&mut self,
config: &Pubkey,
Expand Down
281 changes: 274 additions & 7 deletions integration_tests/tests/vault/enqueue_withdrawal.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::fixtures::fixture::TestBuilder;

Check warning on line 1 in integration_tests/tests/vault/enqueue_withdrawal.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/restaking/restaking/integration_tests/tests/vault/enqueue_withdrawal.rs
use crate::fixtures::vault_client::VaultStakerWithdrawTicketRoot;
use jito_vault_core::vault_staker_withdraw_ticket::VaultStakerWithdrawTicket;
use solana_sdk::signature::{Keypair, Signer};
use spl_associated_token_account::get_associated_token_address;

Check warning on line 6 in integration_tests/tests/vault/enqueue_withdrawal.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/restaking/restaking/integration_tests/tests/vault/enqueue_withdrawal.rs
use crate::fixtures::fixture::TestBuilder;

#[tokio::test]
async fn test_enqueue_withdrawal_success() {
async fn test_enqueue_withdraw_more_than_staked_fails() {
let mut fixture = TestBuilder::new().await;

let mut vault_program_client = fixture.vault_program_client();
Expand Down Expand Up @@ -55,10 +56,276 @@ async fn test_enqueue_withdrawal_success() {
vault_program_client
.do_enqueue_withdraw(&vault_root, &depositor, 49_500)
.await
.unwrap_err();
}

#[tokio::test]
async fn test_enqueue_withdraw_one_to_one_success() {
let mut fixture = TestBuilder::new().await;

let mut vault_program_client = fixture.vault_program_client();
let mut restaking_pool_client = fixture.restaking_program_client();

let (_vault_config_admin, vault_root) =
vault_program_client.setup_vault(100, 100).await.unwrap();

let _restaking_config_admin = restaking_pool_client.setup_config().await.unwrap();

let operator_root = restaking_pool_client.setup_operator().await.unwrap();
let avs_root = restaking_pool_client.setup_avs().await.unwrap();

restaking_pool_client
.operator_avs_opt_in(&operator_root, &avs_root.avs_pubkey)
.await
.unwrap();
restaking_pool_client
.avs_operator_opt_in(&avs_root, &operator_root.operator_pubkey)
.await
.unwrap();

restaking_pool_client
.avs_vault_opt_in(&avs_root, &vault_root.vault_pubkey)
.await
.unwrap();
restaking_pool_client
.operator_vault_opt_in(&operator_root, &vault_root.vault_pubkey)
.await
.unwrap();

// let withdrawal = vault_program_client
// .enqueue_withdraw(&vault_root, &depositor, &depositor_lrt_token_account, 1000)
// .await
// .unwrap();
vault_program_client
.vault_avs_opt_in(&vault_root, &avs_root.avs_pubkey)
.await
.unwrap();
vault_program_client
.vault_operator_opt_in(&vault_root, &operator_root.operator_pubkey)
.await
.unwrap();

let vault = vault_program_client
.get_vault(&vault_root.vault_pubkey)
.await
.unwrap();

let depositor = Keypair::new();
fixture.transfer(&depositor.pubkey(), 100.0).await.unwrap();
fixture
.mint_to(&vault.supported_mint(), &depositor.pubkey(), 100_000)
.await
.unwrap();

fixture
.create_ata(&vault.lrt_mint(), &depositor.pubkey())
.await
.unwrap();

vault_program_client
.mint_to(
&vault_root.vault_pubkey,
&vault.lrt_mint(),
&depositor,
&get_associated_token_address(&depositor.pubkey(), &vault.supported_mint()),
&get_associated_token_address(&vault_root.vault_pubkey, &vault.supported_mint()),
&get_associated_token_address(&depositor.pubkey(), &vault.lrt_mint()),
&get_associated_token_address(&vault.fee_owner(), &vault.lrt_mint()),
None,
100_000,
)
.await
.unwrap();

let vault_lrt_account = fixture
.get_token_account(&get_associated_token_address(
&depositor.pubkey(),
&vault.lrt_mint(),
))
.await
.unwrap();
assert_eq!(vault_lrt_account.amount, 99_000);

vault_program_client
.delegate(&vault_root, &operator_root.operator_pubkey, 100_000)
.await
.unwrap();

let vault_delegation_list = vault_program_client
.get_vault_delegation_list(&vault_root.vault_pubkey)
.await
.unwrap();

let delegation = vault_delegation_list.delegations().get(0).unwrap();
assert_eq!(delegation.staked_amount(), 100_000);
assert_eq!(delegation.delegated_security().unwrap(), 100_000);

let VaultStakerWithdrawTicketRoot { base } = vault_program_client
.do_enqueue_withdraw(&vault_root, &depositor, 99_000)
.await
.unwrap();

let vault_delegation_list = vault_program_client
.get_vault_delegation_list(&vault_root.vault_pubkey)
.await
.unwrap();

let delegation = vault_delegation_list.delegations().get(0).unwrap();
// this is 1,000 because 1% of the fee went to the vault fee account, the assets still staked
// are for the LRT in the fee account to unstake later
assert_eq!(delegation.staked_amount(), 1_000);
assert_eq!(delegation.enqueued_for_withdraw_amount(), 99_000);
assert_eq!(delegation.delegated_security().unwrap(), 100_000);

let vault_staker_withdraw_ticket = vault_program_client
.get_vault_staker_withdraw_ticket(&vault_root.vault_pubkey, &depositor.pubkey(), &base)
.await
.unwrap();
assert_eq!(vault_staker_withdraw_ticket.lrt_amount(), 99_000);
assert_eq!(
vault_staker_withdraw_ticket.withdraw_allocation_amount(),
99_000
);
}

#[tokio::test]
async fn test_enqueue_withdraw_with_reward_not_delegated_ok() {
let mut fixture = TestBuilder::new().await;
let mut vault_program_client = fixture.vault_program_client();
let mut restaking_program_client = fixture.restaking_program_client();

// Setup vault with initial deposit
let (_vault_config_admin, vault_root) = vault_program_client.setup_vault(0, 0).await.unwrap();
let _restaking_config_admin = restaking_program_client.setup_config().await.unwrap();

// Setup operator and AVS
let operator_root = restaking_program_client.setup_operator().await.unwrap();
let avs_root = restaking_program_client.setup_avs().await.unwrap();

// Setup necessary relationships
restaking_program_client
.operator_avs_opt_in(&operator_root, &avs_root.avs_pubkey)
.await
.unwrap();
restaking_program_client
.avs_operator_opt_in(&avs_root, &operator_root.operator_pubkey)
.await
.unwrap();
restaking_program_client
.avs_vault_opt_in(&avs_root, &vault_root.vault_pubkey)
.await
.unwrap();
restaking_program_client
.operator_vault_opt_in(&operator_root, &vault_root.vault_pubkey)
.await
.unwrap();
vault_program_client
.vault_avs_opt_in(&vault_root, &avs_root.avs_pubkey)
.await
.unwrap();
vault_program_client
.vault_operator_opt_in(&vault_root, &operator_root.operator_pubkey)
.await
.unwrap();

let vault = vault_program_client
.get_vault(&vault_root.vault_pubkey)
.await
.unwrap();

// Initial deposit
let depositor = Keypair::new();
fixture.transfer(&depositor.pubkey(), 100.0).await.unwrap();
fixture
.mint_to(&vault.supported_mint(), &depositor.pubkey(), 100_000)
.await
.unwrap();
fixture
.create_ata(&vault.lrt_mint(), &depositor.pubkey())
.await
.unwrap();

// Mint LRT tokens to depositor
vault_program_client
.mint_to(
&vault_root.vault_pubkey,
&vault.lrt_mint(),
&depositor,
&get_associated_token_address(&depositor.pubkey(), &vault.supported_mint()),
&get_associated_token_address(&vault_root.vault_pubkey, &vault.supported_mint()),
&get_associated_token_address(&depositor.pubkey(), &vault.lrt_mint()),
&get_associated_token_address(&vault.fee_owner(), &vault.lrt_mint()),
None,
100_000,
)
.await
.unwrap();

// Delegate all funds to the operator
vault_program_client
.delegate(&vault_root, &operator_root.operator_pubkey, 100_000)
.await
.unwrap();

// Simulate rewards by adding more tokens to the vault
fixture
.mint_to(&vault.supported_mint(), &vault_root.vault_pubkey, 10_000)
.await
.unwrap();
vault_program_client
.do_update_vault(&vault_root.vault_pubkey)
.await
.unwrap();

// Enqueue withdrawal for half of the original deposit
let withdraw_amount = 50_000;
let VaultStakerWithdrawTicketRoot { base } = vault_program_client
.do_enqueue_withdraw(&vault_root, &depositor, withdraw_amount)
.await
.unwrap();

// Verify the withdraw ticket
let withdraw_ticket = vault_program_client
.get_vault_staker_withdraw_ticket(&vault_root.vault_pubkey, &depositor.pubkey(), &base)
.await
.unwrap();

assert_eq!(withdraw_ticket.lrt_amount(), withdraw_amount);

// The actual assets to be withdrawn should be more than the LRT amount due to rewards
assert_eq!(withdraw_ticket.withdraw_allocation_amount(), 55_000);

// Verify the vault delegation list
let vault_delegation_list = vault_program_client
.get_vault_delegation_list(&vault_root.vault_pubkey)
.await
.unwrap();

let delegation = vault_delegation_list.delegations().get(0).unwrap();
assert_eq!(delegation.staked_amount(), 45_000);
assert_eq!(delegation.enqueued_for_withdraw_amount(), 55_000);
assert_eq!(delegation.delegated_security().unwrap(), 100_000);
}

#[tokio::test]
async fn test_enqueue_withdraw_with_slash_ok() {}

#[tokio::test]
async fn test_enqueue_withdraw_with_multiple_operators_pro_rata_ok() {}

#[tokio::test]
async fn test_enqueue_withdraw_at_epoch_boundary() {}

#[tokio::test]
async fn test_enqueue_withdraw_with_existing_cooldowns() {}

#[tokio::test]
async fn test_enqueue_withdraw_with_zero_amount() {}

#[tokio::test]
async fn test_enqueue_withdraw_insufficient_balance() {}

#[tokio::test]
async fn test_enqueue_withdraw_concurrent_requests() {}

#[tokio::test]
async fn test_enqueue_multiple_same_ticket() {}

#[tokio::test]
async fn test_enqueue_delegation_list_update_needed() {}
Loading

0 comments on commit 08e60b5

Please sign in to comment.