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: Add kotlin bindings for UI fields method #73

Closed
wants to merge 3 commits into from
Closed
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
25 changes: 23 additions & 2 deletions crates/kotlin-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ uniffi::setup_scaffolding!();

use alloy::primitives::{Bytes, U256, U64};
use alloy::providers::Provider;
use yttrium::chain_abstraction::api::route::RouteResponse;
use yttrium::chain_abstraction::api::route::{
RouteResponse, RouteResponseAvailable,
};
use yttrium::chain_abstraction::api::status::{
StatusResponse, StatusResponseCompleted,
};
use yttrium::chain_abstraction::api::Transaction as CATransaction;
use yttrium::chain_abstraction::currency::Currency;

use alloy::{network::Ethereum, providers::ReqwestProvider};
use relay_rpc::domain::ProjectId;
use std::time::Duration;
use yttrium::chain_abstraction::client::Client;
use yttrium::chain_abstraction::client::{Client, RouteUiFields};
use yttrium::config::Config;
use yttrium::transaction::send::safe_test::{
Address, OwnerSignature as YOwnerSignature, PrimitiveSignature,
Expand Down Expand Up @@ -156,6 +159,24 @@ impl ChainAbstractionClient {
.map_err(|e| Error::General(e.to_string()))
}

pub async fn get_ui_fields(
&self,
route_response: RouteResponseAvailable,
initial_transaction: InitTransaction,
currency: Currency,
) -> Result<RouteUiFields, Error> {
let ca_transaction = CATransaction::from(initial_transaction);
self.client
.get_route_ui_fields(
route_response,
ca_transaction,
currency,
"".to_string(),
)
.await
.map_err(|e| Error::General(e.to_string()))
}

pub async fn estimate_fees(
&self,
chain_id: String,
Expand Down
7 changes: 7 additions & 0 deletions crates/yttrium/src/chain_abstraction/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ use alloy::primitives::{
U256,
};

#[cfg(feature = "uniffi")]
uniffi::custom_type!(Unit, String, {
try_lift: |val| Ok(val.parse()?),
lower: |obj| obj.to_string(),
});

#[derive(Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
Copy link
Member

Choose a reason for hiding this comment

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

pub struct Amount {
pub symbol: String, // USDC, USD
pub amount: U256, // e.g. 40000, 4
Expand Down
56 changes: 41 additions & 15 deletions crates/yttrium/src/chain_abstraction/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Client {
txn: Transaction,
eip1559_fees: &HashMap<&String, Eip1559Estimation>,
providers: &HashMap<&String, ReqwestProvider>,
) -> (Transaction, Eip1559Estimation, U256) {
) -> (Transaction, Eip1559EstimationFields, U256) {
let eip1559_estimation = *eip1559_fees.get(&txn.chain_id).unwrap();
let l1_data_fee = get_l1_data_fee(
TransactionRequest::default()
Expand Down Expand Up @@ -176,7 +176,8 @@ impl Client {
.expect("fee overflow")
.checked_add(l1_data_fee)
.expect("fee overflow in adding");
(txn, eip1559_estimation, fee)

(txn, Eip1559EstimationFields::from(eip1559_estimation), fee)
}

let mut estimated_transactions =
Expand All @@ -202,13 +203,13 @@ impl Client {
fn compute_amounts(
(txn, eip1559_estimation, fee): (
Transaction,
Eip1559Estimation,
Eip1559EstimationFields,
U256,
),
total_local_fee: &mut U256,
const_local_unit: Unit,
prices: &PriceResponseBody,
) -> (Transaction, Eip1559Estimation, TransactionFee) {
) -> TxnDetails {
let fungible = prices.fungibles.first().unwrap();
// let fungible = prices
// .fungibles
Expand All @@ -229,11 +230,10 @@ impl Client {

let local_fee = fee * fungible_local_exchange_rate;
*total_local_fee += local_fee;

(
txn,
eip1559_estimation,
TransactionFee {
TxnDetails {
transaction: txn,
eip1559: eip1559_estimation,
transaction_fee: TransactionFee {
fee: Amount::new(
fungible.symbol.clone(),
fee,
Expand All @@ -245,7 +245,7 @@ impl Client {
const_local_unit,
),
},
)
}
}

let mut route = Vec::with_capacity(estimated_transactions.len());
Expand All @@ -265,12 +265,12 @@ impl Client {
);

Ok(RouteUiFields {
route,
route_details: route,
// bridge: TransactionFee {
// fee: Amount::zero(),
// local_fee: Amount::zero(),
// },
initial,
initial_details: initial,
local_total: Amount::new(
"USD".to_owned(),
total_local_fee,
Expand Down Expand Up @@ -365,21 +365,47 @@ impl Client {
}

#[derive(Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
pub struct RouteUiFields {
pub route: Vec<TxnDetails>,
pub route_details: Vec<TxnDetails>,
// pub bridge: TxnDetails,
pub initial: TxnDetails,
pub initial_details: TxnDetails,
pub local_total: Amount,
}

pub type TxnDetails = (Transaction, Eip1559Estimation, TransactionFee);
#[derive(Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
pub struct TxnDetails {
pub transaction: Transaction,
pub eip1559: Eip1559EstimationFields,
pub transaction_fee: TransactionFee,
}

#[derive(Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
pub struct Eip1559EstimationFields {
Copy link
Member

Choose a reason for hiding this comment

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

Can we please move this stringification to kotlin-ffi so that the types in yttrium remain Rust-idiomatic? I.e. map it in kotlin-ffi's get_ui_fields() into a new set of types

The next version of UniFFI will supporting adding UniFFI to types from external crates like this Eip1559Estimation so we can remove that mapping layer

pub max_fee_per_gas: String,
pub max_priority_fee_per_gas: String,
}

#[derive(Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
pub struct TransactionFee {
pub fee: Amount,
pub local_fee: Amount,
}

impl From<Eip1559Estimation> for Eip1559EstimationFields {
fn from(estimation: Eip1559Estimation) -> Self {
Eip1559EstimationFields {
max_fee_per_gas: estimation.max_fee_per_gas.to_string(),
max_priority_fee_per_gas: estimation
.max_priority_fee_per_gas
.to_string(),
}
}
}

// #[cfg(test)]
// mod tests {
// use super::*;
Expand Down
1 change: 1 addition & 0 deletions crates/yttrium/src/chain_abstraction/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
// TODO get Blockchain API to use these types?

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Enum))]
#[serde(rename_all = "lowercase")]
pub enum Currency {
Usd,
Expand Down
Loading