【fisco bcos】基于ABI调用智能合约
参考官方文档:https://fisco-bcos-documentation.readthedocs.io/zh-cn/latest/docs/sdk/java_sdk/assemble_transaction.html
先放一下智能合约:
(就是一个很简单的插入和查找嗯)
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 TableFactorytableFactory.createTable(TABLE_NAME, "recordid", "description,remark");}// add record to the tablefunction addRecord(string memory recordid, string memory description, string memory remark)publicreturns (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 recordidfunction getRecord(string memory recordid)publicreturns (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后端的代码:
(也就是一个很简单的例子)
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";@Testpublic void test() throws Exception {// 初始化BcosSDK对象BcosSDK sdk = BcosSDK.build(configFile);// 获取Client对象,此处传入的群组ID为1Client 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』,函数参数类型为paramsTransactionResponse 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』函数,参数为recordidTransactionResponse 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("返回值长度不正确");}}}
}