cita-sdk-java的使用

# 一、环境准备

  • jdk 1.8 (可通过 java -version 查看版本)
  • IDEA 2020

# 二、创建项目

File->New Project

66a2g1.png (opens new window)

为项目和GroupId命名

66ag3R.png (opens new window)

点击Finish完成项目创建

# 三、配置项目

打开build.gradle文件,在dependencies中添加compile('com.citahub.cita:core:20.2.0'),然后点击大象图标,让其加载 gradle 配置的变化并下载所需的包

66aRjx.png (opens new window)

等待加载完成,项目结构如下:

66acC9.png (opens new window)

# 四、样例代码

src/test/java中新建一个类,命名为TestApplication,粘贴以下代码

import com.citahub.cita.protocol.CITAj;
import com.citahub.cita.protocol.core.DefaultBlockParameter;
import com.citahub.cita.protocol.core.methods.request.Transaction;
import com.citahub.cita.protocol.core.methods.response.*;
import com.citahub.cita.protocol.http.HttpService;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;

/**
 * @ClassName TestApplication
 * @Description TODO
 * @Author 藏剑
 * @Date 2021/3/17 11:20
 * @Version 1.0
 */
public class TestApplication {
    @Test
    void testContext() throws IOException, InterruptedException {
        String privateKey = "0x5a0257a4778057a8a7d97809bd209055b2fbafa654ce7d31ec7191066b9225e6"; // 测试链私钥(有代币)
        // 合约的二进制编码
        String contractCode = "60806040526040518060400160405280600381526020017f78746d000000000000000000000000000000000000000000000000000000000081526020015060006000509080519060200190610055929190610069565b503480156100635760006000fd5b50610182565b8280546100759061011b565b90600052602060002090601f01602090048101928261009757600085556100e3565b82601f106100b057805160ff19168380011785556100e3565b828001600101855582156100e3579182015b828111156100e257825182600050909055916020019190600101906100c2565b5b5090506100f091906100f4565b5090565b6100f9565b8082111561011357600081815060009055506001016100f9565b509056610181565b60006002820490506001821680151561013557607f821691505b6020821081141561014957610148610150565b5b505b919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b565b5b61050c806101916000396000f3fe60806040523480156100115760006000fd5b50600436106100465760003560e01c80635353a2d81461004c578063cc80f6f314610068578063d9e0101e1461008657610046565b60006000fd5b6100666004803603810190610061919061029d565b6100b6565b005b6100706100d4565b60405161007d919061031b565b60405180910390f35b6100a0600480360381019061009b919061029d565b61016e565b6040516100ad919061031b565b60405180910390f35b80600060005090805190602001906100cf92919061017d565b505b50565b6060600060005080546100e6906103fa565b80601f0160208091040260200160405190810160405280929190818152602001828054610112906103fa565b801561015f5780601f106101345761010080835404028352916020019161015f565b820191906000526020600020905b81548152906001019060200180831161014257829003601f168201915b5050505050905061016b565b90565b6060819050610178565b919050565b828054610189906103fa565b90600052602060002090601f0160209004810192826101ab57600085556101f7565b82601f106101c457805160ff19168380011785556101f7565b828001600101855582156101f7579182015b828111156101f657825182600050909055916020019190600101906101d6565b5b5090506102049190610208565b5090565b61020d565b80821115610227576000818150600090555060010161020d565b5090566104d5565b600061024261023d84610365565b61033e565b90508281526020810184848401111561025b5760006000fd5b6102668482856103b5565b505b9392505050565b600082601f83011215156102835760006000fd5b813561029384826020860161022f565b9150505b92915050565b6000602082840312156102b05760006000fd5b600082013567ffffffffffffffff8111156102cb5760006000fd5b6102d78482850161026f565b9150505b92915050565b60006102ec82610397565b6102f681856103a3565b93506103068185602086016103c5565b61030f816104c3565b84019150505b92915050565b6000602082019050818103600083015261033581846102e1565b90505b92915050565b600061034861035a565b9050610354828261042f565b5b919050565b600060405190505b90565b600067ffffffffffffffff8211156103805761037f610492565b5b610389826104c3565b90506020810190505b919050565b6000815190505b919050565b60008282526020820190505b92915050565b828183376000838301525b505050565b60005b838110156103e45780820151818401525b6020810190506103c8565b838111156103f3576000848401525b505b505050565b60006002820490506001821680151561041457607f821691505b6020821081141561042857610427610461565b5b505b919050565b610438826104c3565b810181811067ffffffffffffffff8211171561045757610456610492565b5b80604052505b5050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b565b6000601f19601f83011690505b919050565bfea2646970667358221220216651c6cb1eef197c5f43d0f4f2e88c1cbce889b0de4209bdf570845116c56e64736f6c63430008020033";
        CITAj service = CITAj.build(new HttpService("https://testnet.citahub.com")); // 与测试链建立连接

        // 获取当前块高度
        AppBlockNumber blockNumberResult = service.appBlockNumber().send();
        BigInteger blockNumber = blockNumberResult.getBlockNumber();
        System.out.println("当前块高度:"+blockNumber);

        // 获取测试链的节点数
        NetPeerCount netPeerCount = service.netPeerCount().send();
        BigInteger peerCount = netPeerCount.getQuantity();
        System.out.println("链上节点数:"+peerCount);

        // 部署合约
        AppMetaData appMetaData;
        appMetaData = service.appMetaData(DefaultBlockParameter.valueOf("latest")).send(); // 从测试链中获取一些元信息
        String chainIdHex = appMetaData.getAppMetaDataResult().getChainIdV1();
        BigInteger chainId = new BigInteger(chainIdHex.substring(2), 16);
        int version = appMetaData.getAppMetaDataResult().getVersion();
        // nonce: 随机数或者通过特定的逻辑生成的随机信息,nonce是为了避免重放攻击。
        // quota: 交易执行费用,也就是矿工费,就像以太坊中的 gasPrice * gasLimit。
        // valid_until_block: 超时机制,valid_until_block 可以定义的范围是 (currentHeight, currentHeight + 100]。交易在valid_until_block之后会作废。
        long currentHeight = service.appBlockNumber().send().getBlockNumber().longValue();
        long validUntilBlock = currentHeight + 80;
        Random random = new Random(System.currentTimeMillis());
        String nonce = String.valueOf(Math.abs(random.nextLong()));
        long quota = 1000000;
        Transaction tx = Transaction.createContractTransaction(nonce, quota, validUntilBlock, version, chainId, "0", contractCode);

        String rawTx = tx.sign(privateKey); // 对交易进行签名
        AppSendTransaction deployContractResult = service.appSendRawTransaction(rawTx).send();

        //get receipt and address from transaction
        String txHash = deployContractResult.getSendTransactionResult().getHash();
        System.out.println("合约部署的交易哈希:"+txHash);
        Thread.sleep(10000); // 链上接收到这笔交易,打包、共识是需要时间的;等待出块后才能查询到交易的回执
        TransactionReceipt txReceipt = service.appGetTransactionReceipt(txHash).send().getTransactionReceipt();
        String contractAddr = txReceipt.getContractAddress(); // 合约地址
        System.out.println("合约部署的地址:"+contractAddr);

    }
}

运行查看结果如下:

66ay4J.png (opens new window)

# 常见问题

  1. 中文编码乱码
img img

# 其他

更多 API 文档请查阅citahub/cita-sdk-java/docs

供参考的一些链接:

citahub/cita-sdk-java (opens new window)

社区关于“sdk”的搜索结果 (opens new window)

CITA-JAVA-SDK 使用疑问,如何确定一个交易上链成功了? (opens new window)

关于 Gradle (opens new window)