Skip to content

Commit

Permalink
feat/refactor: add price field for FundingRate, and rename it to Fund…
Browse files Browse the repository at this point in the history
…ingInfo
  • Loading branch information
RequiemOfSoul committed Nov 13, 2023
1 parent 6ebe78e commit e5e0635
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
13 changes: 7 additions & 6 deletions provider/src/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bigdecimal::BigDecimal;
use std::collections::HashMap;
use bigdecimal::num_bigint::BigInt;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -142,7 +143,7 @@ pub struct ResponseMarginParams {
pub struct ResponseContractParams {
pub initial_margin_rate: u16,
pub maintenance_margin_rate: u16,
pub acc_funding_rate: i32,
pub acc_funding_price: BigInt,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand All @@ -164,21 +165,21 @@ pub struct ResponsePosition {
pub direction: bool,
pub price: BigDecimal,
pub size: BigDecimal,
pub acc_funding_rate: i32,
pub acc_funding_price: BigIntSerdeWrapper,
}

impl ResponsePosition {
pub fn new(
direction: bool,
price: BigDecimal,
size: BigDecimal,
acc_funding_rate: i32,
acc_funding_price: BigIntSerdeWrapper,
) -> Self {
Self {
direction,
price,
size,
acc_funding_rate,
acc_funding_price,
}
}
}
Expand Down Expand Up @@ -307,8 +308,8 @@ pub struct ContractParamsUpdateResp {
pub new_maintenance_margin_rate: u16,
pub old_initial_margin_rate: u16,
pub new_initial_margin_rate: u16,
pub old_acc_funding_rate: i32,
pub new_acc_funding_rate: i32,
pub old_acc_funding_price: BigIntSerdeWrapper,
pub new_acc_funding_price: BigIntSerdeWrapper,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
17 changes: 10 additions & 7 deletions types/src/tx_type/contract/funding.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
use crate::basic_types::pack::pack_fee_amount;
use crate::basic_types::{AccountId, GetBytes, Nonce, PairId, SubAccountId, TokenId};
use crate::params::{
FUNDING_RATE_BYTES, PAIR_BIT_WIDTH, SIGNED_BATCH_FUNDING_BIT_WIDTH, SIGNED_FUNDING_BIT_WIDTH,
};
use crate::params::{FUNDING_RATE_BYTES, PAIR_BIT_WIDTH, PRICE_BIT_WIDTH, SIGNED_BATCH_FUNDING_BIT_WIDTH, SIGNED_FUNDING_BIT_WIDTH};
use crate::prelude::validator::*;
#[cfg(feature = "ffi")]
use crate::tx_builder::FundingBuilder;
use crate::tx_type::{TxTrait, ZkSignatureTrait};
use num::BigUint;
use num::traits::ToBytes;
use serde::{Deserialize, Serialize};
use validator::Validate;
use zklink_sdk_signers::zklink_signer::ZkLinkSignature;
use zklink_sdk_utils::serde::BigUintSerdeAsRadix10Str;
use crate::prelude::pad_front;

#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize, Validate)]
#[derive(Default, Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase")]
pub struct FundingRate {
pub struct FundingInfo {
#[validate(custom = "pair_validator")]
pub pair_id: PairId,
// TODO: can it be lower than 0?
#[serde(with = "BigUintSerdeAsRadix10Str")]
#[validate(custom = "price_validator")]
pub price: BigUint,
pub funding_rate: i16,
}

impl GetBytes for FundingRate {
impl GetBytes for FundingInfo {
fn get_bytes(&self) -> Vec<u8> {
let bytes_len = self.bytes_len();
let mut funding_rate_encode = Vec::with_capacity(bytes_len);
funding_rate_encode.push(*self.pair_id as u8);
funding_rate_encode.extend(pad_front(&self.price.to_be_bytes(), PRICE_BIT_WIDTH));
// For the convenience of the circuit, we use the true code instead of the original complement.
let mut rate_bytes = self.funding_rate.unsigned_abs().to_be_bytes();
if self.funding_rate.is_negative() {
Expand Down
2 changes: 1 addition & 1 deletion types/src/tx_type/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod update_global_var;

pub use auto_deleveraging::AutoDeleveraging;
pub use contract_matching::{Contract, ContractMatching};
pub use funding::{Funding, FundingRate};
pub use funding::{Funding, FundingInfo};
pub use liquidation::Liquidation;
pub use prices::{ContractPrice, OraclePrices, SpotPriceInfo};
pub use update_global_var::{Parameter, UpdateGlobalVar};
Expand Down
8 changes: 4 additions & 4 deletions types/src/tx_type/contract/update_global_var.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::funding::FundingRate;
use super::funding::FundingInfo;
use crate::basic_types::{AccountId, ChainId, GetBytes, MarginId, PairId, SubAccountId, TokenId};
use crate::prelude::validator::*;
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -66,7 +66,7 @@ pub enum Parameter {
},
/// update the funding rates to accumulated funding rates of the Global Vars for all position(contract pair) in this period
#[serde(rename_all = "camelCase")]
FundingRates { funding_rates: Vec<FundingRate> },
FundingInfos { infos: Vec<FundingInfo> },
/// modify the initial margin rate of every margin
#[serde(rename_all = "camelCase")]
InitialMarginRate { pair_id: PairId, rate: u16 },
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Parameter {
Parameter::MarginInfo { .. } => Self::MARGIN_INFO_PARAM_TYPE,
Parameter::InitialMarginRate { .. } => Self::INITIAL_MARGIN_RATE_PARAM_TYPE,
Parameter::MaintenanceMarginRate { .. } => Self::MAINTENANCE_MARGIN_RATE_PARAM_TYPE,
Parameter::FundingRates { .. } => Self::FUNDING_RATE_PARAM_TYPE,
Parameter::FundingInfos { .. } => Self::FUNDING_RATE_PARAM_TYPE,
}
}

Expand Down Expand Up @@ -124,7 +124,7 @@ impl GetBytes for Parameter {
.into_iter()
.chain(rate.to_be_bytes())
.collect(),
Parameter::FundingRates { funding_rates } => funding_rates.get_bytes(),
Parameter::FundingInfos{ infos } => infos.get_bytes(),
});
bytes
}
Expand Down
2 changes: 1 addition & 1 deletion types/src/tx_type/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub fn order_nonce_validator(nonce: &Nonce) -> Result<(), ValidationError> {
///
pub fn parameter_validator(param: &Parameter) -> Result<(), ValidationError> {
match param {
Parameter::FundingRates { funding_rates } => {
Parameter::FundingInfos { infos: funding_rates } => {
if funding_rates.len() != USED_POSITION_NUMBER {
return Err(ValidationError::new("update funding rates number mismatch"));
}
Expand Down

0 comments on commit e5e0635

Please sign in to comment.