Skip to content

Commit

Permalink
feat: Added web3LoadTransactions in mirai_web3 to load user trans…
Browse files Browse the repository at this point in the history
…actions on a specific token
  • Loading branch information
i-asimkhan committed Feb 21, 2024
1 parent 192ced1 commit 33c469d
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/mirai_web3/lib/action_parsers/action_parsers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export 'package:mirai_web3/action_parsers/mirai_web3_load_tokens/web3_load_token
export 'package:mirai_web3/action_parsers/mirai_web3_validate_address/mirai_web3_validate_address_parser.dart';
export 'package:mirai_web3/action_parsers/mirai_web3_transfer_token/web3_transfer_token_parser.dart';
export 'package:mirai_web3/action_parsers/web3_sign_message/web3_sign_message_parser.dart';
export 'package:mirai_web3/action_parsers/mirai_web3_load_transactions/mirai_web3_load_transactions_parser.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:mirai_framework/mirai_framework.dart';
import 'package:mirai_web3/services/web_modal_service.dart';

class MiraiWeb3LoadTransactionsParser
extends MiraiActionParser<Map<String, dynamic>> {
const MiraiWeb3LoadTransactionsParser();

@override
String get actionType => 'web3LoadTransactions';

@override
getModel(Map<String, dynamic> json) => json;

@override
FutureOr<dynamic> onCall(
BuildContext context, Map<String, dynamic> model) async {
return await Web3ModalService.loadTransactions(
tokenAddress: model['token_address']);
}
}
19 changes: 19 additions & 0 deletions packages/mirai_web3/lib/models/transaction_details.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:mirai_web3/models/token.dart';

class TransactionDetails {
TransactionDetails({
required this.senderAddress,
required this.receiverAddress,
required this.amount,
required this.received,
required this.tranHash,
required this.tranToken,
});

final String senderAddress;
final String receiverAddress;
final BigInt amount;
final bool received;
final String tranHash;
final Token tranToken;
}
75 changes: 74 additions & 1 deletion packages/mirai_web3/lib/services/web_modal_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:mirai_web3/models/chain_details.dart';

import 'package:http/http.dart';
import 'package:mirai_web3/models/chain_meta_data.dart';
import 'package:mirai_web3/models/contract_details.dart';
import 'package:mirai_web3/models/token.dart';
import 'package:mirai_web3/models/transaction_details.dart';
import 'package:web3modal_flutter/pages/select_network_page.dart';
import 'package:web3modal_flutter/web3modal_flutter.dart';
import 'package:web3modal_flutter/widgets/widget_stack/widget_stack_singleton.dart';
Expand Down Expand Up @@ -263,6 +264,78 @@ class Web3ModalService {
return null;
}

static Future<List<TransactionDetails>> loadTransactions({
required String tokenAddress,
int offset = 0,
}) async {
List<TransactionDetails> transactions = [];
try {
final contract =
_contracts.firstWhere((contract) => contract.address == tokenAddress);
final contractToken = _contractTokens
.firstWhere((contract) => contract.address == tokenAddress);

// Create DeployedContract object using contract's ABI and address
final deployedContract = DeployedContract(
ContractAbi.fromJson(
jsonEncode(contract.abi),
contract.name,
),
EthereumAddress.fromHex(
contract.address,
),
);

final client = Web3Client(contract.rpcUrl, Client());

final currentBlockNumber = await client.getBlockNumber();
const int difference = 50000;
int blockNumber = currentBlockNumber - (difference * offset);

final transferEvent = deployedContract.event('Transfer');
final contractHex = bytesToHex(transferEvent.signature,
padToEvenLength: true, include0x: true);

await client
.getLogs(
FilterOptions(
// events of a user wallet
fromBlock: BlockNum.exact(blockNumber - difference),
toBlock: BlockNum.exact(blockNumber),
address: EthereumAddress.fromHex(contract.address),
topics: [
[
contractHex,
]
]),
)
.then((logs) {
for (FilterEvent l in logs) {
final result = transferEvent.decodeResults(l.topics!, l.data!);

if (result
.contains(EthereumAddress.fromHex(connectedWalletAddress))) {
transactions.add(
TransactionDetails(
senderAddress: (result[0] as EthereumAddress).hex,
receiverAddress: (result[1] as EthereumAddress).hex,
amount: result[2] as BigInt,
received: (result[1] as EthereumAddress).hex ==
connectedWalletAddress,
tranHash: l.transactionHash ?? '',
tranToken: contractToken,
),
);
}
}
});
} catch (e) {
debugPrint(e.toString());
}

return transactions;
}

static Future<void> disconnect() async {
_service.closeModal();
await _service.disconnect();
Expand Down
1 change: 1 addition & 0 deletions packages/mirai_web3/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
flutter:
sdk: flutter
freezed_annotation: ^2.2.0
http: ^1.2.0
json_annotation: ^4.8.1
mirai_framework: ^0.0.1
# intl: ^0.19.0
Expand Down

0 comments on commit 33c469d

Please sign in to comment.