向彪fisco bcos入门教程,编写核心的智能合约代码(七)

前言:

上一章节,我们已经配置了webase,并且写了一个HW的合约,能够正常的和区块链底层交互。这个章节我们来写我们的博客留言的智能合约代码。

 

 

正文:

 

第一章节我们就分析了需要上链的摘要信息如下:

 

sid 被评论的文章或者页面的ID

nickname 评论人的昵称

content 评论的内容

 

合约代码如下:

 

pragma solidity>=0.4.24 <0.6.11;

 

import "./Table.sol";

 

contract CommentService {

event SetResult(int256 count);

 

KVTableFactory tableFactory;

string constant TABLE_NAME = "t_comment";

 

constructor() public {

//The fixed address is 0x1010 for KVTableFactory

tableFactory = KVTableFactory(0x1010);

// the parameters of createTable are tableName,keyField,"vlaueFiled1,vlaueFiled2,vlaueFiled3,..."

tableFactory.createTable(TABLE_NAME, "id", "sid,nickname,content");

}

 

// 获取链上的信息

function get(string memory id) public view returns (bool, int256, string memory,string memory) {

KVTable table = tableFactory.openTable(TABLE_NAME);

bool ok = false;

Entry entry;

(ok, entry) = table.get(id);

int256 sid;

string memory nickname;

string memory content;

if (ok) {

sid= entry.getInt("sid");

nickname = entry.getString("nickname");

content = entry.getString("content");

}

return (ok, sid, nickname,content);

}

 

// 评论的摘要信息上链

function set(string memory id, int256 sid, string memory nickname,string memory content)

public

returns (int256)

{

KVTable table = tableFactory.openTable(TABLE_NAME);

Entry entry = table.newEntry();

// the length of entry's field value should < 16MB

entry.set("id", id);

entry.set("sid", sid);

entry.set("nickname", nickname);

entry.set("content", content);

// the first parameter length of set should <= 255B

int256 count = table.set(id, entry);

emit SetResult(count);

return count;

}

}

 

官方的Table.sol

contract TableFactory {

function openTable(string memory) public view returns (Table) {} //open table

function createTable(string memory, string memory, string memory) public returns (int256) {} //create table

}

 

//select condition

contract Condition {

function EQ(string memory, int256) public {}

function EQ(string memory, string memory) public {}

 

function NE(string memory, int256) public {}

function NE(string memory, string memory) public {}

 

function GT(string memory, int256) public {}

function GE(string memory, int256) public {}

 

function LT(string memory, int256) public {}

function LE(string memory, int256) public {}

 

function limit(int256) public {}

function limit(int256, int256) public {}

}

 

//one record

contract Entry {

function getInt(string memory) public view returns (int256) {}

function getUInt(string memory) public view returns (int256) {}

function getAddress(string memory) public view returns (address) {}

function getBytes64(string memory) public view returns (bytes1[64] memory) {}

function getBytes32(string memory) public view returns (bytes32) {}

function getString(string memory) public view returns (string memory) {}

 

function set(string memory, int256) public {}

function set(string memory, uint256) public {}

function set(string memory, string memory) public {}

function set(string memory, address) public {}

}

 

//record sets

contract Entries {

function get(int256) public view returns (Entry) {}

function size() public view returns (int256) {}

}

 

//Table main contract

contract Table {

function select(string memory, Condition) public view returns (Entries) {}

function insert(string memory, Entry) public returns (int256) {}

function update(string memory, Entry, Condition) public returns (int256) {}

function remove(string memory, Condition) public returns (int256) {}

 

function newEntry() public view returns (Entry) {}

function newCondition() public view returns (Condition) {}

}

 

contract KVTableFactory {

function openTable(string memory) public view returns (KVTable) {}

function createTable(string memory, string memory, string memory) public returns (int256) {}

}

 

//KVTable per permiary key has only one Entry

contract KVTable {

function get(string memory) public view returns (bool, Entry) {}

function set(string memory, Entry) public returns (int256) {}

function newEntry() public view returns (Entry) {}

}

 

 

我们测试一下:

 

写数据

向彪fisco bcos入门教程,编写核心的智能合约代码(七)_第1张图片

 

 

 

读数据

向彪fisco bcos入门教程,编写核心的智能合约代码(七)_第2张图片

都正常

我们选择用rest api来调用合约上链,如果是正式项目,建议用官方的sdk来调用合约,和大佬交流大佬说sdk效率更高。

向彪fisco bcos入门教程,编写核心的智能合约代码(七)_第3张图片

官方文档如下:

https://webasedoc.readthedocs.io/zh_CN/latest/docs/WeBASE-Front/interface.html?highlight=handleWithSign#id320

接口URL

http://localhost:5002/WeBASE-Front/trans/handleWithSign

调用方法

HTTP POST

请求参数

1)参数表

序号

中文

参数名

类型

最大长度

必填

说明

1

用户编号

signUserId

String

64

WeBASE-Sign用户编号(查询方法可不传)

2

合约名称

contractName

String

 

 

3

合约地址

contractAddress

String

 

 

4

方法名

funcName

String

 

 

5

合约编译后生成的abi文件内容

contractAbi

List

 

合约中单个函数的ABI,若不存在同名函数可以传入整个合约ABI,格式:JSONArray

6

方法参数

funcParam

List

 

JSON数组,多个参数以逗号分隔(参数为数组时同理),如:["str1",["arr1","arr2"]]

7

群组ID

groupId

int

 

默认为1

8

合约版本

version

String

 

CNS中合约版本,该字段在v1.3.0+版本已弃用

2)数据格式

{ "groupId" :1, "signUserId": "458ecc77a08c486087a3dcbc7ab5a9c3", "contractAbi":[{"constant":true,"inputs":[],"name":"getVersion","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getStorageCell","outputs":[{"name":"","type":"string"},{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"n","type":"string"}],"name":"setVersion","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"storageHash","type":"string"},{"name":"storageInfo","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}], "contractAddress":"0x14d5af9419bb5f89496678e3e74ce47583f8c166", "funcName":"set", "funcParam":["test"] }

示例:

curl -X POST "http://localhost:5002/WeBASE-Front/trans/handleWithSign" -H "accept: */*" -H "Content-Type: application/json" -d "{ \"contractAbi\": [ { \"outputs\": [], \"constant\": false, \"payable\": false, \"inputs\": [ { \"name\": \"n\", \"type\": \"string\" } ], \"name\": \"set\", \"stateMutability\": \"nonpayable\", \"type\": \"function\" }, { \"outputs\": [ { \"name\": \"\", \"type\": \"string\" } ], \"constant\": true, \"payable\": false, \"inputs\": [], \"name\": \"get\", \"stateMutability\": \"view\", \"type\": \"function\" }, { \"payable\": false, \"inputs\": [], \"stateMutability\": \"nonpayable\", \"type\": \"constructor\" }, { \"inputs\": [ { \"indexed\": false, \"name\": \"name\", \"type\": \"string\" } ], \"name\": \"nameEvent\", \"anonymous\": false, \"type\": \"event\" } ], \"contractAddress\": \"0x7571ff73f1a37ca07f678aebc4d8213e7ef5c266\", \"funcName\": \"set\", \"funcParam\": [ \"test\" ], \"groupId\": 1, \"signUserId\": "458ecc77a08c486087a3dcbc7ab5a9c3"}"

响应参数

a、正确查询交易返回值信息

{"Hi,Welcome!"}

b、正确发送数据上链返回值信息(交易收据)

{ "transactionHash": "0x0b426a58af8ba449742b937f1e9b2b225335638707b93d6b296dfd8107edddd7", "transactionIndex": 0, "blockHash": "0xc8eb7a983ecb8c2a0a64450a059d2cf3de8c8d786211dcec48ab9c47219ee8f7", "blockNumber": 36985, "gasUsed": 35400, "contractAddress": "0x0000000000000000000000000000000000000000", "root": null, "status": "0x0", "from": "0xb173ca9a2e07efe6007aee751a013849d53e7c29", "to": "0x7571ff73f1a37ca07f678aebc4d8213e7ef5c266", "input": "0x4ed3885e000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000047465737400000000000000000000000000000000000000000000000000000000", "output": "0x", "logs": [ { "removed": false, "logIndex": null, "transactionIndex": null, "transactionHash": null, "blockHash": null, "blockNumber": null, "address": "0x7571ff73f1a37ca07f678aebc4d8213e7ef5c266", "data": "0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000047465737400000000000000000000000000000000000000000000000000000000", "type": null, "topics": [ "0x9645e7fb5eec05c0f156d4901a10663561199c6dd0401214a0b833fe0022d899" ], "logIndexRaw": null, "blockNumberRaw": null, "transactionIndexRaw": null } ], "logsBloom": "0x00000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000020000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000", "gasUsedRaw": "0x8a48", "statusOK": true, "blockNumberRaw": "0x9079", "transactionIndexRaw": "0x0" }

 

 

 

 

postman来测试一下,都正常

向彪fisco bcos入门教程,编写核心的智能合约代码(七)_第4张图片

 

向彪fisco bcos入门教程,编写核心的智能合约代码(七)_第5张图片

那么本章节到此结束。

你可能感兴趣的:(fisco,bcos,区块链)