-
How to use How to subscribe new blocks, or subscribe to account balance for instance?
Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
You can refer to a sample here: #385 (comment)
Anyway, those should also be available through the lib as in the example above: |
Beta Was this translation helpful? Give feedback.
-
Thank you, his example works :) So this is my test code working: Future<void> startBlockListening() async {
final storageBlockKeyQuery = [duniterApi.query.system.numberKey()];
await duniterApi.rpc.state.subscribeStorage(
storageBlockKeyQuery,
(storageChangeSet) async {
final blockNumber = await duniterApi.query.system.number();
log.d(blockNumber);
},
);
}
final Map<String, StreamSubscription<StorageChangeSet>>
_addressSubscriptionMap = {};
Future<void> startBalanceListening(String address) async {
if (_addressSubscriptionMap[address] != null) return;
final accountPubKey = Address.decode(address).pubkey;
final storageAccountKeyQuery = [
duniterApi.query.system.accountKey(accountPubKey)
];
_addressSubscriptionMap[address] =
await duniterApi.rpc.state.subscribeStorage(
storageAccountKeyQuery,
(storageChangeSet) async {
final accountInfo =
await duniterApi.query.system.account(accountPubKey);
log.d(accountInfo.data.free);
},
);
}
Future<void> stopBalancesListening(String address) async {
if (_addressSubscriptionMap[address] == null) return;
final subscription = _addressSubscriptionMap[address];
if (subscription == null) return;
_addressSubscriptionMap.remove(address);
await subscription.cancel();
} But the thing is, here we resend a request for each changes: Isn't there a way to avoid this by retrieving the correct data directly from |
Beta Was this translation helpful? Give feedback.
-
The best way to subscribe to |
Beta Was this translation helpful? Give feedback.
Thank you, his example works :)
So this is my test code working: