本流程在test目录下,其中用到的 solc-0.4.25 和 abigen 工具网上教程都比较详细,就暂时不展开聊,今天就大概描述流程。
1.将目录下的test.sol文件编译
pragma solidity ^0.4.25; import "./Table.sol"; contract test { string constant TABLE_NAME = "test2"; event insertEvent(string id, string title, uint iuser_id,uint ruser_id,int stauts); event updateEvent(string id, int stauts); constructor() public{ TableFactory tf = TableFactory(0x1001); tf.createTable(TABLE_NAME,"id","title,iuser_id,ruser_id,stauts,start_time,sign_time"); } function set(string id, string title, uint iuser_id,uint ruser_id,int stauts) public returns(int) { TableFactory tf = TableFactory(0x1001); Table table = tf.openTable(TABLE_NAME); uint t = 0; Entry entry = table.newEntry(); entry.set("id",id); entry.set("title",title); entry.set("iuser_id",iuser_id); entry.set("ruser_id",ruser_id); entry.set("stauts",stauts); entry.set("start_time",now); entry.set("sign_time",t); int count = table.insert(id,entry); emit insertEvent(id,title,iuser_id,ruser_id,stauts); return count; } function update(string id,int stauts) public returns(int) { TableFactory tf = TableFactory(0x1001); Table table = tf.openTable(TABLE_NAME); Condition condition = table.newCondition(); //condition.EQ("id",id); Entry entry = table.newEntry(); entry.set("stauts",stauts); if (stauts == 5){ entry.set("sign_time",now); } int count = table.update(id,entry,condition); emit updateEvent(id,stauts); return count; } function get(string id) public view returns (string,uint,uint,int,uint,uint) { TableFactory tf = TableFactory(0x1001); Table table = tf.openTable(TABLE_NAME); Condition condition = table.newCondition(); //condition.EQ("id",id); Entries entries = table.select(id,condition); if(entries.size() == 0){ return("", 0, 0, 0, 0, 0); } uint iuser_id; uint ruser_id; int stauts; iuser_id = entries.get(0).getUInt("iuser_id"); ruser_id = entries.get(0).getUInt("ruser_id"); stauts = entries.get(0).getInt("stauts"); return (entries.get(0).getString("title"), iuser_id, ruser_id, stauts,entries.get(0).getUInt("start_time"), entries.get(0).getUInt("sign_time")); } }test.sol
由于引用了Table.sol,所以还要将Table.sol放到目录下。
pragma solidity ^0.4.24; contract TableFactory { function openTable(string) public view returns (Table); //open table function createTable(string, string, string) public returns (int256); //create table } //select condition contract Condition { function EQ(string, int256) public view; function EQ(string, string) public view; function EQ(string, address) public view; function NE(string, int256) public view; function NE(string, string) public view; function GT(string, int256) public view; function GE(string, int256) public view; function LT(string, int256) public view; function LE(string, int256) public view; function limit(int256) public view; function limit(int256, int256) public view; } //one record contract Entry { function getInt(string) public view returns (int256); function getUInt(string) public view returns (uint256); function getAddress(string) public view returns (address); function getBytes64(string) public view returns (bytes1[64]); function getBytes32(string) public view returns (bytes32); function getString(string) public view returns (string); function set(string, int256) public; function set(string, uint256) public; function set(string, string) public; function set(string, 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, Condition) public view returns (Entries); function insert(string, Entry) public returns (int256); function update(string, Entry, Condition) public returns (int256); function remove(string, Condition) public returns (int256); function newEntry() public view returns (Entry); function newCondition() public view returns (Condition); } contract KVTableFactory { function openTable(string) public view returns (KVTable); function createTable(string, string, string) public returns (int256); } //KVTable per permiary key has only one Entry contract KVTable { function get(string) public view returns (bool, Entry); function set(string, Entry) public returns (int256); function newEntry() public view returns (Entry); }Table.sol
然后执行命令:
./solc-0.4.25 --bin --abi -o ./ ./test.sol
结果如下:
2.abigen生成go文件
./abigen --bin ./test.bin --abi ./test.abi --pkg test --type test --out ./test.go
结果如下:
3.复制节点的SDK文件
将上面的sdk文件夹,直接复制到test目录下
4.使用get_account.sh脚本生成账户私钥
该脚本在FISCO的console中,可以直接复制下来执行
./get_account.sh
执行完毕,生成一个目录,里面有我们需要的账户私钥文件(这个账户私钥文件(*.pem),可以重复使用),将它拷贝到test目录下 。
5.配置config.toml
可以拷贝go-sdk里的config.toml
也可以直接新建一个config.toml文件
[Network] #type rpc or channel Type="channel" CAFile="./sdk/ca.crt" Cert="./sdk/sdk.crt" Key="./sdk/sdk.key" # if the certificate context is not empty, use it, otherwise read from the certificate file # multi lines use triple quotes CAContext='''''' KeyContext='''''' CertContext='''''' [[Network.Connection]] NodeURL="127.0.0.1:20200" GroupID=1 # [[Network.Connection]] # NodeURL="127.0.0.1:20200" # GroupID=2 [Account] # only support PEM format for now KeyFile="./accounts/0x2b4c58c9b6bb9165931c9956f96d69723bb92754.pem" [Chain] ChainID=1 SMCrypto=false [log] Path="./"config.toml
将config.toml的KeyFile修改为上一步获得的pem文件的名字,然后其他的NodeURL和GroupID等,根据自己需要进行修改。
6.使用接口
在test目录下新建一个cmd目录,编写一个main.go来调用链码
package main import ( "math/big" "fmt" "log" "test" "github.com/FISCO-BCOS/go-sdk/client" "github.com/FISCO-BCOS/go-sdk/conf" "github.com/ethereum/go-ethereum/common" ) func main(){ configs, err := conf.ParseConfigFile("config.toml") //读取config.toml文件 if err != nil { log.Fatal(err) } config := &configs[0] client, err := client.Dial(config) //加载配置文件,生成client进行相关链操作 if err != nil { log.Fatal(err) } contractAddress := common.HexToAddress("0xac05ff0709f443db85f7cd524e142e1778a8e426") // 这里请放入刚刚部署的合约地址,注意,是你自己的机子部署的地址 instance, err := test.NewTest(contractAddress, client) //根据地址和client生成test合约对象 if err != nil { log.Fatal(err) } testSession := &test.TestSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()} a := big.NewInt(7) b := big.NewInt(8) c := big.NewInt(0) _,tx,receipt,err := testSession.Set("5","gosdk_title",a,b,c) if err != nil{ log.Fatal(err) } fmt.Printf("tx sent: %s\n", tx.Hash().Hex()) //调用set方法的交易hash fmt.Printf("transaction hash of receipt: %s\n", receipt.GetTransactionHash()) //调用set方法的交易hash ,与上面的hash是一样,只是存在不同的地方而已 title,iuser_id,ruser_id,stauts,start_time,sign_time,err := testSession.Get("5") if err != nil{ log.Fatal(err) } fmt.Printf("title: %s\niuser_id: %d\nruser_id: %d\nstauts: %d\nstart_time: %d\nsign_time: %d\n",title,iuser_id,ruser_id,stauts,start_time,sign_time) }main.go
在执行这个main.go之前应该先部署链码,但是由于笔者是先在webase上编写test.sol部署后,才进行go-sdk的配置的,所以这里就不重复部署了。
go mod init go mod tidy go run ./cmd/main.go
结果如下:
标签:function,string,public,webase,returns,go,view,id,sdk From: https://www.cnblogs.com/Owhy/p/18194331