Skip to content

Commit

Permalink
v0.2: FundAccount + Openfunds data
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0ece committed May 10, 2024
1 parent fd39c80 commit 3e9d456
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 177 deletions.
90 changes: 40 additions & 50 deletions anchor/programs/glam/src/instructions/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ use crate::error::ManagerError;
use crate::state::*;

#[derive(Accounts)]
#[instruction(name: String)]
#[instruction(fund_model: FundModel)]
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(), fund_model.created.as_ref().unwrap().key.as_ref()], bump, payer = manager, space = 8 + FundAccount::INIT_SIZE)]
pub fund: Box<Account<'info, FundAccount>>,

#[account(init, seeds = [b"openfunds".as_ref(), fund.key().as_ref()], bump, payer = manager, space = FundMetadataAccount::INIT_SIZE)]
pub openfunds: Box<Account<'info, FundMetadataAccount>>,

#[account(mut, seeds = [b"treasury".as_ref(), fund.key().as_ref()], bump)]
pub treasury: SystemAccount<'info>,

Expand All @@ -22,35 +25,8 @@ pub struct InitializeFund<'info> {

pub fn initialize_fund_handler<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, InitializeFund<'info>>,
fund_name: String,
fund_symbol: String,
fund_uri: String,
asset_weights: Vec<u32>,
activate: bool,
fund_model: FundModel,
) -> Result<()> {
//
// Validate the input
//
require!(
fund_name.as_bytes().len() <= MAX_FUND_NAME,
ManagerError::InvalidFundName
);
require!(
fund_symbol.as_bytes().len() <= MAX_FUND_SYMBOL,
ManagerError::InvalidFundSymbol
);
require!(
fund_uri.as_bytes().len() <= MAX_FUND_URI,
ManagerError::InvalidFundUri
);

let assets_len = ctx.remaining_accounts.len();
require!(assets_len <= MAX_ASSETS, ManagerError::InvalidAssetsLen);
require!(
asset_weights.len() == assets_len,
ManagerError::InvalidAssetsLen
);

//
// Create the treasury account
//
Expand Down Expand Up @@ -83,28 +59,42 @@ pub fn initialize_fund_handler<'c: 'info, 'info>(
// Initialize the fund
//
let fund = &mut ctx.accounts.fund;
let asset_mints: Vec<Pubkey> = ctx
.remaining_accounts
.into_iter()
.map(|a| a.key())
.collect();
let treasury = &mut ctx.accounts.treasury;
let openfunds = &mut ctx.accounts.openfunds;

let model = fund_model.clone();
if let Some(fund_name) = model.name {
require!(
fund_name.len() < MAX_FUND_NAME,
ManagerError::InvalidFundName
);
fund.name = fund_name;
}
if let Some(fund_uri) = model.uri {
require!(fund_uri.len() < MAX_FUND_URI, ManagerError::InvalidFundUri);
fund.uri = fund_uri;
}
if let Some(openfunds_uri) = model.openfunds_uri {
require!(
openfunds_uri.len() < MAX_FUND_URI,
ManagerError::InvalidFundUri
);
fund.openfunds_uri = openfunds_uri;
}

fund.treasury = treasury.key();
fund.openfunds = openfunds.key();
fund.manager = ctx.accounts.manager.key();
fund.treasury = ctx.accounts.treasury.key();

/*fund.init(
fund_name,
fund_symbol,
fund_uri,
ctx.accounts.manager.key(),
ctx.accounts.treasury.key(),
asset_mints,
asset_weights,
ctx.bumps.fund,
ctx.bumps.treasury,
Clock::get()?.unix_timestamp,
activate,
);*/

//
// Initialize openfunds
//
let openfunds_metadata = FundMetadataAccount::from(fund_model);
openfunds.fund_pubkey = fund.key();
openfunds.company = openfunds_metadata.company;
openfunds.fund = openfunds_metadata.fund;
openfunds.share_classes = openfunds_metadata.share_classes;
openfunds.fund_managers = openfunds_metadata.fund_managers;

msg!("Fund created: {}", ctx.accounts.fund.key());
Ok(())
Expand Down
16 changes: 3 additions & 13 deletions anchor/programs/glam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use anchor_lang::prelude::*;
use crate::instructions::*;
pub use constants::*;
pub use state::fund::*;
pub use state::model::*;

declare_id!("Gco1pcjxCMYjKJjSNJ7mKV7qezeUTE7arXJgy7PAPNRc");

Expand All @@ -20,20 +21,9 @@ pub mod glam {

pub fn initialize<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, InitializeFund<'info>>,
fund_name: String,
fund_symbol: String,
fund_uri: String,
asset_weights: Vec<u32>,
activate: bool,
fund: FundModel,
) -> Result<()> {
manager::initialize_fund_handler(
ctx,
fund_name,
fund_symbol,
fund_uri,
asset_weights,
activate,
)
manager::initialize_fund_handler(ctx, fund)
}

pub fn add_share_class<'c: 'info, 'info>(
Expand Down
10 changes: 4 additions & 6 deletions anchor/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,23 @@ export class GlamClient {
const fundModel = this.enrichFundModelInitialize(fund);
const fundPDA = this.getFundPDA(fundModel);
const treasury = this.getTreasuryPDA(fundPDA);
const share = this.getShareClassPDA(fundPDA, 0);
// const share = this.getShareClassPDA(fundPDA, 0);
const openfunds = this.getOpenfundsPDA(fundPDA);
const manager = this.getManager();

//TODO: add instructions to "addShareClass" in the same tx
const txSig = ""; /*await this.program.methods
const txSig = await this.program.methods
.initialize(fundModel)
.accounts({
fund: fundPDA,
treasury,
openfunds,
share,
manager,
tokenProgram: TOKEN_2022_PROGRAM_ID
manager
})
.preInstructions([
ComputeBudgetProgram.setComputeUnitLimit({ units: 500_000 })
])
.rpc();*/
.rpc();
return [txSig, fundPDA];
}

Expand Down
25 changes: 7 additions & 18 deletions anchor/target/idl/glam.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"isMut": true,
"isSigner": false
},
{
"name": "openfunds",
"isMut": true,
"isSigner": false
},
{
"name": "treasury",
"isMut": true,
Expand All @@ -35,26 +40,10 @@
],
"args": [
{
"name": "fundName",
"type": "string"
},
{
"name": "fundSymbol",
"type": "string"
},
{
"name": "fundUri",
"type": "string"
},
{
"name": "assetWeights",
"name": "fund",
"type": {
"vec": "u32"
"defined": "FundModel"
}
},
{
"name": "activate",
"type": "bool"
}
]
},
Expand Down
50 changes: 14 additions & 36 deletions anchor/target/types/glam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export type Glam = {
"isMut": true,
"isSigner": false
},
{
"name": "openfunds",
"isMut": true,
"isSigner": false
},
{
"name": "treasury",
"isMut": true,
Expand All @@ -35,26 +40,10 @@ export type Glam = {
],
"args": [
{
"name": "fundName",
"type": "string"
},
{
"name": "fundSymbol",
"type": "string"
},
{
"name": "fundUri",
"type": "string"
},
{
"name": "assetWeights",
"name": "fund",
"type": {
"vec": "u32"
"defined": "FundModel"
}
},
{
"name": "activate",
"type": "bool"
}
]
},
Expand Down Expand Up @@ -2856,6 +2845,11 @@ export const IDL: Glam = {
"isMut": true,
"isSigner": false
},
{
"name": "openfunds",
"isMut": true,
"isSigner": false
},
{
"name": "treasury",
"isMut": true,
Expand All @@ -2874,26 +2868,10 @@ export const IDL: Glam = {
],
"args": [
{
"name": "fundName",
"type": "string"
},
{
"name": "fundSymbol",
"type": "string"
},
{
"name": "fundUri",
"type": "string"
},
{
"name": "assetWeights",
"name": "fund",
"type": {
"vec": "u32"
"defined": "FundModel"
}
},
{
"name": "activate",
"type": "bool"
}
]
},
Expand Down
18 changes: 0 additions & 18 deletions anchor/tests/glam_drift.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,4 @@ describe("glam_drift", () => {
}
}, 30_000);
*/
it("Close fund", async () => {
const fund = await program.account.fundAccount.fetchNullable(fundPDA);
expect(fund).not.toBeNull();

await program.methods
.close()
.accounts({
fund: fundPDA,
manager: manager.publicKey
})
.rpc({ commitment });

// The account should no longer exist, returning null.
const closedAccount = await program.account.fundAccount.fetchNullable(
fundPDA
);
expect(closedAccount).toBeNull();
});
});
Loading

0 comments on commit 3e9d456

Please sign in to comment.