Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Improvements #120

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions trident-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand All @@ -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
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>abi</artifactId>
<version>0.8.0</version>
<scope>system</scope>
<systemPath>your path</systemPath>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>utils</artifactId>
<version>0.8.0</version>
<scope>system</scope>
<systemPath>your path</systemPath>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>core</artifactId>
<version>0.8.0</version>
<scope>system</scope>
<systemPath>your path</systemPath>
</dependency>

<dependencies>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>abi</artifactId>
<version>0.8.0</version>
<scope>system</scope>
<systemPath>your path</systemPath>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>utils</artifactId>
<version>0.8.0</version>
<scope>system</scope>
<systemPath>your path</systemPath>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>core</artifactId>
<version>0.8.0</version>
<scope>system</scope>
<systemPath>your path</systemPath>
</dependency>
</dependencies>
```
1 change: 1 addition & 0 deletions trident-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ allprojects {
group = 'org.tron.trident'

repositories {
gradlePluginPortal()
mavenCentral()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Bool>() {
}));

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}