22
33import com .google .protobuf .ByteString ;
44import com .googlecode .cqengine .query .simple .In ;
5+ import io .grpc .ManagedChannel ;
6+ import io .grpc .ManagedChannelBuilder ;
57import javafx .scene .AmbientLight ;
68import lombok .extern .slf4j .Slf4j ;
79import org .testng .annotations .*;
810import org .testng .Assert ;
911import org .tron .api .GrpcAPI ;
12+ import org .tron .api .WalletGrpc ;
13+ import org .tron .common .crypto .ECKey ;
1014import org .tron .common .utils .ByteArray ;
1115import org .tron .protos .Contract ;
1216import org .tron .protos .Protocol ;
17+ import stest .tron .wallet .common .client .Configuration ;
1318import stest .tron .wallet .common .client .WalletClient ;
1419import stest .tron .wallet .common .client .utils .Base58 ;
20+ import stest .tron .wallet .common .client .utils .TransactionUtils ;
1521
22+ import java .math .BigInteger ;
1623import java .util .Optional ;
1724
1825
@@ -38,8 +45,16 @@ public class Wallettest_p0_002 {
3845 private static final Long AMOUNT = 101L ;
3946
4047 private static final long now = System .currentTimeMillis ();
41- private static final String name = "testAssetIssue_" + Long .toString (now );
48+ private static String name = "testAssetIssue_" + Long .toString (now );
4249 private static final long TotalSupply = now ;
50+ String Description = "just-test" ;
51+ String Url = "https://github.com/tronprotocol/wallet-cli/" ;
52+
53+ private ManagedChannel channelFull = null ;
54+ private WalletGrpc .WalletBlockingStub blockingStubFull = null ;
55+ //private String fullnode = "39.105.111.178:50051";
56+ private String fullnode = Configuration .getByPath ("testng.conf" ).getStringList ("fullnode.ip.list" ).get (0 );
57+ //private String search_fullnode = Configuration.getByPath("testng.conf").getStringList("fullnode.ip.list").get(1);
4358
4459 public static void main (String [] args ) {
4560 logger .info ("test man." );
@@ -52,47 +67,45 @@ public void beforeClass() {
5267 walletClient = new WalletClient (testKey002 );
5368 walletClient .init (0 );
5469
70+ channelFull = ManagedChannelBuilder .forTarget (fullnode )
71+ .usePlaintext (true )
72+ .build ();
73+ blockingStubFull = WalletGrpc .newBlockingStub (channelFull );
74+
5575 }
5676
57- @ Test (enabled = false )
77+ @ Test (enabled = true )
5878 public void TestAssetIssue () {
59- //long TotalSupply = 100000000L;
60- int TrxNum = 1 ;
61- int IcoNum = 100 ;
62- long StartTime = 1522583680000L ;
63- long EndTime = 1525089280000L ;
64- int DecayRatio = 1 ;
65- int VoteScore = 2 ;
66- String Description = "just-test" ;
67- String Url = "https://github.com/tronprotocol/wallet-cli/" ;
6879
69- try {
70- Contract .AssetIssueContract .Builder builder = Contract .AssetIssueContract .newBuilder ();
71- builder .setOwnerAddress (ByteString .copyFrom (walletClient .getAddress ()));
72- builder .setName (ByteString .copyFrom (name .getBytes ()));
73- builder .setTotalSupply (TotalSupply );
74- builder .setTrxNum (TrxNum );
75- builder .setNum (IcoNum );
76- builder .setStartTime (StartTime );
77- builder .setEndTime (EndTime );
78- //builder.setDecayRatio(DecayRatio);
79- builder .setVoteScore (VoteScore );
80- builder .setDescription (ByteString .copyFrom (Description .getBytes ()));
81- builder .setUrl (ByteString .copyFrom (Url .getBytes ()));
80+ ByteString addressBS1 = ByteString .copyFrom (FROM_ADDRESS );
81+ Protocol .Account request1 = Protocol .Account .newBuilder ().setAddress (addressBS1 ).build ();
82+ GrpcAPI .AssetIssueList assetIssueList1 = blockingStubFull
83+ .getAssetIssueByAccount (request1 );
84+ Optional <GrpcAPI .AssetIssueList > queryAssetByAccount = Optional .ofNullable (assetIssueList1 );
85+ if (queryAssetByAccount .get ().getAssetIssueCount () == 0 ){
86+ try {
87+ Thread .sleep (16000 );
88+ } catch (InterruptedException e ) {
89+ e .printStackTrace ();
90+ }
91+ //新建一笔通证
92+ Assert .assertTrue (CreateAssetIssue (FROM_ADDRESS ,name ,TotalSupply , 1 ,100 ,now +900000 ,now +10000000000L ,
93+ 1 , Description , Url , testKey002 ));
94+ }
95+ else {
96+ logger .info ("This account already create an assetisue" );
97+ Optional <GrpcAPI .AssetIssueList > queryAssetByAccount1 = Optional .ofNullable (assetIssueList1 );
98+ name = ByteArray .toStr (queryAssetByAccount1 .get ().getAssetIssue (0 ).getName ().toByteArray ());
8299
83- Boolean ret = walletClient .createAssetIssue (builder .build ());
84- Assert .assertTrue (ret );
85- } catch (Exception ex ) {
86- ex .printStackTrace ();
87100 }
88101
89- logger .info ("this is TestAssetIssue test case" );
90102 }
91103
92- @ Test (enabled = false )
104+ @ Test (enabled = true )
93105 public void TestGetAssetIssueByName () {
94106 Contract .AssetIssueContract ret = walletClient .getAssetIssueByName (name );
95- Assert .assertEquals (ret .getTotalSupply (), TotalSupply );
107+ Assert .assertTrue (ret .getOwnerAddress () != null );
108+ //Assert.assertEquals(ret.getTotalSupply(), TotalSupply);
96109
97110
98111 //logger.info(ByteArray.toStr(walletClient.getAssetIssueByName(name).getName().toByteArray()));
@@ -106,11 +119,21 @@ public void TestGetAssetIssueByName() {
106119 }
107120
108121
109- @ Test (enabled = false )
122+ @ Test (enabled = true )
110123 public void TestTransferAsset () {
111124 //byte assertName[] = name.getBytes();
112125 //logger.info(Long.toString(walletClient.getAssetIssueByName(name).getTotalSupply()));
113126 Boolean ret = walletClient .transferAsset (TO_ADDRESS , name .getBytes (), AMOUNT );
127+ if (ret == false ){
128+ try {
129+ Thread .sleep (16000 );
130+ } catch (InterruptedException e ) {
131+ e .printStackTrace ();
132+ }
133+ ret = walletClient .transferAsset (TO_ADDRESS , name .getBytes (), AMOUNT );
134+
135+ }
136+
114137 //logger.info(Long.toString(walletClient.getAssetIssueByName(name).getTotalSupply()));
115138 Assert .assertTrue (ret );
116139 logger .info ("this is TestTransferAsset" );
@@ -119,18 +142,19 @@ public void TestTransferAsset() {
119142 }
120143
121144
122- @ Test (enabled = false )
145+ @ Test (enabled = true )
123146 public void TestGetAssetIssueByAccount () {
124147 Optional <GrpcAPI .AssetIssueList > result = walletClient .getAssetIssueByAccount (FROM_ADDRESS );
148+ Assert .assertTrue (result .get ().getAssetIssueCount () == 1 );
125149
126- GrpcAPI .AssetIssueList assetissuelist = result .get ();
150+ // GrpcAPI.AssetIssueList assetissuelist = result.get();
127151 //logger.info(Integer.toString(result.get().getAssetIssue(0).getDecayRatio()));
128152 //logger.info(Long.toString(result.get().getAssetIssue(0).getTotalSupply()));
129153 //logger.info(ByteArray.toStr(result.get().getAssetIssue(result.get().getAssetIssueCount()-1).getName().toByteArray()));
130154 //logger.info(ByteArray.toStr(result.get().getAssetIssue(0).getName().toByteArray()));
131155 //logger.info(ByteArray.toStr(result.get().getAssetIssue(0).getName().toByteArray()));
132- logger .info (Integer .toString (result .get ().getAssetIssueCount ()));
133-
156+ // logger.info(Integer.toString(result.get().getAssetIssueCount()));
157+ /*
134158 Boolean foundThisName = false;
135159 for (int j = 0; j < result.get().getAssetIssueCount(); j++) {
136160 logger.info(result.get().getAssetIssue(j).getName().toString());
@@ -143,20 +167,21 @@ public void TestGetAssetIssueByAccount() {
143167 }
144168 }
145169 Assert.assertTrue(foundThisName);
146- logger .info ("TestGetAssetIssueByAccount" );
170+ logger.info("TestGetAssetIssueByAccount");*/
147171
148172 }
149173
150174
151175
152176
153- @ Test (enabled = false )
177+ @ Test (enabled = true )
154178 public void TestGetAssetIssueList (){
155179 Optional <GrpcAPI .AssetIssueList > result = walletClient .getAssetIssueList ();
156180 GrpcAPI .AssetIssueList getAssetIssueList = result .get ();
157181 logger .info (Integer .toString (result .get ().getAssetIssueCount ()));
182+ Assert .assertTrue (result .get ().getAssetIssueCount () > 0 );
158183
159- Boolean foundThisName = false ;
184+ /* Boolean foundThisName = false;
160185 for (int j =0; j<result.get().getAssetIssueCount(); j++) {
161186 logger.info(ByteArray.toStr(result.get().getAssetIssue(j).getName().toByteArray()));
162187 if (result.get().getAssetIssue(j).getTotalSupply() == TotalSupply){
@@ -165,7 +190,7 @@ public void TestGetAssetIssueList(){
165190 }
166191 }
167192 Assert.assertTrue(foundThisName);
168- logger .info ("TestGetAssetIssueList" );
193+ logger.info("TestGetAssetIssueList");*/
169194 }
170195
171196 @ Test (enabled = false )
@@ -186,19 +211,85 @@ public void TestGetAssetIssueByTimestamp(){
186211
187212 }
188213
189- @ Test (enabled = false )
214+ @ Test (enabled = true )
190215 public void TestParticipateAssetIssue (){
191216 Contract .ParticipateAssetIssueContract result = walletClient .participateAssetIssueContract (TO_ADDRESS , name .getBytes (),FROM_ADDRESS , AMOUNT );
192- Assert .assertTrue (AMOUNT != 0 );
193- logger .info (Long .toString (result .getAmount ()));
217+
218+ Assert .assertTrue (result .getAmount () == AMOUNT );
219+ //Assert.assertTrue(AMOUNT != 0);
220+ //logger.info(Long.toString(result.getAmount()));
194221 //logger.info(result.getAssetName());
195- logger .info ("This is TestParticipateAssetIssue." );
222+ // logger.info("This is TestParticipateAssetIssue.");
196223 }
197224
198225 @ AfterClass
199226 public void afterClass () {
200227 logger .info ("this is after class" );
201228 }
202229
230+ public Boolean CreateAssetIssue (byte [] address , String name , Long TotalSupply , Integer TrxNum , Integer IcoNum , Long StartTime , Long EndTime ,
231+ Integer VoteScore , String Description , String URL , String priKey ){
232+ //long TotalSupply = 100000000L;
233+ //int TrxNum = 1;
234+ //int IcoNum = 100;
235+ //long StartTime = 1522583680000L;
236+ //long EndTime = 1525089280000L;
237+ //int DecayRatio = 1;
238+ //int VoteScore = 2;
239+ //String Description = "just-test";
240+ //String Url = "https://github.com/tronprotocol/wallet-cli/";
241+ ECKey temKey = null ;
242+ try {
243+ BigInteger priK = new BigInteger (priKey , 16 );
244+ temKey = ECKey .fromPrivate (priK );
245+ } catch (Exception ex ) {
246+ ex .printStackTrace ();
247+ }
248+ ECKey ecKey = temKey ;
249+ //Protocol.Account search = queryAccount(ecKey, blockingStubFull);
250+
251+ try {
252+ Contract .AssetIssueContract .Builder builder = Contract .AssetIssueContract .newBuilder ();
253+ builder .setOwnerAddress (ByteString .copyFrom (address ));
254+ builder .setName (ByteString .copyFrom (name .getBytes ()));
255+ builder .setTotalSupply (TotalSupply );
256+ builder .setTrxNum (TrxNum );
257+ builder .setNum (IcoNum );
258+ builder .setStartTime (StartTime );
259+ builder .setEndTime (EndTime );
260+ //builder.setDecayRatio(DecayRatio);
261+ builder .setVoteScore (VoteScore );
262+ builder .setDescription (ByteString .copyFrom (Description .getBytes ()));
263+ builder .setUrl (ByteString .copyFrom (URL .getBytes ()));
264+
265+ Protocol .Transaction transaction = blockingStubFull .createAssetIssue (builder .build ());
266+ if (transaction == null || transaction .getRawData ().getContractCount () == 0 ) {
267+ logger .info ("transaction == null, create assetissue failed" );
268+ return false ;
269+ }
270+ transaction = signTransaction (ecKey ,transaction );
271+ GrpcAPI .Return response = blockingStubFull .broadcastTransaction (transaction );
272+ if (response .getResult () == false ){
273+ logger .info (ByteArray .toStr (response .getMessage ().toByteArray ()));
274+ return false ;
275+ }
276+ else {
277+ logger .info (name );
278+ return true ;
279+ }
280+ } catch (Exception ex ) {
281+ ex .printStackTrace ();
282+ return false ;
283+ }
284+ }
285+
286+ private Protocol .Transaction signTransaction (ECKey ecKey , Protocol .Transaction transaction ) {
287+ if (ecKey == null || ecKey .getPrivKey () == null ) {
288+ logger .warn ("Warning: Can't sign,there is no private key !!" );
289+ return null ;
290+ }
291+ transaction = TransactionUtils .setTimestamp (transaction );
292+ return TransactionUtils .sign (transaction , ecKey );
293+ }
203294
204295}
0 commit comments