-
Notifications
You must be signed in to change notification settings - Fork 119
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
mah454
wants to merge
5
commits into
tronprotocol:main
Choose a base branch
from
mah454:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Some Improvements #120
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ allprojects { | |
group = 'org.tron.trident' | ||
|
||
repositories { | ||
gradlePluginPortal() | ||
mavenCentral() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
trident-java/core/src/test/java/org/tron/trident/core/inceptors/TronWalletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not recommended to display private keys in plaintext. |
||
/** | ||
* 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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments and methods do not match.