22
33import com .google .protobuf .InvalidProtocolBufferException ;
44import com .google .protobuf .Message ;
5+ import io .grpc .ClientInterceptor ;
56import org .tron .trident .abi .FunctionEncoder ;
67import org .tron .trident .abi .datatypes .Function ;
78import org .tron .trident .api .GrpcAPI ;
4041import org .tron .trident .proto .Contract .ParticipateAssetIssueContract ;
4142import org .tron .trident .proto .Contract .UnfreezeAssetContract ;
4243import org .tron .trident .proto .Contract .AccountPermissionUpdateContract ;
44+ import org .tron .trident .proto .Response ;
4345import org .tron .trident .proto .Response .TransactionExtention ;
4446import org .tron .trident .proto .Response .TransactionReturn ;
4547import org .tron .trident .proto .Response .NodeInfo ;
6264import org .tron .trident .proto .Contract .ProposalCreateContract ;
6365import org .tron .trident .proto .Contract .ProposalApproveContract ;
6466import org .tron .trident .proto .Contract .ProposalDeleteContract ;
67+ import org .tron .trident .proto .Contract .CancelAllUnfreezeV2Contract ;
6568import org .tron .trident .utils .Base58Check ;
6669
6770import org .tron .trident .proto .Contract .FreezeBalanceV2Contract ;
@@ -143,6 +146,18 @@ public ApiWrapper(String grpcEndpoint, String grpcEndpointSolidity, String hexPr
143146 keyPair = new KeyPair (hexPrivateKey );
144147 }
145148
149+ public ApiWrapper (String grpcEndpoint , String grpcEndpointSolidity , String hexPrivateKey , List <ClientInterceptor > clientInterceptors ) {
150+ channel = ManagedChannelBuilder .forTarget (grpcEndpoint )
151+ .intercept (clientInterceptors )
152+ .usePlaintext ()
153+ .build ();
154+ channelSolidity = ManagedChannelBuilder .forTarget (grpcEndpointSolidity ).usePlaintext ().build ();
155+ blockingStub = WalletGrpc .newBlockingStub (channel );
156+ blockingStubSolidity = WalletSolidityGrpc .newBlockingStub (channelSolidity );
157+ keyPair = new KeyPair (hexPrivateKey );
158+ }
159+
160+
146161 public void close () {
147162 channel .shutdown ();
148163 channelSolidity .shutdown ();
@@ -567,6 +582,25 @@ public TransactionExtention unfreezeBalanceV2(String ownerAddress, long unfreeze
567582 }
568583
569584
585+ /**
586+ * Stake2.0 API
587+ * Cancel all the unstaking transactions in the waiting period
588+ * @param ownerAddress owner address
589+ * @return TransactionExtention
590+ * @throws IllegalException if fail to delegate resource
591+ */
592+ public TransactionExtention cancelAllUnfreezeV2 (String ownerAddress ) throws IllegalException {
593+
594+ CancelAllUnfreezeV2Contract cancelUnfreezeV2Contract =
595+ CancelAllUnfreezeV2Contract .newBuilder ()
596+ .setOwnerAddress (parseAddress (ownerAddress ))
597+ .build ();
598+
599+ TransactionExtention txnExt = createTransactionExtention (cancelUnfreezeV2Contract , Transaction .Contract .ContractType .CancelAllUnfreezeV2Contract );
600+
601+ return txnExt ;
602+ }
603+
570604 /**
571605 * Stake2.0 API
572606 * Delegate bandwidth or energy resources to other accounts
@@ -597,6 +631,41 @@ public TransactionExtention delegateResource(String ownerAddress, long balance,
597631 return txnExt ;
598632 }
599633
634+ /**
635+ * Stake2.0 API
636+ * Delegate bandwidth or energy resources to other accounts
637+ * @param ownerAddress owner address
638+ * @param balance Amount of TRX staked for resources to be delegated, unit is sun
639+ * @param resourceCode Resource type, can be 0("BANDWIDTH") or 1("ENERGY")
640+ * @param receiverAddress Resource receiver address
641+ * @param lock Whether it is locked, if it is set to true,
642+ * the delegated resources cannot be undelegated within 3 days.
643+ * When the lock time is not over, if the owner delegates the same type of resources using the lock to the same address,
644+ * the lock time will be reset to 3 days
645+ * @param lockPeriod The lockup period, unit is blocks, data type is int256,
646+ * It indicates how many blocks the resource delegating is locked before it can be undelegated.
647+ * @return TransactionExtention
648+ * @throws IllegalException if fail to delegate resource
649+ */
650+ public TransactionExtention delegateResourceV2 (String ownerAddress , long balance , int resourceCode ,String receiverAddress ,boolean lock , long lockPeriod ) throws IllegalException {
651+ ByteString rawOwner = parseAddress (ownerAddress );
652+ ByteString rawReciever = parseAddress (receiverAddress );
653+ DelegateResourceContract delegateResourceContract =
654+ DelegateResourceContract .newBuilder ()
655+ .setOwnerAddress (rawOwner )
656+ .setBalance (balance )
657+ .setReceiverAddress (rawReciever )
658+ .setLock (lock )
659+ .setResourceValue (resourceCode )
660+ .build ();
661+ if (lock ) {
662+ delegateResourceContract = delegateResourceContract .toBuilder ().setLockPeriod (lockPeriod ).build ();
663+ }
664+ TransactionExtention txnExt = createTransactionExtention (delegateResourceContract , Transaction .Contract .ContractType .DelegateResourceContract );
665+
666+ return txnExt ;
667+ }
668+
600669 /**
601670 * Stake2.0 API
602671 * unDelegate resource
@@ -1941,5 +2010,111 @@ public Block getBlockById(String blockID) {
19412010 }
19422011
19432012
2013+ /**
2014+ * Estimate the energy required for the successful execution of smart contract transactions
2015+ * This API is closed by default in tron node.
2016+ * To open this interface, the two coniguration items vm.estimateEnergy and vm.supportConstant must be enabled in the node configuration file at the same time.
2017+ * @param ownerAddr Owner address that triggers the contract. If visible=true, use base58check format, otherwise use hex format.
2018+ * For constant call you can use the all-zero address.
2019+ * @param contractAddr Smart contract address.
2020+ * @param function contract function
2021+ * @return EstimateEnergyMessage. Estimated energy to run the contract
2022+ */
2023+ public Response .EstimateEnergyMessage estimateEnergy (String ownerAddr , String contractAddr , Function function ) {
2024+ Contract cntr = getContract (contractAddr );
2025+
2026+ cntr .setOwnerAddr (parseAddress (ownerAddr ));
2027+ String encodedHex = FunctionEncoder .encode (function );
2028+ TriggerSmartContract trigger =
2029+ TriggerSmartContract .newBuilder ()
2030+ .setOwnerAddress (cntr .getOwnerAddr ())
2031+ .setContractAddress (cntr .getCntrAddr ())
2032+ .setData (parseHex (encodedHex ))
2033+ .build ();
2034+ Response .EstimateEnergyMessage estimateEnergyMessage = blockingStub .estimateEnergy (trigger );
2035+
2036+ return estimateEnergyMessage ;
2037+ }
2038+
2039+ /**
2040+ * Estimate the energy required for the successful execution of smart contract transactions
2041+ * This API is closed by default in tron node. To open this interface, the two configuration items vm.estimateEnergy and vm.supportConstant must be enabled in the node configuration file at the same time.
2042+ * @param ownerAddr Owner address that triggers the contract. If visible=true, use base58check format, otherwise use hex format.
2043+ * For constant call you can use the all-zero address.
2044+ * @param contractAddr Smart contract address.
2045+ * @param callData The data passed along with a transaction that allows us to interact with smart contracts.
2046+ * @return EstimateEnergyMessage. Estimated energy to run the contract
2047+ */
2048+ public Response .EstimateEnergyMessage estimateEnergyV2 (String ownerAddr , String contractAddr , String callData ) {
2049+ Contract cntr = getContract (contractAddr );
2050+
2051+ cntr .setOwnerAddr (parseAddress (ownerAddr ));
2052+ TriggerSmartContract trigger =
2053+ TriggerSmartContract .newBuilder ()
2054+ .setOwnerAddress (cntr .getOwnerAddr ())
2055+ .setContractAddress (cntr .getCntrAddr ())
2056+ .setData (ByteString .copyFrom (ByteArray .fromHexString (callData )))
2057+ .build ();
2058+ Response .EstimateEnergyMessage estimateEnergyMessage = blockingStub .estimateEnergy (trigger );
2059+
2060+ return estimateEnergyMessage ;
2061+ }
2062+
2063+ /**
2064+ * make a trigger call. Trigger call consumes energy and bandwidth.
2065+ * @param ownerAddr the current caller
2066+ * @param contractAddr smart contract address
2067+ * @param callData The data passed along with a transaction that allows us to interact with smart contracts.
2068+ * @return transaction builder. TransactionExtention detail.
2069+ */
2070+ public TransactionBuilder triggerCallV2 (String ownerAddr , String contractAddr , String callData ) {
2071+ Contract cntr = getContract (contractAddr );
2072+
2073+ TransactionExtention txnExt = callWithoutBroadcastV2 (ownerAddr , cntr , callData );
2074+
2075+ return new TransactionBuilder (txnExt .getTransaction ());
2076+ }
2077+
2078+
2079+ /**
2080+ * call function without signature and broadcasting
2081+ * @param ownerAddr the caller
2082+ * @param cntr the contract
2083+ * @param callData The data passed along with a transaction that allows us to interact with smart contracts.
2084+ * @return TransactionExtention
2085+ */
2086+ private TransactionExtention callWithoutBroadcastV2 (String ownerAddr , Contract cntr , String callData ) {
2087+ cntr .setOwnerAddr (parseAddress (ownerAddr ));
2088+ // Make a TriggerSmartContract contract
2089+ TriggerSmartContract trigger =
2090+ TriggerSmartContract .newBuilder ()
2091+ .setOwnerAddress (cntr .getOwnerAddr ())
2092+ .setContractAddress (cntr .getCntrAddr ())
2093+ .setData (ByteString .copyFrom (ByteArray .fromHexString (callData )))
2094+ .build ();
2095+
2096+ // System.out.println("trigger:\n" + trigger);
2097+
2098+ TransactionExtention txnExt = blockingStub .triggerConstantContract (trigger );
2099+ // System.out.println("txn id => " + toHex(txnExt.getTxid().toByteArray()));
2100+
2101+ return txnExt ;
2102+ }
2103+
2104+ /**
2105+ * make a constant call - no broadcasting
2106+ * @param ownerAddr the current caller.
2107+ * @param contractAddr smart contract address.
2108+ * @param callData The data passed along with a transaction that allows us to interact with smart contracts.
2109+ * @return TransactionExtention.
2110+ */
2111+ public TransactionExtention constantCallV2 (String ownerAddr , String contractAddr , String callData ) {
2112+ Contract cntr = getContract (contractAddr );
2113+
2114+ TransactionExtention txnExt = callWithoutBroadcastV2 (ownerAddr , cntr , callData );
2115+
2116+ return txnExt ;
2117+ }
2118+
19442119
19452120}
0 commit comments