start method

Future<NetworkParams?> start(
  1. Keyring keyring, {
  2. List<NetworkParams>? nodes,
  3. KeyringEVM? keyringEVM,
  4. NetworkParams? nodeEVM,
})

This method will be called while App switched to a plugin. In this method, the plugin will:

  1. connect to nodes.
  2. retrieve network const & state.
  3. subscribe balances & set balancesStore.

Implementation

Future<NetworkParams?> start(Keyring keyring,
    {List<NetworkParams>? nodes,
    KeyringEVM? keyringEVM,
    NetworkParams? nodeEVM}) async {
  if (nodeEVM == null) {
    final res = await sdk.api.connectNode(keyring, nodes ?? nodeList);
    if (res == null) return null;

    keyring.setSS58(res.ss58);
    await updateNetworkState();

    if (keyring.current.address != null) {
      sdk.api.account.subscribeBalance(keyring.current.address,
          (BalanceData data) {
        _updateBalances(keyring.current, data);
      });
    }

    onStarted(keyring);

    return res;
  }

  final evmRes = await sdk.api.connectEVM(nodeEVM);
  if (evmRes == null) return null;

  if (keyringEVM?.current.address != null) {
    final data = await sdk.api.eth.account
        .getNativeTokenBalance(keyringEVM?.current.address ?? '');

    _updateBalances(
        keyringEVM!.current.toKeyPairData(),
        BalanceData()
          ..accountId = keyringEVM.current.address
          ..freeBalance = data
          ..availableBalance = data
          ..lockedBalance = '0'
          ..reservedBalance = '0');
  }

  onStartedEVM(keyringEVM!);

  return evmRes;
}