Skip to content

Commit

Permalink
update manager
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0ece committed Apr 21, 2024
1 parent beb7423 commit b7a6c79
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 3 deletions.
9 changes: 7 additions & 2 deletions anchor/programs/glam/src/instructions/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::error::ManagerError;
use crate::state::fund::*;

#[derive(Accounts)]
#[instruction(name: String)]
#[instruction(symbol: String)]
pub struct InitializeFund<'info> {
#[account(init, seeds = [b"fund".as_ref(), manager.key().as_ref(), name.as_ref()], bump, payer = manager, space = 8 + Fund::INIT_SIZE + ShareClassMetadata::INIT_SIZE)]
#[account(init, seeds = [b"fund".as_ref(), manager.key().as_ref(), symbol.as_ref()], bump, payer = manager, space = 8 + Fund::INIT_SIZE + ShareClassMetadata::INIT_SIZE)]
pub fund: Box<Account<'info, Fund>>,

#[account(init, seeds = [b"treasury".as_ref(), fund.key().as_ref()], bump, payer = manager, space = 8 + Treasury::INIT_SIZE)]
Expand Down Expand Up @@ -66,6 +66,7 @@ pub fn initialize_fund_handler<'c: 'info, 'info>(
let treasury = &mut ctx.accounts.treasury;

fund.manager = ctx.accounts.manager.key();
fund.creator = fund.manager;
fund.treasury = treasury.key();
fund.name = fund_name;
fund.symbol = fund_symbol;
Expand Down Expand Up @@ -326,6 +327,7 @@ pub struct UpdateFund<'info> {
pub fn update_fund_handler<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, UpdateFund<'info>>,
name: Option<String>,
manager: Option<Pubkey>,
uri: Option<String>,
asset_weights: Option<Vec<u32>>,
activate: Option<bool>,
Expand All @@ -336,6 +338,9 @@ pub fn update_fund_handler<'c: 'info, 'info>(
require!(name.as_bytes().len() <= 50, ManagerError::InvalidFundName);
fund.name = name;
}
if let Some(manager) = manager {
fund.manager = manager;
}
if let Some(uri) = uri {
require!(uri.as_bytes().len() <= 100, ManagerError::InvalidFundName);
fund.uri = uri;
Expand Down
3 changes: 2 additions & 1 deletion anchor/programs/glam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ pub mod glam {
pub fn update<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, UpdateFund<'info>>,
name: Option<String>,
manager: Option<Pubkey>,
uri: Option<String>,
asset_weights: Option<Vec<u32>>,
activate: Option<bool>,
) -> Result<()> {
manager::update_fund_handler(ctx, name, uri, asset_weights, activate)
manager::update_fund_handler(ctx, name, manager, uri, asset_weights, activate)
}
pub fn close(ctx: Context<CloseFund>) -> Result<()> {
manager::close_handler(ctx)
Expand Down
2 changes: 2 additions & 0 deletions anchor/programs/glam/src/state/fund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub const MAX_FUND_URI: usize = 100;

#[account]
pub struct Fund {
pub creator: Pubkey, // 32
pub manager: Pubkey, // 32
pub treasury: Pubkey, // 32
pub assets_len: u8, // 1
Expand All @@ -27,6 +28,7 @@ pub struct Fund {
}
impl Fund {
pub const INIT_SIZE: usize = 32
+ 32
+ 32
+ 1
+ (32 + 4) * MAX_ASSETS
Expand Down
10 changes: 10 additions & 0 deletions anchor/target/idl/glam.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
"option": "string"
}
},
{
"name": "manager",
"type": {
"option": "publicKey"
}
},
{
"name": "uri",
"type": {
Expand Down Expand Up @@ -538,6 +544,10 @@
"type": {
"kind": "struct",
"fields": [
{
"name": "creator",
"type": "publicKey"
},
{
"name": "manager",
"type": "publicKey"
Expand Down
20 changes: 20 additions & 0 deletions anchor/target/types/glam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ export type Glam = {
"option": "string"
}
},
{
"name": "manager",
"type": {
"option": "publicKey"
}
},
{
"name": "uri",
"type": {
Expand Down Expand Up @@ -538,6 +544,10 @@ export type Glam = {
"type": {
"kind": "struct",
"fields": [
{
"name": "creator",
"type": "publicKey"
},
{
"name": "manager",
"type": "publicKey"
Expand Down Expand Up @@ -903,6 +913,12 @@ export const IDL: Glam = {
"option": "string"
}
},
{
"name": "manager",
"type": {
"option": "publicKey"
}
},
{
"name": "uri",
"type": {
Expand Down Expand Up @@ -1341,6 +1357,10 @@ export const IDL: Glam = {
"type": {
"kind": "struct",
"fields": [
{
"name": "creator",
"type": "publicKey"
},
{
"name": "manager",
"type": "publicKey"
Expand Down
40 changes: 40 additions & 0 deletions anchor/tests/glam_investor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,4 +783,44 @@ describe("glam_investor", () => {
// in reality it will be less due to fees but toFixed(2) rounds it up
expect((Number(shares.supply) / 1e9).toFixed(2)).toEqual("2.50");
});

it("Update fund", async () => {
const newFundName = "Updated fund name";
const newManager = Keypair.generate();
await program.methods
.update(newFundName, newManager.publicKey, null, null, true)
.accounts({
fund: fundPDA,
manager: manager.publicKey
})
.rpc({ commitment });
let fund = await program.account.fund.fetch(fundPDA);
expect(fund.name).toEqual(newFundName);
expect(fund.manager).toEqual(newManager.publicKey);

try {
await program.methods
.update(newFundName, manager.publicKey, null, null, true)
.accounts({
fund: fundPDA,
manager: manager.publicKey
})
.rpc({ commitment });
} catch(e) {
expect(e.message).toContain("Error: not authorized");
}

await program.methods
.update(newFundName, manager.publicKey, null, null, true)
.accounts({
fund: fundPDA,
manager: newManager.publicKey
})
.signers([newManager])
.rpc({ commitment });
fund = await program.account.fund.fetch(fundPDA);
expect(fund.name).toEqual(newFundName);
expect(fund.manager).toEqual(manager.publicKey);
});

});

0 comments on commit b7a6c79

Please sign in to comment.