프로그래밍/블럭체인

이더리움 트랜잭션 로그 가져오기

jonelove71 2020. 3. 5. 17:04

- 이더리움 API를 이용해 이더리움 트랜잭션 로그를 파싱 후 List로 변환 함수

 

public List getTranLogs(String addr) throws Exception
{
     List lst = new ArrayList();
     String url = "http://api.etherscan.io/api?module=account&action=txlist&"
           + "apikey=이더스캔API페이지에서 받은 API KEY 값&sort=desc";
     url = url + "&address=" + addr;

     Communicator comm = new Communicator();
     String strJson = comm.getHttpJsonFromURL(url);
     JSONObject jsonObj = new JSONObject(strJson);
     String status = jsonObj.getString("status");
     if (status.equals("1"))
     {
         JSONArray jsonArr = jsonObj.getJSONArray("result");
         for (int i=0; i<jsonArr.length(); i++)
         {
              JSONObject json = jsonArr.getJSONObject(i);
              String from = json.getString("from");
              String to = json.getString("to");
              String value = json.getString("value");
              String category = "보냄";
              String targetaddr = "";
              if (from.toUpperCase().contains(addr.toUpperCase())) {
                   category = "보냄";
                   targetaddr = to;
               }else if (to.toUpperCase().contains(addr.toUpperCase())) {
                  category = "받음";
                 targetaddr = from;
              }
              String timestamp = json.getString("timeStamp");
              HashMap map = new HashMap();
              double amount = Convert.fromWei(value, (Convert.Unit)Convert.Unit.ETHER).doubleValue();
              String temp = new DecimalFormat("##,###,###,###,###,##0.0000000000000000").format(amount);
              map.put("amount", temp);
              map.put("category", category);
              map.put("address", targetaddr);

              Date date = new Date(Long.parseLong(timestamp)*1000);    
              SimpleDateFormat ConTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              String createTime = ConTimeFormat.format(date);
              map.put("time", createTime);
              lst.add(map);
         }
    }
    return lst;
}

 

사용법 : getTranLogs("트랜잭션 조회할 이더리움 주소") 함수 호출