Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Custom runtime api versioning + struct versioning #5415

Merged
merged 7 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ web3 = { version = "0.19" }
x25519-dalek = { version = "2.0" }
zeroize = { version = "1.7.0" }
zmq = { git = "https://github.com/chainflip-io/rust-zmq.git", tag = "chainflip-v0.9.2+1" }
obake = { version = "1.0.5" }

# PolkadotSdk Pallets
pallet-aura = { git = "https://github.com/chainflip-io/polkadot-sdk.git", tag = "chainflip-substrate-1.15.2+2", default-features = false }
Expand Down
143 changes: 79 additions & 64 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use pallet_cf_pools::{
use pallet_cf_swapping::SwapLegInfo;
use sc_client_api::{BlockchainEvents, HeaderBackend};
use serde::{Deserialize, Serialize};
use sp_api::{ApiError, CallApiAt};
use sp_api::{ApiError, ApiExt, CallApiAt};
use sp_core::U256;
use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT, UniqueSaturatedInto},
Expand All @@ -55,10 +55,10 @@ use state_chain_runtime::{
AuctionState, BoostPoolDepth, BoostPoolDetails, BrokerInfo, ChainAccounts,
CustomRuntimeApi, DispatchErrorWithMessage, ElectoralRuntimeApi, FailingWitnessValidators,
LiquidityProviderBoostPoolInfo, LiquidityProviderInfo, RuntimeApiPenalty,
TaintedTransactionEvents, ValidatorInfo, VaultSwapDetails,
SimulatedSwapInformation, TaintedTransactionEvents, ValidatorInfo, VaultSwapDetails,
},
safe_mode::RuntimeSafeMode,
Hash, NetworkFee, SolanaInstance,
Block, Hash, NetworkFee, SolanaInstance,
};
use std::{
collections::{BTreeMap, HashMap},
Expand Down Expand Up @@ -422,6 +422,26 @@ pub struct RpcSwapOutputV2 {
pub egress_fee: RpcFee,
pub broker_commission: RpcFee,
}
fn into_rpc_swap_output(
simulated_swap_info: SimulatedSwapInformation,
from_asset: Asset,
to_asset: Asset,
) -> RpcSwapOutputV2 {
RpcSwapOutputV2 {
intermediary: simulated_swap_info.intermediary.map(Into::into),
output: simulated_swap_info.output.into(),
network_fee: RpcFee {
asset: cf_primitives::STABLE_ASSET,
amount: simulated_swap_info.network_fee.into(),
},
ingress_fee: RpcFee { asset: from_asset, amount: simulated_swap_info.ingress_fee.into() },
egress_fee: RpcFee { asset: to_asset, amount: simulated_swap_info.egress_fee.into() },
broker_commission: RpcFee {
asset: cf_primitives::STABLE_ASSET,
amount: simulated_swap_info.broker_fee.into(),
},
}
}

#[derive(Serialize, Deserialize, Clone)]
pub enum SwapRateV2AdditionalOrder {
Expand Down Expand Up @@ -1424,71 +1444,66 @@ where
additional_orders: Option<Vec<SwapRateV2AdditionalOrder>>,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<RpcSwapOutputV2> {
let amount = amount
.try_into()
.map_err(|_| "Swap input amount too large.")
.and_then(|amount: u128| {
if amount == 0 {
Err("Swap input amount cannot be zero.")
} else {
Ok(amount)
}
})
.map_err(|s| ErrorObject::owned(ErrorCode::InvalidParams.code(), s, None::<()>))?;
let additional_orders = additional_orders.map(|additional_orders| {
additional_orders
.into_iter()
.map(|additional_order| match additional_order {
SwapRateV2AdditionalOrder::LimitOrder {
base_asset,
quote_asset,
side,
tick,
sell_amount,
} =>
state_chain_runtime::runtime_apis::SimulateSwapAdditionalOrder::LimitOrder {
base_asset,
quote_asset,
side,
tick,
sell_amount: sell_amount.unique_saturated_into(),
},
})
.collect()
});
self.with_runtime_api(at, |api, hash| {
Ok::<_, CfApiError>(
api.cf_pool_simulate_swap(
if api.api_version::<dyn CustomRuntimeApi<Block>>(hash).unwrap().unwrap() < 2 {
let old_result = api.cf_pool_simulate_swap_before_version_2(
hash,
from_asset,
to_asset,
amount
.try_into()
.map_err(|_| "Swap input amount too large.")
.and_then(|amount: u128| {
if amount == 0 {
Err("Swap input amount cannot be zero.")
} else {
Ok(amount)
}
})
.map_err(|s| {
ErrorObject::owned(ErrorCode::InvalidParams.code(), s, None::<()>)
})?,
broker_commission,
dca_parameters,
additional_orders.map(|additional_orders| {
additional_orders
.into_iter()
.map(|additional_order| {
match additional_order {
SwapRateV2AdditionalOrder::LimitOrder {
base_asset,
quote_asset,
side,
tick,
sell_amount,
} => state_chain_runtime::runtime_apis::SimulateSwapAdditionalOrder::LimitOrder {
base_asset,
quote_asset,
side,
tick,
sell_amount: sell_amount.unique_saturated_into(),
}
}
})
.collect()
}),
)?
.map(|simulated_swap_info_v2| RpcSwapOutputV2 {
intermediary: simulated_swap_info_v2.intermediary.map(Into::into),
output: simulated_swap_info_v2.output.into(),
network_fee: RpcFee {
asset: cf_primitives::STABLE_ASSET,
amount: simulated_swap_info_v2.network_fee.into(),
},
ingress_fee: RpcFee {
asset: from_asset,
amount: simulated_swap_info_v2.ingress_fee.into(),
},
egress_fee: RpcFee {
asset: to_asset,
amount: simulated_swap_info_v2.egress_fee.into(),
},
broker_commission: RpcFee {
asset: cf_primitives::STABLE_ASSET,
amount: simulated_swap_info_v2.broker_fee.into(),
},
})?,
)
amount,
additional_orders,
)?;
Ok(old_result.map(|old_version| {
into_rpc_swap_output(old_version.into(), from_asset, to_asset)
})?)
} else {
Ok::<_, CfApiError>(
api.cf_pool_simulate_swap(
hash,
from_asset,
to_asset,
amount,
broker_commission,
dca_parameters,
additional_orders,
)?
.map(|simulated_swap_info_v2| {
into_rpc_swap_output(simulated_swap_info_v2, from_asset, to_asset)
})?,
)
}
})
}

Expand Down
1 change: 1 addition & 0 deletions state-chain/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ nanorand = { workspace = true, features = ["wyrand"] }
serde = { workspace = true, features = ["derive", "alloc"] }
ethabi = { workspace = true }
bitvec = { workspace = true }
obake = { workspace = true }

# Chainflip local dependencies
cf-amm = { workspace = true }
Expand Down
19 changes: 18 additions & 1 deletion state-chain/runtime/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,33 @@ pub struct BrokerInfo {
}

/// Struct that represents the estimated output of a Swap.
#[obake::versioned]
#[obake(version("1.0.0"))]
#[obake(version("2.0.0"))]
#[derive(Encode, Decode, TypeInfo)]
pub struct SimulatedSwapInformation {
pub intermediary: Option<AssetAmount>,
pub output: AssetAmount,
pub network_fee: AssetAmount,
pub ingress_fee: AssetAmount,
pub egress_fee: AssetAmount,
#[obake(cfg(">=2.0"))]
pub broker_fee: AssetAmount,
}

impl From<SimulatedSwapInformation!["1.0.0"]> for SimulatedSwapInformation {
fn from(value: SimulatedSwapInformation!["1.0.0"]) -> Self {
Self {
intermediary: value.intermediary,
output: value.output,
network_fee: value.network_fee,
ingress_fee: value.ingress_fee,
egress_fee: value.egress_fee,
broker_fee: Default::default(),
}
}
}

#[derive(Debug, Decode, Encode, TypeInfo)]
pub enum DispatchErrorWithMessage {
Module(Vec<u8>),
Expand Down Expand Up @@ -299,7 +316,7 @@ decl_runtime_apis!(
to: Asset,
amount: AssetAmount,
additional_limit_orders: Option<Vec<SimulateSwapAdditionalOrder>>,
) -> Result<SimulatedSwapInformation, DispatchErrorWithMessage>;
) -> Result<SimulatedSwapInformation!["1.0.0"], DispatchErrorWithMessage>;
fn cf_pool_simulate_swap(
from: Asset,
to: Asset,
Expand Down
Loading