22
33import static org .apache .commons .lang3 .StringUtils .EMPTY ;
44import static org .tron .common .enums .NetType .CUSTOM ;
5+ import static org .tron .common .enums .NetType .MAIN ;
6+ import static org .tron .common .enums .NetType .NILE ;
7+ import static org .tron .common .enums .NetType .SHASTA ;
58import static org .tron .common .utils .CommandHelpUtil .getCommandHelp ;
69import static org .tron .common .utils .Utils .EMPTY_STR ;
710import static org .tron .common .utils .Utils .MAX_LENGTH ;
2225import static org .tron .ledger .console .ConsoleColor .ANSI_RED ;
2326import static org .tron .ledger .console .ConsoleColor .ANSI_RESET ;
2427
28+ import com .alibaba .fastjson .JSON ;
29+ import com .alibaba .fastjson .JSONObject ;
2530import com .beust .jcommander .JCommander ;
2631import com .beust .jcommander .Parameter ;
2732import com .google .common .primitives .Longs ;
2833import com .google .protobuf .InvalidProtocolBufferException ;
2934import java .io .File ;
3035import java .io .IOException ;
3136import java .math .BigInteger ;
37+ import java .nio .file .Files ;
38+ import java .nio .file .Path ;
3239import java .nio .file .Paths ;
3340import java .security .InvalidKeyException ;
3441import java .security .NoSuchAlgorithmException ;
4350import java .util .HashMap ;
4451import java .util .Iterator ;
4552import java .util .List ;
53+ import java .util .Optional ;
4654import java .util .regex .Matcher ;
4755import java .util .regex .Pattern ;
56+ import java .util .stream .Collectors ;
57+ import java .util .stream .Stream ;
4858import org .apache .commons .lang3 .ArrayUtils ;
4959import org .apache .commons .lang3 .tuple .Pair ;
5060import org .bouncycastle .util .encoders .Hex ;
6676import org .tron .common .utils .ByteUtil ;
6777import org .tron .common .utils .PathUtil ;
6878import org .tron .common .utils .Utils ;
79+ import org .tron .core .dao .Tx ;
6980import org .tron .core .exception .CancelException ;
7081import org .tron .core .exception .CipherException ;
82+ import org .tron .core .manager .TxHistoryManager ;
7183import org .tron .core .manager .UpdateAccountPermissionInteractive ;
7284import org .tron .keystore .StringUtils ;
7385import org .tron .ledger .TronLedgerGetAddress ;
@@ -167,6 +179,7 @@ public class Client {
167179 "GetTransactionInfoByBlockNum" ,
168180 "GetTransactionInfoById" ,
169181 "GetTransactionSignWeight" ,
182+ "GetUsdtBalance" ,
170183 "Help" ,
171184 "ImportWallet" ,
172185 "ImportWalletByMnemonic" ,
@@ -3598,10 +3611,18 @@ private void run() {
35983611 sendCoin (parameters );
35993612 break ;
36003613 }
3601- case "transferUSDT " : {
3614+ case "transferusdt " : {
36023615 transferUSDT (parameters );
36033616 break ;
36043617 }
3618+ case "getusdttransferbyid" : {
3619+ getUsdtTransferById (parameters );
3620+ break ;
3621+ }
3622+ case "getusdtbalance" : {
3623+ getUsdtBalance (parameters );
3624+ break ;
3625+ }
36053626 case "transferasset" : {
36063627 transferAsset (parameters );
36073628 break ;
@@ -3976,9 +3997,87 @@ private void run() {
39763997 }
39773998 }
39783999
3979- private void transferUSDT (String [] parameters ) {
4000+ private void getUsdtTransferById (String [] parameters ) throws IOException {
4001+ NetType netType = WalletApi .getCurrentNetwork ();
4002+ if (netType != MAIN && netType != NILE && netType != SHASTA ) {
4003+ System .out .println ("This command does not support the current network." );
4004+ return ;
4005+ }
4006+ if (parameters == null || parameters .length != 1 ) {
4007+ System .out .println ("GetUsdtTransferById needs 1 parameter like the following: " );
4008+ System .out .println ("GetUsdtTransferById txId " );
4009+ return ;
4010+ }
4011+ String txId = parameters [0 ];
4012+ TxHistoryManager txHistoryManager = new TxHistoryManager ();
4013+ Path filePath = txHistoryManager .getNetworkFilePath (netType );
4014+ txHistoryManager .ensureNetworkDirectoryExists (netType );
4015+ List <Tx > txs = new ArrayList <>();
4016+ try (Stream <String > lines = Files .lines (filePath )) {
4017+ txs = Files .exists (filePath ) ? lines
4018+ .filter (line -> !line .trim ().isEmpty ()).map (txHistoryManager ::lineToTx )
4019+ .filter (Optional ::isPresent )
4020+ .map (Optional ::get ).collect (Collectors .toList ()) : new ArrayList <>();
4021+ } catch (IOException e ) {
4022+ System .err .println ("Failed to count transactions: " + e .getMessage ());
4023+ }
4024+ Optional <Tx > foundTx = txs .stream ()
4025+ .filter (tx -> tx .getId ().equals (txId ))
4026+ .findFirst ();
4027+
4028+ if (foundTx .isPresent ()) {
4029+ Tx tx = foundTx .get ();
4030+ JSONObject json = new JSONObject (true );
4031+ json .put ("id" , tx .getId ());
4032+ json .put ("type" , tx .getType ());
4033+ json .put ("from" , tx .getFrom ());
4034+ json .put ("to" , tx .getTo ());
4035+ json .put ("amount" , Long .parseLong (tx .getAmount ()));
4036+ String prefix = netType == MAIN ? EMPTY : (netType .name ().toLowerCase () + "." );
4037+ json .put ("tronscanQueryUrl" , String .format ("https://%stronscan.org/#/transaction/%s" , prefix , txId ));
4038+ System .out .println (JSON .toJSONString (json , true ));
4039+ } else {
4040+ System .out .println ("The USDT transfer you inquired about does not exist." );
4041+ }
4042+ }
4043+
4044+ private void getUsdtBalance (String [] parameters ) throws Exception {
4045+ NetType netType = WalletApi .getCurrentNetwork ();
4046+ if (netType != MAIN && netType != NILE && netType != SHASTA ) {
4047+ System .out .println ("This command does not support the current network." );
4048+ return ;
4049+ }
4050+ byte [] ownerAddress ;
4051+ if (ArrayUtils .isEmpty (parameters )) {
4052+ ownerAddress = null ;
4053+ } else if (parameters .length == 1 ) {
4054+ ownerAddress = WalletApi .decodeFromBase58Check (parameters [0 ]);
4055+ if (ownerAddress == null ) {
4056+ System .out .println ("The address you entered is invalid." );
4057+ return ;
4058+ }
4059+ } else {
4060+ System .out .println ("GetUsdtBalance needs no parameter or 1 parameter like the following: " );
4061+ System .out .println ("GetUsdtBalance Address " );
4062+ return ;
4063+ }
4064+ Pair <Boolean , Long > pair = walletApiWrapper .getUsdtBalance (ownerAddress );
4065+ if (Boolean .TRUE .equals (pair .getLeft ())) {
4066+ long balance = pair .getRight ();
4067+ System .out .println ("USDT balance = " + balance );
4068+ } else {
4069+ System .out .println ("GetUsdtBalance " + failedHighlight () + " !!!!" );
4070+ }
4071+ }
4072+
4073+ private void transferUSDT (String [] parameters ) throws Exception {
4074+ NetType netType = WalletApi .getCurrentNetwork ();
4075+ if (netType != MAIN && netType != NILE && netType != SHASTA ) {
4076+ System .out .println ("This command does not support the current network." );
4077+ return ;
4078+ }
39804079 if (parameters == null || (parameters .length != 2 && parameters .length != 3 )) {
3981- System .out .println ("TransferUSDT needs 2 parameters like following: " );
4080+ System .out .println ("TransferUSDT needs at least 2 parameters like following: " );
39824081 System .out .println ("TransferUSDT [OwnerAddress] ToAddress Amount" );
39834082 return ;
39844083 }
@@ -3992,23 +4091,20 @@ private void transferUSDT(String[] parameters) {
39924091 return ;
39934092 }
39944093 }
3995-
39964094 String base58ToAddress = parameters [index ++];
3997- byte [] toAddress = WalletApi .decodeFromBase58Check (base58ToAddress );
3998- if (toAddress == null ) {
3999- System .out .println ("Invalid toAddress." );
4000- return ;
4095+ String amountStr = parameters [index ];
4096+ String inputStr = String .format ("\" %s\" ,%s" , base58ToAddress , amountStr );
4097+ final String methodStr = "transfer(address,uint256)" ;
4098+ byte [] input = Hex .decode (AbiUtil .parseMethod (methodStr , inputStr , false ));
4099+ byte [] contractAddress = WalletApi .decodeFromBase58Check (netType .getUsdtAddress ());
4100+ boolean result = walletApiWrapper .callContract (
4101+ ownerAddress , contractAddress , 0 , input , 1000000000 , 0 , "" , false );
4102+ if (result ) {
4103+ System .out .println ("Transfer " + amountStr + " to " + base58ToAddress + " " + successfulHighlight () + ".\n "
4104+ + "Please check the given transaction id to get the result on blockchain using getTransactionInfoById command" );
4105+ } else {
4106+ System .out .println ("Transfer " + amountStr + " to " + base58ToAddress + " " + failedHighlight () + "." );
40014107 }
4002-
4003- String amountStr = parameters [index ++];
4004- long amount = Long .parseLong (amountStr );
4005-
4006- // boolean result = walletApiWrapper.transferUSDT(ownerAddress, toAddress, amount);
4007- // if (result) {
4008- // System.out.println("Transfer " + amount + " to " + base58ToAddress + " " + successfulHighlight() + " !!");
4009- // } else {
4010- // System.out.println("Transfer " + amount + " to " + base58ToAddress + " " + failedHighlight() + " !!");
4011- // }
40124108 }
40134109
40144110 private void viewBackupRecords (String [] parameters ) {
0 commit comments