-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
funds-manager: Reorganize api types and helpers ahead of execution impl
- Loading branch information
Showing
16 changed files
with
649 additions
and
475 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//! Serialization helpers for the funds manager API | ||
|
||
/// A module for serializing and deserializing addresses as strings | ||
pub(crate) mod address_string_serialization { | ||
use std::str::FromStr; | ||
|
||
use ethers::types::Address; | ||
use serde::{de::Error, Deserialize, Deserializer, Serializer}; | ||
|
||
/// Serialize an address to a string | ||
pub fn serialize<S: Serializer>(address: &Address, s: S) -> Result<S::Ok, S::Error> { | ||
s.serialize_str(&address.to_string()) | ||
} | ||
|
||
/// Deserialize a string to an address | ||
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Address, D::Error> { | ||
let s = String::deserialize(d)?; | ||
Address::from_str(&s).map_err(|_| D::Error::custom("Invalid address")) | ||
} | ||
} | ||
|
||
/// A module for serializing and deserializing U256 as strings | ||
pub(crate) mod u256_string_serialization { | ||
use ethers::types::U256; | ||
use serde::{de::Error, Deserialize, Deserializer, Serializer}; | ||
|
||
/// Serialize a U256 to a string | ||
pub fn serialize<S: Serializer>(value: &U256, s: S) -> Result<S::Ok, S::Error> { | ||
s.serialize_str(&value.to_string()) | ||
} | ||
|
||
/// Deserialize a string to a U256 | ||
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<U256, D::Error> { | ||
let s = String::deserialize(d)?; | ||
U256::from_dec_str(&s).map_err(|_| D::Error::custom("Invalid U256 value")) | ||
} | ||
} | ||
|
||
/// A module for serializing and deserializing bytes from a hex string | ||
pub(crate) mod bytes_string_serialization { | ||
use ethers::types::Bytes; | ||
use hex::FromHex; | ||
use serde::{de::Error, Deserialize, Deserializer, Serializer}; | ||
|
||
/// Serialize bytes to a hex string | ||
pub fn serialize<S: Serializer>(value: &Bytes, s: S) -> Result<S::Ok, S::Error> { | ||
let hex = format!("{value:#x}"); | ||
s.serialize_str(&hex) | ||
} | ||
|
||
/// Deserialize a hex string to bytes | ||
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Bytes, D::Error> { | ||
let s = String::deserialize(d)?; | ||
Bytes::from_hex(s).map_err(|_| D::Error::custom("Invalid bytes value")) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use ethers::types::{Address, Bytes, U256}; | ||
use rand::{thread_rng, Rng}; | ||
|
||
/// Test serialization and deserialization of an address | ||
#[test] | ||
fn test_address_serialization() { | ||
let addr = Address::random(); | ||
let serialized = serde_json::to_string(&addr).unwrap(); | ||
let deserialized: Address = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!(addr, deserialized); | ||
} | ||
|
||
/// Test serialization and deserialization of a U256 | ||
#[test] | ||
fn test_u256_serialization() { | ||
let mut rng = thread_rng(); | ||
let mut bytes = [0u8; 32]; | ||
rng.fill(&mut bytes); | ||
let value = U256::from(bytes); | ||
|
||
let serialized = serde_json::to_string(&value).unwrap(); | ||
let deserialized: U256 = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!(value, deserialized); | ||
} | ||
|
||
/// Test serialization and deserialization of bytes | ||
#[test] | ||
fn test_bytes_serialization() { | ||
const N: usize = 32; | ||
let mut rng = thread_rng(); | ||
let bytes: Bytes = (0..N).map(|_| rng.gen_range(0..=u8::MAX)).collect(); | ||
|
||
let serialized = serde_json::to_string(&bytes).unwrap(); | ||
let deserialized: Bytes = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!(bytes, deserialized); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! API types for managing and redeeming fees | ||
|
||
use renegade_api::types::ApiWallet; | ||
use serde::{Deserialize, Serialize}; | ||
use uuid::Uuid; | ||
|
||
// -------------- | ||
// | Api Routes | | ||
// -------------- | ||
|
||
/// The route through which a client may start the fee indexing process | ||
pub const INDEX_FEES_ROUTE: &str = "index-fees"; | ||
/// The route through which a client may start the fee redemption process | ||
pub const REDEEM_FEES_ROUTE: &str = "redeem-fees"; | ||
/// The route to get fee wallets | ||
pub const GET_FEE_WALLETS_ROUTE: &str = "get-fee-wallets"; | ||
/// The route to withdraw a fee balance | ||
pub const WITHDRAW_FEE_BALANCE_ROUTE: &str = "withdraw-fee-balance"; | ||
|
||
// ------------- | ||
// | Api Types | | ||
// ------------- | ||
|
||
/// The response containing fee wallets | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct FeeWalletsResponse { | ||
/// The wallets managed by the funds manager | ||
pub wallets: Vec<ApiWallet>, | ||
} | ||
|
||
/// The request body for withdrawing a fee balance | ||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct WithdrawFeeBalanceRequest { | ||
/// The ID of the wallet to withdraw from | ||
pub wallet_id: Uuid, | ||
/// The mint of the asset to withdraw | ||
pub mint: String, | ||
} |
Oops, something went wrong.