Giter Site home page Giter Site logo

Comments (6)

leonardocustodio avatar leonardocustodio commented on September 6, 2024 1

system.account is the storage key, it will subscribe to all balance changes of every account. If you want a specific account would be system.account(the account). I'm not in the computer right now so can't write a sample for you but I can do it in a few hours.

from polkadart.

leonardocustodio avatar leonardocustodio commented on September 6, 2024

Hello, I believe you can subscribe to the storage key system.accounts, let me know if it works out.

from polkadart.

raqeta avatar raqeta commented on September 6, 2024

I've tried to do it like in code below.
Should I add System and Account? Is it right launch point to subscribe?
And also I didn't get any updates and I suppose I have some mistakes with address encoding.

Future startBalanceListening(String address, Function(BalanceData) onUpdate) async {
// address for example == '5GTCTWCBQ797s8cf9KFBgAGQgpwdAHnoy37aTT1PgC37PEEN'
final account = Address.decode(address);
_subscription = await _myDot.rpc.state.subscribeStorage(
[account.pubkey], // should I add ['System', 'Account'] here and how ?
(set) async {
// do something after changing
},
);
}

from polkadart.

raqeta avatar raqeta commented on September 6, 2024

thanks, I'll try :)

from polkadart.

raqeta avatar raqeta commented on September 6, 2024

Thanks to your help, chatGpt and official Polkadot.js documentation I find out everything work correct with this code :)

  final MyChainDot _myChainDot;
  final Map<String, StreamSubscription<StorageChangeSet>> _addressSubscriptionMap = {};

  @override
  Future<void> startBalanceListening(String? address, Function(BalanceData) onUpdate) async {
    if (address == null || address.isEmpty || _addressSubscriptionMap[address] != null) return;
    final accountPubKey = Address.decode(address).pubkey;
    final storageAccountKeyQuery = [_myChainDot.query.system.accountKey(accountPubKey)];
    _addressSubscriptionMap[address] = await _myChainDot.rpc.state.subscribeStorage(
      storageAccountKeyQuery,
      (storageChangeSet) async {
        final accountInfo = await _myChainDot.query.system.account(accountPubKey);
        onUpdate.call(
          BalanceData(
            accountId: address,
            accountNonce: accountInfo.nonce,
            availableBalance: (accountInfo.data.free + accountInfo.data.reserved + accountInfo.data.frozen),
          ),
        );
      },
    );
  }

  @override
  Future<void> stopBalancesListening(String? address) async {
    if (address == null || address.isEmpty || _addressSubscriptionMap[address] == null) return;
    final subscription = _addressSubscriptionMap[address];
    if (subscription == null) return;
    _addressSubscriptionMap.remove(address);
    await subscription.cancel();
  }

from polkadart.

leonardocustodio avatar leonardocustodio commented on September 6, 2024

Great, glad it helped

from polkadart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.