/
Contract.java
931 lines (845 loc) · 42 KB
/
Contract.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
/*
* Copyright 2020 The caver-java Authors
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.klaytn.caver.contract;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.klaytn.caver.Caver;
import com.klaytn.caver.abi.ABI;
import com.klaytn.caver.abi.datatypes.Type;
import com.klaytn.caver.methods.request.CallObject;
import com.klaytn.caver.methods.request.KlayLogFilter;
import com.klaytn.caver.methods.response.KlayLogs;
import com.klaytn.caver.methods.response.TransactionReceipt;
import com.klaytn.caver.transaction.AbstractFeeDelegatedTransaction;
import com.klaytn.caver.transaction.AbstractTransaction;
import com.klaytn.caver.transaction.response.PollingTransactionReceiptProcessor;
import com.klaytn.caver.transaction.response.TransactionReceiptProcessor;
import com.klaytn.caver.wallet.IWallet;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web3j.protocol.ObjectMapperFactory;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.EthSubscribe;
import org.web3j.protocol.exceptions.TransactionException;
import org.web3j.protocol.websocket.events.LogNotification;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
public class Contract {
/**
* A caver instance.
*/
Caver caver;
/**
* A contract ABI(Application Binary interface) json string.
*/
String abi;
/**
* A contract address.
*/
String contractAddress;
/**
* The map where method name string and ContractMethod mapped.
*/
Map<String, ContractMethod> methods;
/**
* The map where event name string and ContractEvent mapped.
*/
Map<String, ContractEvent> events;
/**
* The ContractMethod instance related Contract's constructor.
*/
ContractMethod constructor;
/**
* The default send option. When you execute call() or send() without SendOptions, defaultSendOptions will be used.
*/
SendOptions defaultSendOptions;
/**
* The class instance implemented IWallet to sign transaction.
*/
IWallet wallet;
private static final Logger LOGGER = LoggerFactory.getLogger(Contract.class);
/**
* Creates a Contract instance.
* @param caver A Caver instance.
* @param abi A contract's ABI(Application Binary interface) json string.
* @throws IOException
*/
public Contract(Caver caver, String abi) throws IOException{
this(caver, abi, null);
}
/**
* Creates a Contract instance.
* @param caver A Caver instance
* @param abi A contract's ABI(Application Binary Interface) json string.
* @param contractAddress An address string of contract deployed on Klaytn.
* @throws IOException
*/
public Contract(Caver caver, String abi, String contractAddress) throws IOException {
setAbi(abi);
setCaver(caver);
setContractAddress(contractAddress);
setDefaultSendOptions(new SendOptions());
setWallet(caver.getWallet());
}
/**
* Deploy a contract
* @param sendOptions A SendOption instance.
* @param contractBinaryData A smart contract binary data.
* @param constructorParams The smart contract constructor parameters.
* @return Contract
* @throws TransactionException
* @throws NoSuchMethodException
* @throws IOException
* @throws InstantiationException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public Contract deploy(SendOptions sendOptions, String contractBinaryData, Object... constructorParams) throws TransactionException, IOException, NoSuchMethodException, InstantiationException, ClassNotFoundException, IllegalAccessException, InvocationTargetException {
ContractDeployParams deployParams = new ContractDeployParams(contractBinaryData, Arrays.asList(constructorParams));
return deploy(deployParams, sendOptions);
}
/**
* Deploy a contract
* @param sendOptions A SendOption instance
* @param receiptProcessor A TransactionReceiptProcessor instance.
* @param contractBinaryData A smart contract binary data.
* @param constructorParams The smart contract constructor parameters.
* @return Contract
* @throws TransactionException
* @throws NoSuchMethodException
* @throws IOException
* @throws InstantiationException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public Contract deploy(SendOptions sendOptions, TransactionReceiptProcessor receiptProcessor, String contractBinaryData, Object... constructorParams) throws TransactionException, IOException, NoSuchMethodException, InstantiationException, ClassNotFoundException, IllegalAccessException, InvocationTargetException {
ContractDeployParams deployParams = new ContractDeployParams(contractBinaryData, Arrays.asList(constructorParams));
return deploy(deployParams, sendOptions, receiptProcessor);
}
/**
* Deploy a contract.
* It sets TransactionReceiptProcessor to PollingTransactionReceiptProcessor instance.
* @param deployParam A DeployParam instance.
* @param sendOptions A SendOption instance.
* @return Contract
* @throws TransactionException
* @throws IOException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public Contract deploy(ContractDeployParams deployParam, SendOptions sendOptions) throws TransactionException, IOException, NoSuchMethodException, InstantiationException, ClassNotFoundException, IllegalAccessException, InvocationTargetException {
return deploy(deployParam, sendOptions, new PollingTransactionReceiptProcessor(caver, 1000, 15));
}
/**
* Deploy a contract
* @param deployParam A DeployParam instance.
* @param sendOptions A SendOption instance.
* @param processor A TransactionReceiptProcessor instance.
* @return Contract
* @throws TransactionException
* @throws IOException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public Contract deploy(ContractDeployParams deployParam, SendOptions sendOptions, TransactionReceiptProcessor processor) throws TransactionException, IOException, NoSuchMethodException, InstantiationException, ClassNotFoundException, IllegalAccessException, InvocationTargetException {
deployParam.getDeployParams().add(0, deployParam.getBytecode());
TransactionReceipt.TransactionReceiptData receiptData = this.getMethod("constructor").send(deployParam.getDeployParams(), sendOptions, processor);
String contractAddress = receiptData.getContractAddress();
this.setContractAddress(contractAddress);
return this;
}
/**
* Subscribes to an event and unsubscribes immediately after the first event or error.
* @param eventName The name of the event in the contract.
* @param paramsOption The filter events by indexed parameters.
* @param callback The callback function that handled to returned data.
* @return Disposable instance that able to unsubscribe.
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public Disposable once(String eventName, EventFilterOptions paramsOption, Consumer<LogNotification> callback) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Map options = new HashMap<>();
List topics = null;
if(eventName.equals("allEvents")) {
if(paramsOption != null) {
LOGGER.warn("If eventName has 'allEvent', passed paramOption will be ignored.");
}
} else {
ContractEvent event = this.getEvent(eventName);
if(paramsOption.getTopics() == null || paramsOption.getTopics().size() == 0) {
topics = EventFilterOptions.convertsTopic(event, paramsOption);
} else {
topics = paramsOption.getTopics();
}
}
options.put("address", getContractAddress());
options.put("topics", topics);
final Request<?, EthSubscribe> subscribeRequest = new Request<>(
"klay_subscribe",
Arrays.asList("logs", options),
this.caver.rpc.getWeb3jService(),
EthSubscribe.class
);
final Flowable<LogNotification> events = this.caver.rpc.getWeb3jService().subscribe(subscribeRequest, "klay_unsubscribe", LogNotification.class);
return events.take(1).subscribe(callback);
}
/**
* Get past events for this contract.
* @param eventName The name of the event in the contract.
* @param filterOption The KlayLogFilter instance to filter event.
* @return KlayLogs
* @throws IOException
*/
public KlayLogs getPastEvent(String eventName, KlayLogFilter filterOption) throws IOException {
ContractEvent event = getEvent(eventName);
filterOption.addSingleTopic(ABI.encodeEventSignature(event));
KlayLogs logs = caver.rpc.klay.getLogs(filterOption).send();
return logs;
}
/**
* Execute smart contract method in the EVM without sending any transaction.
* @param methodName The smart contract method name to execute.
* @param methodArguments The arguments that need to execute smart contract method.
* @return List
* @throws IOException
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public List<Type> call(String methodName, Object... methodArguments) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
return call(CallObject.createCallObject(), methodName, methodArguments);
}
/**
* Execute smart contract method in the EVM without sending any transaction.
* When creating CallObject, it need not to fill 'data', 'to' fields.
* The 'data', 'to' fields automatically filled in call() method.
* @param callObject A CallObject instance to 'call' smart contract method.
* @param methodName The smart contract method name to execute.
* @param methodArguments The arguments that need to execute smart contract method.
* @return List
* @throws IOException
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public List<Type> call(CallObject callObject, String methodName, Object... methodArguments) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
return this.getMethod(methodName).call(Arrays.asList(methodArguments), callObject);
}
/**
* Execute smart contract method in the EVM without sending any transaction.
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.
* @param methodName The smart contract method name to execute.
* @param methodArguments The arguments that need to execute smart contract method.
* @return List
* @throws IOException
* @throws ClassNotFoundException
*/
public List<Type> callWithSolidityType(String methodName, Type... methodArguments) throws IOException, ClassNotFoundException {
return callWithSolidityType(CallObject.createCallObject(), methodName, methodArguments);
}
/**
* Execute smart contract method in the EVM without sending any transaction.
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.
* When creating CallObject, it need not to fill 'data', 'to' fields.
* The 'data', 'to' fields automatically filled in call() method.
* @param callObject A CallObject instance to 'call' smart contract method.
* @param methodName The smart contract method name to execute.
* @param methodArguments The arguments that need to execute smart contract method.
* @return List
* @throws IOException
* @throws ClassNotFoundException
*/
public List<Type> callWithSolidityType(CallObject callObject, String methodName, Type... methodArguments) throws IOException, ClassNotFoundException {
return this.getMethod(methodName).callWithSolidityWrapper(Arrays.asList(methodArguments), callObject);
}
/**
* Send a transaction to smart contract and execute its method.
* It is used defaultSendOption field to sendOptions.
* It sets TransactionReceiptProcessor to PollingTransactionReceiptProcessor.
* @param methodName The smart contract method name to execute
* @param methodArguments The arguments that need to execute smart contract method.
* @return TransactionReceiptData
* @throws TransactionException
* @throws IOException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public TransactionReceipt.TransactionReceiptData send(String methodName, Object... methodArguments) throws TransactionException, IOException, NoSuchMethodException, InstantiationException, ClassNotFoundException, IllegalAccessException, InvocationTargetException {
return send(null, methodName, methodArguments);
}
/**
* Send a transaction to smart contract and execute its method.
* It sets TransactionReceiptProcessor to PollingTransactionReceiptProcessor.
* @param options An option to execute smart contract method.
* @param methodName The smart contract method name to execute
* @param methodArguments The arguments that need to execute smart contract method.
* @return TransactionReceiptData
* @throws TransactionException
* @throws IOException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public TransactionReceipt.TransactionReceiptData send(SendOptions options, String methodName, Object... methodArguments) throws TransactionException, IOException, NoSuchMethodException, InstantiationException, ClassNotFoundException, IllegalAccessException, InvocationTargetException {
return send(options, new PollingTransactionReceiptProcessor(caver, 1000, 15), methodName, methodArguments);
}
/**
* Send a transaction to smart contract and execute its method.
* @param options An option to execute smart contract method.
* @param receiptProcessor A TransactionReceiptProcessor to get receipt.
* @param methodName The smart contract method name to execute
* @param methodArguments The arguments that need to execute smart contract method.
* @return TransactionReceiptData
* @throws TransactionException
* @throws IOException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public TransactionReceipt.TransactionReceiptData send(SendOptions options, TransactionReceiptProcessor receiptProcessor, String methodName, Object... methodArguments) throws TransactionException, IOException, NoSuchMethodException, InstantiationException, ClassNotFoundException, IllegalAccessException, InvocationTargetException {
ContractMethod contractMethod = this.getMethod(methodName);
return contractMethod.send(Arrays.asList(methodArguments), options, receiptProcessor);
}
/**
* Send a transaction to smart contract and execute its method using solidity type wrapper class.
* It is used defaultSendOption field to sendOptions
* It sets TransactionReceiptProcessor to PollingTransactionReceiptProcessor.
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.
* @param methodName The smart contract method name to execute
* @param methodArguments The arguments that need to execute smart contract method.
* @return TransactionReceiptData
* @throws IOException
* @throws TransactionException
*/
public TransactionReceipt.TransactionReceiptData sendWithSolidityType(String methodName, Type... methodArguments) throws IOException, TransactionException {
return sendWithSolidityType(null, methodName, methodArguments);
}
/**
* Send a transaction to smart contract and execute its method using solidity type wrapper class.
* It sets TransactionReceiptProcessor to PollingTransactionReceiptProcessor.
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.
* @param options An option to execute smart contract method.
* @param methodName The smart contract method name to execute
* @param methodArguments The arguments that need to execute smart contract method.
* @return TransactionReceiptData
* @throws IOException
* @throws TransactionException
*/
public TransactionReceipt.TransactionReceiptData sendWithSolidityType(SendOptions options, String methodName, Type... methodArguments) throws IOException, TransactionException {
return sendWithSolidityType(options, new PollingTransactionReceiptProcessor(caver, 1000, 15), methodName, methodArguments);
}
/**
* Send a transaction to smart contract and execute its method using solidity type wrapper class.
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.
* @param options An option to execute smart contract method.
* @param receiptProcessor A TransactionReceiptProcessor to get receipt.
* @param methodName The smart contract method name to execute
* @param methodArguments The arguments that need to execute smart contract method.
* @return TransactionReceiptData
* @throws IOException
* @throws TransactionException
*/
public TransactionReceipt.TransactionReceiptData sendWithSolidityType(SendOptions options, TransactionReceiptProcessor receiptProcessor, String methodName, Type... methodArguments) throws IOException, TransactionException {
ContractMethod contractMethod = this.getMethod(methodName);
return contractMethod.sendWithSolidityWrapper(Arrays.asList(methodArguments), options, receiptProcessor);
}
/**
* Create and sign a transaction with the input data generated by the passed argument.<p>
* <pre>
* If the method name is a "constructor", it creates a transaction related to SmartContractDeploy and sign it.
* The arguments parsed as follow.
* - arguments[0] : Smart contract's bytecode.
* - others : The constructor arguments to deploy smart contract.
* <code>
* Caver caver = new Caver(Caver.DEFAULT_URL);
* String abi = "abi";
* String bytecode = "Contract bytecode";
*
* SendOptions sendOptions = new SendOptions();
* sendOptions.setFrom("0x{from}");
* sendOptions.setGas(BigInteger.valueOf(100000000));
*
* Contract contract = caver.contract.create(abi);
* contract.setDefaultSendOptions(sendOptions);
*
* contract.sign("constructor", bytecode, constructor_param1, constructor_param2...);
* </code>
* It is used defaultSendOption field to sendOptions.
* </pre>
* @param methodName The smart contract's method name to create a transaction and sign it.
* @param methodArguments The list of arguments to execute or deploy a smart contract.
* @return AbstractTransaction
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws IOException
*/
public AbstractTransaction sign(String methodName, Object... methodArguments) throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
return this.getMethod(methodName).sign(Arrays.asList(methodArguments));
}
/**
* Create and sign a transaction with the input data generated by the passed argument.<p>
* It creates a transaction related to SmartContractDeploy or SmartContractExecution to deploy or execute smart contract method.
* <pre>
* If the method name is a "constructor", it creates a transaction related to SmartContractDeploy and sign it.
* The arguments parsed as follow.
* - arguments[0] : Smart contract's bytecode.
* - others : The constructor arguments to deploy smart contract.
* <code>
* Caver caver = new Caver(Caver.DEFAULT_URL);
* String abi = "abi";
* String bytecode = "Contract bytecode";
*
* Contract contract = caver.contract.create(abi);
*
* SendOptions sendOptions = new SendOptions();
* sendOptions.setFrom("0x{from}");
* sendOptions.setGas(BigInteger.valueOf(100000000));
*
* contract.sign(sendOptions, "constructor", bytecode, constructor_param1, constructor_param2...);
* </code>
* </pre>
* @param sendOptions An option to deploy or execute smart contract method.
* @param methodName The smart contract's method name to create a transaction and sign it.
* @param methodArguments The list of arguments to execute or deploy a smart contract.
* @return AbstractTransaction
* @throws IOException
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public AbstractTransaction sign(SendOptions sendOptions, String methodName, Object... methodArguments) throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
return this.getMethod(methodName).sign(Arrays.asList(methodArguments), sendOptions);
}
/**
* Create and sign a transaction with the input data generated by the passed argument that wrapped by solidity type class.<p>
* It creates a transaction related to SmartContractExecution to execute smart contract method.<p>
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.<p>
* It is used defaultSendOption field to sendOptions.
* @param methodName The smart contract's method name to create a transaction and sign it.
* @param methodArguments The list of arguments to execute or deploy a smart contract.
* @return AbstractTransaction
* @throws IOException
*/
public AbstractTransaction signWithSolidityType(String methodName, Type... methodArguments) throws IOException {
return this.getMethod(methodName).signWithSolidityWrapper(Arrays.asList(methodArguments));
}
/**
* Create and sign a transaction with the input data generated by the passed argument that wrapped by solidity type class.<p>
* It creates a transaction related to SmartContractExecution to execute smart contract method.<p>
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.<p>
* @param sendOptions An option to execute smart contract method.
* @param methodName The smart contract's method name to create a transaction and sign it.
* @param methodArguments The list of arguments to execute or deploy a smart contract.
* @return AbstractTransaction
* @throws IOException
*/
public AbstractTransaction signWithSolidityType(SendOptions sendOptions, String methodName, Type... methodArguments) throws IOException {
return this.getMethod(methodName).signWithSolidityWrapper(Arrays.asList(methodArguments), sendOptions);
}
/**
* Create and sign a transaction as a fee payer with the input data generated by the passed argument.<p>
* <pre>
* If the method name is a "constructor", it creates a transaction related to FeeDelegatedSmartContractDeploy and sign it.
* The arguments parsed as follow.
* - arguments[0] : Smart contract's bytecode.
* - others : The constructor arguments to deploy smart contract.
* <code>
* Caver caver = new Caver(Caver.DEFAULT_URL);
* String abi = "Contract ABI data";
* String bytecode = "Contract bytecode";
*
* SendOptions sendOptions = new SendOptions();
* sendOptions.setFrom("0x{from}");
* sendOptions.setGas(BigInteger.valueOf(100000000));
* sendOptions.setFeeDelegation(true);
* sendOptions.setFeePayer("0x{feePayer}");
*
* Contract contract = caver.contract.create(abi);
* contract.setDefaultSendOptions(sendOptions);
*
* contract.signAsFeePayer("constructor", bytecode, constructor_param1, constructor_param2...);
* </code>
* </pre>
* @param methodName The smart contract's method name to create a transaction and sign it.
* @param methodArguments The list of arguments to execute or deploy a smart contract.
* @return AbstractTransaction
* @throws IOException
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public AbstractTransaction signAsFeePayer(String methodName, Object... methodArguments) throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
return this.getMethod(methodName).signAsFeePayer(Arrays.asList(methodArguments));
}
/**
/**
* Create and sign a transaction as a fee payer with the input data generated by the passed argument.<p>
* <pre>
* If the method name is a "constructor", it creates a transaction related to FeeDelegatedSmartContractDeploy and sign it.
* The arguments parsed as follow.
* - arguments[0] : Smart contract's bytecode.
* - others : The constructor arguments to deploy smart contract.
* <code>
* Caver caver = new Caver(Caver.DEFAULT_URL);
* String abi = "Contract ABI data";
* String bytecode = "Contract bytecode";
*
* SendOptions sendOptions = new SendOptions();
* sendOptions.setFrom("0x{from}");
* sendOptions.setGas(BigInteger.valueOf(100000000));
* sendOptions.setFeeDelegation(true);
* sendOptions.setFeePayer("0x{feePayer}");
*
* Contract contract = caver.contract.create(abi);
* contract.signAsFeePayer(sendOptions, "constructor", bytecode, constructor_param1, constructor_param2...);
* </code>
* </pre>
* @param sendOptions An option to execute smart contract method.
* @param methodName The smart contract's method name to create a transaction and sign it.
* @param methodArguments The list of arguments to execute or deploy a smart contract.
* @return AbstractTransaction
* @throws IOException
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public AbstractFeeDelegatedTransaction signAsFeePayer(SendOptions sendOptions, String methodName, Object... methodArguments) throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
return this.getMethod(methodName).signAsFeePayer(Arrays.asList(methodArguments), sendOptions);
}
/**
* Create and sign a transaction as a fee payer with the input data generated by the passed argument that wrapped by solidity type class.<p>
* It creates a transaction related to FeeDelegatedSmartContractExecution to execute smart contract method.<p>
* It is used defaultSendOption field to sendOptions.<p>
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.
* @param methodName The smart contract's method name to create a transaction and sign it.
* @param methodArguments A List of parameter that wrapped by solidity type class.
* @return AbstractTransaction
* @throws IOException
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public AbstractFeeDelegatedTransaction signAsFeePayerWithSolidityType(String methodName, Type... methodArguments) throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
return this.getMethod(methodName).signAsFeePayerWithSolidityWrapper(Arrays.asList(methodArguments));
}
/**
* Create and sign a transaction as a fee payer with the input data generated by the passed argument that wrapped by solidity type class.<p>
* It creates a transaction related to FeeDelegatedSmartContractExecution to execute smart contract method.<p>
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.
* @param sendOptions An option to execute smart contract method.
* @param methodName The smart contract's method name to create a transaction and sign it.
* @param methodArguments A List of parameter that wrapped by solidity type class.
* @return AbstractTransaction
* @throws IOException
*/
public AbstractFeeDelegatedTransaction signAsFeePayerWithSolidityType(SendOptions sendOptions, String methodName, Type... methodArguments) throws IOException {
return this.getMethod(methodName).signAsFeePayerWithSolidityWrapper(Arrays.asList(methodArguments), sendOptions);
}
/**
* Encodes the ABI for the method in Contract. The resulting hex string is 32-bit function signature hash plus the passed parameters in Solidity tightly packed format.
* @param methodName The smart contract method name to encode.
* @param methodArguments The arguments that need to execute smart contract method.
* @return String
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public String encodeABI(String methodName, Object... methodArguments) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
ContractMethod method = this.getMethod(methodName);
return method.encodeABI(Arrays.asList(methodArguments));
}
/**
* Encodes the ABI for the method in Contract with Solidity type wrapper reference. The resulting hex string is 32-bit function signature hash plus the passed parameters in Solidity tightly packed format.
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.
* @param methodName The smart contract method name to encode.
* @param methodArguments The arguments that need to execute smart contract method.
* @return String
*/
public String encodeABIWithSolidityType(String methodName, Type... methodArguments) {
ContractMethod method = this.getMethod(methodName);
return method.encodeABIWithSolidityWrapper(Arrays.asList(methodArguments));
}
/**
* Estimate the gas to execute the contract's method.
* @param callObject An option to execute smart contract method.
* @param methodName The smart contract method name.
* @param methodArguments The arguments that need to execute smart contract method.
* @return String
* @throws NoSuchMethodException
* @throws IOException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws ClassNotFoundException
*/
public String estimateGas(CallObject callObject, String methodName, Object... methodArguments) throws NoSuchMethodException, IOException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException {
ContractMethod method = this.getMethod(methodName);
return method.estimateGas(Arrays.asList(methodArguments), callObject);
}
/**
* Estimate the gas to execute the contract's method with Solidity type wrapper reference.
* It is recommended to use this function when you want to execute one of the functions with the same number of parameters.
* @param callObject An option to execute smart contract method.
* @param methodName The smart contract method name.
* @param methodArguments The arguments that need to execute smart contract method.
* @return String
* @throws IOException
*/
public String estimateGasWithSolidityType(CallObject callObject, String methodName, Type... methodArguments) throws IOException {
ContractMethod method = this.getMethod(methodName);
return method.estimateGasWithSolidityWrapper(Arrays.asList(methodArguments), callObject);
}
/**
* Returns the ContractMethod instance corresponding to the method name.
* @param methodName The method name.
* @return ContractMethod
*/
public ContractMethod getMethod(String methodName) {
ContractMethod contractMethod = this.getMethods().get(methodName);
if(contractMethod == null) {
throw new NullPointerException(methodName + " method is not exist.");
}
return this.getMethods().get(methodName);
}
/**
* Returns the ContractEvent instance corresponding to the event name.
* @param eventName The event name.
* @return ContractEvent
*/
public ContractEvent getEvent(String eventName) {
ContractEvent contractEvent = this.getEvents().get(eventName);
if(contractEvent == null) {
throw new NullPointerException(eventName + " event is not exist.");
}
return this.getEvents().get(eventName);
}
/**
* Getter function for caver.
* @return Caver
*/
public Caver getCaver() {
return caver;
}
/**
* Getter function for contract's abi.
* @return String
*/
public String getAbi() {
return abi;
}
/**
* Getter function for contract address.
* @return String
*/
public String getContractAddress() {
return contractAddress;
}
/**
* Getter function for methods.
* @return Map
*/
public Map<String, ContractMethod> getMethods() {
return methods;
}
/**
* Getter function for events.
* @return Map
*/
public Map<String, ContractEvent> getEvents() {
return events;
}
/**
* Getter function for Contract's constructor function info.
* @return ContractMethod.
*/
public ContractMethod getConstructor() {
return constructor;
}
/**
* Getter function for DefaultSendOptions
* @return SendOptions
*/
public SendOptions getDefaultSendOptions() {
return defaultSendOptions;
}
/**
* Getter function for wallet
* @return IWallet
*/
public IWallet getWallet() {
return wallet;
}
/**
* Setter function for Caver.
* @param caver The Caver instance.
*/
void setCaver(Caver caver) {
this.caver = caver;
//When Caver instance changes, the caver instance of each ContractMethod is also replaced.
if(this.methods != null && this.methods.size() != 0) {
this.getMethods().values().forEach(value -> value.setCaver(caver));
}
}
/**
* Setter function for contract's abi.
* @param abi The abi json string.
* @throws IOException
*/
void setAbi(String abi) throws IOException {
this.abi = abi;
//When abi changes, It newly set a "methods" and "events".
init(this.abi);
}
/**
* Setter function for contract address.
* @param contractAddress The contract address.
*/
void setContractAddress(String contractAddress) {
this.contractAddress = contractAddress;
//When contract address changes, the contract address of each ContractMethod is also replaced.
if(this.methods != null && this.methods.size() != 0) {
this.getMethods().values().forEach(value -> value.setContractAddress(this.contractAddress));
}
}
/**
* Setter function for methods.
* @param methods The map where method name string and ContractMethod mapped.
*/
void setMethods(Map<String, ContractMethod> methods) {
this.methods = methods;
}
/**
* Setter function for events.
* @param events The map where event name string and ContractEvent mapped.
*/
void setEvents(Map<String, ContractEvent> events) {
this.events = events;
}
/**
* Setter function for constructor.
* @param constructor The ContractMethod instance related Contract's constructor.
*/
void setConstructor(ContractMethod constructor) {
this.constructor = constructor;
}
/**
* Setter function for wallet
* @param wallet The class instance implemented IWallet to sign transaction.
*/
public void setWallet(IWallet wallet) {
this.wallet = wallet;
if(this.methods != null && this.methods.size() != 0) {
this.getMethods().values().forEach(value -> value.setWallet(this.wallet));
}
}
/**
* Setter function for defaultSendOption
* @param defaultSendOptions The sendOptions to set DefaultSendOptions field.
*/
public void setDefaultSendOptions(SendOptions defaultSendOptions) {
this.defaultSendOptions = defaultSendOptions;
if(this.methods != null && this.methods.size() != 0) {
this.getMethods().values().forEach(value -> value.setDefaultSendOptions(this.defaultSendOptions));
}
}
/**
* Parse ABI json string and generate the mapped data related to method and event.
* @param abi The contract's ABI(Application Binary Interface) json string.
* @throws IOException
*/
private void init(String abi) throws IOException {
ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
methods = new HashMap<>();
events = new HashMap<>();
JsonNode root = objectMapper.readTree(abi);
Iterator<JsonNode> iterator = root.iterator();
while(iterator.hasNext()) {
JsonNode element = iterator.next();
if(element.get("type").asText().equals("function")) {
ContractMethod newMethod = objectMapper.readValue(element.toString(), ContractMethod.class);
newMethod.setSignature(ABI.encodeFunctionSignature(newMethod));
ContractMethod existedMethod = this.methods.get(newMethod.getName());
if(existedMethod != null) {
boolean isWarning = existedMethod.getNextContractMethods().stream().anyMatch(contractMethod -> {
return contractMethod.getInputs().size() == newMethod.getInputs().size();
});
if(existedMethod.getInputs().size() == newMethod.getInputs().size() || isWarning) {
LOGGER.warn("An overloaded function with the same number of parameters may not be executed normally. Please use *withSolidityWrapper methods in ContractMethod class.");
}
existedMethod.getNextContractMethods().add(newMethod);
} else {
methods.put(newMethod.getName(), newMethod);
}
} else if(element.get("type").asText().equals("event")) {
ContractEvent event = objectMapper.readValue(element.toString(), ContractEvent.class);
event.setSignature(ABI.encodeEventSignature(event));
events.put(event.getName(), event);
} else if(element.get("type").asText().equals("constructor")) {
ContractMethod method = objectMapper.readValue(element.toString(), ContractMethod.class);
//add a constructor info in methods.
methods.put("constructor", method);
this.constructor = method;
}
}
//if the constructor is not existed in ABI, creates a dummy instance and adds it.
if(methods.get("constructor") == null) {
ContractMethod method = new ContractMethod();
method.setType(ContractMethod.TYPE_CONSTRUCTOR);
method.setInputs(new ArrayList<ContractIOType>());
methods.put("constructor", method);
this.constructor = method;
}
}
}