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: Expose vault swaps from ingress-egress-tracker #5463

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions Cargo.lock

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

10 changes: 8 additions & 2 deletions api/bin/chainflip-ingress-egress-tracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async-trait = { workspace = true }
bitcoin = { workspace = true, features = ["serde"] }
futures = { workspace = true }
hex = { workspace = true, default-features = true }
log = { workspace = true, default-features = true }
serde = { workspace = true, default-features = true }
serde_json = { workspace = true }
tokio = { workspace = true }
Expand All @@ -25,17 +26,21 @@ config = { workspace = true }
redis = { workspace = true, features = ["tokio-comp"] }

sp-core = { workspace = true, default-features = true }
codec = { workspace = true, default-features = true, features = ["derive", "full"] }
codec = { workspace = true, default-features = true, features = [
"derive",
"full",
] }

# Local dependencies
chainflip-engine = { workspace = true }
cf-utilities = { workspace = true, default-features = true }
cf-primitives = { workspace = true, default-features = true }
pallet-cf-environment = { workspace = true, default-features = true }
pallet-cf-ingress-egress = { workspace = true, default-features = true }
pallet-cf-broadcast = { workspace = true, default-features = true }
state-chain-runtime = { workspace = true, default-features = true }
cf-chains = { workspace = true, default-features = true }
custom-rpc = { workspace = true }
chainflip-api = { workspace = true }

[build-dependencies]
substrate-build-script-utils = { workspace = true }
Expand All @@ -45,3 +50,4 @@ frame-support = { workspace = true, default-features = true }
insta = { workspace = true, features = ["json"] }
jsonrpsee = { workspace = true, features = ["full"] }
mockall = { workspace = true }
chainflip-engine = { workspace = true, features = ["client-mocks"] }
1 change: 1 addition & 0 deletions api/bin/chainflip-ingress-egress-tracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async fn start(

#[tokio::main]
async fn main() -> anyhow::Result<()> {
chainflip_api::use_chainflip_account_id_encoding();
let settings = DepositTrackerSettings::load_settings_from_all_sources(
// Not using the config root or settings dir.
"".to_string(),
Expand Down
20 changes: 8 additions & 12 deletions api/bin/chainflip-ingress-egress-tracker/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use std::time::Duration;

#[async_trait]
pub trait Store: Sync + Send + 'static {
type Output: Sync + Send + 'static;

async fn save_to_array<S: Storable>(&mut self, storable: &S) -> anyhow::Result<Self::Output>;
async fn save_singleton<S: Storable>(&mut self, storable: &S) -> anyhow::Result<Self::Output>;
async fn save_to_array<S: Storable>(&mut self, storable: &S) -> anyhow::Result<()>;
async fn save_singleton<S: Storable>(&mut self, storable: &S) -> anyhow::Result<()>;
}

#[derive(Clone)]
Expand All @@ -24,15 +22,13 @@ impl RedisStore {

#[async_trait]
impl Store for RedisStore {
type Output = ();

async fn save_to_array<S: Storable>(&mut self, storable: &S) -> anyhow::Result<()> {
let key = storable.get_key();
let key = storable.key();
self.con
.rpush::<String, String, ()>(key.clone(), serde_json::to_string(storable)?)
.await?;
self.con
.expire::<String, ()>(key, storable.get_expiry_duration().as_secs() as i64)
.expire::<String, ()>(key, storable.expiry_duration().as_secs() as i64)
.await?;

Ok(())
Expand All @@ -41,9 +37,9 @@ impl Store for RedisStore {
async fn save_singleton<S: Storable>(&mut self, storable: &S) -> anyhow::Result<()> {
self.con
.set_ex::<String, String, ()>(
storable.get_key(),
storable.key(),
serde_json::to_string(storable)?,
storable.get_expiry_duration().as_secs(),
storable.expiry_duration().as_secs(),
)
.await?;

Expand All @@ -54,9 +50,9 @@ impl Store for RedisStore {
pub trait Storable: Serialize + Sized + Sync + 'static {
const DEFAULT_EXPIRY_DURATION: Duration = Duration::from_secs(3600);

fn get_key(&self) -> String;
fn key(&self) -> String;

fn get_expiry_duration(&self) -> Duration {
fn expiry_duration(&self) -> Duration {
Self::DEFAULT_EXPIRY_DURATION
}
}
6 changes: 4 additions & 2 deletions api/bin/chainflip-ingress-egress-tracker/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use cf_chains::instances::{ChainInstanceAlias, ChainInstanceFor};
use cf_primitives::BroadcastId;
use chainflip_api::primitives::BroadcastId;
use chainflip_engine::state_chain_observer::client::{
chain_api::ChainApi, storage_api::StorageApi, STATE_CHAIN_CONNECTION,
};
use pallet_cf_broadcast::TransactionOutIdFor;
use sp_core::bounded::alloc::sync::Arc;

use tracing::log;

pub fn hex_encode_bytes(bytes: &[u8]) -> String {
format!("0x{}", hex::encode(bytes))
}

pub async fn get_broadcast_id<I, StateChainClient>(
state_chain_client: &StateChainClient,
state_chain_client: Arc<StateChainClient>,
tx_out_id: &TransactionOutIdFor<state_chain_runtime::Runtime, ChainInstanceFor<I>>,
) -> Option<BroadcastId>
where
Expand Down
23 changes: 13 additions & 10 deletions api/bin/chainflip-ingress-egress-tracker/src/witnessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ pub mod state_chain;

use self::state_chain::handle_call;
use crate::{settings::DepositTrackerSettings, store::RedisStore};
use anyhow::anyhow;
use cf_chains::dot::PolkadotHash;
use cf_primitives::{chains::assets::eth::Asset, NetworkEnvironment};
use cf_utilities::task_scope;
use chainflip_api::primitives::{
chains::assets::eth::Asset as EthAsset, Asset, NetworkEnvironment,
};
use chainflip_engine::{
state_chain_observer::{
self,
Expand All @@ -18,8 +19,10 @@ use chainflip_engine::{
},
witness::common::epoch_source::EpochSource,
};

use anyhow::anyhow;
use sp_core::H160;
use std::{collections::HashMap, ops::Deref};
use std::collections::HashMap;

#[derive(Clone)]
pub(super) struct EnvironmentParameters {
Expand All @@ -29,7 +32,7 @@ pub(super) struct EnvironmentParameters {
flip_contract_address: H160,
usdc_contract_address: H160,
usdt_contract_address: H160,
supported_erc20_tokens: HashMap<H160, cf_primitives::Asset>,
supported_erc20_tokens: HashMap<H160, Asset>,
dot_genesis_hash: PolkadotHash,
pub chainflip_network: NetworkEnvironment,
}
Expand Down Expand Up @@ -66,15 +69,15 @@ async fn get_env_parameters(state_chain_client: &StateChainClient<()>) -> Enviro
.expect("Failed to fetch Ethereum supported assets");

let flip_contract_address =
*supported_erc20_tokens.get(&Asset::Flip).expect("FLIP not supported");
*supported_erc20_tokens.get(&EthAsset::Flip).expect("FLIP not supported");

let usdc_contract_address =
*supported_erc20_tokens.get(&Asset::Usdc).expect("USDC not supported");
*supported_erc20_tokens.get(&EthAsset::Usdc).expect("USDC not supported");

let usdt_contract_address =
*supported_erc20_tokens.get(&Asset::Usdt).expect("USDT not supported");
*supported_erc20_tokens.get(&EthAsset::Usdt).expect("USDT not supported");

let supported_erc20_tokens: HashMap<H160, cf_primitives::Asset> = supported_erc20_tokens
let supported_erc20_tokens: HashMap<H160, Asset> = supported_erc20_tokens
.into_iter()
.map(|(asset, address)| (address, asset.into()))
.collect();
Expand Down Expand Up @@ -112,7 +115,7 @@ pub(super) async fn start(
store: RedisStore,
) -> anyhow::Result<()> {
let (state_chain_stream, unfinalized_chain_stream, state_chain_client) = {
state_chain_observer::client::StateChainClient::connect_without_account(
state_chain_observer::client::StateChainClient::<(), _>::connect_without_account(
scope,
&settings.state_chain_ws_endpoint,
)
Expand All @@ -132,7 +135,7 @@ pub(super) async fn start(
let state_chain_client = state_chain_client.clone();

async move {
handle_call(call, &mut store, chainflip_network, state_chain_client.deref())
handle_call(call, &mut store, chainflip_network, state_chain_client)
.await
.map_err(|err| anyhow!("failed to handle call: {err:?}"))
.unwrap()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use cf_primitives::EpochIndex;
use cf_utilities::task_scope::Scope;
use chainflip_api::primitives::EpochIndex;
use chainflip_engine::{
btc::retry_rpc::{BtcRetryRpcApi, BtcRetryRpcClient},
settings::NodeContainer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use cf_primitives::EpochIndex;
use cf_utilities::task_scope::Scope;
use chainflip_api::primitives::EpochIndex;
use chainflip_engine::{
dot::retry_rpc::DotRetryRpcClient,
settings::NodeContainer,
Expand Down
18 changes: 10 additions & 8 deletions api/bin/chainflip-ingress-egress-tracker/src/witnessing/eth.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Context;
use cf_chains::{Chain, Ethereum};
use cf_primitives::chains::assets::eth::Asset;
use cf_utilities::task_scope;
use chainflip_api::primitives::{
chains::assets::eth::Asset as EthAsset, Asset, EpochIndex, ForeignChain,
};
use std::sync::Arc;

use chainflip_engine::{
Expand Down Expand Up @@ -37,7 +39,7 @@ pub(super) async fn start<ProcessCall, ProcessingFut>(
witness_call: ProcessCall,
) -> anyhow::Result<()>
where
ProcessCall: Fn(state_chain_runtime::RuntimeCall, cf_primitives::EpochIndex) -> ProcessingFut
ProcessCall: Fn(state_chain_runtime::RuntimeCall, EpochIndex) -> ProcessingFut
+ Send
+ Sync
+ Clone
Expand Down Expand Up @@ -73,7 +75,7 @@ where
.erc20_deposits::<_, _, _, UsdcEvents>(
witness_call.clone(),
eth_client.clone(),
Asset::Usdc,
EthAsset::Usdc,
env_params.usdc_contract_address,
)
.await?
Expand All @@ -85,7 +87,7 @@ where
.erc20_deposits::<_, _, _, FlipEvents>(
witness_call.clone(),
eth_client.clone(),
Asset::Flip,
EthAsset::Flip,
env_params.flip_contract_address,
)
.await?
Expand All @@ -97,7 +99,7 @@ where
.erc20_deposits::<_, _, _, UsdtEvents>(
witness_call.clone(),
eth_client.clone(),
Asset::Usdt,
EthAsset::Usdt,
env_params.usdt_contract_address,
)
.await?
Expand All @@ -109,7 +111,7 @@ where
.ethereum_deposits(
witness_call.clone(),
eth_client.clone(),
Asset::Eth,
EthAsset::Eth,
env_params.eth_address_checker_address,
env_params.eth_vault_address,
)
Expand All @@ -123,8 +125,8 @@ where
witness_call.clone(),
eth_client.clone(),
env_params.eth_vault_address,
cf_primitives::Asset::Eth,
cf_primitives::ForeignChain::Ethereum,
Asset::Eth,
ForeignChain::Ethereum,
env_params.supported_erc20_tokens.clone(),
)
.logging("witnessing Vault")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: api/bin/chainflip-ingress-egress-tracker/src/witnessing/state_chain.rs
expression: "store.storage.get(\"vault_deposit:Ethereum:b5c8bd9430b6cc87a0e2fe110ece6bf527fa4f170a4bc8cd032f768fc5219838\").unwrap()"
---
{"affiliate_fees":[{"account":"cFHtoB6DrnqUVY4DwMHCVCtgCLsiHvv98oGw8k66tazF2ToFv","bps":10}],"amount":"0x64","boost_fee":5,"broker_fee":{"account":"cFHsUq1uK5opJudRDczhdPVj6LGoVTqYsfj71tbHfKsTAzkJJ","bps":10},"ccm_deposit_metadata":null,"dca_params":{"chunk_interval":100,"number_of_chunks":5},"deposit_chain_block_height":1,"deposit_details":null,"destination_address":"0x0000000000000000000000000000000000000000000000000000000000000000","input_asset":{"asset":"ETH","chain":"Ethereum"},"output_asset":{"asset":"FLIP","chain":"Ethereum"},"refund_params":{"min_price":"0x0","refund_address":{"Eth":"0x541f563237a309b3a61e33bdf07a8930bdba8d99"},"retry_duration":0}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be awesome to have a ccm_deposit_metadata in here to check how it looks. but not a blocker

Loading
Loading