[AVLE Refactoring] Fetching Data from Multiple RPC servers

in avle •  3 years ago 

I have been refactoring the AVLE dapp which is developed using Flutter (Dart language).

image.png
(AVLE Dapp is in beta and will be open after the refactorization)

I thought I set up multiple RPC (remote procedure call) servers to handle server-down situations, but i did not.

So I tried to refactor that part of code. By the way, AVLE dapp uses dsteem which is a typescript steem interface library, which is awesome.

First I setup a client of dsteem like this to use multiple RPC servers.

class SteemConfig {
  static const rpcServers = [
    'https://api.steemit.com',
    'https://api.steemitdev.com',
    'https://api.steemzzang.com',
    'https://api.steem.buzz',
  ];
...
}

// dsteem client
  Client client = Client(
    SteemConfig.rpcServers,
    ClientOptions(
        chainId:
            '0000000000000000000000000000000000000000000000000000000000000000',
        addressPrefix: 'STM',
        timeout: 5000),
  );

Now the client tries to connect to the next working server when the one is not working.

Fetch account and profile data in Dart using dsteem

The following is an example function to fetch user's account data and profile data.

// fetch user data
  Future<SteemAccount?> fetchAccount(String username) async {
    final params = [username];
    print('fetchAccount. params: $params');

    try {
      final accountFuture = promiseToFuture(
         client.call2('database_api', 'get_accounts', [params]));
      // fetch author profile
      final profileFuture =
          promiseToFuture(client.call2('bridge', 'get_profile', params));
      // resolve futures
      final resolvedFutures = await Future.wait([accountFuture, profileFuture]);
      // convert account jsobject to map
      final accountMap = dartify<Map<String, dynamic>>(resolvedFutures[0][0]);
      // convert profile jsobject to map
      final profileMap = dartify<Map<String, dynamic>>(resolvedFutures[1]);
      // build account
      final account = SteemAccount.fromMap(accountMap, profileMap);
      return account;
    } catch (error) {
      printError('failed to fetch account data $error');
      return null;
    }
  }

In the above code, the dsteem client is using client.call2(...) function to fetch account data.
(The name of call2 is originally call but there was an issue in Dart. More detail is here in Korean. https://steemit.com/hive-101145/@etainclub/play-steem-x-pwa-flutter-js)

Previously, I used the following code to fetch account data:

   final body = {
      'jsonrpc': '2.0',
      'method': 'call',
      'params': [
        'database_api',
        'get_accounts',
        [
          [username]
        ]
      ]
    };
};
 var accountResponse = await Dio().post('https://api.steemit.com', data: body);

As you see, if the api.steemit.com is dead, then no fetching is done.

I need to refactor other fetching functions like this. And the AVLE's code is written by Dart which needs to make an adapter to pass arguments to dsteem library one by one. That's a lot but it is easy to make them.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!
Sort Order:  
  ·  3 years ago  ·  

RPC problems have been happening very often this month. It seems that until now there is no concrete solution to overcome this. The same issue is happening again now, very hard to post even though it has been replaced with some available RPC nodes.

image.png


Posted from https://blurt.live

Congratulations, your post has been curated by @dsc-r2cornell. You can use the tag #R2cornell. Also, find us on Discord

Manually curated by Blessed-girl

logo3 Discord.png

Felicitaciones, su publicación ha sido votada por @ dsc-r2cornell. Puedes usar el tag #R2cornell. También, nos puedes encontrar en Discord


Posted from https://blurtlatam.com

  ·  3 years ago  ·  

고생이 많으십니다~!


Posted from https://blurtlatam.com

  ·  3 years ago  ·  

고맙습니다~