【fisco bcos】基于ABI调用智能合约

参考官方文档:https://fisco-bcos-documentation.readthedocs.io/zh-cn/latest/docs/sdk/java_sdk/assemble_transaction.html

先放一下智能合约:

(就是一个很简单的插入和查找嗯)

java 复制代码
pragma solidity ^0.4.25;
pragma experimental ABIEncoderV2;

import "./Table.sol";

contract record {
    event AddRecordResult(int256 count);
    event GetRecordResult(string description, string remark);

    TableFactory tableFactory;
    string constant TABLE_NAME = "record";
    constructor() public {
        tableFactory = TableFactory(0x1001); //The fixed address is 0x1001 for TableFactory
        tableFactory.createTable(TABLE_NAME, "recordid", "description,remark");
    }

    // add record to the table
    function addRecord(string memory recordid, string memory description, string memory remark)
    public
    returns (int256)
    {
        Table table = tableFactory.openTable(TABLE_NAME);
    
        Entry entry = table.newEntry();
        entry.set("recordid", recordid);
        entry.set("description", description);
        entry.set("remark", remark);
    
        int256 count = table.insert(recordid, entry);
        emit AddRecordResult(count);
    
        return count;
    }

    // get record by recordid
    function getRecord(string memory recordid)
    public
    returns (string memory, string memory)
    {
        Table table = tableFactory.openTable(TABLE_NAME);
    
        Condition condition = table.newCondition();
        condition.EQ("recordid", recordid);
    
        Entries entries = table.select(recordid, condition);
    
        require(entries.size() > 0, "Record does not exist");
    
        Entry entry = entries.get(0);
    
        string memory description = entry.getString("description");
        string memory remark = entry.getString("remark");
    
        emit GetRecordResult(description, remark);
    
        return (description, remark);
    }
}

然后是Java后端的代码:

(也就是一个很简单的例子)

java 复制代码
import org.fisco.bcos.sdk.BcosSDK;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.transaction.manager.TransactionProcessorFactory;
import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class RecordTest {
    // 获取配置文件路径
    public final String configFile = "src/main/resources/config-example.toml";

    @Test
    public void test() throws Exception {
        // 初始化BcosSDK对象
        BcosSDK sdk = BcosSDK.build(configFile);
        // 获取Client对象,此处传入的群组ID为1
        Client client = sdk.getClient(Integer.valueOf(1));
        // 构造AssembleTransactionProcessor对象,需要传入client对象,CryptoKeyPair对象和abi、binary文件存放的路径。abi和binary文件需要在上一步复制到定义的文件夹中。
        CryptoKeyPair keyPair = client.getCryptoSuite().createKeyPair();
        AssembleTransactionProcessor transactionProcessor = TransactionProcessorFactory.createAssembleTransactionProcessor(client, keyPair, "src/main/resources/abi/", "");

        // 同步方式发送交易
        // 创建调用交易函数的参数
        String recordId = "1";
        String description = "description";
        String remark = "remark";

        List<Object> params = new ArrayList<>();
        params.add(recordId);
        params.add(description);
        params.add(remark);

        // 调用record合约,调用函数名为『addRecord』,函数参数类型为params
        TransactionResponse transactionResponse = transactionProcessor.sendTransactionAndGetResponseByContractLoader("record", "0xc0c669591f444b440b8c053ff977df2f34c2681f", "addRecord", params);

        // 打印返回值
        List<Object> returnValues = transactionResponse.getReturnObject();
        if (returnValues != null) {
            for (Object value : returnValues) {
                System.out.println("上链返回值:"+value.toString());
            }
        }


        // 调用合约查询接口
        List<Object> params2 = new ArrayList<>();
        params2.add("1");
        // 调用record合约的『getRecord』函数,参数为recordid
        TransactionResponse transactionResponse2 = transactionProcessor.sendTransactionAndGetResponseByContractLoader("record", "0xc0c669591f444b440b8c053ff977df2f34c2681f", "getRecord", params2);
        // 打印返回值
        List<Object> returnValues2 = transactionResponse2.getReturnObject();
        if (returnValues2 != null) {
            // 检查返回值的长度是否正确
            if (returnValues2.size() == 2) {
                String description2 = (String) returnValues2.get(0);
                String remark2 = (String) returnValues2.get(1);
                System.out.println("Description: " + description2);
                System.out.println("Remark: " + remark2);
            } else {
                System.out.println("返回值长度不正确");
            }
        }
    }
}
相关推荐
终端域名6 小时前
中本聪思想与Web3的困境:从理论到现实的跨越
web3·区块链·元宇宙
大白猴1 天前
大白话解析 Solidity 中的防重放参数
区块链·智能合约·solidity·时间戳·重放攻击·nonce·防重放参数
小明的小名叫小明1 天前
区块链技术原理(12)-以太坊区块
区块链
大白猴1 天前
大白话解析“入口点合约”
区块链·智能合约·solidity·以太坊·账户抽象·入口点合约·erc4337
余_弦1 天前
区块链中的密码学 —— 零知识证明
算法·区块链·以太坊
木鱼时刻1 天前
肖臻《区块链技术与应用》第14-15讲 超越货币:以太坊如何用“智能合约”开启去中心化应用时代
去中心化·区块链·智能合约
电报号dapp1192 天前
公链开发竞争白热化:如何设计下一代高性能、可扩展的区块链基础设施?
web3·去中心化·区块链·智能合约
爱看科技2 天前
微美全息(NASDAQ:WIMI)Raft携手节点动态评估:引领联盟链高性能共识新潮流
去中心化·区块链
区块链蓝海2 天前
YouBallin正式上线:用Web3重塑创作者经济
区块链
Dynadot_tech2 天前
区块链 + 域名Web3时代域名投资的新风口(上)
web3·区块链·域名·dynadot·域名市场·域名投资