Skip to content

Commit

Permalink
add initialize_vault_depositor
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Jun 1, 2023
1 parent 1680703 commit 2b2aa75
Show file tree
Hide file tree
Showing 24 changed files with 263 additions and 344 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ node_modules
test-ledger
.idea
migrations
ts/sdk/src/**/*.js
ts/sdk/src/**/*.js.map
2 changes: 1 addition & 1 deletion Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ address = "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH"
program = "./deps/drift.so"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
test = "yarn anchor-tests"
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"license": "Apache-2.0",
"scripts": {
"prettify": "prettier --check './ts/sdk/src/**/*.ts' './tests/**.ts'",
"prettify:fix": "prettier --write './ts/sdk/src/**/*.ts' './tests/**.ts'",
"lint": "eslint . --ext ts --quiet",
"lint:fix": "eslint . --ext ts --fix",
"prepare": "husky install"
"prepare": "husky install",
"update-types": "cp target/types/drift_vaults.ts ts/sdk/src/types/drift_vaults.ts",
"anchor-tests": "yarn update-types && yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
},
"devDependencies": {
"chai": "^4.3.4",
Expand Down
3 changes: 2 additions & 1 deletion programs/drift_vaults/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ default = []
[dependencies]
anchor-lang = { git = "https://github.com/drift-labs/anchor.git", rev = "ed950fe", version = "0.26.0" }
drift = { git = "https://github.com/drift-labs/protocol-v2.git", features = ["cpi", "mainnet-beta"] }
bytemuck = { version = "1.4.0" }
bytemuck = { version = "1.4.0" }
static_assertions = "1.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{Size, Vault, VaultDepositor};
use anchor_lang::prelude::*;

pub fn initialize_vault_depositor(ctx: Context<InitializeVaultDepositor>) -> Result<()> {
let mut vault_depositor = ctx.accounts.vault_depositor.load_init()?;
vault_depositor.vault = ctx.accounts.vault.key();
vault_depositor.pubkey = ctx.accounts.vault_depositor.key();
vault_depositor.authority = *ctx.accounts.authority.key;

Ok(())
}

#[derive(Accounts)]
pub struct InitializeVaultDepositor<'info> {
pub vault: AccountLoader<'info, Vault>,
#[account(
init,
seeds = [b"vault_depositor", vault.key().as_ref()],
space = Vault::SIZE,
bump,
payer = payer
)]
pub vault_depositor: AccountLoader<'info, VaultDepositor>,
pub authority: Signer<'info>,
#[account(mut)]
pub payer: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
}
2 changes: 2 additions & 0 deletions programs/drift_vaults/src/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub use initialize_vault::*;
pub use initialize_vault_depositor::*;

mod initialize_vault;
mod initialize_vault_depositor;
4 changes: 4 additions & 0 deletions programs/drift_vaults/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ pub mod drift_vaults {
pub fn initialize_vault(ctx: Context<InitializeVault>, name: [u8; 32]) -> Result<()> {
instructions::initialize_vault(ctx, name)
}

pub fn initialize_vault_depositor(ctx: Context<InitializeVaultDepositor>) -> Result<()> {
instructions::initialize_vault_depositor(ctx)
}
}
2 changes: 2 additions & 0 deletions programs/drift_vaults/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub use traits::*;
pub use vault::*;
pub use vault_depositor::*;

mod traits;
mod vault;
mod vault_depositor;
11 changes: 2 additions & 9 deletions programs/drift_vaults/src/state/vault.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::Size;
use anchor_lang::prelude::*;
use static_assertions::const_assert_eq;

#[account(zero_copy)]
#[derive(Eq, PartialEq, Debug)]
Expand All @@ -23,12 +24,4 @@ impl Size for Vault {
const SIZE: usize = 169;
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn impl_size() {
assert_eq!(super::Vault::SIZE, std::mem::size_of::<Vault>() + 8)
}
}
const_assert_eq!(Vault::SIZE, std::mem::size_of::<Vault>() + 8);
24 changes: 24 additions & 0 deletions programs/drift_vaults/src/state/vault_depositor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::Size;
use anchor_lang::prelude::*;
use static_assertions::const_assert_eq;

#[account(zero_copy)]
#[derive(Eq, PartialEq, Debug)]
#[repr(C)]
pub struct VaultDepositor {
/// The vault deposited into
pub vault: Pubkey,
/// The vault depositor account's pubkey. It is a pda of vault and authority
pub pubkey: Pubkey,
/// The authority is the address w permission to deposit/withdraw
pub authority: Pubkey,
}

impl Size for VaultDepositor {
const SIZE: usize = 104;
}

const_assert_eq!(
VaultDepositor::SIZE,
std::mem::size_of::<VaultDepositor>() + 8
);
19 changes: 15 additions & 4 deletions tests/driftVaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { AdminClient, BN } from '@drift-labs/sdk';
import { mockUSDCMint } from './testHelpers';
import { Keypair } from '@solana/web3.js';
import { assert } from 'chai';
import { VaultClient } from '../ts/sdk/src/vaultClient';
import { VaultClient } from '../ts/sdk/src';
import { getVaultAddressSync } from '../ts/sdk/src';
import { encodeName } from '../ts/sdk/lib/name';

describe('driftVaults', () => {
// Configure the client to use the local cluster.
Expand All @@ -31,19 +33,28 @@ describe('driftVaults', () => {

let usdcMint: Keypair;

const vaultName = 'crisp vault';
const vault = getVaultAddressSync(program.programId, encodeName(vaultName));

before(async () => {
usdcMint = await mockUSDCMint(provider);
await adminClient.initialize(usdcMint.publicKey, false);
await adminClient.subscribe();
});

it('Is initialized!', async () => {
const name = 'crisp vault';
after(async () => {
await adminClient.unsubscribe();
});

await vaultClient.initializeVault(name);
it('Initialize Vault', async () => {
await vaultClient.initializeVault(vaultName);

await adminClient.fetchAccounts();
assert(adminClient.getStateAccount().numberOfAuthorities.eq(new BN(1)));
assert(adminClient.getStateAccount().numberOfSubAccounts.eq(new BN(1)));
});

it('Initialize Vault Depositor', async () => {
await vaultClient.initializeVaultDepositor(vault);
});
});
2 changes: 0 additions & 2 deletions ts/sdk/lib/addresses.d.ts

This file was deleted.

31 changes: 0 additions & 31 deletions ts/sdk/lib/addresses.js

This file was deleted.

3 changes: 0 additions & 3 deletions ts/sdk/lib/name.d.ts

This file was deleted.

19 changes: 0 additions & 19 deletions ts/sdk/lib/name.js

This file was deleted.

113 changes: 0 additions & 113 deletions ts/sdk/lib/types/drift_vaults.d.ts

This file was deleted.

Loading

0 comments on commit 2b2aa75

Please sign in to comment.