[AVLE Refactoring] Fetching Data from Multiple RPC servers

in hive-192037 •  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)

As you might know that steemit.com was dead a couple of days ago. And I noticed that AVLE was dead too. I thought I set up multiple RPC (remote procedure call) servers to handle this kind of situation. But the AVLE dapp is dependent solely on 'api.steemit.com'

So I tried to refactor that part of code. By the way, AVLE dapp uses dsteem which is a ypescript 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.


cc.
@pennsif

Please vote for me as a witness.

I am ranked at 30 now. I go up slowly day by day. Thank you guys who vote for me.

https://steemitwallet.com/~witnesses
(find etainclub ranked at 30)
or here:
https://steemyy.com/witness-voting/?witness=etainclub&action=approve

@steemcurator01, Thank you for Your Support

@misterdelegation Could you delegate steem power to help me manage dapps (PLAY STEEM, AVLE, and EtainClub)?

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 STEEM!
Sort Order:  

Upvoted! Thank you for supporting witness @jswit.
default.jpg

오늘도 응원합니다 이타인님^^