Skip to content

Commit

Permalink
Rename acls
Browse files Browse the repository at this point in the history
  • Loading branch information
yurushao committed Aug 18, 2024
1 parent 36a07b7 commit 1d5bc69
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 44 deletions.
2 changes: 1 addition & 1 deletion anchor/programs/glam/src/instructions/jupiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub fn jupiter_swap<'c: 'info, 'info>(
amount: u64,
data: Vec<u8>,
) -> Result<()> {
if let Some(acls) = ctx.accounts.fund.acls() {
if let Some(acls) = ctx.accounts.fund.delegate_acls() {
// Check if the signer is allowed to swap any asset
let can_swap_any_asset = acls.iter().any(|acl| {
acl.pubkey == *ctx.accounts.signer.key
Expand Down
32 changes: 18 additions & 14 deletions anchor/programs/glam/src/instructions/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,43 +304,47 @@ pub fn update_fund_handler<'c: 'info, 'info>(
//
// 2) a fund acl with same pubkey doesn't exist
// - add the acl
if !fund_model.acls.is_empty() {
if !fund_model.delegate_acls.is_empty() {
// Add the acls field if it doesn't exist
let acls_engine_field_exists = fund.params[0]
.iter()
.any(|field| field.name == EngineFieldName::Acls);
.any(|field| field.name == EngineFieldName::DelegateAcls);

if !acls_engine_field_exists {
msg!("Adding acls field to fund params");
fund.params[0].push(EngineField {
name: EngineFieldName::Acls,
value: EngineFieldValue::VecAcl { val: Vec::new() },
name: EngineFieldName::DelegateAcls,
value: EngineFieldValue::VecDelegateAcl { val: Vec::new() },
});
}

let to_delete: Vec<Pubkey> = fund_model
.acls
.delegate_acls
.clone()
.iter()
.filter(|acl| acl.permissions.is_empty())
.map(|acl| acl.pubkey)
.collect();
if !to_delete.is_empty() {
for EngineField { name, value } in &mut fund.params[0] {
if let (EngineFieldName::Acls, EngineFieldValue::VecAcl { val }) = (name, value) {
if let (EngineFieldName::DelegateAcls, EngineFieldValue::VecDelegateAcl { val }) =
(name, value)
{
val.retain(|acl| !to_delete.contains(&acl.pubkey));
}
}
}
let to_upsert = fund_model
.acls
.delegate_acls
.clone()
.into_iter()
.filter(|acl| !acl.permissions.is_empty());

for new_acl in to_upsert {
for EngineField { name, value } in &mut fund.params[0] {
if let (EngineFieldName::Acls, EngineFieldValue::VecAcl { val }) = (name, value) {
if let (EngineFieldName::DelegateAcls, EngineFieldValue::VecDelegateAcl { val }) =
(name, value)
{
if let Some(existing_acl) =
val.iter_mut().find(|acl| acl.pubkey == new_acl.pubkey)
{
Expand All @@ -354,27 +358,27 @@ pub fn update_fund_handler<'c: 'info, 'info>(
}

// Update enabled integrations for the fund
if !fund_model.integrations.is_empty() {
if !fund_model.integration_acls.is_empty() {
// Check if the integrations field exists
// Add the integrations field if it doesn't exist
let integrations_engine_field_exists = fund.params[0]
.iter()
.any(|field| field.name == EngineFieldName::Integrations);
.any(|field| field.name == EngineFieldName::IntegrationAcls);

if !integrations_engine_field_exists {
msg!("Adding integrations field to fund params");
fund.params[0].push(EngineField {
name: EngineFieldName::Integrations,
value: EngineFieldValue::VecIntegrations { val: Vec::new() },
name: EngineFieldName::IntegrationAcls,
value: EngineFieldValue::VecIntegrationAcl { val: Vec::new() },
});
}

for EngineField { name, value } in &mut fund.params[0] {
if let (EngineFieldName::Integrations, EngineFieldValue::VecIntegrations { val }) =
if let (EngineFieldName::IntegrationAcls, EngineFieldValue::VecIntegrationAcl { val }) =
(name, value)
{
val.clear();
val.extend(fund_model.integrations.clone());
val.extend(fund_model.integration_acls.clone());
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions anchor/programs/glam/src/state/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anchor_lang::prelude::*;

use super::acl::*;
use super::integrations::*;
use super::model::*;
use super::openfunds::*;

Expand All @@ -13,8 +12,8 @@ pub enum EngineFieldName {
AssetsWeights,
ShareClassAllowlist,
ShareClassBlocklist,
Acls,
Integrations,
DelegateAcls,
IntegrationAcls,
}

#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
Expand All @@ -35,8 +34,8 @@ pub enum EngineFieldValue {
Timestamp { val: i64 },
VecPubkey { val: Vec<Pubkey> },
VecU32 { val: Vec<u32> },
VecAcl { val: Vec<Acl> },
VecIntegrations { val: Vec<Integration> },
VecDelegateAcl { val: Vec<DelegateAcl> },
VecIntegrationAcl { val: Vec<IntegrationAcl> },
}

#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
Expand Down Expand Up @@ -133,12 +132,12 @@ impl FundAccount {
return None;
}

pub fn acls(&self) -> Option<&Vec<Acl>> {
pub fn delegate_acls(&self) -> Option<&Vec<DelegateAcl>> {
for EngineField { name, value } in &self.params[0] {
match name {
EngineFieldName::Acls => {
EngineFieldName::DelegateAcls => {
return match value {
EngineFieldValue::VecAcl { val: v } => Some(v),
EngineFieldValue::VecDelegateAcl { val: v } => Some(v),
_ => None,
};
}
Expand Down
32 changes: 29 additions & 3 deletions anchor/programs/glam/src/state/acl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub enum AccessError {
NotAuthorized,
}

/**
* Delegate ACL
*/
#[derive(AnchorDeserialize, AnchorSerialize, Clone, PartialEq, Debug)]
pub enum Permission {
DriftDeposit,
Expand All @@ -22,11 +25,34 @@ pub enum Permission {
}

#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
pub struct Acl {
pub struct DelegateAcl {
pub pubkey: Pubkey,
pub permissions: Vec<Permission>,
}

/**
* Integration ACL
*/
#[derive(AnchorDeserialize, AnchorSerialize, Clone, PartialEq, Debug)]
pub enum IntegrationName {
Drift,
StakePool,
NativeStaking,
Marinade,
Jupiter,
}

#[derive(AnchorDeserialize, AnchorSerialize, Clone, PartialEq, Debug)]
pub enum IntegrationFeature {
All,
}

#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
pub struct IntegrationAcl {
pub name: IntegrationName,
pub features: Vec<IntegrationFeature>,
}

pub fn check_access(fund: &FundAccount, signer: &Pubkey, permission: Permission) -> Result<()> {
if fund.manager == *signer {
return Ok(());
Expand All @@ -38,7 +64,7 @@ pub fn check_access(fund: &FundAccount, signer: &Pubkey, permission: Permission)
permission
);

if let Some(acls) = fund.acls() {
if let Some(acls) = fund.delegate_acls() {
for acl in acls {
if acl.pubkey == *signer && acl.permissions.contains(&permission) {
return Ok(());
Expand All @@ -63,7 +89,7 @@ pub fn check_access_any(
allowed_permissions
);

if let Some(acls) = fund.acls() {
if let Some(acls) = fund.delegate_acls() {
for acl in acls {
for permission in &allowed_permissions {
if acl.pubkey == *signer && acl.permissions.contains(permission) {
Expand Down
10 changes: 0 additions & 10 deletions anchor/programs/glam/src/state/integrations.rs

This file was deleted.

3 changes: 0 additions & 3 deletions anchor/programs/glam/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ pub use assets::*;

pub mod acl;
pub use acl::*;

pub mod integrations;
pub use integrations::*;
7 changes: 2 additions & 5 deletions anchor/programs/glam/src/state/model/model.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anchor_lang::prelude::*;

use super::super::acl::*;
use super::super::integrations::*;

// Fund
//
Expand All @@ -28,10 +27,8 @@ pub struct FundModel {
pub created: Option<CreatedModel>,

// ACLs
pub acls: Vec<Acl>,

// Enabled integrations
pub integrations: Vec<Integration>,
pub delegate_acls: Vec<DelegateAcl>,
pub integration_acls: Vec<IntegrationAcl>,

// Openfunds
pub is_raw_openfunds: Option<bool>,
Expand Down

0 comments on commit 1d5bc69

Please sign in to comment.