TRC20 코드 액세스


 
 
 
 
 
 
잔액 조회
    @Test
    public void balanceOfTrc20() throws Throwable {
        String queryAddress = "TXoDY8b3upAAkPxaK5B845zd8x44bFh1nX";

        String url = tronUrl + "/wallet/triggerconstantcontract";
        JSONObject param = new JSONObject();
        param.put("owner_address", TronUtils.toHexAddress(queryAddress));
        param.put("contract_address", TronUtils.toHexAddress(contract));
        param.put("function_selector", "balanceOf(address)");
        List inputParameters = new ArrayList<>();
        inputParameters.add(new Address(TronUtils.toHexAddress(queryAddress).substring(2)));
        param.put("parameter", FunctionEncoder.encodeConstructor(inputParameters));
        String result = HttpClientUtils.postJson(url, param.toJSONString());
        System.out.println(result);
        BigDecimal amount = BigDecimal.ZERO;
        if (StringUtils.isNotEmpty(result)) {
            JSONObject obj = JSONObject.parseObject(result);
            JSONArray results = obj.getJSONArray("constant_result");
            if (results != null && results.size() > 0) {
                BigInteger _amount = new BigInteger(results.getString(0), 16);
                amount = new BigDecimal(_amount).divide(decimal, 6, RoundingMode.FLOOR);
            }
        }
        System.out.println(String.format("  %s balance=%s", queryAddress, amount.toString()));
    }

 
대체
 
    @Test
    public void sendTrc20() throws Throwable {
        String privateKey = "6d7bad55912c455795c399b70e5ec15e3a355c6b87c3358936";
        String toAddress = "TNt2sv1K93HaGdMsdsYpJCorjw1yciE3wZ";
        BigDecimal amount = new BigDecimal("400");

        String ownerAddress = TronUtils.getAddressByPrivateKey(privateKey);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("contract_address", TronUtils.toHexAddress(contract));
        jsonObject.put("function_selector", "transfer(address,uint256)");
        List inputParameters = new ArrayList<>();
        inputParameters.add(new Address(TronUtils.toHexAddress(toAddress).substring(2)));
        inputParameters.add(new Uint256(amount.multiply(decimal).toBigInteger()));
        String parameter = FunctionEncoder.encodeConstructor(inputParameters);
        jsonObject.put("parameter", parameter);
        jsonObject.put("owner_address", TronUtils.toHexAddress(ownerAddress));
        jsonObject.put("call_value", 0);
        jsonObject.put("fee_limit", 6000000L);
        //      ,   TransactionExtention,        .
        String trans1 = HttpClientUtils.postJson(tronUrl + "/wallet/triggersmartcontract", jsonObject.toString());
        JSONObject result = JSONObject.parseObject(trans1);
        System.out.println(result);
        if (result.containsKey("Error")) {
            System.out.println("send error==========");
            return;
        }
        JSONObject tx = result.getJSONObject("transaction");
        tx.getJSONObject("raw_data").put("data", Hex.toHexString("  laoxiang".getBytes()));//    
        String txid = TronUtils.signAndBroadcast(tronUrl, privateKey, tx);
        if (txid != null) {
            System.out.println("  Id:" + txid);
        }
    }

브로드캐스트
    public static String signAndBroadcast(String tronUrl, String privateKey, JSONObject transaction) throws Throwable {
        System.out.println("  :" + tronUrl + "   privatekey:" + privateKey + "    transation: " + transaction);
        if (tronUrl.endsWith("/")) {
            tronUrl = tronUrl.substring(0, tronUrl.length() - 1);
        }
        Protocol.Transaction tx = packTransaction(transaction.toJSONString());
        byte[] bytes = signTransactionByte(tx.toByteArray(), ByteArray.fromHexString(privateKey));
        //      Transaction   protobuf      , HEX   
        String signTransation = Hex.toHexString(bytes);
        System.out.println("    signTransation:" + signTransation);
        JSONObject jsonObjectGB = new JSONObject();
        jsonObjectGB.put("transaction", signTransation);
        //     protobuf          Transaction.
        String url = tronUrl + "/wallet/broadcasthex";
        String transationCompelet1 = HttpClientUtils.postJson(url, jsonObjectGB.toString());
        System.out.println("    :" + transationCompelet1);
        JSONObject transationCompelet = JSONObject.parseObject(transationCompelet1);
        if (transationCompelet.getBoolean("result")) {
            return transationCompelet.getString("txid");
        } else {
            log.error(String.format("      :%s", transationCompelet1));
            return null;
        }
//        return null;
    }

 
다른 건 시간 나면 보내주세요.
도움이 필요한 연락처: laoaxiang829

좋은 웹페이지 즐겨찾기