Watching account
This example connects to aeWallet and watches for updates to the selected account.
tip
Ensure that the aeWallet application is running and unlocked before attempting connection.
- Javascript / Typescript
- Flutter
info
This snippet uses Javascript
language to keep things simple.
But @archethicjs/sdk
can be used in a TypeScript
or Javascript
project.
index.html
<html>
<head>
<script type="module" src="main.js"/>
</head>
</html>
main.js
import Archethic, { ConnectionState } from "https://esm.sh/@archethicjs/sdk";
const archethicClient = new Archethic()
await archethicClient.connect()
/// Listen to rpc wallet connection status changes
const accountSubscription = await archethicClient.rpcWallet.onCurrentAccountChange(async (account) => {
console.log(account)
})
setTimeout(
async () => {
await archethicClient.rpcWallet.close()
archethicClient.rpcWallet.unsubscribe(accountSubscription)
},
20000,
)
import 'dart:convert';
import 'dart:developer';
import 'package:archethic_wallet_client/archethic_wallet_client.dart';
Future<void> main() async {
final aewalletClient = await ArchethicDAppClient.auto(
origin: const RequestOrigin(
name: 'FlutterDappExample',
),
replyBaseUrl: 'flutterdappexample://dapp.example',
);
final subscription =
await aewalletClient.subscribeCurrentAccount().valueOrThrow;
subscription.updates.listen((account) {
log('Account updated : ${jsonEncode(account.toJson())} ...');
});
await aewalletClient.connect();
await Future.delayed(Duration(seconds: 10));
await aewalletClient.unsubscribeCurrentAccount(subscription.id);
await aewalletClient.close();
}