IDE部署合约超时
合约代码
package main
import (
"chainmaker.org/chainmaker/contract-sdk-go/v2/pb/protogo"
"chainmaker.org/chainmaker/contract-sdk-go/v2/sdk"
"encoding/json"
"strconv"
)
// TradeContract contract for trade
type TradeContract struct {
}
type Trade struct {
TradeId string `json:"tradeId"`
// 商品名称
GoodId string `json:"goodId"`
// 购买者
Buyer string `json:"buyer"`
// 卖家
Seller string `json:"seller"`
// 定价
Price int64 `json:"price"`
// 数量
Amount int64 `json:"amount"`
// 总价
TotalPrice int64 `json:"totalPrice"`
// 交易时间
TradeTime int64 `json:"tradeTime"`
}
func ArgsToTrade() *Trade {
params := sdk.Instance.GetArgs()
tradeId := string(params["tradeId"])
goodId := string(params["goodId"])
buyer := string(params["buyer"])
seller := string(params["seller"])
price := string(params["price"])
amount := string(params["amount"])
totalPrice := string(params["totalPrice"])
tradeTime := string(params["tradeTime"])
// 尝试类型转换
priceInt, err := strconv.ParseInt(price, 10, 64)
if err != nil {
return nil
}
amountInt, err := strconv.ParseInt(amount, 10, 64)
if err != nil {
return nil
}
totalPriceInt, err := strconv.ParseInt(totalPrice, 10, 64)
if err != nil {
return nil
}
tradeTimeInt, err := strconv.ParseInt(tradeTime, 10, 64)
if err != nil {
return nil
}
return NewTrade(tradeId, goodId, buyer, seller, priceInt, amountInt, totalPriceInt, tradeTimeInt)
}
// NewTrade 创建新的交易记录
func NewTrade(tradeId string, goodId string, buyer string, seller string, price int64, amount int64, totalPrice int64, tradeTime int64) *Trade {
trade := &Trade{
TradeId: tradeId,
GoodId: goodId,
Buyer: buyer,
Seller: seller,
Price: price,
Amount: amount,
TotalPrice: totalPrice,
TradeTime: tradeTime,
}
return trade
}
// InitContract install contract func
func (t *TradeContract) InitContract() protogo.Response {
return sdk.Success([]byte("Init contract success"))
}
// UpgradeContract upgrade contract func
func (t *TradeContract) UpgradeContract() protogo.Response {
return sdk.Success([]byte("Upgrade contract success"))
}
// InvokeContract the entry func of invoke contract func
func (t *TradeContract) InvokeContract(method string) protogo.Response {
switch method {
case "save":
return t.save()
case "findByTradeId":
return t.findByTradeId()
case "delTrade":
return t.delTrade()
default:
return sdk.Error("invalid method")
}
}
// saveToChain 保存到区块链
func saveToChain(t *Trade) error {
// 保存到区块链
tradeBytes, err := json.Marshal(t)
if err != nil {
return err
}
// 存储数据
err = sdk.Instance.PutStateByte("trade_bytes_id", t.TradeId, tradeBytes)
if err != nil {
return err
}
return nil
}
// save 保存交易记录
func (t *TradeContract) save() protogo.Response {
trade := ArgsToTrade()
if trade == nil {
return sdk.Error("args error")
}
// 发送事件
sdk.Instance.EmitEvent("topic_save", []string{trade.TradeId, trade.GoodId, trade.Buyer, trade.Seller})
// 存储数据
err := saveToChain(trade)
if err != nil {
return sdk.Error("fail to save trade bytes")
}
// 记录日志
sdk.Instance.Infof("save trade success, tradeId: %s", trade.TradeId)
return sdk.Success([]byte("save success"))
}
// findByTradeId 根据交易id查询交易记录
func (t *TradeContract) findByTradeId() protogo.Response {
// 获取参数
tradeId := string(sdk.Instance.GetArgs()["tradeId"])
// 从区块链中获取数据
tradeBytes, err := sdk.Instance.GetStateByte("trade_bytes_id", tradeId)
if err != nil {
return sdk.Error("get state error")
}
// 反序列化
trade := &Trade{}
err = json.Unmarshal(tradeBytes, trade)
//if err != nil {
// return sdk.Error("json unmarshal error")
//}
// 记录日志
sdk.Instance.Infof("find trade success, tradeId: %s", trade.TradeId)
return sdk.Success(tradeBytes)
}
// deleteFromChain 从区块链中删除数据
func deleteFromChain(trade *Trade) error {
// 删除旧数据
err := sdk.Instance.DelState("trade_bytes_id", trade.TradeId)
if err != nil {
return err
}
return nil
}
// delTrade 删除交易记录
func (t *TradeContract) delTrade() protogo.Response {
// 获取参数
tradeId := string(sdk.Instance.GetArgs()["tradeId"])
// 从区块链中获取数据
tradeBytes, err := sdk.Instance.GetStateByte("trade_bytes_id", tradeId)
if err != nil {
return sdk.Error("get state error")
}
// 反序列化
trade := &Trade{}
err = json.Unmarshal(tradeBytes, trade)
if err != nil {
return sdk.Error("json unmarshal error")
}
// 记录日志
sdk.Instance.Infof("find trade success, tradeId: %s", trade.TradeId)
// 删除旧数据
err = deleteFromChain(trade)
if err != nil {
return sdk.Error("fail to delete trade bytes")
}
return sdk.Success([]byte("delete success"))
}
func main() {
}
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information