mirror of
https://github.com/MAGICGrants/flutter_libsparkmobile.git
synced 2026-01-09 21:17:56 -05:00
add spark send fee estimate function
This commit is contained in:
@@ -745,6 +745,81 @@ abstract final class LibSpark {
|
||||
|
||||
return hashes;
|
||||
}
|
||||
|
||||
static int estimateSparkFee({
|
||||
required String privateKeyHex,
|
||||
int index = 1,
|
||||
required int sendAmount,
|
||||
required bool subtractFeeFromAmount,
|
||||
required List<
|
||||
({
|
||||
String serializedCoin,
|
||||
String serializedCoinContext,
|
||||
int groupId,
|
||||
int height,
|
||||
})>
|
||||
serializedCoins,
|
||||
required int privateRecipientsCount,
|
||||
}) {
|
||||
|
||||
final privateKeyPtr =
|
||||
privateKeyHex.to32BytesFromHex().unsignedCharPointer();
|
||||
|
||||
final serializedCoinsPtr = malloc.allocate<DartSpendCoinData>(
|
||||
sizeOf<DartSpendCoinData>() * serializedCoins.length);
|
||||
for (int i = 0; i < serializedCoins.length; i++) {
|
||||
final b64CoinDecoded = base64Decode(serializedCoins[i].serializedCoin);
|
||||
serializedCoinsPtr[i].serializedCoin =
|
||||
malloc.allocate<CCDataStream>(sizeOf<CCDataStream>());
|
||||
serializedCoinsPtr[i].serializedCoin.ref.data =
|
||||
b64CoinDecoded.unsignedCharPointer();
|
||||
serializedCoinsPtr[i].serializedCoin.ref.length = b64CoinDecoded.length;
|
||||
|
||||
final b64ContextDecoded =
|
||||
base64Decode(serializedCoins[i].serializedCoinContext);
|
||||
serializedCoinsPtr[i].serializedCoinContext =
|
||||
malloc.allocate<CCDataStream>(sizeOf<CCDataStream>());
|
||||
serializedCoinsPtr[i].serializedCoinContext.ref.data =
|
||||
b64ContextDecoded.unsignedCharPointer();
|
||||
serializedCoinsPtr[i].serializedCoinContext.ref.length =
|
||||
b64ContextDecoded.length;
|
||||
|
||||
serializedCoinsPtr[i].groupId = serializedCoins[i].groupId;
|
||||
serializedCoinsPtr[i].height = serializedCoins[i].height;
|
||||
}
|
||||
|
||||
final result = _bindings.estimateSparkFee(
|
||||
privateKeyPtr,
|
||||
index,
|
||||
sendAmount,
|
||||
subtractFeeFromAmount ? 1 : 0,
|
||||
serializedCoinsPtr,
|
||||
serializedCoins.length,
|
||||
privateRecipientsCount,
|
||||
);
|
||||
|
||||
for (int i = 0; i < serializedCoins.length; i++) {
|
||||
malloc.free(serializedCoinsPtr[i].serializedCoinContext);
|
||||
malloc.free(serializedCoinsPtr[i].serializedCoin);
|
||||
}
|
||||
malloc.free(serializedCoinsPtr);
|
||||
malloc.free(privateKeyPtr);
|
||||
|
||||
if (result.ref.error.address != nullptr.address) {
|
||||
final ex = Exception(
|
||||
result.ref.error.cast<Utf8>().toDartString(),
|
||||
);
|
||||
malloc.free(result.ref.error);
|
||||
malloc.free(result);
|
||||
throw ex;
|
||||
}
|
||||
|
||||
final fee = result.ref.fee;
|
||||
|
||||
malloc.free(result);
|
||||
|
||||
return fee;
|
||||
}
|
||||
}
|
||||
|
||||
extension on Pointer<UnsignedChar> {
|
||||
|
||||
@@ -294,6 +294,40 @@ class FlutterLibsparkmobileBindings {
|
||||
ffi.Pointer<ffi.UnsignedChar>, ffi.Int)>>('hashTags');
|
||||
late final _hashTags = _hashTagsPtr.asFunction<
|
||||
ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.UnsignedChar>, int)>();
|
||||
|
||||
ffi.Pointer<SparkFeeResult> estimateSparkFee(
|
||||
ffi.Pointer<ffi.UnsignedChar> keyData,
|
||||
int index,
|
||||
int sendAmount,
|
||||
int subtractFeeFromAmount,
|
||||
ffi.Pointer<DartSpendCoinData> coins,
|
||||
int coinsLength,
|
||||
int privateRecipientsLength,
|
||||
) {
|
||||
return _estimateSparkFee(
|
||||
keyData,
|
||||
index,
|
||||
sendAmount,
|
||||
subtractFeeFromAmount,
|
||||
coins,
|
||||
coinsLength,
|
||||
privateRecipientsLength,
|
||||
);
|
||||
}
|
||||
|
||||
late final _estimateSparkFeePtr = _lookup<
|
||||
ffi.NativeFunction<
|
||||
ffi.Pointer<SparkFeeResult> Function(
|
||||
ffi.Pointer<ffi.UnsignedChar>,
|
||||
ffi.Int,
|
||||
ffi.Int,
|
||||
ffi.Int,
|
||||
ffi.Pointer<DartSpendCoinData>,
|
||||
ffi.Int,
|
||||
ffi.Int)>>('estimateSparkFee');
|
||||
late final _estimateSparkFee = _estimateSparkFeePtr.asFunction<
|
||||
ffi.Pointer<SparkFeeResult> Function(ffi.Pointer<ffi.UnsignedChar>, int,
|
||||
int, int, ffi.Pointer<DartSpendCoinData>, int, int)>();
|
||||
}
|
||||
|
||||
/// FFI-friendly wrapper for a spark::Coin.
|
||||
@@ -647,3 +681,10 @@ final class DartSpendCoinData extends ffi.Struct {
|
||||
@ffi.Int()
|
||||
external int height;
|
||||
}
|
||||
|
||||
final class SparkFeeResult extends ffi.Struct {
|
||||
external ffi.Pointer<ffi.Char> error;
|
||||
|
||||
@ffi.Int()
|
||||
external int fee;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user