Skip to content

Commit

Permalink
fix: Kotlin bindings should give hex not decimal (#88)
Browse files Browse the repository at this point in the history
* fix: Kotlin bindings should give hex not decimal

* chore: add tests

* chore: assert supported currency

* chore: fix cargo udeps warnings
  • Loading branch information
chris13524 authored Dec 12, 2024
1 parent 433b401 commit 99aa22d
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 23 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/release-kotlin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:
inputs:
version:
description: 'Version to release (e.g. 0.0.1)'
description: "Version to release (e.g. 0.0.1)"
required: true

env:
Expand All @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
target: [aarch64-linux-android, armv7-linux-androideabi]

steps:
- uses: actions/checkout@v3

Expand All @@ -37,9 +37,9 @@ jobs:
- name: Set up Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
distribution: "temurin"
java-version: "17"

- name: Set up Android SDK
uses: android-actions/setup-android@v3
with:
Expand All @@ -50,22 +50,22 @@ jobs:
- name: Build with Gradle
run: |
./gradlew clean assembleRelease -Pversion=${{ env.VERSION }}
- name: Install cargo-ndk
run: |
cargo install cargo-ndk
- name: Build Rust library
run: |
cargo ndk -t ${{ matrix.target }} build --release --features=uniffi/cli
- name: Generate Kotlin bindings (once)
if: ${{ matrix.target == 'aarch64-linux-android' }}
run: |
cargo run --features=uniffi/cli --bin uniffi-bindgen generate --library target/${{ matrix.target }}/release/libuniffi_yttrium.so --language kotlin --out-dir yttrium/kotlin-bindings
cargo run --features=uniffi/cli --bin uniffi-bindgen generate --library target/${{ matrix.target }}/release/libuniffi_yttrium.so --language kotlin --out-dir yttrium/kotlin-bindings
- name: Prepare artifacts
run: |
run: |
# Map Rust targets to Android ABI names
declare -A abi_map
abi_map[aarch64-linux-android]="arm64-v8a"
Expand All @@ -79,7 +79,7 @@ jobs:
mkdir -p yttrium/libs/$abi_name
cp target/${{ matrix.target }}/release/libuniffi_yttrium.so yttrium/libs/$abi_name/
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
Expand Down
73 changes: 66 additions & 7 deletions crates/kotlin-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use {
alloy::{
network::Ethereum,
primitives::{
Bytes as FFIBytes, U128 as FFIU128, U256 as FFIU256, U64 as FFIU64,
Bytes as FFIBytes, Uint, U128 as FFIU128, U256 as FFIU256,
U64 as FFIU64,
},
providers::{Provider, ReqwestProvider},
},
Expand Down Expand Up @@ -58,19 +59,25 @@ uniffi::custom_type!(FFIAddress, String, {
lower: |obj| obj.to_string(),
});

fn uint_to_hex<const BITS: usize, const LIMBS: usize>(
obj: Uint<BITS, LIMBS>,
) -> String {
format!("0x{obj:x}")
}

uniffi::custom_type!(FFIU64, String, {
try_lift: |val| Ok(val.parse()?),
lower: |obj| obj.to_string(),
lower: |obj| uint_to_hex(obj),
});

uniffi::custom_type!(FFIU128, String, {
try_lift: |val| Ok(val.parse()?),
lower: |obj| obj.to_string(),
lower: |obj| uint_to_hex(obj),
});

uniffi::custom_type!(FFIU256, String, {
try_lift: |val| Ok(val.parse()?),
lower: |obj| obj.to_string(),
lower: |obj| uint_to_hex(obj),
});

uniffi::custom_type!(FFIBytes, String, {
Expand Down Expand Up @@ -450,9 +457,13 @@ impl From<InitTransaction> for CATransaction {

#[cfg(test)]
mod tests {
use alloy::{
network::Ethereum,
providers::{Provider, ReqwestProvider},
use {
super::*,
alloy::{
network::Ethereum,
primitives::{address, bytes},
providers::{Provider, ReqwestProvider},
},
};

#[tokio::test]
Expand All @@ -470,4 +481,52 @@ mod tests {

println!("estimate: {estimate:?}");
}

#[test]
fn test_address_lower() {
let ffi_u64 = address!("abababababababababababababababababababab");
let u = ::uniffi::FfiConverter::<crate::UniFfiTag>::lower(ffi_u64);
let s: String =
::uniffi::FfiConverter::<crate::UniFfiTag>::try_lift(u).unwrap();
assert_eq!(s, format!("0xABaBaBaBABabABabAbAbABAbABabababaBaBABaB"));
}

#[test]
fn test_u64_lower() {
let num = 1234567890;
let ffi_u64 = FFIU64::from(num);
let u = ::uniffi::FfiConverter::<crate::UniFfiTag>::lower(ffi_u64);
let s: String =
::uniffi::FfiConverter::<crate::UniFfiTag>::try_lift(u).unwrap();
assert_eq!(s, format!("0x{num:x}"));
}

#[test]
fn test_u128_lower() {
let num = 1234567890;
let ffi_u64 = FFIU128::from(num);
let u = ::uniffi::FfiConverter::<crate::UniFfiTag>::lower(ffi_u64);
let s: String =
::uniffi::FfiConverter::<crate::UniFfiTag>::try_lift(u).unwrap();
assert_eq!(s, format!("0x{num:x}"));
}

#[test]
fn test_u256_lower() {
let num = 1234567890;
let ffi_u64 = FFIU256::from(num);
let u = ::uniffi::FfiConverter::<crate::UniFfiTag>::lower(ffi_u64);
let s: String =
::uniffi::FfiConverter::<crate::UniFfiTag>::try_lift(u).unwrap();
assert_eq!(s, format!("0x{num:x}"));
}

#[test]
fn test_bytes_lower() {
let ffi_u64 = bytes!("aabbccdd");
let u = ::uniffi::FfiConverter::<crate::UniFfiTag>::lower(ffi_u64);
let s: String =
::uniffi::FfiConverter::<crate::UniFfiTag>::try_lift(u).unwrap();
assert_eq!(s, format!("0xaabbccdd"));
}
}
4 changes: 4 additions & 0 deletions crates/yttrium/src/chain_abstraction/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ impl Client {
// TODO use this to e.g. modify priority fee
// _speed: String,
) -> Result<RouteUiFields, RouteUiFieldsError> {
if currency != Currency::Usd {
unimplemented!("Only USD currency is supported for now");
}

let chains = route_response
.transactions
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/yttrium/src/chain_abstraction/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {

// TODO get Blockchain API to use these types?

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Enum))]
#[serde(rename_all = "lowercase")]
pub enum Currency {
Expand Down
56 changes: 56 additions & 0 deletions crates/yttrium/src/uniffi_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,59 @@ fn funding_metadata_to_amount(value: FundingMetadata) -> Amount {
fn funding_metadata_to_bridging_fee_amount(value: FundingMetadata) -> Amount {
value.to_bridging_fee_amount()
}

#[cfg(test)]
mod tests {
use {
super::*,
alloy::primitives::{address, bytes},
};

#[test]
fn test_address_lower() {
let ffi_u64 = address!("abababababababababababababababababababab");
let u = ::uniffi::FfiConverter::<crate::UniFfiTag>::lower(ffi_u64);
let s: String =
::uniffi::FfiConverter::<crate::UniFfiTag>::try_lift(u).unwrap();
assert_eq!(s, format!("0xABaBaBaBABabABabAbAbABAbABabababaBaBABaB"));
}

#[test]
fn test_u64_lower() {
let num = 1234567890;
let ffi_u64 = U64::from(num);
let u = ::uniffi::FfiConverter::<crate::UniFfiTag>::lower(ffi_u64);
let s: String =
::uniffi::FfiConverter::<crate::UniFfiTag>::try_lift(u).unwrap();
assert_eq!(s, format!("0x{num:x}"));
}

#[test]
fn test_u128_lower() {
let num = 1234567890;
let ffi_u64 = U128::from(num);
let u = ::uniffi::FfiConverter::<crate::UniFfiTag>::lower(ffi_u64);
let s: String =
::uniffi::FfiConverter::<crate::UniFfiTag>::try_lift(u).unwrap();
assert_eq!(s, format!("0x{num:x}"));
}

#[test]
fn test_u256_lower() {
let num = 1234567890;
let ffi_u64 = U256::from(num);
let u = ::uniffi::FfiConverter::<crate::UniFfiTag>::lower(ffi_u64);
let s: String =
::uniffi::FfiConverter::<crate::UniFfiTag>::try_lift(u).unwrap();
assert_eq!(s, format!("0x{num:x}"));
}

#[test]
fn test_bytes_lower() {
let ffi_u64 = bytes!("aabbccdd");
let u = ::uniffi::FfiConverter::<crate::UniFfiTag>::lower(ffi_u64);
let s: String =
::uniffi::FfiConverter::<crate::UniFfiTag>::try_lift(u).unwrap();
assert_eq!(s, format!("0xaabbccdd"));
}
}
7 changes: 4 additions & 3 deletions crates/yttrium_dart/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ log.workspace = true
# name = "dart_bindgen"
# path = "dart_bindgen.rs"

# [package.metadata.cargo-udeps.ignore]
# # These crates are needed for Linux builds, but will give unused deps error when built on macOS
# normal = ["openssl", "openssl-sys"]
[package.metadata.cargo-udeps.ignore]
# These crates are needed for Linux builds, but will give unused deps error when built on macOS
# TODO this is moved over from kotlin-ffi; do we need these two crates for yttrium_dart?
normal = ["openssl", "openssl-sys"]

0 comments on commit 99aa22d

Please sign in to comment.