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

Feature/deposit cap api #18

Merged
merged 9 commits into from
Apr 12, 2022
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
2 changes: 2 additions & 0 deletions programs/castle-lending-aggregator/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ pub mod init;
pub mod rebalance;
pub mod reconcile;
pub mod refresh;
pub mod update_deposit_cap;
pub mod withdraw;

pub use deposit::*;
pub use init::*;
pub use rebalance::*;
pub use reconcile::*;
pub use refresh::*;
pub use update_deposit_cap::*;
pub use withdraw::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use anchor_lang::prelude::*;

use crate::state::Vault;
use std::convert::Into;

#[derive(Accounts)]
pub struct UpdateDepositCap<'info> {
#[account(
charlieyou marked this conversation as resolved.
Show resolved Hide resolved
mut,
has_one = owner,
)]
pub vault: Box<Account<'info, Vault>>,

pub owner: Signer<'info>,
}
charlieyou marked this conversation as resolved.
Show resolved Hide resolved

pub fn handler(ctx: Context<UpdateDepositCap>, deposit_cap_new_value: u64) -> ProgramResult {
#[cfg(feature = "debug")]
msg!("New deposit cap value: {}", deposit_cap_new_value);

ctx.accounts.vault.pool_size_limit = deposit_cap_new_value;
Ok(())
}
9 changes: 8 additions & 1 deletion programs/castle-lending-aggregator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ pub mod castle_lending_aggregator {
_bumps: InitBumpSeeds,
strategy_type: StrategyType,
fees: FeeArgs,
pool_size_limit: u64
pool_size_limit: u64,
) -> ProgramResult {
instructions::init::handler(ctx, _bumps, strategy_type, fees, pool_size_limit)
}

pub fn update_deposit_cap(
ctx: Context<UpdateDepositCap>,
deposit_cap_new_value: u64,
) -> ProgramResult {
instructions::update_deposit_cap::handler(ctx, deposit_cap_new_value)
}

pub fn deposit(ctx: Context<Deposit>, reserve_token_amount: u64) -> ProgramResult {
instructions::deposit::handler(ctx, reserve_token_amount)
}
Expand Down
42 changes: 42 additions & 0 deletions sdk/src/castle_lending_aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ export type CastleLendingAggregator = {
}
];
},
{
name: "updateDepositCap";
accounts: [
{
name: "vault";
isMut: true;
isSigner: false;
},
{
name: "owner";
isMut: false;
isSigner: true;
}
];
args: [
{
name: "depositCapNewValue";
type: "u64";
}
];
},
{
name: "deposit";
accounts: [
Expand Down Expand Up @@ -1117,6 +1138,27 @@ export const IDL: CastleLendingAggregator = {
},
],
},
{
name: "updateDepositCap",
accounts: [
{
name: "vault",
isMut: true,
isSigner: false,
},
{
name: "owner",
isMut: false,
isSigner: true,
},
],
args: [
{
name: "depositCapNewValue",
type: "u64",
},
],
},
{
name: "deposit",
accounts: [
Expand Down
25 changes: 24 additions & 1 deletion sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class VaultClient {
return new VaultClient(program, vaultId, vaultState, solend, port, jet);
}

private async reload() {
async reload() {
this.vaultState = await this.program.account.vault.fetch(this.vaultId);
// TODO reload underlying asset data also?
}
Expand Down Expand Up @@ -292,6 +292,29 @@ export class VaultClient {
};
}

/**
* @param new_value
* @returns
*/
async updateDepositCap(
owner: Keypair,
new_value: number
): Promise<TransactionSignature[]> {
const updateCommand = new Transaction();
updateCommand.add(
this.program.instruction.updateDepositCap(
new anchor.BN(new_value),
{
accounts: {
vault: this.vaultId,
owner: owner.publicKey,
},
}
)
);
return [await this.program.provider.send(updateCommand, [owner])];
}

/**
*
* TODO refactor to be more clear
Expand Down
47 changes: 46 additions & 1 deletion tests/castle-lending-aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ describe("castle-vault", () => {
await mintReserveToken(userReserveTokenAccount, qty);
try {
await depositToVault(qty);
assert.ok(false);
assert.fail("Deposit should be rejected but was not.");
} catch (err) {
// TODO check err
}
Expand All @@ -387,6 +387,51 @@ describe("castle-vault", () => {
assert.equal(userLpBalance, 0);
assert.equal(lpTokenSupply, 0);
});

it("Update deposit cap", async function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an unhappy-case test that it fails if not signed / signed by wrong person?

const newDepositCap = poolSizeLimit * 0.24;
const txs = await vaultClient.updateDepositCap(
owner,
newDepositCap
);
await provider.connection.confirmTransaction(
txs[txs.length - 1],
"singleGossip"
);
await vaultClient.reload();
assert.equal(
newDepositCap,
vaultClient.vaultState.poolSizeLimit.toNumber()
);
});

it("Reject unauthorized deposit cap update", async function () {
const noPermissionUser = Keypair.generate();

const prevDepositCap =
vaultClient.vaultState.poolSizeLimit.toNumber();
const newDepositCap = prevDepositCap * 0.24;

try {
const txs = await vaultClient.updateDepositCap(
noPermissionUser,
newDepositCap
);
await provider.connection.confirmTransaction(
txs[txs.length - 1],
"singleGossip"
);
assert.fail("Transaction should be rejected but was not.");
} catch (err) {
// TODO check err
}

await vaultClient.reload();
assert.equal(
prevDepositCap,
vaultClient.vaultState.poolSizeLimit.toNumber()
);
});
}

function testRebalance(
Expand Down