| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702 |
- use crate::rate::Rate;
- use crate::symbol_state::SymbolState;
- use crate::time::TimestampUs;
- use crate::PriceFeedId;
- use crate::{api::Channel, price::Price};
- use serde::{Deserialize, Serialize};
- use std::time::Duration;
- #[derive(Serialize, Deserialize, Debug, Default, Eq, PartialEq)]
- #[serde(untagged)]
- pub enum JrpcId {
- String(String),
- Int(i64),
- #[default]
- Null,
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
- pub struct PythLazerAgentJrpcV1 {
- pub jsonrpc: JsonRpcVersion,
- #[serde(flatten)]
- pub params: JrpcCall,
- #[serde(default)]
- pub id: JrpcId,
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
- #[serde(tag = "method", content = "params")]
- #[serde(rename_all = "snake_case")]
- pub enum JrpcCall {
- PushUpdate(FeedUpdateParams),
- PushUpdates(Vec<FeedUpdateParams>),
- GetMetadata(GetMetadataParams),
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
- pub struct FeedUpdateParams {
- pub feed_id: PriceFeedId,
- pub source_timestamp: TimestampUs,
- pub update: UpdateParams,
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
- #[serde(tag = "type")]
- pub enum UpdateParams {
- #[serde(rename = "price")]
- PriceUpdate {
- price: Price,
- best_bid_price: Option<Price>,
- best_ask_price: Option<Price>,
- },
- #[serde(rename = "funding_rate")]
- FundingRateUpdate {
- price: Option<Price>,
- rate: Rate,
- #[serde(default = "default_funding_rate_interval", with = "humantime_serde")]
- funding_rate_interval: Option<Duration>,
- },
- }
- fn default_funding_rate_interval() -> Option<Duration> {
- None
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
- pub struct Filter {
- pub name: Option<String>,
- pub asset_type: Option<String>,
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
- pub struct GetMetadataParams {
- pub names: Option<Vec<String>>,
- pub asset_types: Option<Vec<String>>,
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
- pub enum JsonRpcVersion {
- #[serde(rename = "2.0")]
- V2,
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
- #[serde(untagged)]
- pub enum JrpcResponse<T> {
- Success(JrpcSuccessResponse<T>),
- Error(JrpcErrorResponse),
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
- pub struct JrpcSuccessResponse<T> {
- pub jsonrpc: JsonRpcVersion,
- pub result: T,
- pub id: JrpcId,
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
- pub struct JrpcErrorResponse {
- pub jsonrpc: JsonRpcVersion,
- pub error: JrpcErrorObject,
- pub id: JrpcId,
- }
- #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
- pub struct JrpcErrorObject {
- pub code: i64,
- pub message: String,
- #[serde(skip_serializing_if = "Option::is_none")]
- pub data: Option<serde_json::Value>,
- }
- #[derive(Debug, Eq, PartialEq)]
- pub enum JrpcError {
- ParseError(String),
- InternalError(String),
- SendUpdateError(FeedUpdateParams),
- }
- // note: error codes can be found in the rfc https://www.jsonrpc.org/specification#error_object
- impl From<JrpcError> for JrpcErrorObject {
- fn from(error: JrpcError) -> Self {
- match error {
- JrpcError::ParseError(error_message) => JrpcErrorObject {
- code: -32700,
- message: "Parse error".to_string(),
- data: Some(error_message.into()),
- },
- JrpcError::InternalError(error_message) => JrpcErrorObject {
- code: -32603,
- message: "Internal error".to_string(),
- data: Some(error_message.into()),
- },
- JrpcError::SendUpdateError(feed_update_params) => JrpcErrorObject {
- code: -32000,
- message: "Internal error".to_string(),
- data: Some(serde_json::to_value(feed_update_params).unwrap()),
- },
- }
- }
- }
- #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
- pub struct SymbolMetadata {
- pub pyth_lazer_id: PriceFeedId,
- pub name: String,
- pub symbol: String,
- pub description: String,
- pub asset_type: String,
- pub exponent: i16,
- pub cmc_id: Option<u32>,
- #[serde(default, with = "humantime_serde", alias = "interval")]
- pub funding_rate_interval: Option<Duration>,
- pub min_publishers: u16,
- pub min_channel: Channel,
- pub state: SymbolState,
- pub hermes_id: Option<String>,
- pub quote_currency: Option<String>,
- }
- #[cfg(test)]
- mod tests {
- use super::*;
- use crate::jrpc::JrpcCall::{GetMetadata, PushUpdate};
- #[test]
- fn test_push_update_price() {
- let json = r#"
- {
- "jsonrpc": "2.0",
- "method": "push_update",
- "params": {
- "feed_id": 1,
- "source_timestamp": 124214124124,
- "update": {
- "type": "price",
- "price": 1234567890,
- "best_bid_price": 1234567891,
- "best_ask_price": 1234567892
- }
- },
- "id": 1
- }
- "#;
- let expected = PythLazerAgentJrpcV1 {
- jsonrpc: JsonRpcVersion::V2,
- params: PushUpdate(FeedUpdateParams {
- feed_id: PriceFeedId(1),
- source_timestamp: TimestampUs::from_micros(124214124124),
- update: UpdateParams::PriceUpdate {
- price: Price::from_integer(1234567890, 0).unwrap(),
- best_bid_price: Some(Price::from_integer(1234567891, 0).unwrap()),
- best_ask_price: Some(Price::from_integer(1234567892, 0).unwrap()),
- },
- }),
- id: JrpcId::Int(1),
- };
- assert_eq!(
- serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
- expected
- );
- }
- #[test]
- fn test_push_update_price_string_id() {
- let json = r#"
- {
- "jsonrpc": "2.0",
- "method": "push_update",
- "params": {
- "feed_id": 1,
- "source_timestamp": 124214124124,
- "update": {
- "type": "price",
- "price": 1234567890,
- "best_bid_price": 1234567891,
- "best_ask_price": 1234567892
- }
- },
- "id": "b6bb54a0-ea8d-439d-97a7-3b06befa0e76"
- }
- "#;
- let expected = PythLazerAgentJrpcV1 {
- jsonrpc: JsonRpcVersion::V2,
- params: PushUpdate(FeedUpdateParams {
- feed_id: PriceFeedId(1),
- source_timestamp: TimestampUs::from_micros(124214124124),
- update: UpdateParams::PriceUpdate {
- price: Price::from_integer(1234567890, 0).unwrap(),
- best_bid_price: Some(Price::from_integer(1234567891, 0).unwrap()),
- best_ask_price: Some(Price::from_integer(1234567892, 0).unwrap()),
- },
- }),
- id: JrpcId::String("b6bb54a0-ea8d-439d-97a7-3b06befa0e76".to_string()),
- };
- assert_eq!(
- serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
- expected
- );
- }
- #[test]
- fn test_push_update_price_null_id() {
- let json = r#"
- {
- "jsonrpc": "2.0",
- "method": "push_update",
- "params": {
- "feed_id": 1,
- "source_timestamp": 124214124124,
- "update": {
- "type": "price",
- "price": 1234567890,
- "best_bid_price": 1234567891,
- "best_ask_price": 1234567892
- }
- },
- "id": null
- }
- "#;
- let expected = PythLazerAgentJrpcV1 {
- jsonrpc: JsonRpcVersion::V2,
- params: PushUpdate(FeedUpdateParams {
- feed_id: PriceFeedId(1),
- source_timestamp: TimestampUs::from_micros(124214124124),
- update: UpdateParams::PriceUpdate {
- price: Price::from_integer(1234567890, 0).unwrap(),
- best_bid_price: Some(Price::from_integer(1234567891, 0).unwrap()),
- best_ask_price: Some(Price::from_integer(1234567892, 0).unwrap()),
- },
- }),
- id: JrpcId::Null,
- };
- assert_eq!(
- serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
- expected
- );
- }
- #[test]
- fn test_push_update_price_without_id() {
- let json = r#"
- {
- "jsonrpc": "2.0",
- "method": "push_update",
- "params": {
- "feed_id": 1,
- "source_timestamp": 745214124124,
- "update": {
- "type": "price",
- "price": 5432,
- "best_bid_price": 5432,
- "best_ask_price": 5432
- }
- }
- }
- "#;
- let expected = PythLazerAgentJrpcV1 {
- jsonrpc: JsonRpcVersion::V2,
- params: PushUpdate(FeedUpdateParams {
- feed_id: PriceFeedId(1),
- source_timestamp: TimestampUs::from_micros(745214124124),
- update: UpdateParams::PriceUpdate {
- price: Price::from_integer(5432, 0).unwrap(),
- best_bid_price: Some(Price::from_integer(5432, 0).unwrap()),
- best_ask_price: Some(Price::from_integer(5432, 0).unwrap()),
- },
- }),
- id: JrpcId::Null,
- };
- assert_eq!(
- serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
- expected
- );
- }
- #[test]
- fn test_push_update_price_without_bid_ask() {
- let json = r#"
- {
- "jsonrpc": "2.0",
- "method": "push_update",
- "params": {
- "feed_id": 1,
- "source_timestamp": 124214124124,
- "update": {
- "type": "price",
- "price": 1234567890
- }
- },
- "id": 1
- }
- "#;
- let expected = PythLazerAgentJrpcV1 {
- jsonrpc: JsonRpcVersion::V2,
- params: PushUpdate(FeedUpdateParams {
- feed_id: PriceFeedId(1),
- source_timestamp: TimestampUs::from_micros(124214124124),
- update: UpdateParams::PriceUpdate {
- price: Price::from_integer(1234567890, 0).unwrap(),
- best_bid_price: None,
- best_ask_price: None,
- },
- }),
- id: JrpcId::Int(1),
- };
- assert_eq!(
- serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
- expected
- );
- }
- #[test]
- fn test_push_update_funding_rate() {
- let json = r#"
- {
- "jsonrpc": "2.0",
- "method": "push_update",
- "params": {
- "feed_id": 1,
- "source_timestamp": 124214124124,
- "update": {
- "type": "funding_rate",
- "price": 1234567890,
- "rate": 1234567891,
- "funding_rate_interval": "8h"
- }
- },
- "id": 1
- }
- "#;
- let expected = PythLazerAgentJrpcV1 {
- jsonrpc: JsonRpcVersion::V2,
- params: PushUpdate(FeedUpdateParams {
- feed_id: PriceFeedId(1),
- source_timestamp: TimestampUs::from_micros(124214124124),
- update: UpdateParams::FundingRateUpdate {
- price: Some(Price::from_integer(1234567890, 0).unwrap()),
- rate: Rate::from_integer(1234567891, 0).unwrap(),
- funding_rate_interval: Duration::from_secs(28800).into(),
- },
- }),
- id: JrpcId::Int(1),
- };
- assert_eq!(
- serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
- expected
- );
- }
- #[test]
- fn test_push_update_funding_rate_without_price() {
- let json = r#"
- {
- "jsonrpc": "2.0",
- "method": "push_update",
- "params": {
- "feed_id": 1,
- "source_timestamp": 124214124124,
- "update": {
- "type": "funding_rate",
- "rate": 1234567891
- }
- },
- "id": 1
- }
- "#;
- let expected = PythLazerAgentJrpcV1 {
- jsonrpc: JsonRpcVersion::V2,
- params: PushUpdate(FeedUpdateParams {
- feed_id: PriceFeedId(1),
- source_timestamp: TimestampUs::from_micros(124214124124),
- update: UpdateParams::FundingRateUpdate {
- price: None,
- rate: Rate::from_integer(1234567891, 0).unwrap(),
- funding_rate_interval: None,
- },
- }),
- id: JrpcId::Int(1),
- };
- assert_eq!(
- serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
- expected
- );
- }
- #[test]
- fn test_send_get_metadata() {
- let json = r#"
- {
- "jsonrpc": "2.0",
- "method": "get_metadata",
- "params": {
- "names": ["BTC/USD"],
- "asset_types": ["crypto"]
- },
- "id": 1
- }
- "#;
- let expected = PythLazerAgentJrpcV1 {
- jsonrpc: JsonRpcVersion::V2,
- params: GetMetadata(GetMetadataParams {
- names: Some(vec!["BTC/USD".to_string()]),
- asset_types: Some(vec!["crypto".to_string()]),
- }),
- id: JrpcId::Int(1),
- };
- assert_eq!(
- serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
- expected
- );
- }
- #[test]
- fn test_get_metadata_without_filters() {
- let json = r#"
- {
- "jsonrpc": "2.0",
- "method": "get_metadata",
- "params": {},
- "id": 1
- }
- "#;
- let expected = PythLazerAgentJrpcV1 {
- jsonrpc: JsonRpcVersion::V2,
- params: GetMetadata(GetMetadataParams {
- names: None,
- asset_types: None,
- }),
- id: JrpcId::Int(1),
- };
- assert_eq!(
- serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
- expected
- );
- }
- #[test]
- fn test_response_format_error() {
- let response = serde_json::from_str::<JrpcErrorResponse>(
- r#"
- {
- "jsonrpc": "2.0",
- "id": 2,
- "error": {
- "message": "Internal error",
- "code": -32603
- }
- }
- "#,
- )
- .unwrap();
- assert_eq!(
- response,
- JrpcErrorResponse {
- jsonrpc: JsonRpcVersion::V2,
- error: JrpcErrorObject {
- code: -32603,
- message: "Internal error".to_string(),
- data: None,
- },
- id: JrpcId::Int(2),
- }
- );
- }
- #[test]
- fn test_response_format_error_string_id() {
- let response = serde_json::from_str::<JrpcErrorResponse>(
- r#"
- {
- "jsonrpc": "2.0",
- "id": "62b627dc-5599-43dd-b2c2-9c4d30f4fdb4",
- "error": {
- "message": "Internal error",
- "code": -32603
- }
- }
- "#,
- )
- .unwrap();
- assert_eq!(
- response,
- JrpcErrorResponse {
- jsonrpc: JsonRpcVersion::V2,
- error: JrpcErrorObject {
- code: -32603,
- message: "Internal error".to_string(),
- data: None,
- },
- id: JrpcId::String("62b627dc-5599-43dd-b2c2-9c4d30f4fdb4".to_string())
- }
- );
- }
- #[test]
- pub fn test_response_format_success() {
- let response = serde_json::from_str::<JrpcSuccessResponse<String>>(
- r#"
- {
- "jsonrpc": "2.0",
- "id": 2,
- "result": "success"
- }
- "#,
- )
- .unwrap();
- assert_eq!(
- response,
- JrpcSuccessResponse::<String> {
- jsonrpc: JsonRpcVersion::V2,
- result: "success".to_string(),
- id: JrpcId::Int(2),
- }
- );
- }
- #[test]
- pub fn test_response_format_success_string_id() {
- let response = serde_json::from_str::<JrpcSuccessResponse<String>>(
- r#"
- {
- "jsonrpc": "2.0",
- "id": "62b627dc-5599-43dd-b2c2-9c4d30f4fdb4",
- "result": "success"
- }
- "#,
- )
- .unwrap();
- assert_eq!(
- response,
- JrpcSuccessResponse::<String> {
- jsonrpc: JsonRpcVersion::V2,
- result: "success".to_string(),
- id: JrpcId::String("62b627dc-5599-43dd-b2c2-9c4d30f4fdb4".to_string()),
- }
- );
- }
- #[test]
- pub fn test_parse_response() {
- let success_response = serde_json::from_str::<JrpcResponse<String>>(
- r#"
- {
- "jsonrpc": "2.0",
- "id": 2,
- "result": "success"
- }"#,
- )
- .unwrap();
- assert_eq!(
- success_response,
- JrpcResponse::Success(JrpcSuccessResponse::<String> {
- jsonrpc: JsonRpcVersion::V2,
- result: "success".to_string(),
- id: JrpcId::Int(2),
- })
- );
- let error_response = serde_json::from_str::<JrpcResponse<String>>(
- r#"
- {
- "jsonrpc": "2.0",
- "id": 3,
- "error": {
- "code": -32603,
- "message": "Internal error"
- }
- }"#,
- )
- .unwrap();
- assert_eq!(
- error_response,
- JrpcResponse::Error(JrpcErrorResponse {
- jsonrpc: JsonRpcVersion::V2,
- error: JrpcErrorObject {
- code: -32603,
- message: "Internal error".to_string(),
- data: None,
- },
- id: JrpcId::Int(3),
- })
- );
- }
- #[test]
- pub fn test_parse_response_string_id() {
- let success_response = serde_json::from_str::<JrpcResponse<String>>(
- r#"
- {
- "jsonrpc": "2.0",
- "id": "id-2",
- "result": "success"
- }"#,
- )
- .unwrap();
- assert_eq!(
- success_response,
- JrpcResponse::Success(JrpcSuccessResponse::<String> {
- jsonrpc: JsonRpcVersion::V2,
- result: "success".to_string(),
- id: JrpcId::String("id-2".to_string()),
- })
- );
- let error_response = serde_json::from_str::<JrpcResponse<String>>(
- r#"
- {
- "jsonrpc": "2.0",
- "id": "id-3",
- "error": {
- "code": -32603,
- "message": "Internal error"
- }
- }"#,
- )
- .unwrap();
- assert_eq!(
- error_response,
- JrpcResponse::Error(JrpcErrorResponse {
- jsonrpc: JsonRpcVersion::V2,
- error: JrpcErrorObject {
- code: -32603,
- message: "Internal error".to_string(),
- data: None,
- },
- id: JrpcId::String("id-3".to_string()),
- })
- );
- }
- }
|