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

Implement amount/fee related pack functions to sdk #299

Merged
merged 4 commits into from
Jun 20, 2024
Merged
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
12 changes: 12 additions & 0 deletions bindings/dart/lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<flutter_rust_bridge::for_generated::rust_async::RwLock<AutoDeleveraging>>
@sealed
class AutoDeleveraging extends RustOpaque {
Expand Down
102 changes: 101 additions & 1 deletion bindings/dart/lib/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
String get codegenVersion => '2.0.0-dev.32';

@override
int get rustContentHash => -1291052308;
int get rustContentHash => -634235047;

static const kDefaultExternalLibraryLoaderConfig =
ExternalLibraryLoaderConfig(
Expand Down Expand Up @@ -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<void> 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;

Expand Down Expand Up @@ -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<void> initApp({dynamic hint}) {
return handler.executeNormal(NormalTask(
Expand All @@ -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;
Expand Down
72 changes: 72 additions & 0 deletions bindings/dart/lib/frb_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3849,6 +3849,42 @@ class RustLibWire implements BaseWire {
WireSyncRust2DartDco Function(
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>();

WireSyncRust2DartDco wire_closest_packable_fee_amount(
ffi.Pointer<wire_cst_list_prim_u_8_strict> fee,
) {
return _wire_closest_packable_fee_amount(
fee,
);
}

late final _wire_closest_packable_fee_amountPtr = _lookup<
ffi.NativeFunction<
WireSyncRust2DartDco Function(
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>>(
'frbgen_zklink_wire_closest_packable_fee_amount');
late final _wire_closest_packable_fee_amount =
_wire_closest_packable_fee_amountPtr.asFunction<
WireSyncRust2DartDco Function(
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>();

WireSyncRust2DartDco wire_closest_packable_token_amount(
ffi.Pointer<wire_cst_list_prim_u_8_strict> amount,
) {
return _wire_closest_packable_token_amount(
amount,
);
}

late final _wire_closest_packable_token_amountPtr = _lookup<
ffi.NativeFunction<
WireSyncRust2DartDco Function(
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>>(
'frbgen_zklink_wire_closest_packable_token_amount');
late final _wire_closest_packable_token_amount =
_wire_closest_packable_token_amountPtr.asFunction<
WireSyncRust2DartDco Function(
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>();

void wire_init_app(
int port_,
) {
Expand All @@ -3863,6 +3899,42 @@ class RustLibWire implements BaseWire {
late final _wire_init_app =
_wire_init_appPtr.asFunction<void Function(int)>();

WireSyncRust2DartDco wire_is_fee_amount_packable(
ffi.Pointer<wire_cst_list_prim_u_8_strict> fee,
) {
return _wire_is_fee_amount_packable(
fee,
);
}

late final _wire_is_fee_amount_packablePtr = _lookup<
ffi.NativeFunction<
WireSyncRust2DartDco Function(
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>>(
'frbgen_zklink_wire_is_fee_amount_packable');
late final _wire_is_fee_amount_packable =
_wire_is_fee_amount_packablePtr.asFunction<
WireSyncRust2DartDco Function(
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>();

WireSyncRust2DartDco wire_is_token_amount_packable(
ffi.Pointer<wire_cst_list_prim_u_8_strict> amount,
) {
return _wire_is_token_amount_packable(
amount,
);
}

late final _wire_is_token_amount_packablePtr = _lookup<
ffi.NativeFunction<
WireSyncRust2DartDco Function(
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>>(
'frbgen_zklink_wire_is_token_amount_packable');
late final _wire_is_token_amount_packable =
_wire_is_token_amount_packablePtr.asFunction<
WireSyncRust2DartDco Function(
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>();

void
rust_arc_increment_strong_count_RustOpaque_flutter_rust_bridgefor_generatedrust_asyncRwLockAutoDeleveraging(
ffi.Pointer<ffi.Void> ptr,
Expand Down
27 changes: 27 additions & 0 deletions bindings/dart/rust/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -1081,6 +1082,32 @@ impl Wallet {
}
}

#[frb(sync)]
pub fn is_token_amount_packable(amount: String) -> Result<bool> {
let amount = BigUint::from_str(&amount)?;
Ok(pack::is_token_amount_packable(&amount))
}

#[frb(sync)]
pub fn is_fee_amount_packable(fee: String) -> Result<bool> {
let fee = BigUint::from_str(&fee)?;
Ok(pack::is_fee_amount_packable(&fee))
}

#[frb(sync)]
pub fn closest_packable_token_amount(amount: String) -> Result<String> {
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<String> {
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();
Expand Down
28 changes: 28 additions & 0 deletions bindings/dart/rust/src/frb_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading
Loading