Skip to content

Commit

Permalink
Drift & PG (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0ece authored Oct 5, 2024
1 parent 06bd6e9 commit 50a6bce
Show file tree
Hide file tree
Showing 22 changed files with 439 additions and 235 deletions.
24 changes: 24 additions & 0 deletions anchor/programs/glam/src/instructions/drift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,36 @@ pub fn drift_place_orders_handler<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, DriftPlaceOrders<'info>>,
order_params: Vec<OrderParams>,
) -> Result<()> {
let fund = &ctx.accounts.fund;
for order in &order_params {
let permission = match order.market_type {
MarketType::Spot => Permission::DriftSpotMarket,
MarketType::Perp => Permission::DriftPerpMarket,
};
acl::check_access(&ctx.accounts.fund, &ctx.accounts.manager.key, permission)?;

match order.market_type {
MarketType::Spot => {
if let Some(drift_market_indexes_spot) = fund.drift_market_indexes_spot() {
if drift_market_indexes_spot.len() > 0 {
require!(
drift_market_indexes_spot.contains(&(order.market_index as u32)),
AccessError::NotAuthorized
);
}
}
}
MarketType::Perp => {
if let Some(drift_market_indexes_perp) = fund.drift_market_indexes_perp() {
if drift_market_indexes_perp.len() > 0 {
require!(
drift_market_indexes_perp.contains(&(order.market_index as u32)),
AccessError::NotAuthorized
);
}
}
}
}
}

let fund_key = ctx.accounts.fund.key();
Expand Down
42 changes: 42 additions & 0 deletions anchor/programs/glam/src/instructions/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,48 @@ pub fn update_fund_handler<'c: 'info, 'info>(
}
}

if !fund_model.drift_market_indexes_perp.is_empty() {
let mut found = false;
for EngineField { name, value } in &mut fund.params[0] {
if let (EngineFieldName::DriftMarketIndexesPerp, EngineFieldValue::VecU32 { val }) =
(name, value)
{
val.clear();
val.extend(fund_model.drift_market_indexes_perp.clone());
found = true;
}
}
if !found {
fund.params[0].push(EngineField {
name: EngineFieldName::DriftMarketIndexesPerp,
value: EngineFieldValue::VecU32 {
val: fund_model.drift_market_indexes_perp,
},
});
}
}

if !fund_model.drift_market_indexes_spot.is_empty() {
let mut found = false;
for EngineField { name, value } in &mut fund.params[0] {
if let (EngineFieldName::DriftMarketIndexesSpot, EngineFieldValue::VecU32 { val }) =
(name, value)
{
val.clear();
val.extend(fund_model.drift_market_indexes_spot.clone());
found = true;
}
}
if !found {
fund.params[0].push(EngineField {
name: EngineFieldName::DriftMarketIndexesSpot,
value: EngineFieldValue::VecU32 {
val: fund_model.drift_market_indexes_spot,
},
});
}
}

Ok(())
}

Expand Down
32 changes: 32 additions & 0 deletions anchor/programs/glam/src/state/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum EngineFieldName {
IntegrationAcls,
ExternalTreasuryAccounts, // external accounts with treasury assets
LockUp, // share class
DriftMarketIndexesPerp,
DriftMarketIndexesSpot,
}

#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
Expand Down Expand Up @@ -203,6 +205,36 @@ impl FundAccount {
return None;
}

pub fn drift_market_indexes_perp(&self) -> Option<&Vec<u32>> {
for EngineField { name, value } in &self.params[0] {
match name {
EngineFieldName::DriftMarketIndexesPerp => {
return match value {
EngineFieldValue::VecU32 { val: v } => Some(v),
_ => None,
};
}
_ => { /* ignore */ }
}
}
return None;
}

pub fn drift_market_indexes_spot(&self) -> Option<&Vec<u32>> {
for EngineField { name, value } in &self.params[0] {
match name {
EngineFieldName::DriftMarketIndexesSpot => {
return match value {
EngineFieldValue::VecU32 { val: v } => Some(v),
_ => None,
};
}
_ => { /* ignore */ }
}
}
return None;
}

pub fn add_to_engine_field(&mut self, engine_field_name: EngineFieldName, pubkey: Pubkey) {
// Try to find the MarinadeTickets field, if it exists.
let mut engine_field = self.params[0]
Expand Down
2 changes: 1 addition & 1 deletion anchor/programs/glam/src/state/acl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct DelegateAcl {
pub enum IntegrationName {
Drift,
SplStakePool,
SanctunmStakePool,
SanctumStakePool,
NativeStaking,
Marinade,
Jupiter,
Expand Down
2 changes: 2 additions & 0 deletions anchor/programs/glam/src/state/model/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct FundModel {
// ACLs
pub delegate_acls: Vec<DelegateAcl>,
pub integration_acls: Vec<IntegrationAcl>,
pub drift_market_indexes_perp: Vec<u32>,
pub drift_market_indexes_spot: Vec<u32>,

// Openfunds
pub is_raw_openfunds: bool,
Expand Down
10 changes: 7 additions & 3 deletions anchor/src/client/drift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ export class DriftClient {

public async updateUserCustomMarginRatio(
fund: PublicKey,
marginRatio: number,
maxLeverage: number, // 1=1x, 2=2x ... 50=50x leverage
subAccountId: number = 0
): Promise<TransactionSignature> {
const tx = await this.updateUserCustomMarginRatioTx(
fund,
marginRatio,
maxLeverage,
subAccountId
);
return await this.base.sendAndConfirm(tx);
Expand Down Expand Up @@ -237,13 +237,17 @@ export class DriftClient {

public async updateUserCustomMarginRatioTx(
fund: PublicKey,
marginRatio: number,
maxLeverage: number, // 1=1x, 2=2x ... 50=50x leverage
subAccountId: number = 0,
apiOptions: ApiTxOptions = {}
): Promise<VersionedTransaction> {
const manager = apiOptions.signer || this.base.getManager();
const [user] = this.getUser(fund, subAccountId);

const MARGIN_PRECISION = 10_000;
// https://github.com/drift-labs/protocol-v2/blob/babed162b08b1fe34e49a81c5aa3e4ec0a88ecdf/programs/drift/src/math/constants.rs#L183-L184
const marginRatio = MARGIN_PRECISION / maxLeverage;

const tx = await this.base.program.methods
.driftUpdateUserCustomMarginRatio(subAccountId, marginRatio)
.accounts({
Expand Down
2 changes: 2 additions & 0 deletions anchor/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export const FundModel = class<FundModel> {
assetsWeights: obj.assetsWeights || [],
delegateAcls: obj.delegateAcls || [],
integrationAcls: obj.integrationAcls || [],
driftMarketIndexesPerp: obj.driftMarketIndexesPerp || [],
driftMarketIndexesSpot: obj.driftMarketIndexesSpot || [],
shareClasses: obj.shareClasses
? obj.shareClasses.map(
(shareClass: any) =>
Expand Down
63 changes: 58 additions & 5 deletions anchor/target/idl/glam.json
Original file line number Diff line number Diff line change
Expand Up @@ -3866,13 +3866,48 @@
"errors": [
{
"code": 6000,
"name": "NotAuthorized",
"msg": "Signer is not authorized"
"name": "CloseNotEmptyError",
"msg": "Error closing account: not empty"
},
{
"code": 6001,
"name": "IntegrationDisabled",
"msg": "Integration is disabled"
"name": "NotAuthorizedError",
"msg": "Error: not authorized"
},
{
"code": 6002,
"name": "InvalidFundName",
"msg": "Invalid fund name: max 30 chars"
},
{
"code": 6003,
"name": "InvalidFundSymbol",
"msg": "Too many assets: max 50"
},
{
"code": 6004,
"name": "InvalidFundUri",
"msg": "Too many assets: max 20"
},
{
"code": 6005,
"name": "InvalidAssetsLen",
"msg": "Too many assets: max 100"
},
{
"code": 6006,
"name": "InvalidAssetsWeights",
"msg": "Number of weights should match number of assets"
},
{
"code": 6007,
"name": "InvalidAssetForSwap",
"msg": "Asset cannot be swapped"
},
{
"code": 6008,
"name": "InvalidSwap",
"msg": "Swap failed"
}
],
"types": [
Expand Down Expand Up @@ -4138,6 +4173,12 @@
},
{
"name": "LockUp"
},
{
"name": "DriftMarketIndexesPerp"
},
{
"name": "DriftMarketIndexesSpot"
}
]
}
Expand Down Expand Up @@ -4730,6 +4771,18 @@
}
}
},
{
"name": "drift_market_indexes_perp",
"type": {
"vec": "u32"
}
},
{
"name": "drift_market_indexes_spot",
"type": {
"vec": "u32"
}
},
{
"name": "is_raw_openfunds",
"type": "bool"
Expand Down Expand Up @@ -4903,7 +4956,7 @@
"name": "SplStakePool"
},
{
"name": "SanctunmStakePool"
"name": "SanctumStakePool"
},
{
"name": "NativeStaking"
Expand Down
63 changes: 58 additions & 5 deletions anchor/target/types/glam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3872,13 +3872,48 @@ export type Glam = {
"errors": [
{
"code": 6000,
"name": "notAuthorized",
"msg": "Signer is not authorized"
"name": "closeNotEmptyError",
"msg": "Error closing account: not empty"
},
{
"code": 6001,
"name": "integrationDisabled",
"msg": "Integration is disabled"
"name": "notAuthorizedError",
"msg": "Error: not authorized"
},
{
"code": 6002,
"name": "invalidFundName",
"msg": "Invalid fund name: max 30 chars"
},
{
"code": 6003,
"name": "invalidFundSymbol",
"msg": "Too many assets: max 50"
},
{
"code": 6004,
"name": "invalidFundUri",
"msg": "Too many assets: max 20"
},
{
"code": 6005,
"name": "invalidAssetsLen",
"msg": "Too many assets: max 100"
},
{
"code": 6006,
"name": "invalidAssetsWeights",
"msg": "Number of weights should match number of assets"
},
{
"code": 6007,
"name": "invalidAssetForSwap",
"msg": "Asset cannot be swapped"
},
{
"code": 6008,
"name": "invalidSwap",
"msg": "Swap failed"
}
],
"types": [
Expand Down Expand Up @@ -4144,6 +4179,12 @@ export type Glam = {
},
{
"name": "lockUp"
},
{
"name": "driftMarketIndexesPerp"
},
{
"name": "driftMarketIndexesSpot"
}
]
}
Expand Down Expand Up @@ -4736,6 +4777,18 @@ export type Glam = {
}
}
},
{
"name": "driftMarketIndexesPerp",
"type": {
"vec": "u32"
}
},
{
"name": "driftMarketIndexesSpot",
"type": {
"vec": "u32"
}
},
{
"name": "isRawOpenfunds",
"type": "bool"
Expand Down Expand Up @@ -4909,7 +4962,7 @@ export type Glam = {
"name": "splStakePool"
},
{
"name": "sanctunmStakePool"
"name": "sanctumStakePool"
},
{
"name": "nativeStaking"
Expand Down
Loading

0 comments on commit 50a6bce

Please sign in to comment.