diff --git a/bindings/dart/lib/api.dart b/bindings/dart/lib/api.dart index 1287d72..e41c79c 100644 --- a/bindings/dart/lib/api.dart +++ b/bindings/dart/lib/api.dart @@ -6,6 +6,18 @@ import 'frb_generated.dart'; import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; +bool isTokenAmountPackable({required String amount, dynamic hint}) => + RustLib.instance.api.isTokenAmountPackable(amount: amount, hint: hint); + +bool isFeeAmountPackable({required String fee, dynamic hint}) => + RustLib.instance.api.isFeeAmountPackable(fee: fee, hint: hint); + +String closestPackableTokenAmount({required String amount, dynamic hint}) => + RustLib.instance.api.closestPackableTokenAmount(amount: amount, hint: hint); + +String closestPackableFeeAmount({required String fee, dynamic hint}) => + RustLib.instance.api.closestPackableFeeAmount(fee: fee, hint: hint); + // Rust type: RustOpaqueNom> @sealed class AutoDeleveraging extends RustOpaque { diff --git a/bindings/dart/lib/frb_generated.dart b/bindings/dart/lib/frb_generated.dart index cb6e76f..b385d9b 100644 --- a/bindings/dart/lib/frb_generated.dart +++ b/bindings/dart/lib/frb_generated.dart @@ -56,7 +56,7 @@ class RustLib extends BaseEntrypoint { String get codegenVersion => '2.0.0-dev.32'; @override - int get rustContentHash => -1291052308; + int get rustContentHash => -634235047; static const kDefaultExternalLibraryLoaderConfig = ExternalLibraryLoaderConfig( @@ -494,8 +494,16 @@ abstract class RustLibApi extends BaseApi { ZkLinkSigner zkLinkSignerStarknetSig({required String sig, dynamic hint}); + String closestPackableFeeAmount({required String fee, dynamic hint}); + + String closestPackableTokenAmount({required String amount, dynamic hint}); + Future initApp({dynamic hint}); + bool isFeeAmountPackable({required String fee, dynamic hint}); + + bool isTokenAmountPackable({required String amount, dynamic hint}); + RustArcIncrementStrongCountFnType get rust_arc_increment_strong_count_AutoDeleveraging; @@ -3518,6 +3526,52 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { argNames: ["sig"], ); + @override + String closestPackableFeeAmount({required String fee, dynamic hint}) { + return handler.executeSync(SyncTask( + callFfi: () { + var arg0 = cst_encode_String(fee); + return wire.wire_closest_packable_fee_amount(arg0); + }, + codec: DcoCodec( + decodeSuccessData: dco_decode_String, + decodeErrorData: dco_decode_AnyhowException, + ), + constMeta: kClosestPackableFeeAmountConstMeta, + argValues: [fee], + apiImpl: this, + hint: hint, + )); + } + + TaskConstMeta get kClosestPackableFeeAmountConstMeta => const TaskConstMeta( + debugName: "closest_packable_fee_amount", + argNames: ["fee"], + ); + + @override + String closestPackableTokenAmount({required String amount, dynamic hint}) { + return handler.executeSync(SyncTask( + callFfi: () { + var arg0 = cst_encode_String(amount); + return wire.wire_closest_packable_token_amount(arg0); + }, + codec: DcoCodec( + decodeSuccessData: dco_decode_String, + decodeErrorData: dco_decode_AnyhowException, + ), + constMeta: kClosestPackableTokenAmountConstMeta, + argValues: [amount], + apiImpl: this, + hint: hint, + )); + } + + TaskConstMeta get kClosestPackableTokenAmountConstMeta => const TaskConstMeta( + debugName: "closest_packable_token_amount", + argNames: ["amount"], + ); + @override Future initApp({dynamic hint}) { return handler.executeNormal(NormalTask( @@ -3540,6 +3594,52 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { argNames: [], ); + @override + bool isFeeAmountPackable({required String fee, dynamic hint}) { + return handler.executeSync(SyncTask( + callFfi: () { + var arg0 = cst_encode_String(fee); + return wire.wire_is_fee_amount_packable(arg0); + }, + codec: DcoCodec( + decodeSuccessData: dco_decode_bool, + decodeErrorData: dco_decode_AnyhowException, + ), + constMeta: kIsFeeAmountPackableConstMeta, + argValues: [fee], + apiImpl: this, + hint: hint, + )); + } + + TaskConstMeta get kIsFeeAmountPackableConstMeta => const TaskConstMeta( + debugName: "is_fee_amount_packable", + argNames: ["fee"], + ); + + @override + bool isTokenAmountPackable({required String amount, dynamic hint}) { + return handler.executeSync(SyncTask( + callFfi: () { + var arg0 = cst_encode_String(amount); + return wire.wire_is_token_amount_packable(arg0); + }, + codec: DcoCodec( + decodeSuccessData: dco_decode_bool, + decodeErrorData: dco_decode_AnyhowException, + ), + constMeta: kIsTokenAmountPackableConstMeta, + argValues: [amount], + apiImpl: this, + hint: hint, + )); + } + + TaskConstMeta get kIsTokenAmountPackableConstMeta => const TaskConstMeta( + debugName: "is_token_amount_packable", + argNames: ["amount"], + ); + RustArcIncrementStrongCountFnType get rust_arc_increment_strong_count_AutoDeleveraging => wire .rust_arc_increment_strong_count_RustOpaque_flutter_rust_bridgefor_generatedrust_asyncRwLockAutoDeleveraging; diff --git a/bindings/dart/lib/frb_generated.io.dart b/bindings/dart/lib/frb_generated.io.dart index a000b74..5cc3dcb 100644 --- a/bindings/dart/lib/frb_generated.io.dart +++ b/bindings/dart/lib/frb_generated.io.dart @@ -3849,6 +3849,42 @@ class RustLibWire implements BaseWire { WireSyncRust2DartDco Function( ffi.Pointer)>(); + WireSyncRust2DartDco wire_closest_packable_fee_amount( + ffi.Pointer fee, + ) { + return _wire_closest_packable_fee_amount( + fee, + ); + } + + late final _wire_closest_packable_fee_amountPtr = _lookup< + ffi.NativeFunction< + WireSyncRust2DartDco Function( + ffi.Pointer)>>( + 'frbgen_zklink_wire_closest_packable_fee_amount'); + late final _wire_closest_packable_fee_amount = + _wire_closest_packable_fee_amountPtr.asFunction< + WireSyncRust2DartDco Function( + ffi.Pointer)>(); + + WireSyncRust2DartDco wire_closest_packable_token_amount( + ffi.Pointer amount, + ) { + return _wire_closest_packable_token_amount( + amount, + ); + } + + late final _wire_closest_packable_token_amountPtr = _lookup< + ffi.NativeFunction< + WireSyncRust2DartDco Function( + ffi.Pointer)>>( + 'frbgen_zklink_wire_closest_packable_token_amount'); + late final _wire_closest_packable_token_amount = + _wire_closest_packable_token_amountPtr.asFunction< + WireSyncRust2DartDco Function( + ffi.Pointer)>(); + void wire_init_app( int port_, ) { @@ -3863,6 +3899,42 @@ class RustLibWire implements BaseWire { late final _wire_init_app = _wire_init_appPtr.asFunction(); + WireSyncRust2DartDco wire_is_fee_amount_packable( + ffi.Pointer fee, + ) { + return _wire_is_fee_amount_packable( + fee, + ); + } + + late final _wire_is_fee_amount_packablePtr = _lookup< + ffi.NativeFunction< + WireSyncRust2DartDco Function( + ffi.Pointer)>>( + 'frbgen_zklink_wire_is_fee_amount_packable'); + late final _wire_is_fee_amount_packable = + _wire_is_fee_amount_packablePtr.asFunction< + WireSyncRust2DartDco Function( + ffi.Pointer)>(); + + WireSyncRust2DartDco wire_is_token_amount_packable( + ffi.Pointer amount, + ) { + return _wire_is_token_amount_packable( + amount, + ); + } + + late final _wire_is_token_amount_packablePtr = _lookup< + ffi.NativeFunction< + WireSyncRust2DartDco Function( + ffi.Pointer)>>( + 'frbgen_zklink_wire_is_token_amount_packable'); + late final _wire_is_token_amount_packable = + _wire_is_token_amount_packablePtr.asFunction< + WireSyncRust2DartDco Function( + ffi.Pointer)>(); + void rust_arc_increment_strong_count_RustOpaque_flutter_rust_bridgefor_generatedrust_asyncRwLockAutoDeleveraging( ffi.Pointer ptr, diff --git a/bindings/dart/rust/src/api.rs b/bindings/dart/rust/src/api.rs index 92d0717..3ccecb0 100644 --- a/bindings/dart/rust/src/api.rs +++ b/bindings/dart/rust/src/api.rs @@ -10,6 +10,7 @@ use zklink_sdk_signers::zklink_signer::{ signature::ZkLinkSignature as InnerZkLinkSignature, PubKeyHash, ZkLinkSigner as InnerZkLinkSigner, }; +use zklink_sdk_types::basic_types::pack; use zklink_sdk_types::basic_types::{BigUint, GetBytes, ZkLinkAddress}; use zklink_sdk_types::tx_builder::*; use zklink_sdk_types::tx_type::change_pubkey::{ @@ -1081,6 +1082,32 @@ impl Wallet { } } +#[frb(sync)] +pub fn is_token_amount_packable(amount: String) -> Result { + let amount = BigUint::from_str(&amount)?; + Ok(pack::is_token_amount_packable(&amount)) +} + +#[frb(sync)] +pub fn is_fee_amount_packable(fee: String) -> Result { + let fee = BigUint::from_str(&fee)?; + Ok(pack::is_fee_amount_packable(&fee)) +} + +#[frb(sync)] +pub fn closest_packable_token_amount(amount: String) -> Result { + let amount = BigUint::from_str(&amount)?; + let packable_amount = pack::closest_packable_token_amount(&amount); + Ok(packable_amount.to_string()) +} + +#[frb(sync)] +pub fn closest_packable_fee_amount(fee: String) -> Result { + let fee = BigUint::from_str(&fee)?; + let packable_fee = pack::closest_packable_fee_amount(&fee); + Ok(packable_fee.to_string()) +} + #[frb(init)] pub fn init_app() { flutter_rust_bridge::setup_default_user_utils(); diff --git a/bindings/dart/rust/src/frb_generated.io.rs b/bindings/dart/rust/src/frb_generated.io.rs index 0b955af..b1478f6 100644 --- a/bindings/dart/rust/src/frb_generated.io.rs +++ b/bindings/dart/rust/src/frb_generated.io.rs @@ -1416,11 +1416,39 @@ pub extern "C" fn frbgen_zklink_wire_ZkLinkSigner_starknet_sig( wire_ZkLinkSigner_starknet_sig_impl(sig) } +#[no_mangle] +pub extern "C" fn frbgen_zklink_wire_closest_packable_fee_amount( + fee: *mut wire_cst_list_prim_u_8_strict, +) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco { + wire_closest_packable_fee_amount_impl(fee) +} + +#[no_mangle] +pub extern "C" fn frbgen_zklink_wire_closest_packable_token_amount( + amount: *mut wire_cst_list_prim_u_8_strict, +) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco { + wire_closest_packable_token_amount_impl(amount) +} + #[no_mangle] pub extern "C" fn frbgen_zklink_wire_init_app(port_: i64) { wire_init_app_impl(port_) } +#[no_mangle] +pub extern "C" fn frbgen_zklink_wire_is_fee_amount_packable( + fee: *mut wire_cst_list_prim_u_8_strict, +) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco { + wire_is_fee_amount_packable_impl(fee) +} + +#[no_mangle] +pub extern "C" fn frbgen_zklink_wire_is_token_amount_packable( + amount: *mut wire_cst_list_prim_u_8_strict, +) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco { + wire_is_token_amount_packable_impl(amount) +} + #[no_mangle] pub extern "C" fn frbgen_zklink_rust_arc_increment_strong_count_RustOpaque_flutter_rust_bridgefor_generatedrust_asyncRwLockAutoDeleveraging( ptr: *const std::ffi::c_void, diff --git a/bindings/dart/rust/src/frb_generated.rs b/bindings/dart/rust/src/frb_generated.rs index 55b2553..67ffdad 100644 --- a/bindings/dart/rust/src/frb_generated.rs +++ b/bindings/dart/rust/src/frb_generated.rs @@ -32,7 +32,7 @@ flutter_rust_bridge::frb_generated_boilerplate!( default_rust_auto_opaque = RustAutoOpaqueNom, ); pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.32"; -pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -1291052308; +pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -634235047; // Section: executor @@ -2163,6 +2163,38 @@ fn wire_ZkLinkSigner_starknet_sig_impl( }, ) } +fn wire_closest_packable_fee_amount_impl( + fee: impl CstDecode, +) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco { + FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::( + flutter_rust_bridge::for_generated::TaskInfo { + debug_name: "closest_packable_fee_amount", + port: None, + mode: flutter_rust_bridge::for_generated::FfiCallMode::Sync, + }, + move || { + let api_fee = fee.cst_decode(); + transform_result_dco((move || crate::api::closest_packable_fee_amount(api_fee))()) + }, + ) +} +fn wire_closest_packable_token_amount_impl( + amount: impl CstDecode, +) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco { + FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::( + flutter_rust_bridge::for_generated::TaskInfo { + debug_name: "closest_packable_token_amount", + port: None, + mode: flutter_rust_bridge::for_generated::FfiCallMode::Sync, + }, + move || { + let api_amount = amount.cst_decode(); + transform_result_dco((move || { + crate::api::closest_packable_token_amount(api_amount) + })()) + }, + ) +} fn wire_init_app_impl(port_: flutter_rust_bridge::for_generated::MessagePort) { FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::( flutter_rust_bridge::for_generated::TaskInfo { @@ -2177,6 +2209,36 @@ fn wire_init_app_impl(port_: flutter_rust_bridge::for_generated::MessagePort) { }, ) } +fn wire_is_fee_amount_packable_impl( + fee: impl CstDecode, +) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco { + FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::( + flutter_rust_bridge::for_generated::TaskInfo { + debug_name: "is_fee_amount_packable", + port: None, + mode: flutter_rust_bridge::for_generated::FfiCallMode::Sync, + }, + move || { + let api_fee = fee.cst_decode(); + transform_result_dco((move || crate::api::is_fee_amount_packable(api_fee))()) + }, + ) +} +fn wire_is_token_amount_packable_impl( + amount: impl CstDecode, +) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco { + FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::( + flutter_rust_bridge::for_generated::TaskInfo { + debug_name: "is_token_amount_packable", + port: None, + mode: flutter_rust_bridge::for_generated::FfiCallMode::Sync, + }, + move || { + let api_amount = amount.cst_decode(); + transform_result_dco((move || crate::api::is_token_amount_packable(api_amount))()) + }, + ) +} // Section: dart2rust diff --git a/bindings/dart/test/main.dart b/bindings/dart/test/main.dart index 4f25cbd..efe44f4 100644 --- a/bindings/dart/test/main.dart +++ b/bindings/dart/test/main.dart @@ -5,6 +5,21 @@ import '../lib/frb_generated.dart'; Future main() async { await RustLib.init(); + test('Pack Utils', () { + var amount = "1234567899808787"; + print("Original amount: " + amount); + expect(isTokenAmountPackable(amount: amount), false); + amount = closestPackableTokenAmount(amount: amount); + expect(isTokenAmountPackable(amount: amount), true); + print("Converted amount: " + amount); + var fee = "10000567777"; + print("Original fee: " + fee); + expect(isFeeAmountPackable(fee: fee), false); + fee = closestPackableFeeAmount(fee: fee); + expect(isFeeAmountPackable(fee: fee), true); + print("Converted fee: " + fee); + }); + test('Order Matching', () { var signer = Signer.ethSigner(ethPrivateKey: "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"); var maker = signer.createSignedOrder(order: Order( diff --git a/bindings/sdk/src/ffi.udl b/bindings/sdk/src/ffi.udl index 19d9fcf..4d2c689 100644 --- a/bindings/sdk/src/ffi.udl +++ b/bindings/sdk/src/ffi.udl @@ -639,5 +639,11 @@ namespace zklink_sdk { PackedEthSignature eth_signature_of_change_pubkey(ChangePubKey tx, EthSigner eth_signer); [Throws=SignError] ChangePubKey create_signed_change_pubkey(ZkLinkSigner zklink_signer, ChangePubKey tx, ChangePubKeyAuthData eth_auth_data); + + // pack utils + boolean is_token_amount_packable([ByRef] BigUint amount); + boolean is_fee_amount_packable([ByRef] BigUint fee); + BigUint closest_packable_token_amount([ByRef] BigUint amount); + BigUint closest_packable_fee_amount([ByRef] BigUint fee); }; diff --git a/bindings/sdk/src/lib.rs b/bindings/sdk/src/lib.rs index 5948f55..1e6da7f 100644 --- a/bindings/sdk/src/lib.rs +++ b/bindings/sdk/src/lib.rs @@ -19,6 +19,10 @@ use zklink_sdk_signers::zklink_signer::pubkey_hash::PubKeyHash; use zklink_sdk_signers::zklink_signer::public_key::PackedPublicKey; use zklink_sdk_signers::zklink_signer::signature::{PackedSignature, ZkLinkSignature}; +use zklink_sdk_types::basic_types::pack::{ + closest_packable_fee_amount, closest_packable_token_amount, is_fee_amount_packable, + is_token_amount_packable, +}; use zklink_sdk_types::basic_types::tx_hash::TxHash; use zklink_sdk_types::basic_types::zklink_address::ZkLinkAddress; use zklink_sdk_types::basic_types::GetBytes; diff --git a/bindings/wasm/src/utils.rs b/bindings/wasm/src/utils.rs index 39a0c39..c7bd00f 100644 --- a/bindings/wasm/src/utils.rs +++ b/bindings/wasm/src/utils.rs @@ -2,11 +2,25 @@ use std::str::FromStr; use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::JsValue; use zklink_sdk_types::basic_types::pack::{ - closest_packable_fee_amount, closest_packable_token_amount, + closest_packable_fee_amount, closest_packable_token_amount, is_fee_amount_packable, + is_token_amount_packable, }; use zklink_sdk_types::basic_types::BigUint; use zklink_sdk_types::error::TypeError; +#[wasm_bindgen(js_name=isTokenAmountPackable)] +pub fn token_amount_packable(amount: &str) -> Result { + let amount = + BigUint::from_str(amount).map_err(|e| TypeError::InvalidBigIntStr(e.to_string()))?; + Ok(is_token_amount_packable(&amount)) +} + +#[wasm_bindgen(js_name=isFeeAmountPackable)] +pub fn fee_amount_packable(fee: &str) -> Result { + let fee = BigUint::from_str(fee).map_err(|e| TypeError::InvalidBigIntStr(e.to_string()))?; + Ok(is_fee_amount_packable(&fee)) +} + #[wasm_bindgen(js_name=closestPackableTransactionAmount)] pub fn closest_packable_transaction_amount(amount: &str) -> Result { let amount = diff --git a/examples/Cpp/2_withdraw.cpp b/examples/Cpp/2_withdraw.cpp index fec50d7..94c3cd3 100644 --- a/examples/Cpp/2_withdraw.cpp +++ b/examples/Cpp/2_withdraw.cpp @@ -10,13 +10,19 @@ int main() { SubAccountId sub_account_id = 4; ChainId to_chain_id = 5; ZkLinkAddress to_address = "0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9"; - TokenId l2_source_token = 2; + TokenId l2_source_token = 17; TokenId l1_target_token = 17; - BigUint amount = "100000"; - BigUint fee = "1000"; + BigUint amount = "1234567899808787"; + cout << "Original amount: " << amount << "\n"; + amount = closest_packable_token_amount(amount); + cout << "Converted amount: " << amount << "\n"; + BigUint fee = "10000567777"; + cout << "Original fee: " << fee << "\n"; + fee = closest_packable_fee_amount(fee); + cout << "Converted fee: " << fee << "\n"; Nonce nonce = 1; uint16_t withdraw_fee_ratio = 50; - TimeStamp timestamp = 100; + TimeStamp timestamp = 1000000000; WithdrawBuilder builder = { account_id, sub_account_id, to_chain_id, to_address, diff --git a/examples/Golang/2_withdraw.go b/examples/Golang/2_withdraw.go index 2aec030..83b6eca 100644 --- a/examples/Golang/2_withdraw.go +++ b/examples/Golang/2_withdraw.go @@ -5,7 +5,6 @@ import ( "math/big" "encoding/json" "fmt" - "time" "bytes" "io/ioutil" sdk "github.com/zkLinkProtocol/zklink_sdk/examples/Golang/generated/zklink_sdk" @@ -30,13 +29,17 @@ func HighLevelWithdraw() { toAddress := sdk.ZkLinkAddress("0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9") l2SourceToken := sdk.TokenId(17) l1TargetToken := sdk.TokenId(17) - amount := *big.NewInt(1000000) - fee := *big.NewInt(1000) - nonce := sdk.Nonce(1) - withdrawFeeRatio := uint16(50) - // get current timestamp - now := time.Now() - timestamp := sdk.TimeStamp(now.Unix()) + amount := *big.NewInt(1234567899808787) + fmt.Println("Original amount: ", amount) + amount = sdk.ClosestPackableTokenAmount(amount) + fmt.Println("Converted amount:s", amount) + fee := *big.NewInt(10000567777) + fmt.Println("Original fee: ", fee) + fee = sdk.ClosestPackableFeeAmount(fee) + fmt.Println("Converted fee: ", fee) + nonce := sdk.Nonce(1) + withdrawFeeRatio := uint16(50) + timestamp := sdk.TimeStamp(1000000000) builder := sdk.WithdrawBuilder{ AccountId: accountId, @@ -58,7 +61,7 @@ func HighLevelWithdraw() { return } txSignature, err := signer.SignWithdraw(tx, "USDT",nil,nil) - fmt.Println("tx signature: %s", txSignature) + fmt.Println("tx signature: ", txSignature) if err != nil { return } diff --git a/examples/Javascript/js-example/2_transfer.js b/examples/Javascript/js-example/2_transfer.js index 8a03029..e328552 100755 --- a/examples/Javascript/js-example/2_transfer.js +++ b/examples/Javascript/js-example/2_transfer.js @@ -7,8 +7,18 @@ async function testEvmChains() { const to_address = "0x5505a8cD4594Dbf79d8C59C0Df1414AB871CA896"; const ts = Math.floor(Date.now() / 1000); try { - let amount = wasm.closestPackableTransactionAmount("1234567899808787"); - let fee = wasm.closestPackableTransactionFee("10000567777") + let amount = "1234567899808787"; + console.log("Original amount: " + amount); + console.assert(isTokenAmountPackable(amount) == false); + amount = wasm.closestPackableTransactionAmount(amount); + console.assert(isTokenAmountPackable(amount)); + console.log("Converted amount: " + amount); + let fee = "10000567777"; + console.log("Original fee: " + fee); + console.assert(isFeeAmountPackable(fee) == false); + fee = wasm.closestPackableTransactionFee(fee) + console.assert(isFeeAmountPackable(fee)); + console.log("Converted fee: " + fee); let tx_builder = new wasm.TransferBuilder(10, to_address, 1, 1, 18, fee, amount, 1,ts); let transfer = wasm.newTransfer(tx_builder); diff --git a/examples/Python/2_withdraw.py b/examples/Python/2_withdraw.py index cd15b7e..3aa21e3 100644 --- a/examples/Python/2_withdraw.py +++ b/examples/Python/2_withdraw.py @@ -1,4 +1,3 @@ -import time import zklink_sdk as sdk def main(): @@ -7,18 +6,28 @@ def main(): sub_account_id = 4 to_chain_id = 5 to_address = "0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9" - l2_source_token = 2 + l2_source_token = 17 l1_target_token = 17 - amount = "100000" - fee = "1000" + amount = "1234567899808787" + print("Original amount: " + amount) + assert not sdk.is_token_amount_packable(amount) + amount = sdk.closest_packable_token_amount(amount) + assert sdk.is_token_amount_packable(amount) + print("Converted amount: " + amount) + fee = "10000567777" + print("Original fee: " + fee) + assert not sdk.is_fee_amount_packable(fee) + fee = sdk.closest_packable_fee_amount(fee) + assert sdk.is_fee_amount_packable(fee) + print("Converted fee: " + fee) nonce = 1 withdraw_fee_ratio = 50 - timestamp = int(time.time()) + timestamp = 1000000000 builder = sdk.WithdrawBuilder( account_id, - to_chain_id, sub_account_id, + to_chain_id, to_address, l2_source_token, l1_target_token, @@ -26,8 +35,8 @@ def main(): None, fee, nonce, - True, withdraw_fee_ratio, + True, timestamp ) tx = sdk.Withdraw(builder) diff --git a/types/src/tx_type/zklink_tx.rs b/types/src/tx_type/zklink_tx.rs index 8995bcb..5067b62 100644 --- a/types/src/tx_type/zklink_tx.rs +++ b/types/src/tx_type/zklink_tx.rs @@ -16,7 +16,19 @@ use wasm_bindgen::prelude::wasm_bindgen; /// A set of L2 transaction type supported by the zklink network. #[wasm_bindgen] -#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, FromRepr, EnumIter, EnumString, AsRefStr)] +#[derive( + Debug, + Clone, + Copy, + Serialize, + Deserialize, + Eq, + PartialEq, + FromRepr, + EnumIter, + EnumString, + AsRefStr, +)] #[repr(u8)] pub enum ZkLinkTxType { Deposit = 0x01,