Skip to content

Commit

Permalink
Add OldAsset and replace Asset with OldAsset for preserve interface e…
Browse files Browse the repository at this point in the history
…ncodings where needed

squash

cargo fmt --all
  • Loading branch information
AlastairHolmes committed Feb 14, 2024
1 parent e9d31c7 commit 4d19533
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cf_chains::{
address::ToHumanreadableAddress, evm::SchnorrVerificationComponents, AnyChain, Bitcoin, Chain,
Ethereum, Polkadot,
};
use cf_primitives::{Asset, BroadcastId, ForeignChain, NetworkEnvironment};
use cf_primitives::{BroadcastId, ForeignChain, NetworkEnvironment};
use chainflip_engine::state_chain_observer::client::{
chain_api::ChainApi, storage_api::StorageApi,
};
Expand All @@ -30,7 +30,7 @@ enum WitnessInformation {
#[serde(skip_serializing)]
deposit_address: String,
amount: NumberOrHex,
asset: Asset,
asset: cf_chains::assets::any::Asset,
},
Broadcast {
#[serde(skip_serializing)]
Expand Down
22 changes: 11 additions & 11 deletions api/bin/chainflip-lp-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use chainflip_api::{
ApiWaitForResult, LpApi, Order, Tick,
},
primitives::{
chains::{Bitcoin, Ethereum, Polkadot},
chains::{assets::any::OldAsset, Bitcoin, Ethereum, Polkadot},
AccountRole, Asset, ForeignChain, Hash, RedemptionAmount,
},
settings::StateChain,
Expand Down Expand Up @@ -95,7 +95,7 @@ pub mod rpc_types {

#[derive(Serialize, Deserialize, Clone)]
pub struct AssetBalance {
pub asset: Asset,
pub asset: OldAsset,
pub balance: NumberOrHex,
}
}
Expand Down Expand Up @@ -224,8 +224,8 @@ pub struct OrderFills {
pub enum OrderFilled {
LimitOrder {
lp: AccountId,
base_asset: Asset,
quote_asset: Asset,
base_asset: OldAsset,
quote_asset: OldAsset,
side: Order,
id: U256,
tick: Tick,
Expand All @@ -236,8 +236,8 @@ pub enum OrderFilled {
},
RangeOrder {
lp: AccountId,
base_asset: Asset,
quote_asset: Asset,
base_asset: OldAsset,
quote_asset: OldAsset,
id: U256,
range: Range<Tick>,
fees: AssetsMap<U256>,
Expand Down Expand Up @@ -312,7 +312,7 @@ impl RpcServer for RpcServerImpl {
lp_asset_balances
.entry(asset.into())
.or_insert_with(Vec::new)
.push(AssetBalance { asset, balance: amount.into() });
.push(AssetBalance { asset: asset.into(), balance: amount.into() });
}
Ok(lp_asset_balances)
}
Expand Down Expand Up @@ -600,8 +600,8 @@ where
} else {
Some(OrderFilled::LimitOrder {
lp,
base_asset: asset_pair.assets().base,
quote_asset: asset_pair.assets().quote,
base_asset: asset_pair.assets().base.into(),
quote_asset: asset_pair.assets().quote.into(),
side,
id: id.into(),
tick,
Expand Down Expand Up @@ -646,8 +646,8 @@ where
} else {
Some(OrderFilled::RangeOrder {
lp: lp.clone(),
base_asset: asset_pair.assets().base,
quote_asset: asset_pair.assets().quote,
base_asset: asset_pair.assets().base.into(),
quote_asset: asset_pair.assets().quote.into(),
id: id.into(),
range: range.clone(),
fees: fees.map(|_, fees| fees).into(),
Expand Down
18 changes: 10 additions & 8 deletions api/lib/src/lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ impl<T> ApiWaitForResult<T> {

pub mod types {
use super::*;
use cf_chains::assets::any::OldAsset;

#[derive(Serialize, Deserialize, Clone)]
pub struct RangeOrder {
pub base_asset: Asset,
pub quote_asset: Asset,
pub base_asset: OldAsset,
pub quote_asset: OldAsset,
pub id: U256,
pub tick_range: Range<Tick>,
pub liquidity_total: U256,
Expand All @@ -62,8 +64,8 @@ pub mod types {

#[derive(Serialize, Deserialize, Clone)]
pub struct LimitOrder {
pub base_asset: Asset,
pub quote_asset: Asset,
pub base_asset: OldAsset,
pub quote_asset: OldAsset,
pub side: Order,
pub id: U256,
pub tick: Tick,
Expand Down Expand Up @@ -92,8 +94,8 @@ fn collect_range_order_returns(
..
},
) => Some(types::RangeOrder {
base_asset,
quote_asset,
base_asset: base_asset.into(),
quote_asset: quote_asset.into(),
id: id.into(),
size_change: size_change.map(|increase_or_decrese| {
increase_or_decrese.map(|range_order_change| types::RangeOrderChange {
Expand Down Expand Up @@ -130,8 +132,8 @@ fn collect_limit_order_returns(
..
},
) => Some(types::LimitOrder {
base_asset,
quote_asset,
base_asset: base_asset.into(),
quote_asset: quote_asset.into(),
side,
id: id.into(),
tick,
Expand Down
8 changes: 4 additions & 4 deletions api/lib/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use utilities::task_scope;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SwapChannelInfo<C: Chain> {
deposit_address: <C::ChainAccount as ToHumanreadableAddress>::Humanreadable,
source_asset: any::Asset,
destination_asset: any::Asset,
source_asset: any::OldAsset,
destination_asset: any::OldAsset,
}

pub struct PreUpdateStatus {
Expand Down Expand Up @@ -87,8 +87,8 @@ impl QueryApi {
destination_asset, ..
} => Some(SwapChannelInfo {
deposit_address: deposit_channel.address.to_humanreadable(network_environment),
source_asset: deposit_channel.asset.into(),
destination_asset: *destination_asset,
source_asset: Into::<Asset>::into(deposit_channel.asset).into(),
destination_asset: (*destination_asset).into(),
}),
_ => None,
})
Expand Down
51 changes: 28 additions & 23 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use cf_chains::{
Chain,
};
use cf_primitives::{
chains::assets::any, AccountRole, Asset, AssetAmount, BroadcastId, ForeignChain,
NetworkEnvironment, SemVer, SwapOutput,
chains::assets::any::{self, OldAsset},
AccountRole, Asset, AssetAmount, BroadcastId, ForeignChain, NetworkEnvironment, SemVer,
SwapOutput,
};
use cf_utilities::rpc::NumberOrHex;
use core::ops::Range;
Expand Down Expand Up @@ -207,7 +208,7 @@ impl From<PoolInfo> for RpcPoolInfo {

#[derive(Serialize, Deserialize)]
pub struct PoolsEnvironment {
pub fees: HashMap<ForeignChain, HashMap<Asset, Option<RpcPoolInfo>>>,
pub fees: HashMap<ForeignChain, HashMap<cf_chains::assets::any::OldAsset, Option<RpcPoolInfo>>>,
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -241,16 +242,16 @@ pub struct RpcEnvironment {

#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
pub struct PoolPriceV2 {
pub base_asset: Asset,
pub quote_asset: Asset,
pub base_asset: OldAsset,
pub quote_asset: OldAsset,
#[serde(flatten)]
pub price: pallet_cf_pools::PoolPriceV2,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
pub struct RpcPrewitnessedSwap {
pub base_asset: Asset,
pub quote_asset: Asset,
pub base_asset: OldAsset,
pub quote_asset: OldAsset,
pub side: Order,
pub amounts: Vec<U256>,
}
Expand Down Expand Up @@ -469,7 +470,7 @@ pub trait CustomApi {
) -> RpcResult<RpcPrewitnessedSwap>;

#[method(name = "supported_assets")]
fn cf_supported_assets(&self) -> RpcResult<HashMap<ForeignChain, Vec<Asset>>>;
fn cf_supported_assets(&self) -> RpcResult<HashMap<ForeignChain, Vec<OldAsset>>>;

#[method(name = "failed_call")]
fn cf_failed_call(
Expand Down Expand Up @@ -820,8 +821,8 @@ where
) -> RpcResult<PoolPriceV2> {
let hash = self.unwrap_or_best(at);
Ok(PoolPriceV2 {
base_asset,
quote_asset,
base_asset: base_asset.into(),
quote_asset: quote_asset.into(),
price: self
.client
.runtime_api()
Expand Down Expand Up @@ -1056,7 +1057,7 @@ where

let info = self.cf_pool_info(asset, Asset::Usdc, at).ok().map(Into::into);

fees.entry(asset.into()).or_insert_with(HashMap::new).insert(asset, info);
fees.entry(asset.into()).or_insert_with(HashMap::new).insert(asset.into(), info);
}

Ok(PoolsEnvironment { fees })
Expand Down Expand Up @@ -1114,7 +1115,11 @@ where
api.cf_pool_price_v2(hash, base_asset, quote_asset)
.map_err(to_rpc_error)
.and_then(|result| result.map_err(map_dispatch_error))
.map(|price| PoolPriceV2 { base_asset, quote_asset, price })
.map(|price| PoolPriceV2 {
base_asset: base_asset.into(),
quote_asset: quote_asset.into(),
price,
})
},
)
}
Expand All @@ -1132,8 +1137,8 @@ where
sink,
move |api, hash| {
Ok::<RpcPrewitnessedSwap, jsonrpsee::core::Error>(RpcPrewitnessedSwap {
base_asset,
quote_asset,
base_asset: base_asset.into(),
quote_asset: quote_asset.into(),
side,
amounts: api
.cf_prewitness_swaps(hash, base_asset, quote_asset, side)
Expand All @@ -1154,8 +1159,8 @@ where
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<RpcPrewitnessedSwap> {
Ok(RpcPrewitnessedSwap {
base_asset,
quote_asset,
base_asset: base_asset.into(),
quote_asset: quote_asset.into(),
side,
amounts: self
.client
Expand All @@ -1168,10 +1173,10 @@ where
})
}

fn cf_supported_assets(&self) -> RpcResult<HashMap<ForeignChain, Vec<Asset>>> {
let mut chain_to_asset: HashMap<ForeignChain, Vec<Asset>> = HashMap::new();
fn cf_supported_assets(&self) -> RpcResult<HashMap<ForeignChain, Vec<OldAsset>>> {
let mut chain_to_asset: HashMap<ForeignChain, Vec<OldAsset>> = HashMap::new();
Asset::all().for_each(|asset| {
chain_to_asset.entry((asset).into()).or_default().push(asset);
chain_to_asset.entry((asset).into()).or_default().push(asset.into());
});
Ok(chain_to_asset)
}
Expand Down Expand Up @@ -1325,9 +1330,9 @@ mod test {
(ForeignChain::Bitcoin, None),
],
balances: vec![
(Asset::Eth, u128::MAX),
(Asset::Btc, 0),
(Asset::Flip, u128::MAX / 2),
(Asset::Eth.into(), u128::MAX),
(Asset::Btc.into(), 0),
(Asset::Flip.into(), u128::MAX / 2),
],
},
cf_primitives::NetworkEnvironment::Mainnet,
Expand Down Expand Up @@ -1427,7 +1432,7 @@ mod test {
fees: HashMap::from([(
ForeignChain::Ethereum,
HashMap::from([(
Asset::Flip,
Asset::Flip.into(),
Some(
PoolInfo {
limit_order_fee_hundredth_pips: 0,
Expand Down
24 changes: 16 additions & 8 deletions state-chain/primitives/src/chains/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,16 @@ macro_rules! assets {
}
}
}
mod asset_serde_impls {
pub use asset_serde_impls::SerdeAsset as OldAsset;
pub(super) mod asset_serde_impls {
use serde::{Serialize, Deserialize};

/// DO NOT USE THIS TYPE. This is only public to allow consistency in behaviour for out of date RPCs and Runtime API functions, once we remove those apis (and replace them in PRO-1202)
#[derive(Copy, Clone, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Hash, codec::Encode, codec::Decode, scale_info::TypeInfo, codec::MaxEncodedLen)] /* Remove these derives once PRO-1202 is done */
#[repr(u32)]
#[serde(rename_all = "UPPERCASE")]
enum SerdeAsset {
pub enum SerdeAsset {
$(
$gas_asset = $gas_value,
$($asset = $value,)*
Expand All @@ -124,6 +127,16 @@ macro_rules! assets {
}
}
}
impl From<super::Asset> for SerdeAsset {
fn from(asset: super::Asset) -> Self {
match asset {
$(
super::Asset::$gas_asset => SerdeAsset::$gas_asset,
$(super::Asset::$asset => SerdeAsset::$asset,)*
)*
}
}
}

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
Expand All @@ -140,12 +153,7 @@ macro_rules! assets {
where S: serde::Serializer
{
Serialize::serialize(&SerdeAssetOptionalExplicitChain::ExplicitChain {
chain: Some((*self).into()), asset: match self {
$(
super::Asset::$gas_asset => SerdeAsset::$gas_asset,
$(super::Asset::$asset => SerdeAsset::$asset,)*
)*
}
chain: Some((*self).into()), asset: (*self).into()
}, serializer)
}
}
Expand Down
9 changes: 5 additions & 4 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ use sp_version::NativeVersion;
use sp_version::RuntimeVersion;

pub use cf_primitives::{
AccountRole, Asset, AssetAmount, BlockNumber, FlipBalance, SemVer, SwapOutput,
chains::assets::any, AccountRole, Asset, AssetAmount, BlockNumber, FlipBalance, SemVer,
SwapOutput,
};
pub use cf_traits::{
AccountInfo, BidderProvider, CcmHandler, Chainflip, EpochInfo, PoolApi, QualifyNode,
Expand Down Expand Up @@ -1241,9 +1242,9 @@ impl_runtime_apis! {
fn cf_ingress_fee(generic_asset: Asset) -> Option<AssetAmount> {
match generic_asset.into() {
ForeignChainAndAsset::Ethereum(asset) => {
pallet_cf_pools::Pallet::<Runtime>::estimate_swap_input_for_desired_output(
generic_asset,
Asset::Eth,
pallet_cf_pools::Pallet::<Runtime>::estimate_swap_input_for_desired_output(
generic_asset,
Asset::Eth,
pallet_cf_chain_tracking::Pallet::<Runtime, EthereumInstance>::get_tracked_data()
.estimate_ingress_fee(asset)
)
Expand Down

0 comments on commit 4d19533

Please sign in to comment.