Skip to content

Commit

Permalink
feat: Added stored tokens and refresh feature in web3 service in `mir…
Browse files Browse the repository at this point in the history
…ai_web3`
  • Loading branch information
i-asimkhan committed Feb 21, 2024
1 parent 2e11b9e commit c178a59
Showing 1 changed file with 72 additions and 68 deletions.
140 changes: 72 additions & 68 deletions packages/mirai_web3/lib/services/web_modal_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Web3ModalService {
static late W3MService _service;
static late ChainMetadata _chainMetadata;
static late List<ContractDetails> _contracts;
static final List<Token> _contractTokens = [];
static List<Token> _contractTokens = [];
static List<TransactionDetails> _transactions = [];
static String? _signature;

static W3MService get service => _service;
Expand Down Expand Up @@ -149,70 +150,70 @@ class Web3ModalService {
}
}

static Future<List<Token>> loadTokens() async {
if (_contractTokens.isNotEmpty) {
return _contractTokens;
}

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

if (contract.chainId == chainId) {
final results = await Future.wait([
// results[0]
_service.requestReadContract(
deployedContract: deployedContract,
functionName: 'name',
rpcUrl: contract.rpcUrl,
),
// results[1]
_service.requestReadContract(
deployedContract: deployedContract,
functionName: 'totalSupply',
rpcUrl: contract.rpcUrl,
),
// results[2]
_service.requestReadContract(
deployedContract: deployedContract,
functionName: 'balanceOf',
rpcUrl: contract.rpcUrl,
parameters: [
EthereumAddress.fromHex(connectedWalletAddress),
],
static Future<List<Token>> loadTokens({bool refresh = false}) async {
if (_contractTokens.isEmpty || refresh) {
_contractTokens = [];

try {
for (ContractDetails contract in _contracts) {
// Create DeployedContract object using contract's ABI and address
final deployedContract = DeployedContract(
ContractAbi.fromJson(
jsonEncode(contract.abi),
contract.name,
),
// results[3]
_service.requestReadContract(
deployedContract: deployedContract,
functionName: 'decimals',
rpcUrl: contract.rpcUrl,
parameters: [],
),
]);

final name = results[0].toString();
final total = results[1];
final balance = results[2];
final decimals = results[3];

_contractTokens.add(Token(
name: name,
address: contract.address,
supply: total,
balance: balance,
decimals: decimals,
));
EthereumAddress.fromHex(contract.address),
);

if (contract.chainId == chainId) {
final results = await Future.wait([
// results[0]
_service.requestReadContract(
deployedContract: deployedContract,
functionName: 'name',
rpcUrl: contract.rpcUrl,
),
// results[1]
_service.requestReadContract(
deployedContract: deployedContract,
functionName: 'totalSupply',
rpcUrl: contract.rpcUrl,
),
// results[2]
_service.requestReadContract(
deployedContract: deployedContract,
functionName: 'balanceOf',
rpcUrl: contract.rpcUrl,
parameters: [
EthereumAddress.fromHex(connectedWalletAddress),
],
),
// results[3]
_service.requestReadContract(
deployedContract: deployedContract,
functionName: 'decimals',
rpcUrl: contract.rpcUrl,
parameters: [],
),
]);

final name = results[0].toString();
final total = results[1];
final balance = results[2];
final decimals = results[3];

_contractTokens.add(Token(
name: name,
address: contract.address,
supply: total,
balance: balance,
decimals: decimals,
));
}
}
} catch (e) {
debugPrint(e.toString());
}
} catch (e) {
debugPrint(e.toString());
}

return _contractTokens;
Expand Down Expand Up @@ -342,17 +343,20 @@ class Web3ModalService {

static Future<List<TransactionDetails>> loadAllTransactions({
int offset = 0,
bool refresh = false,
}) async {
List<TransactionDetails> transactions = [];
if (_transactions.isEmpty || refresh) {
_transactions = [];

if (_contractTokens.isNotEmpty) {
for (Token token in _contractTokens) {
await loadTransactions(tokenAddress: token.address)
.then((trans) => transactions.addAll(trans));
if (_contractTokens.isNotEmpty) {
for (Token token in _contractTokens) {
await loadTransactions(tokenAddress: token.address)
.then((trans) => _transactions.addAll(trans));
}
}
}

return transactions;
return _transactions;
}

static Future<void> disconnect() async {
Expand Down

0 comments on commit c178a59

Please sign in to comment.