Skip to content

Commit

Permalink
Merge pull request #145 from octopus-network/feat/add-get-state-inter…
Browse files Browse the repository at this point in the history
…face-in-icp-route

feat[icp_route]: add get_route_state interface in icp route
  • Loading branch information
hsxyl authored Oct 6, 2024
2 parents 0628282 + aea1d51 commit b4b1270
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion route/icp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "icp_route"
version = "0.1.3"
version = "0.1.4"
edition = "2021"

[[bin]]
Expand Down
16 changes: 16 additions & 0 deletions route/icp/icp_route.did
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ type Result = variant { Ok; Err : text };
type Result_1 = variant { Ok : GenerateTicketOk; Err : GenerateTicketError };
type Result_2 = variant { Ok; Err : GenerateTicketError };
type RouteArg = variant { Upgrade : opt UpgradeArgs; Init : InitArgs };
type RouteState = record {
hub_principal : principal;
is_timer_running : bool;
next_directive_seq : nat64;
finalized_mint_token_requests : vec record { text; nat64 };
token_ledgers : vec record { text; principal };
chain_id : text;
tokens : vec record { text; Token };
target_chain_factor : vec record { text; nat };
counterparties : vec record { text; Chain };
next_ticket_seq : nat64;
chain_state : ChainState;
failed_tickets : vec Ticket;
fee_token_factor : opt nat;
};
type TargetChainFactor = record {
target_chain_id : text;
target_chain_factor : nat;
Expand Down Expand Up @@ -135,6 +150,7 @@ service : (RouteArg) -> {
get_fee_account : (opt principal) -> (blob) query;
get_log_records : (nat64, nat64) -> (Logs) query;
get_redeem_fee : (text) -> (opt nat64) query;
get_route_state : () -> (RouteState) query;
get_token_ledger : (text) -> (opt principal) query;
get_token_list : () -> (vec TokenResp) query;
mint_token_status : (text) -> (MintTokenStatus) query;
Expand Down
5 changes: 5 additions & 0 deletions route/icp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ pub fn get_redeem_fee(chain_id: ChainId) -> Option<u64> {
})
}

#[query]
pub fn get_route_state() -> icp_route::state::RouteState {
read_state(|s| s.clone())
}

#[query(hidden = true)]
fn http_request(req: HttpRequest) -> HttpResponse {
if ic_cdk::api::data_certificate().is_none() {
Expand Down
52 changes: 52 additions & 0 deletions route/icp/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,55 @@ pub fn replace_state(state: RouteState) {
*s.borrow_mut() = Some(state);
});
}

#[test]
pub fn calculate_icp_fee() {

let target_chain_factor = 1000_f64;
// 假设期望收到的跨链手续费是1$
let cross_chain_fee_of_dollar = 1_f64;

let chain_fee_token_factor = |decimals: i32, price: f64 | {
( (10_f64.powi(decimals)) * cross_chain_fee_of_dollar) / ((price * target_chain_factor) )
};

// eth
let eth_fee_factor = chain_fee_token_factor(18, 3300_f64);
// near
let near_fee_factor = chain_fee_token_factor(24, 6_f64);
// sol
let sol_fee_factor = chain_fee_token_factor(9, 188_f64);
// ton
let ton_fee_factor = chain_fee_token_factor(9, 5_f64);
// sui
let sui_fee_factor = chain_fee_token_factor(9, 1_f64);
// icp
let icp_fee_factor = chain_fee_token_factor(8, 8_f64);
// osmo
let osmo_fee_factor = chain_fee_token_factor(0, 0.00000039_f64);

// btc
let btc_fee_factor = chain_fee_token_factor(8, 56941_f64);


let eth_fee_factor_e = format!("{:.1e}", eth_fee_factor);
let near_fee_factor_e = format!("{:.1e}", near_fee_factor);
let sol_fee_factor_e = format!("{:.1e}", sol_fee_factor);
let ton_fee_factor_e = format!("{:.1e}", ton_fee_factor);
let sui_fee_factor_e = format!("{:.1e}", sui_fee_factor);
let icp_fee_factor_e = format!("{:.1e}", icp_fee_factor);
let osmo_fee_factor_e = format!("{:.1e}", osmo_fee_factor);
let btc_fee_factor_e = format!("{:.1e}", btc_fee_factor);

dbg!(
eth_fee_factor_e,
near_fee_factor_e,
sol_fee_factor_e,
ton_fee_factor_e,
sui_fee_factor_e,
icp_fee_factor_e,
osmo_fee_factor_e,
btc_fee_factor_e
);

}

0 comments on commit b4b1270

Please sign in to comment.