diff --git a/trident-java/README.md b/trident-java/README.md index 44c4d28..6f4c678 100644 --- a/trident-java/README.md +++ b/trident-java/README.md @@ -29,9 +29,9 @@ dependencies { // protobuf & grpc implementation 'com.google.protobuf:protobuf-java:3.11.0' - implementation fileTree(dir:'../core') - implementation fileTree(dir:'../utils') - implementation fileTree(dir:'../abi') + implementation fileTree(dir: '../core') + implementation fileTree(dir: '../utils') + implementation fileTree(dir: '../abi') implementation 'com.google.guava:guava:28.0-jre' } @@ -41,32 +41,35 @@ Or if you are using the jar files as your dependencies: ```groovy dependencies { - implementation fileTree(dir:'your path', include: '*.jar') + implementation fileTree(dir: 'your path', include: '*.jar') } ``` ### Maven Settings ```xml - - org.tron.trident - abi - 0.8.0 - system - your path - - - org.tron.trident - utils - 0.8.0 - system - your path - - - org.tron.trident - core - 0.8.0 - system - your path - + + + + org.tron.trident + abi + 0.8.0 + system + your path + + + org.tron.trident + utils + 0.8.0 + system + your path + + + org.tron.trident + core + 0.8.0 + system + your path + + ``` diff --git a/trident-java/build.gradle b/trident-java/build.gradle index d3d6f19..81d5d48 100644 --- a/trident-java/build.gradle +++ b/trident-java/build.gradle @@ -12,6 +12,7 @@ allprojects { group = 'org.tron.trident' repositories { + gradlePluginPortal() mavenCentral() } } diff --git a/trident-java/core/src/main/java/org/tron/trident/core/ApiWrapper.java b/trident-java/core/src/main/java/org/tron/trident/core/ApiWrapper.java index a1d8ba6..5a517d0 100644 --- a/trident-java/core/src/main/java/org/tron/trident/core/ApiWrapper.java +++ b/trident-java/core/src/main/java/org/tron/trident/core/ApiWrapper.java @@ -4,7 +4,11 @@ import com.google.protobuf.Message; import io.grpc.ClientInterceptor; import org.tron.trident.abi.FunctionEncoder; +import org.tron.trident.abi.TypeReference; +import org.tron.trident.abi.datatypes.Address; +import org.tron.trident.abi.datatypes.Bool; import org.tron.trident.abi.datatypes.Function; +import org.tron.trident.abi.datatypes.generated.Uint256; import org.tron.trident.api.GrpcAPI; import org.tron.trident.api.GrpcAPI.BytesMessage; @@ -435,6 +439,35 @@ public TransactionExtention transfer(String fromAddress, String toAddress, long return txnExt; } + /** + * Transfer TRX. amount in SUN + * + * @param fromAddress owner address + * @param toAddress receive balance + * @param contractAddress contract address + * @param amount transfer amount + * @return TransactionExtention + * @throws IllegalException if fail to transfer + */ + public TransactionExtention transfer(String fromAddress, String toAddress, String contractAddress, long amount) throws IllegalException { + + Function trc20Transfer = new Function("transfer", + Arrays.asList(new Address(toAddress), new Uint256(amount)), + Arrays.asList(new TypeReference() { + })); + + String encodedHex = FunctionEncoder.encode(trc20Transfer); + + TriggerSmartContract trigger = + TriggerSmartContract.newBuilder() + .setOwnerAddress(ApiWrapper.parseAddress(fromAddress)) + .setContractAddress(ApiWrapper.parseAddress(contractAddress)) + .setData(ApiWrapper.parseHex(encodedHex)) + .build(); + + return blockingStub.triggerContract(trigger); + } + /** * Transfers TRC10 Asset * @param fromAddress owner address diff --git a/trident-java/core/src/test/java/org/tron/trident/core/inceptors/TronWalletTest.java b/trident-java/core/src/test/java/org/tron/trident/core/inceptors/TronWalletTest.java new file mode 100644 index 0000000..5b7ae2c --- /dev/null +++ b/trident-java/core/src/test/java/org/tron/trident/core/inceptors/TronWalletTest.java @@ -0,0 +1,128 @@ +package org.tron.trident.core.inceptors; + +import org.bouncycastle.util.encoders.Hex; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.tron.trident.core.ApiWrapper; +import org.tron.trident.core.contract.Contract; +import org.tron.trident.core.contract.Trc20Contract; +import org.tron.trident.core.exceptions.IllegalException; +import org.tron.trident.core.key.KeyPair; +import org.tron.trident.proto.Chain; + +public class TronWalletTest { + + /** + * Test privateKey (Not important) + */ + private static final String MAIN_WALLET_PRIVATE_KEY = "18ef152ae3711498556a22db1f820f33f27080aea51b19d39fdb6752a0591e1f"; + /** + * test address (Not important) + */ + private static final String MAIN_WALLET_ADDRESS = "TFtGcTF8HER3tHAL9Fzu95D5kfBixGYPfL"; + private static final String USDT_CONTRACT_ADDRESS = "TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf"; + + @Test + public void createOfflineAccount() { + KeyPair keyPair = KeyPair.generate(); + String privateKey = keyPair.toPrivateKey(); + String address = keyPair.toBase58CheckAddress(); + System.out.println(privateKey); + System.out.println(address); + Assertions.assertNotNull(privateKey); + Assertions.assertNotNull(address); + } + + /** + * By default get tron (trx) balance + */ + @Test + public void getWalletBalance() { + ApiWrapper wrapper = ApiWrapper.ofNile(MAIN_WALLET_PRIVATE_KEY); + long accountBalance = wrapper.getAccountBalance(MAIN_WALLET_ADDRESS); + System.out.println(accountBalance); + Assertions.assertTrue(accountBalance >= 0); + } + + @Test + public void getWalletBalanceByContract() { + ApiWrapper wrapper = ApiWrapper.ofNile(MAIN_WALLET_PRIVATE_KEY); + Contract contract = wrapper.getContract(USDT_CONTRACT_ADDRESS); //USDT Contract + Trc20Contract trc20Contract = new Trc20Contract(contract, MAIN_WALLET_ADDRESS, wrapper); + System.out.println(trc20Contract.symbol()); + System.out.println(trc20Contract.balanceOf(MAIN_WALLET_ADDRESS)); + System.out.println(trc20Contract.decimals()); + } + + /** + * Activation requires transferring at least 0.1 TRX coins to your new Tron account + */ + @Test + public void activateAccount() { + try { + /* Generate new Address */ + KeyPair keyPair = KeyPair.generate(); + String targetPrivateKey = "18ef152ae3711498556a22db1f820f33f27080aea51b19d39fdb6752a0591e1f"; + String targetAddress = "TFtGcTF8HER3tHAL9Fzu95D5kfBixGYPfL"; + System.out.println(targetPrivateKey); + System.out.println(targetAddress); + + /* Transfer Token (TRX) + * amount = amount * 100000 + * 0.100000 = 1 * 100000 (6 decimal) + * */ + long amount = 100000; + ApiWrapper wrapper = ApiWrapper.ofNile(MAIN_WALLET_PRIVATE_KEY); + Response.TransactionExtention transactionExtention = wrapper.transfer(MAIN_WALLET_ADDRESS, targetAddress, amount); + Chain.Transaction transaction = wrapper.signTransaction(transactionExtention); + Response.TransactionReturn transactionReturn = wrapper.blockingStub.broadcastTransaction(transaction); + System.out.println("> Result : \n" + transactionReturn.toString()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + @Test + public void transferTokenByContract() throws IllegalException { + /* Generate new Address */ + KeyPair keyPair = KeyPair.generate(); + String targetPrivateKey = keyPair.toPrivateKey(); + String targetAddress = keyPair.toBase58CheckAddress(); + System.out.println(targetPrivateKey); + System.out.println(targetAddress); + + /* Create Transaction */ + ApiWrapper wrapper = ApiWrapper.ofNile(MAIN_WALLET_PRIVATE_KEY); + Response.TransactionExtention txnExt = wrapper.transfer(MAIN_WALLET_ADDRESS, targetAddress, USDT_CONTRACT_ADDRESS, 50000000); + System.out.println("Transaction ID (txid) => " + Hex.toHexString(txnExt.getTxid().toByteArray())); + + /* Sign transaction */ + Chain.Transaction signedTxn = wrapper.signTransaction(txnExt); + + /* broadcast on tron network */ + Response.TransactionReturn ret = wrapper.blockingStub.broadcastTransaction(signedTxn); + System.out.println("> Result\n" + ret.toString()); + } + + @Test + public void createSubAccount() throws IllegalException { + /* Generate new Address */ + KeyPair keyPair = KeyPair.generate(); + String subAccountPrivateKey = keyPair.toPrivateKey(); + String subAccountAddress = keyPair.toBase58CheckAddress(); + System.out.println(subAccountPrivateKey); + System.out.println(subAccountAddress); + + /* Create SubAccount */ + ApiWrapper wrapper = ApiWrapper.ofNile(MAIN_WALLET_PRIVATE_KEY); + Response.TransactionExtention txnExt = wrapper.createAccount(MAIN_WALLET_ADDRESS, subAccountAddress); + + /* Sign request */ + Chain.Transaction transaction = wrapper.signTransaction(txnExt); + + /* Broadcast to tron network */ + String result = wrapper.broadcastTransaction(transaction); + System.out.println("Result : " + result); + } +}