[dart] 실습 #1 - 스팀 글로벌 설정정보 가져오기

wonsama(71)
Published in
#zzan
Words
109
Reading
1 min
Listen
Play
7y

190830_스팀잇표지.001.jpeg

실습목표

RPC 20 호출을 테스트 해보도록 한다. 아래 내용을 잘 이해 했다면, 다양한 곳에 적용하여 다양한 기능을 확인 할 수 있다.

  • RPC20 호출
  • get_dynamic_global_properties 호출
  • 결과 확인

소스코드

/*
  [code runner]
  ctrl + alt + n 을 누르면 vscode 에서 손쉽게 실행 가능 

  https://pub.dev/
*/
import 'dart:convert';
import 'package:http/http.dart' as http;

/// 기본 스팀 API 호출 주소
const String _DEFAULT_API_URL = 'https://api.steemit.com';

/// rpc20 호출 결과 [http.Response] 정보에서 정상(200)인 경우 결과값을 넘겨준다 
Map _getResult(http.Response v){
  if(v.statusCode==200){
    Map json = jsonDecode(v.body);
    return json['result'];
  }
  return null;
}

/// 전역 설정 정보를 반환
Future<Map> getDynamicGlobalProperties(){
  return _rpc20(method: 'condenser_api.get_dynamic_global_properties')
  .then((v){
    return _getResult(v);
  })
  .catchError((onError){
      // 3초 후 다시 호출한다
      print(onError.toString());
      return new Future.delayed(const Duration(seconds: 3), () => getDynamicGlobalProperties);
  });
}

/// 포스트로 RPC20을 전송
Future<http.Response> _rpc20({int id=1, String url=_DEFAULT_API_URL,List<Map> params,String method}) async {
    
    Map data = {
      'jsonrpc':'2.0',
      'method':method,
      'id':id,
      'params':params??[],  // params 가 null 인 경우 [] 를 할당
    };

    var body = json.encode(data);
    var response = await http.post(url,
        headers: {"Content-Type": "application/json"},
        body: body
    );
    return response;
  }

  // 테스트용 진입점
  main(){
    getDynamicGlobalProperties().then((onValue)=>print(onValue));
  }

결과

응답 값에서 result 부분만 return 해서 넘겨준다. 기본적으로 key/value 형태의 map 으로 되어 있기때문에 필요한 값에 접근하여 사용하면 됨

[참조] 구조화된 class 로 만들어서 binding 이후 속성값형태로 사용해도 좋으나 오히려 더 일이 커질 수 있으므로 (다양한 class 를 미리 다 만들어야 되고, 속성 값이 변경되면 다시 만들어야 되는 불편함) 비추천.

{head_block_number: 38199287, head_block_id: 0246dff72ab4f590dd0578972d1859283a8a25a5, time: 2019-11-15T15:05:36, current_witness: yabapmatt, total_pow: 514415, num_pow_witnesses: 172, virtual_supply: 368027042.377 STEEM, current_supply: 331224336.992 STEEM, confidential_supply: 0.000 STEEM, init_sbd_supply: 0.000 SBD, current_sbd_supply: 7400659.001 SBD, confidential_sbd_supply: 0.000 SBD, total_vesting_fund_steem: 207782220.551 STEEM, total_vesting_shares: 409912148718.577481 VESTS, total_reward_fund_steem: 0.000 STEEM, total_reward_shares2: 0, pending_rewarded_vesting_shares: 851494789.833154 VESTS, pending_rewarded_vesting_steem: 425060.481 STEEM, sbd_interest_rate: 0, sbd_print_rate: 0, maximum_block_size: 65536, required_actions_partition_percent: 0, current_aslot: 38331712, recent_slots_filled: 340282366920938463463374607431768211455, participation_count: 128, last_irreversible_block_num: 38199268, vote_power_reserve_rate: 10, delegation_return_period: 432000, reverse_auction_seconds: 300, available_account_subsidies: 24778580, sbd_stop_percent: 1000, sbd_start_percent: 900, next_maintenance_time: 2019-11-15T15:26:57, last_budget_time: 2019-11-15T14:26:57, content_reward_percent: 6500, vesting_reward_percent: 1500, sps_fund_percent: 1000, sps_interval_ledger: 44.718 SBD, downvote_pool_percent: 2500}

맺음말

  • 어느정도 강의?소개 자료가 쌓이면 유트부 강의도 해볼 생각중 ...(과연 될지 ;;)
  • 의외로 소스코드 작성하는 것보다 대문 이미지 만든다고 할애한 시간이 더 많았음 ;;
  • 궁금한 점이 있다면 댓글 문의 바랍니다.
[dart] 실습 #1 - 스팀 글로벌 설정정보 가져오기 | Ecency