java springboot 调用波场TRC20

3,494 阅读1分钟

开发使用波场测试网:shasta.tronscan.io

先准备一个波场钱包,到测试网 领取测试TRX. 用Twitter帐户关注@TronTest2 ,发推特@TronTest2,钱包地址后,会给你测试TRX。 image.png

1. 测试网发行TRC20代币

2. Springboot 调用TRC20

不想复制代码了,需要的去github看吧,直接看单元测试吧,就2个类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TronServerApplication.class})
@Slf4j
public class MainTest {
  @Autowired private TronApiService tronApiService;
  @Autowired private TronServiceConfig config;
  public static String toAddress = "TXoDw9PUuBWwp2GmiDvFwdXfja6KREpkyY";

  /** 获取trx,usdt余额 */
  @Test
  public void balanceOf() {
    BigDecimal trxBalanceOf =
        tronApiService.getAccountBalance(config.getHexPrivateKey(), config.getOwnerAddress());
    BigDecimal usdtBalanceOf =
        tronApiService.getUSDTBalanceDecimal(config.getHexPrivateKey(), config.getOwnerAddress());
    log.info("trx 余额:{}", trxBalanceOf);
    log.info("usdt余额:{}", usdtBalanceOf);
  }

  /** 转账TRX */
  @Test
  @SneakyThrows
  public void trunsferTRX() {
    String txid =
        tronApiService.trunsferTRX(
            config.getHexPrivateKey(), config.getOwnerAddress(), toAddress, 100000000L);
    log.info("交易ID:{}", txid);
    Thread.sleep(5000);
    String status = tronApiService.getTransactionStatusById(txid);
    log.info("交易状态:{}", status);
  }

  /** 转账USDT */
  @Test
  @SneakyThrows
  public void trunsferUSDT() {
    String txid =
        tronApiService.trunsferUSDT(
            config.getHexPrivateKey(),
            config.getOwnerAddress(),
            toAddress,
            config.getTrc20Decimals().multiply(new BigInteger("10")));
    log.info("交易ID:{}", txid);
    Thread.sleep(5000);
    String status = tronApiService.getTransactionStatusById(txid);
    log.info("交易状态:{}", status);
  }

  /** 查询TRC20交易记录 */
  @Test
  @SneakyThrows
  public void getTransactionsTRC20() {
    String json = tronApiService.getTransactionsTRC20(config.getOwnerAddress(), false, false);
    log.info("交易记录:{}", json);
  }

  /** 私钥转钱包地址 */
  @Test
  @SneakyThrows
  public void privateKeyToAddress() {
    byte[] rawAddr =
        KeyPair.publicKeyToAddress(
            SECP256K1.PublicKey.create(SECP256K1.PrivateKey.create(config.getHexPrivateKey())));
    String address = Base58Check.bytesToBase58(rawAddr);
    log.info("地址:{}", address);
  }
}

智能合约开发联系飞机 @xiaogui1020

Github 代码

代码中是我的TRON测试账号,我没修改,方便学习的同学测试(不要盗了我的)。

可以按上面的流程,直接发行一个TRC20代币,修改yum配置文件就可以了。