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<Type> 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<Type> 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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是小酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值