diff --git a/README.md b/README.md index 506ebcb9..07e692eb 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ cita-sdk-java, originally adapted from Ethereum web3j, is a Java library for working with Smart Contract and integrating with clients on CITA. +Here is [detail documentation](docs/index.md) + ## Features - Complete implementation of CITA JSON-RPC API over HTTP. @@ -144,6 +146,9 @@ Please find complete code in [TokenAccountExample](https://github.com/cryptape/c ## 简介 cita-sdk-java 是对以太坊 Web3j 进行改写,适配 CITA 的一个 Java 开发包。cita-sdk-java 集成了与 CITA 客户端交互的功能,可以用来对 CITA 发送交易,系统配置,信息查询。 + +开发请参考[详细文档](docs/index.md) + ## 特性 - 通过 HTTP 协议,实现了 CITA 所定义的所有 JSON-RPC 方法。 - 可以通过 Solidity 智能合约生成该合约的 Java 类。这个智能合约的 Java 类作为 java 对智能合约的包裹层,可以使开发和通过 java 方便地对智能合约进行部署和合约方法的调用(支持Solidity 和 Truffle 的格式)。 diff --git a/core/src/main/java/com/cryptape/cita/protocol/account/Account.java b/core/src/main/java/com/cryptape/cita/protocol/account/Account.java index c6762fd7..de3592ab 100644 --- a/core/src/main/java/com/cryptape/cita/protocol/account/Account.java +++ b/core/src/main/java/com/cryptape/cita/protocol/account/Account.java @@ -59,7 +59,7 @@ public AppSendTransaction deploy( version, chainId, value); } - public Future deployAsync( + public Flowable deployAsync( File contractFile, String nonce, long quota, int version, BigInteger chainId, String value) throws IOException, InterruptedException, CompiledContract.ContractCompileError { @@ -70,7 +70,6 @@ public Future deployAsync( version, chainId, value); } - // eth_call: nonce and quota is null // sendTransaction: nonce and quota is necessary public Object callContract( String contractAddress, String funcName, @@ -101,7 +100,7 @@ public Object callContract( Function func; if (functionAbi.isConstant()) { - // eth_call + // call List retsType = new ArrayList<>(); List> retsTypeRef = new ArrayList<>(); List outputs = functionAbi.getOutputs(); @@ -111,7 +110,7 @@ public Object callContract( retsTypeRef.add(retType.getTypeReference()); } func = new Function(functionAbi.getName(), params, retsTypeRef); - return ethCall(contractAddress, func, retsType); + return appCall(contractAddress, func, retsType); } else { // send_transaction func = new Function(functionAbi.getName(), params, Collections.emptyList()); @@ -120,7 +119,7 @@ public Object callContract( } } - public Object ethCall(String contractAddress, Function func, List retsType) + public Object appCall(String contractAddress, Function func, List retsType) throws IOException { String data = FunctionEncoder.encode(func); AppCall call = this.service.appCall(new Call(this.transactionManager.getFromAddress(), diff --git a/core/src/main/java/com/cryptape/cita/tx/RawTransactionManager.java b/core/src/main/java/com/cryptape/cita/tx/RawTransactionManager.java index 5c427a19..90ecd99f 100644 --- a/core/src/main/java/com/cryptape/cita/tx/RawTransactionManager.java +++ b/core/src/main/java/com/cryptape/cita/tx/RawTransactionManager.java @@ -14,6 +14,7 @@ import com.cryptape.cita.protocol.core.methods.response.AppGetTransactionCount; import com.cryptape.cita.protocol.core.methods.response.AppSendTransaction; import com.cryptape.cita.utils.Numeric; +import io.reactivex.Flowable; public class RawTransactionManager extends TransactionManager { @@ -108,7 +109,7 @@ public AppSendTransaction sendTransaction( return citaj.appSendRawTransaction(rawTx).send(); } - public Future sendTransactionAsync( + public Flowable sendTransactionAsync( String to, String data, long quota, String nonce, long validUntilBlock, int version, BigInteger chainId, String value) throws IOException { @@ -124,7 +125,7 @@ public Future sendTransactionAsync( } else if (this.signature != null) { rawTx = transaction.sign(this.signature); } - return citaj.appSendRawTransaction(rawTx).sendAsync(); + return citaj.appSendRawTransaction(rawTx).flowable(); } @Override diff --git a/core/src/main/java/com/cryptape/cita/tx/TransactionManager.java b/core/src/main/java/com/cryptape/cita/tx/TransactionManager.java index 110ed81b..280a7c5d 100644 --- a/core/src/main/java/com/cryptape/cita/tx/TransactionManager.java +++ b/core/src/main/java/com/cryptape/cita/tx/TransactionManager.java @@ -12,11 +12,10 @@ import com.cryptape.cita.protocol.exceptions.TransactionException; /** - * Transaction manager abstraction for executing transactions with Ethereum client via + * Transaction manager abstraction for executing transactions with CITA client via * various mechanisms. */ -///TODO this class includes ethereum methods: remove them later public abstract class TransactionManager { public static final int DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH = 40; diff --git a/docs/account.md b/docs/account.md new file mode 100644 index 00000000..27b052dd --- /dev/null +++ b/docs/account.md @@ -0,0 +1,94 @@ +## Account + +Account 封装了 TransactionManager,通过 CITAj 和账户的私钥进行实例化。 + +Account 使用了 CompiledContract 类,可以直接读取 Solidity 合约文件,生成 ABI 和 BIN 文件以供 TransactionManager 使用。 + +* [New account](#new-account) +* [Deploy contract](#deploy-contract) +* [Call Contract](#call-contract) + +### New account + +**方法名** + +`Account(String privateKey, CITAj service)` + +实例化Account对象。 + +**参数** + +* privateKey - 发送交易地址的私钥 +* service - CITAj 实例 + +**返回值** + +Account + +**示例** + +``` +String privateKey = "{private key}"; +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +Account account = new Account(privateKey, service); +``` +### Deploy contract + +**方法名** + +`AppSendTransaction deploy(File contractFile, BigInteger nonce, long quota, int version, int chainId, String value)` + +部署合约。 + +**参数** + +* contractFile - solidity智能合约文件 +* nonce - 随机数用于防止重放攻击 +* quota - 用户支付矿工的费用 +* version - 链的版本信息 +* chainId - 链Id +* value - 交易中原生token的数量 + +**返回值** + +AppSendTransaction + +**示例** +``` +String privateKey = "{private key}"; +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +Account account = new Account(privateKey, service); +AppSendTransaction appSendTransaction = account.deploy(new File(path), randomNonce(), quota, version, chainId, value); +``` + +### Call contract + +**方法名** + +`Object callContract(String contractAddress, String funcName, BigInteger nonce, long quota, int version, int chainId, String value, Object... args)` + +调用合约方法,根据Abi中对方法的定义判断使用sendRawTransaction还是app_call。 + +**参数** + +* contractAddress - 合约地址 +* funcName - 合约方法名称 +* nonce - 随机数用于防止重放攻击 +* quota - 用户支付矿工的费用 +* version - 链的版本信息 +* chainId - 链Id +* value - 交易中原生token的数量 +* args - 合约方法参数 + +**返回值** + +Object + +**示例** + +``` +String privateKey = "{private key}"; +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +Account account = new Account(privateKey, service); +AppSendTransaction appSendTransaction = (AppSendTransaction) account.callContract(contractAddress, transfer, randomNonce(), quota, version, chainId, value, toAddress, amount); +``` \ No newline at end of file diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 00000000..2ef4a712 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,19 @@ +# cita-sdk-java API 文档 + +## 概述: + +cita-sdk-java 是对于 CITA 进行交互的 Java SDK 包,cita-sdk-java 使用 JSON-RPC 协议对 CITA 网络节点发送包含方法名和参数的请求,请求可以是转账或者合约的部署和调用。 + +在 cita-sdk-java 中,一共有三种方式实现和链上节点的交互: + +第一种: 通过 cita-sdk-java 定义的方法手动输入包括合约的Abi和Bin来进行操作。 + +第二种: 通过codeGen工具将Solidity合约生成Java类,该Java类继承自Contract类并包含合约中定义的所有方法。 +相比于第一种方法的手动拼接参数,这个方式可以不用生成Abi和Bin文件而直接通过Java类中的方法来进行合约的部署和调用。 + +第三种: 通过封装在Account中的方法来构建并发送交易,Account会实例化TransactionManager,TransactionManager 提供了异步和同步方式对合约进行部署和调用。 + +## 目录: +1. [JSON-RPC](jsonrpc.md) +2. [Transaction](transaction.md) +3. [Account](account.md) \ No newline at end of file diff --git a/docs/jsonrpc.md b/docs/jsonrpc.md new file mode 100644 index 00000000..27b729bf --- /dev/null +++ b/docs/jsonrpc.md @@ -0,0 +1,544 @@ +## JSONRPC + +CITAj 接口继承了 CITA 和 CITAjRx 两个接口,CITAj 的实现类(比如JsonRpc2_0CITAj),提供了方法以发送交易的方式对合约进行部署和函数调用。 + +* [Build CITAj](#build-citaj) +* [netPeer](#netpeer) +* [appMetaData](#appmetadata) +* [appBlockNumber](#appblocknumber) +* [appGetBalance](#appgetbalance) +* [appGetAbi](#appgetabi) +* [appGetTransactionCount](#appgettransactioncount) +* [appGetCode](#appgetcode) +* [appSendRawTransaction](#appsendrawtransaction) +* [appCall](#appcall) +* [appGetBlockByHash](#appgetblockbyhash) +* [appGetBlockByNumber](#appgetblockbynumber) +* [appGetTransactionByHash](#appgettransactionbyhash) +* [appGetTransactionReceipt](#appgettransactionreceipt) +* [appNewBlockFilter](#appnewblockfilter) +* [appBlockHashObservable](#appblockhashobservable) +* [appNewFilter](#appnewfilter) +* [appUninstallFilter](#appuninstallfilter) +* [appGetFilterChanges](#appgetfilterchanges) +* [appGetFilterLogs](#appgetfilterlogs) +* [appLogObservable](#applogobservable) + +### Build CITAj + +**方法名** + +`CITAj build (CITAjService citaj)` +根据 CITAjService 类型实例化 CITAj。 + +**参数** + +citaj - CITAjService 实例 + +**返回值** + +CITAj 实例 + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +``` + +### netPeer + +**方法名** + +`Request netPeer()` +获取当前连接节点数。 + +**参数** + +无 + +**返回值** + +Request + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +NetPeerCount netPeerCount = service.netPeerCount().send(); +BigInteger peerCount = netPeerCount.getQuantity(); +``` + +### appMetaData + +**方法名** + +`Request appMetaData(DefaultBlockParameter defaultBlockParameter)` +获取指定块高的元数据。 + +**参数** + +* defaultBlockParameter - 块高度的接口:数字或者关键字 + +**返回值** + +Request + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +DefaultBlockParameter defaultParam = DefaultBlockParameter.valueOf("latest"); +AppMetaDataResult result = service.appMetaData(defaultParam).send(); +int chainId = result.chainId; +String chainName = result.chainName; +String genesisTS = result.genesisTimestamp; +``` +### appBlockNumber + +**方法名** + +`Request appBlockNumber()` +获取当前块高度。 + +**参数** + +无 + +**返回值** + +Request + +**示例** + +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +AppBlockNumber result = service.appBlockNumber().send(); +BigInteger blockNumber = result.getBlockNumber(); +``` +### appGetBalance + +**方法名** + +`Request appGetBalance(String address, DefaultBlockParameter defaultBlockParameter))` + +获取地址余额。 + +**参数** + +* address - 所要查询的地址 +* defaultBlockParameter - 块高度的接口:数字或者关键字 + +**返回值** + +Request + +**示例** + +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +string addr = "{hex cita address starting with 0x}"; +DefaultBlockParameter defaultBlockParameter = DefaultBlockParamter.valueOf("latest"); +AppGetBalance getBalance = service.appGetBalance(addr, defaultBlockParamter).send(); +BigInteger balance = getBalance.getBalance(); +``` + +### appGetAbi + +**方法名** + +`Request appGetAbi(String contractAddress, DefaultBlockParameter defaultBlockParameter)` +获取合约的Abi。 + +**参数** + +* address - 所要查询Abi的合约地址 +* defaultBlockParameter - 块高度的接口:数字或者关键字 + +**返回值** + +Request + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +string addr = "{hex cita address starting with 0x}"; +DefaultBlockParameter defaultBlockParameter = DefaultBlockParamter.valueOf("latest"); +AppGetAbi getAbi = service.appGetAbi(addr, defaultBlockParamter).send(); +String abi = getAbi.getAbi(); +``` + +### appGetTransactionCount + +**方法名** + +`Request appGetTransactionCount(String address, DefaultBlockParameter defaultBlockParameter)` + +获取账户发送合约数量。 + +**参数** + +* address - 所要查询的地址 +* defaultBlockParameter - 块高度的接口:数字或者关键字 + +**返回值** + +Request + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +string addr = "{hex cita address starting with 0x}"; +DefaultBlockParameter defaultBlockParameter = DefaultBlockParamter.valueOf("latest"); +AppGetTransactionCount getTransactionCount = service.appGetTransactionCount(addr, defaultBlockParamter).send(); +BigInteger txCount = getTransactionCount.getTransactionCount(); +``` + +### appGetCode + +**方法名** + +`Request appGetCode(String address, DefaultBlockParameter defaultBlockParameter)` + +获取合约代码。 + +**参数** + +* address - 所要查询的地址 +* defaultBlockParameter - 块高度的接口:数字或者关键字 + +**返回值** + +Request + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +string addr = "{hex cita address starting with 0x}"; +DefaultBlockParameter defaultBlockParameter = DefaultBlockParamter.valueOf("latest"); +AppGetCode getCode = service.appGetCode(addr, defaultBlockParamter).send(); +Sring code = getCode.getCode(); +``` + +### appSendRawTransaction + +**方法名** + +`Request appSendRawTransaction(String signedTransactionData)` + +向区块链节点发送序列化交易。 + +**参数** + +* signedTransaction - 经过签名的交易数据 + +**返回值** + +Request + +**示例** + +``` +//create a signed transaction +Transaction tx = Transaction.createContractTransaction(BigInteger.valueOf(nonce), this.config.getQuota(), this.currentHeight + 88, 0, chainId, value, this.config.getCode()); +tx.sign(this.config.getPrivateKey(), false, false); + +//instantiate a CITAj and send the transaction +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +AppSendTransaction sendTransaction = service.appSendRawTransaction(tx).send(); + +//get hash of the transaction +String hash = sendTransaction.getHash(); +``` + +### appCall + +**方法名** + +`Request appCall(Call call, DefaultBlockParameter defaultBlockParameter)` + +调用合约接口。 + +**参数** + +* call - 合约方法的call的封装 +* defaultBlockParameter - 块高度的接口:数字或者关键字 + +**返回值** + +Request + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +Call call = new Call(from, to, data); +AppCall appCall = service.appCall(call, DefaultBlockParameter.valueOf("latest")).send(); +String result = call.getValue(); +``` + +### appGetBlockByHash + +**方法名** + +`Request appGetBlockByHash(String blockHash, boolean returnFullTransactionObjects)` + +根据块的哈希值查询块信息。 + +**参数** + +* blockHash - 块高度的接口:数字或者关键字 +* returnFullTransactionObjects - 是否返回交易信息 (True: 返回详细交易列表| False: 只返回交易hash) + +**返回值** + +Request + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +String blockHash = "{block hash to search}"; +AppBlock appBlock = service.appGetBlockByHash(blockHash, false).send(); +``` + +### appGetBlockByNumber + +**方法名** + +`Request appGetBlockByNumber(DefaultBlockParameter defaultBlockParameter, boolean returnFullTransactionObjects)` + +根据块高度查询块信息。 + +**参数** + +* defaultBlockParameter - 块高度的接口:数字或者关键字 +* returnFullTransactionObjects - 是否返回交易信息 (True: 返回详细交易列表| False: 只返回交易hash) + +**返回值** + +Request + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +AppBlock appBlock = service.appGetBlockByHash(DefaultBlockParameter.valueOf("latest"), false).send(); +``` + + +### appGetTransactionByHash + +**方法名** + +`Request appGetTransactionByHash(String transactionHash)` + +根据哈希查询交易信息。 + +**参数** + +* transactionHash - 交易哈希 + +**返回值** + +Request + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +String txHash = "{hash of transactino to be searched}"; +AppTransaction responseTx = service.appGetTransactionByHash(txHash).send(); +``` +### appGetTransactionReceipt + +**方法名** + +`Request appGetTransactionReceipt(String transactionHash)` +根据交易哈希查询交易回执。 + +**参数** + +* transactionHash - 交易哈希 + +**返回值** + +Request + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +String txHash = "{hash of transactino to be searched}"; +AppGetTransactionReceipt txReceipt = service.appGetTransactionReceipt(txHash).send(); +``` + +### appNewBlockFilter + +**方法名** + +`Request appNewBlockFilter()` + +创建一个新的块过滤器,当有新的块写入时通知。 + +**参数** + +无 + +**返回值** + +Request + +**示例** + +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +AppFilter appFilter = service.appNewBlockFilter().send(); +BigInteger filterId = appFilter.getFilterId(); +``` + +### appBlockHashObservable + +**方法名** + +`Observable appBlockHashObservable()` +新建一个Block Filter来监听新增块的哈希,返回一个Observable,可以用交互的形式来监听块高的变化。 + +**参数** + +无 + +**返回值** + +Observable + +**示例** +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +Observable blockFitlerObservable = service.appBlockHashObservable(); +AppLog logs = service.appGetFilterLogs(filterId).send(); +blockFitlerObservable.subscribe(block -> { + System.out.println(block.toString()); +}); +``` + +### appNewFilter + +**方法名** + +`Request appNewFilter(com.cryptape.cita.protocol.core.methods.request.AppFilter appFilter)` + +创建一个新的Event过滤器以用来监听合约中的Event。 + +**参数** + +* appFilter - 针对于 CITA 智能合约event的过滤器(定义在Request中的appFilter) + +**返回值** + +Request + +**示例** + +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +com.cryptape.cita.protocol.core.methods.request.AppFilter appFilter = new AppFilter(fromBlock, toBlock, addresses); +AppFilter appFilter = service.appNewFilter(txHash).send(); +BigInteger filterId = appFilter.getFilterId(); +``` + +### appUninstallFilter + +**方法名** + +`Request appUninstallFilter(BigInteger filterId)` + +移除已部署的过滤器。 + +**参数** + +* filterId - 过滤器Id + +**返回值** + +Request + +**示例** + +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +BigInteger filterId = {your filter Id }; +AppUninstallFilter uninstallFilter = service.appUninstallFilter(filterId).send(); +``` + +### appGetFilterChanges + +**方法名** + +`Request appGetFilterChanges(BigInteger filterId)` + +根据过滤器Id查询log,返回上一次查询之后的所有log。 + +**参数** + +* filterId - 过滤器Id + +**返回值** + +Request + +**示例** + +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +BigInteger filterId = {your filter Id }; +AppLog logs = service.appGetFilterChanges(filterId).send(); +List results = logs.getLogs(); +``` + +### appGetFilterLogs + +**方法名** + +`Request appGetFilterLogs(BigInteger filterId)` + +根据过滤器Id查询log,返回符合输入filter Id的所有log。 + +**参数** + +* filterId - 过滤器Id + +**返回值** + +Request + +**示例** + +``` +CITAj service = CITAj.build(new HttpService("http://127.0.0.1")); +BigInteger filterId = {your filter Id }; +AppLog logs = service.appGetFilterLogs(filterId).send(); +List results = logs.getLogs(); +``` + +### appLogObservable + +**方法名** + +`Observable appLogObservable(AppFilter appFilter)` + +根据AppFilter来安装一个新的Filter用以获取历史log和监听新的Log,返回一个Observable以交互的模式监听Log。 + +**参数** + +* AppFilter - 过滤器可以由`appNewFilter`来新建 + +**返回值** + +Observable + +**示例** +``` +Observable appLogObservable = service.appLogObservable(filter); +Observable reponse = appLogObservable.map( + (log) -> { + EventValues eventValues = staticExtractEventParameters(event, (Log)log); + return (String) eventValues.getIndexedValues().get(0).getValue();; + } +); + +reponse.subscribe(x -> { + System.out.println(x); +}); +``` \ No newline at end of file diff --git a/docs/transaction.md b/docs/transaction.md new file mode 100644 index 00000000..4a45ad89 --- /dev/null +++ b/docs/transaction.md @@ -0,0 +1,229 @@ +## Transaction + +Transction定义在core.request中,用于将交易数据封装并且签名(如果需要),交易数据或者签名后的交易数据被appCall()或者appSendRawTransaction()所使用进行合约的调用或者部署。 + +* [Transaction](#transaction) +* [createContractTransaction](#createcontracttransaction) +* [createFunctionCallTransaction](#createfunctioncalltransaction) +* [TransactionManager](#transactionmanager) +* [sendTransaction](#sendtransaction) +* [sendTransactionAsync](#sendtransactionasync) + +### Transaction + +**方法名** + +`Transaction(String to, BigInteger nonce, long quota, long valid_until_block, int version, int chainId, String value, String data)` + +根据参数新建一个交易。 + +**参数** + +* to - 交易将要的发送地址 +* nonce - 随机数用于防止重放攻击 +* quota - 用户支付矿工的费用 +* valid_until_block - 超时机制,超过设定块高取消交易 +* version - 链的版本信息 +* chainId - 链Id +* value - 交易中原生token的数量 +* data - 编码后交易数据(abi) + +**返回值** + +Transaction实例 + +**示例** +``` +CITAj service = CITAj.build(new HttpService("127.0.0.1")); +String to = "{address to which the tx is sent}"; +BigInteger nonce = BigInteger.valueOf(Math.abs(this.random.nextLong())); +long quota = 9999; +long valid_until_block = service.appBlockNumber().send().getBlockNumber() + 88; +int version = 0; +in chainId = 1; +String value = "100000000"; +String init = "{encoded abi}"; +Transaction tx = Transction.createContractTransaction(nonce, quota, valid_until_block, version, chainId, value, init); +``` +### createContractTransaction + +**方法名** + +`createContractTransaction(BigInteger nonce, long quota, long valid_until_block, int version, int chainId, String value, String init)` + +根据参数新建一个部署合约的交易。 + +**参数** + +* nonce - 随机数用于防止重放攻击 +* quota - 用户支付矿工的费用 +* valid_until_block - 超时机制,超过设定块高取消交易 +* version - 链的版本信息 +* chainId - 链Id +* value - 交易中原生token的数量 +* init - 合约编码后数据(abi) + +**返回值** + +Transaction实例 + +**示例** +``` +//create new CITAj service +CITAj service = CITAj.build(new HttpService("127.0.0.1")); + +//settings initiation +BigInteger nonce = BigInteger.valueOf(Math.abs(this.random.nextLong())); +long quota = 9999; +long valid_until_block = service.appBlockNumber().send().getBlockNumber() + 88; +int version = 0; +in chainId = 1; +String value = "100000000"; +String init = "{encoded abi}"; + +//construct transaction +Transaction txToDeployContract = Transction.createContractTransaction(nonce, quota, valid_until_block, version, chainId, value, init); +String signedTx = txToDeployContract.sign(this.config.getPrivateKey(), false, false); +AppSendTransaction appSendTx = service.sendRawTransaction(signedTx); +``` + +### createFunctionCallTransaction + +**方法名** + +`createFunctionCallTransaction(String to, BigInteger nonce, long quota, long valid_until_block, int version, int chainId, String value, String data)` + +根据参数新建一个合约调用的交易。 + +**参数** + +* to - 交易将要的发送地址 +* nonce - 随机数用于防止重放攻击 +* quota - 用户支付矿工的费用 +* valid_until_block - 超时机制,超过设定块高取消交易 +* version - 链的版本信息 +* chainId - 链Id +* value - 交易中原生token的数量 +* data - 编码后交易数据(abi) + +**返回值** + +Transaction实例 + +**示例** + +``` +//create new CITAj service +CITAj service = CITAj.build(new HttpService("127.0.0.1")); + +//settings initiation +String to = "{smart contract address}"; +BigInteger nonce = BigInteger.valueOf(Math.abs(this.random.nextLong())); +long quota = 9999; +long valid_until_block = service.appBlockNumber().send().getBlockNumber() + 88; +int version = 0; +in chainId = 1; +String value = "100000000"; +String init = "{encoded abi}"; + +//construct transaction +Transaction txToDeployContract = Transction.createFunctionCallTransaction(to, nonce, quota, valid_until_block, version, chainId, value, init); +String signedTx = txToDeployContract.sign(this.config.getPrivateKey(), false, false); +AppSendTransaction appSendTx = service.sendRawTransaction(signedTx); +``` +### TransactionManager + +**方法名** + +`TransactionManager(CITAj citaj, Credentials credentials)` + +**参数** + +* citaj - CITAj实例 +* credentials - 发起交易账户的credential + +**返回值** + +TransactionManager实例 + +**示例** +``` +Credentials credentials = Credentials.create(privateKey); +CITAj service = CITAj.build(new HttpService("127.0.0.1")); +TransactionManager transactionManager = new TransactionManager(service, credentials); +``` + +### sendTransaction + +**方法名** + +`AppSendTransaction sendTransaction(String to, String data, long quota, BigInteger nonce, long validUntilBlock, int version, int chainId, String value)` + +通过TransactionManager发送交易。 + +**参数** + +* to - 交易将要的发送地址 +* data - 编码后交易数据(abi) +* quota - 用户支付矿工的费用 +* nonce - 随机数用于防止重放攻击 +* valid_until_block - 超时机制,超过设定块高取消交易 +* version - 链的版本信息 +* chainId - 链Id +* value - 交易中原生token的数量 + +**返回值** + +AppSendTransaction + +**示例** + +``` +CitaTransactionManager transactionManager = new CitaTransactionManager(service, credentials); +String to = "{address to which the contract is sent}"; +String contractBin = "{contract bin or function call bin}"; +BigInteger quota = 99999; +BigInteger nonce = BigInteger.valueOf(Math.abs(this.random.nextLong())); +long valid_until_block = service.appBlockNumber().send().getBlockNumber() + 88; +int version = 0; +int chainId = 1; +String value = "0"; +AppSendTransaction appSendTransaction = citaTransactionManager.sendTransaction(to, contractBin, quota, nonce, valid_until_block, BigInteger.valueOf(version), chainId, value); +``` + +### sendTransactionAsync + +**方法名** + +`Flowable sendTransactionAsync(String to, String data, long quota, BigInteger nonce, long validUntilBlock, int version, int chainId, String value)` +通过TransactionManager发送交易。 + +**参数** + +* to - 交易将要的发送地址 +* data - 编码后交易数据(abi) +* quota - 用户支付矿工的费用 +* nonce - 随机数用于防止重放攻击 +* valid_until_block - 超时机制,超过设定块高取消交易 +* version - 链的版本信息 +* chainId - 链Id +* value - 交易中原生token的数量 + +**返回值** + +AppSendTransaction + +**示例** + +``` +TransactionManager transactionManager = new TransactionManager(service, credentials); +String to = "{address to which the contract is sent}"; +String contractBin = "{contract bin or function call bin}"; +BigInteger quota = 99999; +BigInteger nonce = BigInteger.valueOf(Math.abs(this.random.nextLong())); +long valid_until_block = service.appBlockNumber().send().getBlockNumber() + 88; +int version = 0; +int chainId = 1; +String value = "0"; +Flowable appSendTransaction = transactionManager.sendTransaction(to, contractBin, quota, nonce, valid_until_block, BigInteger.valueOf(version), chainId, value); +``` \ No newline at end of file