Skip to content

Commit

Permalink
fix: separate initial transaction type (#98)
Browse files Browse the repository at this point in the history
* chore: separate initial transaction type

* chore: rename gas_limit

* fix: remove nonce from initial txn

* regenerate dart code

* chore: lint

* chore: add 3rd txn type, rename data -> input, add Flutter CI

* chore: rename job

---------

Co-authored-by: Alfreedom <[email protected]>
  • Loading branch information
chris13524 and quetool authored Dec 17, 2024
1 parent 79fdd0d commit befa0cb
Show file tree
Hide file tree
Showing 15 changed files with 372 additions and 2,687 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,86 @@ jobs:
mkdir -p yttrium/libs/$abi_name
cp target/${{ matrix.target }}/release/libuniffi_yttrium.so yttrium/libs/$abi_name/
flutter:
runs-on: macos-latest-xlarge

steps:
# Checkout repository
- name: Checkout repository
uses: actions/checkout@v3

# Cache Flutter dependencies
- name: Cache Flutter dependencies
uses: actions/cache@v3
with:
path: ~/.pub-cache
key: ${{ runner.os }}-flutter-${{ hashFiles('**/pubspec.yaml') }}
restore-keys: |
${{ runner.os }}-flutter-
# Install Rust toolchain
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
components: rust-src

# Cache Cargo dependencies
- name: Cache Cargo dependencies
uses: actions/cache@v3
with:
path: ~/.cargo
key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.toml') }}
restore-keys: |
${{ runner.os }}-cargo-
# Install Java 17
- name: Install Java 17
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'
architecture: x86_64
cache: 'gradle'

# Cache Gradle
- name: Cache Gradle
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# Install Flutter
- name: Install Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: '3.24.5'

# Install flutter_rust_bridge_codegen
- name: Install flutter_rust_bridge_codegen
run: |
cargo install flutter_rust_bridge_codegen
# Get package dependencies
- name: Get package dependencies
shell: bash
working-directory: crates/yttrium_dart
run: |
flutter pub get
# Generate Dart Bindings
- name: Generate Dart Bindings
shell: bash
working-directory: crates/yttrium_dart
run: |
flutter_rust_bridge_codegen generate --config-file flutter_rust_bridge.yaml
- run: git diff crates/yttrium_dart
- run: if [ -n "$(git diff crates/yttrium_dart)" ]; then exit 1; fi
100 changes: 5 additions & 95 deletions crates/kotlin-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ use {
yttrium::{
account_client::{AccountClient as YAccountClient, SignerType},
chain_abstraction::{
self,
amount::Amount,
api::{
route::{RouteResponse, RouteResponseAvailable},
status::{StatusResponse, StatusResponseCompleted},
Transaction as CATransaction,
InitialTransaction,
},
client::Client,
currency::Currency,
route_ui_fields::TransactionFee,
route_ui_fields::RouteUiFields,
},
config::Config,
private_key_service::PrivateKeyService,
Expand Down Expand Up @@ -86,50 +84,6 @@ uniffi::custom_type!(FFIBytes, String, {
lower: |obj| obj.to_string(),
});

#[derive(Debug, uniffi::Record)]
pub struct RouteUiFields {
pub route: Vec<TxnDetails>,
pub bridge: Vec<TransactionFee>,
pub initial: TxnDetails,
pub local_total: Amount,
}

impl From<yttrium::chain_abstraction::route_ui_fields::RouteUiFields>
for RouteUiFields
{
fn from(
source: yttrium::chain_abstraction::route_ui_fields::RouteUiFields,
) -> Self {
Self {
route: source.route.into_iter().map(Into::into).collect(),
bridge: source.bridge,
initial: source.initial.into(),
local_total: source.local_total,
}
}
}

#[derive(Debug, uniffi::Record)]
pub struct TxnDetails {
pub transaction: CATransaction,
pub estimate: Eip1559Estimation,
pub fee: TransactionFee,
}

impl From<yttrium::chain_abstraction::route_ui_fields::TxnDetails>
for TxnDetails
{
fn from(
source: yttrium::chain_abstraction::route_ui_fields::TxnDetails,
) -> Self {
Self {
transaction: source.transaction,
estimate: source.estimate.into(),
fee: source.fee,
}
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, uniffi::Record)]
pub struct Eip1559Estimation {
/// The base fee per gas.
Expand All @@ -149,31 +103,6 @@ impl From<alloy::providers::utils::Eip1559Estimation> for Eip1559Estimation {
}
}

// uniffi::custom_type!(Eip1559Estimation, FfiEip1559Estimation, {
// try_lift: |val| Ok(Eip1559Estimation {
// max_fee_per_gas: val.max_fee_per_gas.to(),
// max_priority_fee_per_gas: val.max_priority_fee_per_gas.to(),
// }),
// lower: |obj| FfiEip1559Estimation {
// max_fee_per_gas: U128::from(obj.max_fee_per_gas),
// max_priority_fee_per_gas: U128::from(obj.max_priority_fee_per_gas),
// },
// });

#[derive(uniffi::Record)]
pub struct InitTransaction {
pub from: FFIAddress,
pub to: FFIAddress,
pub value: FFIU256,
pub gas: FFIU64,
pub gas_price: FFIU256,
pub data: FFIBytes,
pub nonce: FFIU64,
pub max_fee_per_gas: FFIU256,
pub max_priority_fee_per_gas: FFIU256,
pub chain_id: String,
}

#[derive(uniffi::Record)]
pub struct PreparedSendTransaction {
pub hash: String,
Expand Down Expand Up @@ -215,23 +144,21 @@ impl ChainAbstractionClient {

pub async fn route(
&self,
transaction: InitTransaction,
initial_transaction: InitialTransaction,
) -> Result<RouteResponse, FFIError> {
let ca_transaction = CATransaction::from(transaction);
self.client
.route(ca_transaction)
.route(initial_transaction)
.await
.map_err(|e| FFIError::General(e.to_string()))
}

pub async fn get_route_ui_fields(
&self,
route_response: RouteResponseAvailable,
initial_transaction: chain_abstraction::api::Transaction,
currency: Currency,
) -> Result<RouteUiFields, FFIError> {
self.client
.get_route_ui_fields(route_response, initial_transaction, currency)
.get_route_ui_fields(route_response, currency)
.await
.map(Into::into)
.map_err(|e| FFIError::General(e.to_string()))
Expand Down Expand Up @@ -447,23 +374,6 @@ impl From<FFITransaction> for YTransaction {
}
}

impl From<InitTransaction> for CATransaction {
fn from(source: InitTransaction) -> Self {
CATransaction {
from: source.from,
to: source.to,
value: source.value,
gas: source.gas,
gas_price: source.gas_price,
data: source.data,
nonce: source.nonce,
max_fee_per_gas: source.max_fee_per_gas,
max_priority_fee_per_gas: source.max_priority_fee_per_gas,
chain_id: source.chain_id,
}
}
}

#[cfg(test)]
mod tests {
use {
Expand Down
88 changes: 81 additions & 7 deletions crates/yttrium/src/chain_abstraction/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,101 @@
use {
alloy::primitives::{Address, Bytes, U256, U64},
alloy::{
network::TransactionBuilder,
primitives::{Address, Bytes, U128, U256, U64},
rpc::types::TransactionRequest,
},
alloy_provider::utils::Eip1559Estimation,
serde::{Deserialize, Serialize},
};

pub mod fungible_price;
pub mod route;
pub mod status;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
#[serde(rename_all = "camelCase")]
pub struct InitialTransaction {
// CAIP-2 chain ID
pub chain_id: String,

pub from: Address,
pub to: Address,
pub value: U256,
pub input: Bytes,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
#[serde(rename_all = "camelCase")]
pub struct Transaction {
// CAIP-2 chain ID
pub chain_id: String,

pub from: Address,
pub to: Address,
pub value: U256,
pub gas: U64,
pub data: Bytes,
pub input: Bytes,

pub gas_limit: U64,
pub nonce: U64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
#[serde(rename_all = "camelCase")]
pub struct FeeEstimatedTransaction {
// CAIP-2 chain ID
pub chain_id: String,

// deprecated
pub gas_price: U256,
pub max_fee_per_gas: U256,
pub max_priority_fee_per_gas: U256,
pub from: Address,
pub to: Address,
pub value: U256,
pub input: Bytes,

pub gas_limit: U64,
pub nonce: U64,

pub max_fee_per_gas: U128,
pub max_priority_fee_per_gas: U128,
}

impl FeeEstimatedTransaction {
pub fn from_transaction_and_estimate(
transaction: Transaction,
estimate: Eip1559Estimation,
) -> Self {
Self {
chain_id: transaction.chain_id,
from: transaction.from,
to: transaction.to,
value: transaction.value,
input: transaction.input,
gas_limit: transaction.gas_limit,
nonce: transaction.nonce,
max_fee_per_gas: U128::from(estimate.max_fee_per_gas),
max_priority_fee_per_gas: U128::from(
estimate.max_priority_fee_per_gas,
),
}
}

pub fn into_transaction_request(self) -> TransactionRequest {
let chain_id = self
.chain_id
.strip_prefix("eip155:")
.unwrap()
.parse::<U64>()
.unwrap();
TransactionRequest::default()
.with_chain_id(chain_id.to())
.with_from(self.from)
.with_to(self.to)
.with_value(self.value)
.with_input(self.input)
.with_gas_limit(self.gas_limit.to())
.with_nonce(self.nonce.to())
.with_max_fee_per_gas(self.max_fee_per_gas.to())
.with_max_priority_fee_per_gas(self.max_priority_fee_per_gas.to())
}
}
4 changes: 2 additions & 2 deletions crates/yttrium/src/chain_abstraction/api/route.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
super::Transaction,
super::{InitialTransaction, Transaction},
crate::chain_abstraction::amount::Amount,
alloy::primitives::{utils::Unit, Address, U256},
relay_rpc::domain::ProjectId,
Expand All @@ -16,7 +16,7 @@ pub struct RouteQueryParams {

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RouteRequest {
pub transaction: Transaction,
pub transaction: InitialTransaction,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
Loading

0 comments on commit befa0cb

Please sign in to comment.