55import java .net .URL ;
66import java .util .List ;
77
8- import org .junit .Assert ;
8+ import org .junit .Before ;
99import org .junit .Test ;
1010import org .slf4j .Logger ;
1111import org .slf4j .LoggerFactory ;
1212import org .web3j .crypto .CipherException ;
1313import org .web3j .crypto .Credentials ;
1414import org .web3j .crypto .WalletUtils ;
1515import org .web3j .platon .contracts .TicketContract ;
16+ import org .web3j .platon .contracts .TicketContract .VoteTicketEventEventResponse ;
1617import org .web3j .protocol .Web3j ;
1718import org .web3j .protocol .core .methods .response .TransactionReceipt ;
1819import org .web3j .protocol .http .HttpService ;
19- import org .web3j .tx .ReadonlyTransactionManager ;
2020import org .web3j .tx .gas .DefaultWasmGasProvider ;
2121
22+ import com .alibaba .fastjson .JSON ;
23+ import com .alibaba .fastjson .JSONObject ;
24+
2225public class TicketContractTest {
2326
2427 private Logger logger = LoggerFactory .getLogger (TicketContractTest .class );
2528
2629 private static final Credentials CREDENTIALS = loadCredentials ();
30+
31+ private TicketContract contract ;
2732
2833 private static Credentials loadCredentials () {
2934 Credentials credentials = null ;
3035 try {
31- URL url = TicketContractTest .class .getClassLoader ().getResource ("sophia/contracts/build /admin.json" );
36+ URL url = TicketContractTest .class .getClassLoader ().getResource ("wallet /admin.json" );
3237 String path = url .getPath ();
3338 credentials = WalletUtils .loadCredentials ("88888888" ,path );
3439 } catch (IOException e ) {
@@ -39,133 +44,136 @@ private static Credentials loadCredentials() {
3944 return credentials ;
4045 }
4146
42- private Web3j web3j = Web3j .build (new HttpService ("http://10.10.8.209:6789" ));
43-
44- @ Test
45- public void getPrice () throws Exception {
46- TicketContract contract = TicketContract .load (
47+ private Web3j web3j = Web3j .build (new HttpService ("http://192.168.9.76:8789" ));
48+ // private Web3j web3j = Web3j.build(new HttpService("http://10.10.8.209:6789"));
49+
50+
51+ @ Before
52+ public void init () {
53+ contract = TicketContract .load (
4754 web3j ,
48- new ReadonlyTransactionManager ( web3j , TicketContract . CONTRACT_ADDRESS ) ,
55+ CREDENTIALS ,
4956 new DefaultWasmGasProvider ()
5057 );
58+ }
5159
60+ @ Test
61+ public void getPrice () throws Exception {
5262 String price = contract .GetTicketPrice ().send ();
5363 logger .debug ("Ticket price: {}" ,price );
5464 }
55-
56-
65+
5766 @ Test
58- public void VoteTicket () throws Exception {
59- TicketContract contract = TicketContract .load (
60- web3j ,
61- CREDENTIALS ,
62- new DefaultWasmGasProvider ()
63- );
64-
65- TransactionReceipt receipt = contract
66- .VoteTicket (BigInteger .TEN ,BigInteger .ONE ,"0x1f3a8672348ff6b789e416762ad53e69063138b8eb4d8780101658f24b2369f1a8e09499226b467d8bc0c4e03e1dc903df857eeb3c67733d21b6aaee2840e429" )
67- .send ();
68-
69- List <TicketContract .VoteTicketEventEventResponse > eventEvents = contract .getVoteTicketEventEvents (receipt );
70- eventEvents .forEach (e ->logger .debug ("param1:{}" ,e .param1 ));
71-
72- /*String a = receipt.getLogs().get(0).getData();
73- RlpList rl = RlpDecoder.decode(Hex.decode(a.replace("0x","")));
74-
75- List<RlpType> rlpTypes = rl.getValues();
76- RlpList rlpList1 = (RlpList) rlpTypes.get(0);
77- RlpString rlpString = (RlpString) rlpList1.getValues().get(0);
78- String typecode = Hex.toHexString(rlpString.getBytes());
79- byte[] hexByte = Numeric.hexStringToByteArray(typecode);
80- Type result = PlatOnTypeDecoder.decode(hexByte, Utf8String.class);
81- logger.debug("{}",result.getValue());*/
82-
83- Assert .assertEquals ("0x1" ,receipt .getStatus ());
67+ public void GetPoolRemainder () throws Exception {
68+ String detail = contract .GetPoolRemainder ().send ();
69+ logger .debug ("{}" ,detail );
8470 }
8571
72+
8673 @ Test
87- public void GetCandidateTicketIds () throws Exception {
88- TicketContract contract = TicketContract .load (
89- web3j ,
90- new ReadonlyTransactionManager (web3j ,TicketContract .CONTRACT_ADDRESS ),
91- new DefaultWasmGasProvider ()
92- );
74+ public void VoteTicket () throws Exception {
75+ //选票单价
76+ String priceStr = contract .GetTicketPrice ().send ();
77+ BigInteger price = new BigInteger (priceStr );
78+ //购票数量
79+ BigInteger count = BigInteger .valueOf (5L );
80+ //候选人节点Id
81+ String nodeId = "0x4f6c8fd10bfb512793f81a3594120c76b6991d3d06c0cc652035cbfae3fcd7cdc3f3d7a82021dfdb9ea99f014755ec1a640d832a0362b47be688bb31d504f62d" ;
82+
83+ //调用接口
84+ TransactionReceipt receipt = contract .VoteTicket (count , price , nodeId ).send ();
85+ logger .debug ("TicketContract TransactionReceipt:{}" , JSON .toJSONString (receipt ));
86+
87+ //查看返回event
88+ List <VoteTicketEventEventResponse > events = contract .getVoteTicketEventEvents (receipt );
89+ JSONObject result = null ;
90+ for (VoteTicketEventEventResponse event : events ) {
91+ result = JSON .parseObject (event .param1 );
92+ logger .debug ("TicketContract event:{}" , result );
93+ }
94+
95+ //获得票id
96+ String txHash = receipt .getTransactionHash ();
97+ int tickets = result .getIntValue ("Data" );
98+
99+ List <String > ticketList = contract .VoteTicketIds (tickets , txHash );
100+ logger .debug ("TicketContract tickets:{}" , ticketList );
101+
102+ // 20:09:43.610 [main] DEBUG org.web3j.contract.test.TicketContractTest - TicketContract TransactionReceipt:{"blockHash":"0x85a49bf683c123b4e650721338acad3c6bd3ff71e0f20de8baaf16e5e3e90f67","blockNumber":15548,"blockNumberRaw":"0x3cbc","cumulativeGasUsed":34832,"cumulativeGasUsedRaw":"0x8810","from":"0x493301712671ada506ba6ca7891f436d29185821","gasUsed":34832,"gasUsedRaw":"0x8810","logs":[{"address":"0x1000000000000000000000000000000000000002","blockHash":"0x85a49bf683c123b4e650721338acad3c6bd3ff71e0f20de8baaf16e5e3e90f67","blockNumber":15548,"blockNumberRaw":"0x3cbc","data":"0xebaa7b22526574223a747275652c2244617461223a2235222c224572724d7367223a2273756363657373227d","logIndex":0,"logIndexRaw":"0x0","removed":false,"topics":["0xee64e28ecf7347fbf257cb1ced716cf9d51a1e24f06e0bbe2c3d4f40924049c9"],"transactionHash":"0x7afe907caf45da59bef88b6b708fe45958143a41baa51f6c4410f87d1338e49f","transactionIndex":0,"transactionIndexRaw":"0x0"}],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000001000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000200000000000000","status":"0x1","statusOK":true,"to":"0x1000000000000000000000000000000000000002","transactionHash":"0x7afe907caf45da59bef88b6b708fe45958143a41baa51f6c4410f87d1338e49f","transactionIndex":0,"transactionIndexRaw":"0x0"}
103+ // 20:09:43.629 [main] DEBUG org.web3j.contract.test.TicketContractTest - TicketContract event:{"Ret":true,"ErrMsg":"success","Data":"5"}
104+ // 20:09:43.633 [main] DEBUG org.web3j.contract.test.TicketContractTest - TicketContract tickets:[0x6bf2236d95a98c798abf760e43d8a1a0f375ce095f6f286198053800262988c5, 0x7f3d95634ebdbf0121a7de207b00cf2d2b4846000ec41b4a8a88d1e019701a5e, 0x57d1a6fcd7932a9615f593dbc6d6999912e170286ed1be0c0b31f6f14087c3b4, 0x3ad918e72305b6666efe2abd232554c34bab3f7cd5aa679334cb3ca3a14851ce, 0xe4f0b42906eed5f5e77cb233bd11d30e664f32b5602bda7f84d155462d496cbb]
93105
94- String ids = contract
95- .GetBatchCandidateTicketIds ("0abaf3219f454f3d07b6cbcf3c10b6b4ccf605202868e2043b6f5db12b745df0604ef01ef4cb523adc6d9e14b83a76dd09f862e3fe77205d8ac83df707969b47" )
96- .send ();
97- logger .debug ("CandidateTicketIds: {}" ,ids );
98106 }
99-
107+
100108 @ Test
101- public void GetBatchCandidateTicketIds () throws Exception {
102- TicketContract contract = TicketContract .load (
103- web3j ,
104- new ReadonlyTransactionManager (web3j ,TicketContract .CONTRACT_ADDRESS ),
105- new DefaultWasmGasProvider ()
106- );
109+ public void GetTicketDetail () throws Exception {
110+ //票id
111+ String ticketId = "0x6bf2236d95a98c798abf760e43d8a1a0f375ce095f6f286198053800262988c5" ;
107112
108- String detail = contract .GetBatchCandidateTicketIds ( "0abaf3219f454f3d07b6cbcf3c10b6b4ccf605202868e2043b6f5db12b745df0604ef01ef4cb523adc6d9e14b83a76dd09f862e3fe77205d8ac83df707969b47" ).send ();
113+ String detail = contract .GetTicketDetail ( ticketId ).send ();
109114
110115 logger .debug ("{}" ,detail );
111116
112117 }
113-
118+
114119 @ Test
115- public void GetTicketDetail () throws Exception {
116- TicketContract contract = TicketContract .load (
117- web3j ,
118- new ReadonlyTransactionManager (web3j ,TicketContract .CONTRACT_ADDRESS ),
119- new DefaultWasmGasProvider ()
120- );
120+ public void GetBatchTicketDetail () throws Exception {
121+ StringBuilder stringBuilder = new StringBuilder ();
122+ //票id
123+ stringBuilder .append ("0x6bf2236d95a98c798abf760e43d8a1a0f375ce095f6f286198053800262988c5" );
124+ //分割
125+ stringBuilder .append (":" );
126+ //票id
127+ stringBuilder .append ("0x7f3d95634ebdbf0121a7de207b00cf2d2b4846000ec41b4a8a88d1e019701a5e" );
121128
122- String detail = contract .GetTicketDetail ( "0x34bdecd0fa6b8d85b1fa436eac6066aca2f51cc5e84fec278bff7df781310982" ).send ();
129+ String detail = contract .GetBatchTicketDetail ( stringBuilder . toString () ).send ();
123130
124131 logger .debug ("{}" ,detail );
125132
126133 }
127-
134+
128135 @ Test
129- public void GetBatchTicketDetail () throws Exception {
130- String ticketIds = "0x34bdecd0fa6b8d85b1fa436eac6066aca2f51cc5e84fec278bff7df781310982:0x29d376241e85cba191406c7a698cb4aa322ec7971c89e9c7f7123c81b64fc7ba:0x8156589c8f71e981a3835362542ebf10bfbf93bc01cebc3b2c7185416f209508" ;
131- TicketContract contract = TicketContract .load (
132- web3j ,
133- new ReadonlyTransactionManager (web3j ,TicketContract .CONTRACT_ADDRESS ),
134- new DefaultWasmGasProvider ()
135- );
136+ public void GetCandidateTicketIds () throws Exception {
137+ //节点id
138+ String nodeId = "0x4f6c8fd10bfb512793f81a3594120c76b6991d3d06c0cc652035cbfae3fcd7cdc3f3d7a82021dfdb9ea99f014755ec1a640d832a0362b47be688bb31d504f62d" ;
139+
140+ String ids = contract .GetCandidateTicketIds (nodeId ).send ();
141+
142+ logger .debug ("CandidateTicketIds: {}" ,ids );
143+ }
144+
145+
146+ @ Test
147+ public void GetBatchCandidateTicketIds () throws Exception {
136148
137- String detail = contract .GetBatchTicketDetail (ticketIds ).send ();
149+ StringBuilder stringBuilder = new StringBuilder ();
150+ //候选人id
151+ stringBuilder .append ("0x4f6c8fd10bfb512793f81a3594120c76b6991d3d06c0cc652035cbfae3fcd7cdc3f3d7a82021dfdb9ea99f014755ec1a640d832a0362b47be688bb31d504f62d" );
152+ //分割
153+ stringBuilder .append (":" );
154+ //候选人id
155+ stringBuilder .append ("0x01d033b5b07407e377a3eb268bdc3f07033774fb845b7826a6b741430c5e6b719bda5c4877514e8052fa5dbc2f20fb111a576f6696b6a16ca765de49e11e0541" );
156+
157+
158+ String detail = contract .GetBatchCandidateTicketIds (stringBuilder .toString ()).send ();
138159
139160 logger .debug ("{}" ,detail );
140161
141162 }
142163
164+
165+
143166 @ Test
144167 public void GetCandidateEpoch () throws Exception {
145- TicketContract contract = TicketContract .load (
146- web3j ,
147- new ReadonlyTransactionManager (web3j ,TicketContract .CONTRACT_ADDRESS ),
148- new DefaultWasmGasProvider ()
149- );
150-
151- String detail = contract .GetCandidateEpoch ("0abaf3219f454f3d07b6cbcf3c10b6b4ccf605202868e2043b6f5db12b745df0604ef01ef4cb523adc6d9e14b83a76dd09f862e3fe77205d8ac83df707969b47" ).send ();
168+ //节点id
169+ String nodeId = "0x4f6c8fd10bfb512793f81a3594120c76b6991d3d06c0cc652035cbfae3fcd7cdc3f3d7a82021dfdb9ea99f014755ec1a640d832a0362b47be688bb31d504f62d" ;
170+
171+ String detail = contract .GetCandidateEpoch (nodeId ).send ();
152172
153173 logger .debug ("{}" ,detail );
154174
155175 }
156176
157- @ Test
158- public void GetPoolRemainder () throws Exception {
159- TicketContract contract = TicketContract .load (
160- web3j ,
161- new ReadonlyTransactionManager (web3j ,TicketContract .CONTRACT_ADDRESS ),
162- new DefaultWasmGasProvider ()
163- );
164-
165- String detail = contract .GetPoolRemainder ().send ();
166-
167- logger .debug ("{}" ,detail );
168177
169- }
170178
171179}
0 commit comments