当前位置: 首页 > 工具软件 > Trident > 使用案例 >

trident-java进行TRC20转账

席成仁
2023-12-01
/**
     * USDT转账
     * @param fromAccount 转账的账户
     * @param fromPrivateKey 转账账户的私钥
     * @param toAccount 要转给的账户
     * @param contractAddress TRC20的合同地址
     * @param sunAmount 转账金额
     * @return
     */
    public String transferUSDT(String fromAccount,String fromPrivateKey,String toAccount,
                               String contractAddress,String sunAmount){
        String hashNumber = null;
        ApiWrapper wrapper = ApiWrapper.ofShasta(fromPrivateKey);
        try {
            Contract contract = wrapper.getContract(contractAddress); //trc20合同地址 usdt
            Trc20Contract token = new Trc20Contract(contract, fromAccount, wrapper); //trc20的合同token
            BigInteger usdtValue = token.balanceOf(fromAccount); //获取转账账户的USDT余额
            BigInteger sunAmountValue = Convert.toSun(sunAmount, Convert.Unit.TRX).toBigInteger(); //获取想要转账的数额
            if (usdtValue.compareTo(sunAmountValue) >= 0) { //进行比较
                long l = Convert.toSun("10", Convert.Unit.TRX).longValue(); //设置最大矿工费用
                hashNumber = token.transfer(toAccount, sunAmountValue.longValue(), 0, "备注", l); //转账
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  hashNumber;
    }
 类似资料: