Skip to content

Commit

Permalink
redo
Browse files Browse the repository at this point in the history
  • Loading branch information
acdibble committed Oct 23, 2023
1 parent 14ca06c commit e2a1b86
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 89 deletions.
138 changes: 68 additions & 70 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use cf_amm::{
common::{Amount, Price, Tick},
range_orders::Liquidity,
};
use cf_chains::{
address::AddressConverter, btc::BitcoinNetwork, dot::PolkadotHash,
eth::Address as EthereumAddress,
};
use cf_chains::{address::AddressConverter, eth::Address as EthereumAddress};
use cf_primitives::{AccountRole, Asset, AssetAmount, ForeignChain, SemVer, SwapOutput};
use core::ops::Range;
use jsonrpsee::{
Expand Down Expand Up @@ -178,19 +175,8 @@ impl From<SwapOutput> for RpcSwapOutput {
}

#[derive(Serialize, Deserialize)]
pub struct BitcoinEnvironment {
pub network: BitcoinNetwork,
}

#[derive(Serialize, Deserialize)]
pub struct EthereumEnvironment {
pub chain_id: cf_chains::evm::api::EvmChainId,
pub contract_addresses: HashMap<String, String>,
}

#[derive(Serialize, Deserialize)]
pub struct PolkadotEnvironment {
pub genesis_hash: PolkadotHash,
pub struct IngressEgressEnvironment {
pub minimum_deposit_amounts: HashMap<ForeignChain, HashMap<Asset, NumberOrHex>>,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -200,12 +186,14 @@ pub struct FundingEnvironment {
}

#[derive(Serialize, Deserialize)]
pub struct RpcEnvironment {
bitcoin: BitcoinEnvironment,
ethereum: EthereumEnvironment,
polkadot: PolkadotEnvironment,
minimum_deposit_amounts: HashMap<ForeignChain, HashMap<Asset, NumberOrHex>>,
pub struct SwappingEnvironment {
minimum_swap_amounts: HashMap<ForeignChain, HashMap<Asset, NumberOrHex>>,
}

#[derive(Serialize, Deserialize)]
pub struct RpcEnvironment {
ingress_egress: IngressEgressEnvironment,
swapping: SwappingEnvironment,
funding: FundingEnvironment,
}

Expand Down Expand Up @@ -359,6 +347,21 @@ pub trait CustomApi {
) -> RpcResult<Option<AssetsMap<Amount>>>;
#[method(name = "environment")]
fn cf_environment(&self, at: Option<state_chain_runtime::Hash>) -> RpcResult<RpcEnvironment>;
#[method(name = "funding_environment")]
fn cf_funding_environment(
&self,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<FundingEnvironment>;
#[method(name = "swapping_environment")]
fn cf_swapping_environment(
&self,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<SwappingEnvironment>;
#[method(name = "ingress_egress_environment")]
fn cf_ingress_egress_environment(
&self,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<IngressEgressEnvironment>;
#[method(name = "current_compatibility_version")]
fn cf_current_compatibility_version(&self) -> RpcResult<SemVer>;
#[method(name = "min_swap_amount")]
Expand Down Expand Up @@ -801,12 +804,40 @@ where
}

fn cf_environment(&self, at: Option<state_chain_runtime::Hash>) -> RpcResult<RpcEnvironment> {
Ok(RpcEnvironment {
ingress_egress: self.cf_ingress_egress_environment(at)?,
swapping: self.cf_swapping_environment(at)?,
funding: self.cf_funding_environment(at)?,
})
}

fn cf_ingress_egress_environment(
&self,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<IngressEgressEnvironment> {
let runtime_api = &self.client.runtime_api();
let hash = self.unwrap_or_best(at);
let env = runtime_api.cf_environment(hash).map_err(to_rpc_error)?;
let mut minimum_deposit_amounts = HashMap::new();

for asset in Asset::all() {
let deposit_amount =
runtime_api.cf_min_deposit_amount(hash, asset).map_err(to_rpc_error)?.into();
minimum_deposit_amounts
.entry(asset.into())
.or_insert_with(HashMap::new)
.insert(asset, deposit_amount);
}

Ok(IngressEgressEnvironment { minimum_deposit_amounts })
}

fn cf_swapping_environment(
&self,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<SwappingEnvironment> {
let runtime_api = &self.client.runtime_api();
let hash = self.unwrap_or_best(at);
let mut minimum_swap_amounts = HashMap::new();
let mut minimum_deposit_amounts = HashMap::new();

for asset in Asset::all() {
let swap_amount =
Expand All @@ -815,54 +846,21 @@ where
.entry(asset.into())
.or_insert_with(HashMap::new)
.insert(asset, swap_amount);
let deposit_amount =
runtime_api.cf_min_deposit_amount(hash, asset).map_err(to_rpc_error)?.into();
minimum_deposit_amounts
.entry(asset.into())
.or_insert_with(HashMap::new)
.insert(asset, deposit_amount);
}

Ok(RpcEnvironment {
bitcoin: BitcoinEnvironment { network: env.bitcoin_network },
ethereum: EthereumEnvironment {
chain_id: env.ethereum_chain_id,
contract_addresses: HashMap::from([
(
"key_manager".into(),
format!(
"{}",
runtime_api.cf_eth_key_manager_address(hash).map_err(to_rpc_error)?
),
),
(
"state_chain_gateway".into(),
format!(
"{}",
runtime_api
.cf_eth_state_chain_gateway_address(hash)
.map_err(to_rpc_error)?
),
),
(
"flip_token".into(),
format!(
"{}",
runtime_api.cf_eth_flip_token_address(hash).map_err(to_rpc_error)?
),
),
]),
},
polkadot: PolkadotEnvironment { genesis_hash: env.polkadot_genesis_hash },
minimum_deposit_amounts,
minimum_swap_amounts,
funding: FundingEnvironment {
redemption_tax: runtime_api.cf_redemption_tax(hash).map_err(to_rpc_error)?.into(),
minimum_funding_amount: runtime_api
.cf_min_funding(hash)
.map_err(to_rpc_error)?
.into(),
},
Ok(SwappingEnvironment { minimum_swap_amounts })
}

fn cf_funding_environment(
&self,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<FundingEnvironment> {
let runtime_api = &self.client.runtime_api();
let hash = self.unwrap_or_best(at);

Ok(FundingEnvironment {
redemption_tax: runtime_api.cf_redemption_tax(hash).map_err(to_rpc_error)?.into(),
minimum_funding_amount: runtime_api.cf_min_funding(hash).map_err(to_rpc_error)?.into(),
})
}

Expand Down
8 changes: 0 additions & 8 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,14 +1038,6 @@ impl_runtime_apis! {
LiquidityPools::pool_range_order_liquidity_value(base_asset, pair_asset, tick_range, liquidity)
}

fn cf_environment() -> runtime_apis::Environment {
runtime_apis::Environment {
bitcoin_network: Environment::network_environment().into(),
ethereum_chain_id: Environment::ethereum_chain_id(),
polkadot_genesis_hash: Environment::polkadot_genesis_hash(),
}
}

fn cf_min_swap_amount(asset: Asset) -> AssetAmount {
Swapping::minimum_swap_amount(asset)
}
Expand Down
12 changes: 1 addition & 11 deletions state-chain/runtime/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use cf_amm::{
common::{Amount, Price, Tick},
range_orders::Liquidity,
};
use cf_chains::{
btc::BitcoinNetwork, dot::PolkadotHash, eth::Address as EthereumAddress, ForeignChainAddress,
};
use cf_chains::{eth::Address as EthereumAddress, ForeignChainAddress};
use cf_primitives::{
AccountRole, Asset, AssetAmount, EpochIndex, ForeignChain, SemVer, SwapOutput,
};
Expand Down Expand Up @@ -68,13 +66,6 @@ pub struct AuctionState {
pub auction_size_range: (u32, u32),
}

#[derive(Encode, Decode, Eq, PartialEq, TypeInfo)]
pub struct Environment {
pub bitcoin_network: BitcoinNetwork,
pub ethereum_chain_id: cf_chains::evm::api::EvmChainId,
pub polkadot_genesis_hash: PolkadotHash,
}

#[derive(Encode, Decode, Eq, PartialEq, TypeInfo)]
pub struct LiquidityProviderInfo {
pub refund_addresses: Vec<(ForeignChain, Option<ForeignChainAddress>)>,
Expand Down Expand Up @@ -132,7 +123,6 @@ decl_runtime_apis!(
tick_range: Range<Tick>,
liquidity: Liquidity,
) -> Option<Result<AssetsMap<Amount>, DispatchError>>;
fn cf_environment() -> Environment;
fn cf_min_swap_amount(asset: Asset) -> AssetAmount;
fn cf_min_deposit_amount(asset: Asset) -> AssetAmount;
fn cf_prewitness_swaps(from: Asset, to: Asset) -> Option<Vec<AssetAmount>>;
Expand Down

0 comments on commit e2a1b86

Please sign in to comment.