Trident Java SDK

repository·main·Indexed 42 days ago

https://github.com/tronprotocol/trident

A lightweight Java SDK for interacting with the TRON blockchain. It provides support for gRPC interfaces, smart contracts, wallet management, and TRON token standards including TRC10, TRC20, and TRC721. Features the ApiWrapper client for network interactions and transaction management.

Tokens
1.3K
Snippets
4
Records
4
Agent score
3%

What's inside Trident

  1. Add Trident to your build

    main

    Trident can be added to your project using Gradle or Maven. Since version 0.11.0, it supports JDK 1.8 or JDK 17 on Gradle 8.5. The latest version is available on Maven Central.

    ### Gradle
    
    ```groovy
    implementation("io.github.tronprotocol:trident:1.0.0")

    Maven

    <dependency>
      <groupId>io.github.tronprotocol</groupId>
      <artifactId>trident</artifactId>
      <version>1.0.0</version>
    </dependency>
  2. Use a local Trident build

    main

    To use a locally compiled JAR file, copy the JAR to your project's libs directory and add the following dependencies to your build.gradle. Note that you must include specific versions for Guava, gRPC, Protobuf, BouncyCastle, Vert.x, and Fastjson2, as well as the Netty BOM for managing Netty modules.

    dependencies {
        implementation files('libs/trident-1.0.0.jar')
        implementation "com.google.guava:guava:33.0.0-jre"
        implementation "io.grpc:grpc-netty-shaded:1.81.0"
        implementation "io.grpc:grpc-netty:1.81.0"
        implementation "io.grpc:grpc-okhttp:1.81.0"
        implementation "io.grpc:grpc-protobuf:1.81.0"
        implementation "io.grpc:grpc-stub:1.81.0"
        implementation "com.google.protobuf:protobuf-java-util:3.25.8"
        implementation "org.bouncycastle:bcprov-jdk18on:1.84"
        implementation "io.vertx:vertx-core:4.5.27"
        implementation platform("io.netty:netty-bom:4.1.135.Final")
        implementation "io.netty:netty-buffer"
        implementation "com.alibaba.fastjson2:fastjson2:2.0.55"
    }
  3. Transfer TRX using ApiWrapper

    main

    To send TRX, use the transfer method to create a TransactionExtention, sign it using signTransaction, and finally broadcast it using broadcastTransaction to receive a transaction ID.

    // Send TRX
    TransactionExtention transactionExtention = client.transfer("fromAddress", "toAddress", 100_000_000L); //100TRX
    // Sign
    Transaction signedTxn = client.signTransaction(transactionExtention);
    // Broadcast
    String txId = client.broadcastTransaction(signedTxn);
    System.out.println("txId is " + txId);
  4. Initialize the ApiWrapper client

    main

    The ApiWrapper is the primary entry point for interacting with the TRON blockchain. You can initialize it using pre-configured network methods (Mainnet, Shasta, Nile) or via a ApiWrapperBuilder for custom gRPC endpoints and advanced configurations like TLS, timeouts, and interceptors.

    // Initialize with TronGrid mainnet 
    ApiWrapper client = ApiWrapper.ofMainnet("private_key", "api_key"); //api_key from TronGrid
    
    //Or Shasta test net 
    ApiWrapper client = ApiWrapper.ofShasta("private key");
    
    // Or nile testnet
    ApiWrapper client = ApiWrapper.ofNile("private_key");
    
    // Or initialize with a custom grpc endpoint.
    ApiWrapper client = new ApiWrapperBuilder("grpc endpoint")
        .withGrpcEndpointSolidity("solidity grpc endpoint")
        .withPrivateKey("private_key")
        .withApiKey("api_key")             // Optional: API key from TronGrid
        .withTLS()                         // Optional: or withTLS(new File("xxx.crt"))
        .withTimeout(5000)                 // Optional: request timeout in milliseconds
        .addInterceptors(interceptors)     // Optional: custom gRPC interceptors
        .build();