Skip to content

Commit

Permalink
feat: add transaction simulation to web wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoplavkov committed May 22, 2023
1 parent 2b395eb commit cb5824b
Show file tree
Hide file tree
Showing 10 changed files with 421 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/flutter_fuels/lib/js/wallet.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions packages/flutter_fuels/lib/model/call_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'transaction_receipt.dart';

class CallResult {
final List<TransactionReceipt> receipts;

CallResult({required this.receipts});
}
196 changes: 196 additions & 0 deletions packages/flutter_fuels/lib/model/transaction_receipt.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import 'dart:typed_data';

abstract class TransactionReceipt {}

class ReceiptCall extends TransactionReceipt {
final String from;
final String to;
final BigInt amount;
final String assetId;
final BigInt gas;
final BigInt param1;
final BigInt param2;
final BigInt pc;
final BigInt isField;

ReceiptCall({
required this.from,
required this.to,
required this.amount,
required this.assetId,
required this.gas,
required this.param1,
required this.param2,
required this.pc,
required this.isField,
});
}

class ReceiptReturn extends TransactionReceipt {
final String id;
final BigInt val;
final BigInt pc;
final BigInt isField;

ReceiptReturn({
required this.id,
required this.val,
required this.pc,
required this.isField,
});
}

class ReceiptReturnData extends TransactionReceipt {
final String id;
final BigInt ptr;
final BigInt len;
final String digest;
final BigInt pc;
final BigInt isField;

ReceiptReturnData({
required this.id,
required this.ptr,
required this.len,
required this.digest,
required this.pc,
required this.isField,
});
}

class ReceiptPanic extends TransactionReceipt {
final String id;
final BigInt reason;
final BigInt pc;
final BigInt isField;
final String contractId;

ReceiptPanic({
required this.id,
required this.reason,
required this.pc,
required this.isField,
required this.contractId,
});
}

class ReceiptRevert extends TransactionReceipt {
final String id;
final BigInt val;
final BigInt pc;
final BigInt isField;

ReceiptRevert({
required this.id,
required this.val,
required this.pc,
required this.isField,
});
}

class ReceiptLog extends TransactionReceipt {
final String id;
final BigInt val0;
final BigInt val1;
final BigInt val2;
final BigInt val3;
final BigInt pc;
final BigInt isField;

ReceiptLog({
required this.id,
required this.val0,
required this.val1,
required this.val2,
required this.val3,
required this.pc,
required this.isField,
});
}

class ReceiptLogData extends TransactionReceipt {
final String id;
final BigInt val0;
final BigInt val1;
final BigInt ptr;
final BigInt len;
final String digest;
final BigInt pc;
final BigInt isField;

ReceiptLogData({
required this.id,
required this.val0,
required this.val1,
required this.ptr,
required this.len,
required this.digest,
required this.pc,
required this.isField,
});
}

class ReceiptTransfer extends TransactionReceipt {
final String from;
final String to;
final BigInt amount;
final String assetId;
final BigInt pc;
final BigInt isField;

ReceiptTransfer(
{required this.from,
required this.to,
required this.amount,
required this.assetId,
required this.pc,
required this.isField});
}

class ReceiptTransferOut extends TransactionReceipt {
final String from;
final String to;
final BigInt amount;
final String assetId;
final BigInt pc;
final BigInt isField;

ReceiptTransferOut({
required this.from,
required this.to,
required this.amount,
required this.assetId,
required this.pc,
required this.isField,
});
}

class ReceiptScriptResult extends TransactionReceipt {
final BigInt result;
final BigInt gasUsed;

ReceiptScriptResult({
required this.result,
required this.gasUsed,
});
}

class ReceiptMessageOut extends TransactionReceipt {
final String messageID;
final String sender;
final String recipient;
final BigInt amount;
final String nonce;
final String digest;
final Uint8List data;

ReceiptMessageOut({
required this.messageID,
required this.sender,
required this.recipient,
required this.amount,
required this.nonce,
required this.digest,
required this.data,
});
}
18 changes: 15 additions & 3 deletions packages/flutter_fuels/lib/wallet/fuel_wallet.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flutter_fuels/model/call_result.dart';

import 'platform_impl/stub_wallet.dart'
if (dart.library.io) 'platform_impl/mobile_wallet.dart'
if (dart.library.html) 'platform_impl/web_wallet.dart';
Expand Down Expand Up @@ -74,7 +76,7 @@ class FuelWallet {
required int gasPrice,
required int gasLimit,
required int maturity,
}) async {
}) {
return _wallet.transfer(
networkUrl: networkUrl,
privateKey: privateKey,
Expand All @@ -89,7 +91,7 @@ class FuelWallet {

Future<String> signMessage({
required String message,
}) async {
}) {
return _wallet.signMessage(
networkUrl: networkUrl,
privateKey: privateKey,
Expand All @@ -99,14 +101,24 @@ class FuelWallet {

Future<String> sendTransaction({
required dynamic transactionRequest,
}) async {
}) {
return _wallet.sendTransaction(
networkUrl: networkUrl,
privateKey: privateKey,
transactionRequest: transactionRequest,
);
}

Future<CallResult> simulateTransaction({
required dynamic transactionRequest,
}) {
return _wallet.simulateTransaction(
networkUrl: networkUrl,
privateKey: privateKey,
transactionRequest: transactionRequest,
);
}

static String addHexPrefix(String address) {
if (address.startsWith('0x')) {
return address;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flutter_fuels/model/call_result.dart';

abstract class BaseWallet {
Future<Map> generateNewWallet();

Expand Down Expand Up @@ -31,4 +33,10 @@ abstract class BaseWallet {
required String privateKey,
required dynamic transactionRequest,
});

Future<CallResult> simulateTransaction({
required String networkUrl,
required String privateKey,
required dynamic transactionRequest,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ external dynamic newWalletFromMnemonic(String mnemonic);
external dynamic newWalletFromPrivateKey(String privateKey);

@JS('transfer')
external dynamic transfer(
external String transfer(
String privateKey,
String networkUrl,
String destinationB256Address,
Expand All @@ -25,14 +25,21 @@ external dynamic transfer(
);

@JS('signMessage')
external dynamic signMessage(
external String signMessage(
String privateKey,
String networkUrl,
String message,
);

@JS('sendTransaction')
external dynamic sendTransaction(
external String sendTransaction(
String privateKey,
String networkUrl,
String transactionRequestJson,
);

@JS('simulateTransaction')
external String simulateTransaction(
String privateKey,
String networkUrl,
String transactionRequestJson,
Expand Down
10 changes: 10 additions & 0 deletions packages/flutter_fuels/lib/wallet/platform_impl/mobile_wallet.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter_fuels/model/call_result.dart';
import 'package:fuels/fuels.dart';

import '../../ffi/mobile_wrapper.dart';
Expand Down Expand Up @@ -130,4 +131,13 @@ class FuelWalletImpl extends BaseWallet {
// TODO: implement
throw UnimplementedError();
}

@override
Future<CallResult> simulateTransaction(
{required String networkUrl,
required String privateKey,
required transactionRequest}) {
// TODO: implement
throw UnimplementedError();
}
}
10 changes: 10 additions & 0 deletions packages/flutter_fuels/lib/wallet/platform_impl/stub_wallet.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flutter_fuels/model/call_result.dart';

import 'base_wallet.dart';

class FuelWalletImpl extends BaseWallet {
Expand Down Expand Up @@ -51,4 +53,12 @@ class FuelWalletImpl extends BaseWallet {
}) {
throw Exception('Stub implementation');
}

@override
Future<CallResult> simulateTransaction(
{required String networkUrl,
required String privateKey,
required transactionRequest}) {
throw Exception('Stub implementation');
}
}
Loading

0 comments on commit cb5824b

Please sign in to comment.