Skip to content

Commit 9dbe65c

Browse files
committed
add SignedMsgExtensions enum struct variant
1 parent cfa42e7 commit 9dbe65c

File tree

6 files changed

+99
-20
lines changed

6 files changed

+99
-20
lines changed

programs/drift/src/state/order_params.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ pub struct SignedMsgOrderParamsMessage {
864864
pub uuid: [u8; 8],
865865
pub take_profit_order_params: Option<SignedMsgTriggerOrderParams>,
866866
pub stop_loss_order_params: Option<SignedMsgTriggerOrderParams>,
867-
pub max_margin_ratio: Option<u16>,
867+
pub ext: SignedMsgExtensions,
868868
}
869869

870870
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default, Eq, PartialEq, Debug)]
@@ -875,7 +875,64 @@ pub struct SignedMsgOrderParamsDelegateMessage {
875875
pub uuid: [u8; 8],
876876
pub take_profit_order_params: Option<SignedMsgTriggerOrderParams>,
877877
pub stop_loss_order_params: Option<SignedMsgTriggerOrderParams>,
878-
pub max_margin_ratio: Option<u16>,
878+
pub ext: SignedMsgExtensions,
879+
}
880+
881+
#[derive(Clone, Eq, AnchorSerialize, PartialEq, Debug, Default)]
882+
pub enum SignedMsgExtensions {
883+
#[default]
884+
V0,
885+
V1 {
886+
max_margin_ratio: Option<u16>,
887+
},
888+
}
889+
890+
impl SignedMsgExtensions {
891+
pub fn max_margin_ratio(&self) -> Option<u16> {
892+
match self {
893+
Self::V0 => None,
894+
Self::V1 { max_margin_ratio } => *max_margin_ratio,
895+
}
896+
}
897+
}
898+
899+
impl borsh::de::BorshDeserialize for SignedMsgExtensions
900+
where
901+
Option<u16>: borsh::BorshDeserialize,
902+
{
903+
fn deserialize_reader<R: borsh::maybestd::io::Read>(
904+
reader: &mut R,
905+
) -> ::core::result::Result<Self, borsh::maybestd::io::Error> {
906+
match <u8 as borsh::de::BorshDeserialize>::deserialize_reader(reader) {
907+
Ok(tag) => <Self as borsh::de::EnumExt>::deserialize_variant(reader, tag),
908+
// missing tag byte == v0
909+
Err(_) => Ok(Self::V0),
910+
}
911+
}
912+
}
913+
914+
impl borsh::de::EnumExt for SignedMsgExtensions
915+
where
916+
Option<u16>: borsh::BorshDeserialize,
917+
{
918+
fn deserialize_variant<R: borsh::maybestd::io::Read>(
919+
reader: &mut R,
920+
variant_idx: u8,
921+
) -> ::core::result::Result<Self, borsh::maybestd::io::Error> {
922+
let mut return_value = match variant_idx {
923+
0u8 => SignedMsgExtensions::V0,
924+
1u8 => SignedMsgExtensions::V1 {
925+
max_margin_ratio: borsh::BorshDeserialize::deserialize_reader(reader)?,
926+
},
927+
_ => {
928+
return Err(borsh::maybestd::io::Error::new(
929+
borsh::maybestd::io::ErrorKind::InvalidInput,
930+
borsh::maybestd::format!("Unexpected variant index: {:?}", variant_idx),
931+
))
932+
}
933+
};
934+
Ok(return_value)
935+
}
879936
}
880937

881938
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default, Eq, PartialEq, Debug)]

programs/drift/src/validation/sig_verification.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn deserialize_into_verified_message(
9595
uuid: deserialized.uuid,
9696
take_profit_order_params: deserialized.take_profit_order_params,
9797
stop_loss_order_params: deserialized.stop_loss_order_params,
98-
max_margin_ratio: deserialized.max_margin_ratio,
98+
max_margin_ratio: deserialized.ext.max_margin_ratio(),
9999
signature: *signature,
100100
});
101101
} else {
@@ -122,7 +122,7 @@ pub fn deserialize_into_verified_message(
122122
uuid: deserialized.uuid,
123123
take_profit_order_params: deserialized.take_profit_order_params,
124124
stop_loss_order_params: deserialized.stop_loss_order_params,
125-
max_margin_ratio: deserialized.max_margin_ratio,
125+
max_margin_ratio: deserialized.ext.max_margin_ratio(),
126126
signature: *signature,
127127
});
128128
}

programs/drift/src/validation/sig_verification/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ mod sig_verification {
100100
32, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 10, 1, 128, 133, 181, 13, 0, 0, 0, 0,
101101
1, 64, 85, 32, 14, 0, 0, 0, 0, 2, 0, 41, 9, 0, 0, 0, 0, 0, 0, 67, 82, 79, 51, 105, 114,
102102
71, 49, 1, 0, 28, 78, 14, 0, 0, 0, 0, 0, 96, 254, 205, 0, 0, 0, 0, 1, 64, 58, 105, 13,
103-
0, 0, 0, 0, 0, 96, 254, 205, 0, 0, 0, 0, 1, 1,
103+
0, 0, 0, 0, 0, 96, 254, 205, 0, 0, 0, 0, 1, 1, 1, 0,
104104
];
105105

106106
// Test deserialization with delegate signer
@@ -247,7 +247,7 @@ mod sig_verification {
247247
245, 208, 37, 127, 112, 55, 83, 186, 54, 139, 1, 135, 220, 180, 208, 219, 189, 94, 79,
248248
148, 41, 9, 0, 0, 0, 0, 0, 0, 67, 82, 79, 51, 105, 114, 71, 49, 1, 128, 133, 181, 13,
249249
0, 0, 0, 0, 0, 202, 154, 59, 0, 0, 0, 0, 1, 128, 178, 230, 14, 0, 0, 0, 0, 0, 202, 154,
250-
59, 0, 0, 0, 0, 1, 1,
250+
59, 0, 0, 0, 0, 1, 1, 1, 0,
251251
];
252252

253253
// Test deserialization with delegate signer

sdk/src/driftClient.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6487,10 +6487,6 @@ export class DriftClient {
64876487
| SignedMsgOrderParamsDelegateMessage,
64886488
delegateSigner?: boolean
64896489
): SignedMsgOrderParams {
6490-
if (orderParamsMessage.maxMarginRatio === undefined) {
6491-
orderParamsMessage.maxMarginRatio = null;
6492-
}
6493-
64946490
const borshBuf = this.encodeSignedMsgOrderParamsMessage(
64956491
orderParamsMessage,
64966492
delegateSigner

sdk/src/idl/drift.json

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10141,9 +10141,9 @@
1014110141
}
1014210142
},
1014310143
{
10144-
"name": "maxMarginRatio",
10144+
"name": "ext",
1014510145
"type": {
10146-
"option": "u16"
10146+
"defined": "SignedMsgExtensions"
1014710147
}
1014810148
}
1014910149
]
@@ -10194,9 +10194,9 @@
1019410194
}
1019510195
},
1019610196
{
10197-
"name": "maxMarginRatio",
10197+
"name": "ext",
1019810198
"type": {
10199-
"option": "u16"
10199+
"defined": "SignedMsgExtensions"
1020010200
}
1020110201
}
1020210202
]
@@ -12397,6 +12397,28 @@
1239712397
]
1239812398
}
1239912399
},
12400+
{
12401+
"name": "SignedMsgExtensions",
12402+
"type": {
12403+
"kind": "enum",
12404+
"variants": [
12405+
{
12406+
"name": "V0"
12407+
},
12408+
{
12409+
"name": "V1",
12410+
"fields": [
12411+
{
12412+
"name": "maxMarginRatio",
12413+
"type": {
12414+
"option": "u16"
12415+
}
12416+
}
12417+
]
12418+
}
12419+
]
12420+
}
12421+
},
1240012422
{
1240112423
"name": "PostOnlyParam",
1240212424
"type": {
@@ -15974,8 +15996,5 @@
1597415996
"name": "InvalidIfRebalanceSwap",
1597515997
"msg": "Invalid If Rebalance Swap"
1597615998
}
15977-
],
15978-
"metadata": {
15979-
"address": "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH"
15980-
}
15999+
]
1598116000
}

sdk/src/types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ export type SignedMsgOrderParamsMessage = {
12471247
uuid: Uint8Array;
12481248
takeProfitOrderParams: SignedMsgTriggerOrderParams | null;
12491249
stopLossOrderParams: SignedMsgTriggerOrderParams | null;
1250-
maxMarginRatio?: number | null;
1250+
ext: SignedMsgExtensions;
12511251
};
12521252

12531253
export type SignedMsgOrderParamsDelegateMessage = {
@@ -1257,9 +1257,16 @@ export type SignedMsgOrderParamsDelegateMessage = {
12571257
takerPubkey: PublicKey;
12581258
takeProfitOrderParams: SignedMsgTriggerOrderParams | null;
12591259
stopLossOrderParams: SignedMsgTriggerOrderParams | null;
1260-
maxMarginRatio?: number | null;
1260+
ext: SignedMsgExtensions;
12611261
};
12621262

1263+
export class SignedMsgExtensions {
1264+
static readonly V0 = { v0: {} };
1265+
static V1(params: { maxMarginRatio: number }) {
1266+
return { v1: params } as const;
1267+
}
1268+
}
1269+
12631270
export type SignedMsgTriggerOrderParams = {
12641271
triggerPrice: BN;
12651272
baseAssetAmount: BN;

0 commit comments

Comments
 (0)