From e5e063507f47e1a33c99a2732d9e41b22b87209a Mon Sep 17 00:00:00 2001 From: RequiemOfSouls Date: Mon, 13 Nov 2023 19:27:07 +0800 Subject: [PATCH] feat/refactor: add price field for FundingRate, and rename it to FundingInfo --- provider/src/response.rs | 13 +++++++------ types/src/tx_type/contract/funding.rs | 17 ++++++++++------- types/src/tx_type/contract/mod.rs | 2 +- types/src/tx_type/contract/update_global_var.rs | 8 ++++---- types/src/tx_type/validator.rs | 2 +- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/provider/src/response.rs b/provider/src/response.rs index af19e741..faad5969 100644 --- a/provider/src/response.rs +++ b/provider/src/response.rs @@ -1,5 +1,6 @@ use bigdecimal::BigDecimal; use std::collections::HashMap; +use bigdecimal::num_bigint::BigInt; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; @@ -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)] @@ -164,7 +165,7 @@ pub struct ResponsePosition { pub direction: bool, pub price: BigDecimal, pub size: BigDecimal, - pub acc_funding_rate: i32, + pub acc_funding_price: BigIntSerdeWrapper, } impl ResponsePosition { @@ -172,13 +173,13 @@ impl ResponsePosition { 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, } } } @@ -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)] diff --git a/types/src/tx_type/contract/funding.rs b/types/src/tx_type/contract/funding.rs index 29f52e78..1ee6c93b 100644 --- a/types/src/tx_type/contract/funding.rs +++ b/types/src/tx_type/contract/funding.rs @@ -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 { 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() { diff --git a/types/src/tx_type/contract/mod.rs b/types/src/tx_type/contract/mod.rs index 78470a7a..e01b42ba 100644 --- a/types/src/tx_type/contract/mod.rs +++ b/types/src/tx_type/contract/mod.rs @@ -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}; diff --git a/types/src/tx_type/contract/update_global_var.rs b/types/src/tx_type/contract/update_global_var.rs index 41fdd116..8eb17aa2 100644 --- a/types/src/tx_type/contract/update_global_var.rs +++ b/types/src/tx_type/contract/update_global_var.rs @@ -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")] @@ -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 }, + FundingInfos { infos: Vec }, /// modify the initial margin rate of every margin #[serde(rename_all = "camelCase")] InitialMarginRate { pair_id: PairId, rate: u16 }, @@ -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, } } @@ -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 } diff --git a/types/src/tx_type/validator.rs b/types/src/tx_type/validator.rs index ddaa07f4..e077b9b4 100644 --- a/types/src/tx_type/validator.rs +++ b/types/src/tx_type/validator.rs @@ -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")); }