长安链是分2.1.+和2.3.+两个版本,本节面说的是2.1.+的版本
需要2.3.+版本的合约,请看教程(二)!
教程(二)我会写如何查历史数据
编写前的注意事项:
1、运行一条带有Doker_GoVM的链
2、建议直接用官方的在线IDE去写合约,因为写完可以直接测,缺点只是调试不方便。
3、自己拉环境在本地写合约,编译时注意编译环境,官方有提醒你去Linux下去编译。
本教程使用官方的在线IDE去写合约
教程是基于官方文档写的,只是会多写一些解析步骤
1、首先新建一个合约
2、打开main.go文件(这是新增工程的默认存证模板)
package main
import (
"encoding/json"
"log"
"strconv"
"chainmaker/pb/protogo"
"chainmaker/shim"
)
//FactContract 合约对象
type FactContract struct {
}
//Fact 存证对象,存证合约的数据内容
type Fact struct {
FileHash string
FileName string
Time int
}
//NewFact 新建存证对象
func NewFact(fileHash, fileName string, time int) *Fact {
return &Fact{
FileHash: fileHash,
FileName: fileName,
Time: time,
}
}
//InitContract 合约初始化方法
func (f *FactContract) InitContract(stub shim.CMStubInterface) protogo.Response {
return shim.Success([]byte("Init Success"))
}
// UpgradeContract 合约升级方法
func (f *FactContract) UpgradeContract(stub shim.CMStubInterface) protogo.Response {
return shim.Success([]byte("Upgrade Success"))
}
//InvokeContract 调用合约
func (f *FactContract) InvokeContract(stub shim.CMStubInterface) protogo.Response {
//获取调用合约哪个方法
method := string(stub.GetArgs()["method