1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"github.com/zhigui/zigledger/core/chaincode/shim"
pb "github.com/zhigui/zigledger/protos/peer"
"math/big"
)
const (
Transfer = "transfer"
AddUser = "addUser"
GetAccountInfo = "getAccountInfo"
SetAccountInfo = "setAccountInfo"
Suicide = "suicide"
)
type TraceChaincode struct{}
func main() {
err := shim.Start(new(TraceChaincode))
if err != nil {
fmt.Printf("Error starting assetChaincode: %s", err)
}
}
func (t *TraceChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success([]byte("Init success."))
}
func (t *TraceChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
switch function {
case Transfer:
if len(args) != 3 {
return shim.Error("Incorrect number of arguments. Expecting 3.")
}
return t.transfer(stub, args)
case AddUser:
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2.")
}
case GetAccountInfo:
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1.")
}
return t.getAccountInfo(stub, args)
case SetAccountInfo:
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2.")
}
return t.setAccountInfo(stub, args)
case Suicide:
return t.suicide(stub)
}
return shim.Error("Invalid invoke function name.")
}
func (t *TraceChaincode) transfer(stub shim.ChaincodeStubInterface, args []string) pb.Response {
amountType := args[0]
transferAmount := new(big.Int)
transferAmount.SetString(args[1], 10)
transferTo := args[2]
if amountType == "ZIG" {
//if err := stub.Transfer(transferTo, transferType, transferAmount); err != nil {
// return shim.Error("Transfer error" + err.Error())
//}
fmt.Println("transfer zig ", transferTo, transferAmount)
} else {
argsByte := make([][]byte, len(args))
for k, v := range args {
argsByte[k] = []byte(v)
}
stub.InvokeChaincode(amountType, argsByte, "")
fmt.Println("call other contract ", string(argsByte[0]), string(argsByte[1]), string(argsByte[2]))
}
return shim.Success([]byte("success"))
}
func (t *TraceChaincode) setAccountInfo(stub shim.ChaincodeStubInterface, args []string) pb.Response {
address := args[0]
traceInfo := args[1]
if err := stub.PutState("TRACE::" + address, []byte(traceInfo)); err != nil {
shim.Error("PutState error" + err.Error())
}
return shim.Success([]byte("success"))
}
func (t *TraceChaincode) getAccountInfo(stub shim.ChaincodeStubInterface, args []string) pb.Response {
address := args[0]
traceInfo, err := stub.GetState("TRACE::" + address)
if err != nil {
shim.Error("GetState error:" + err.Error())
}
return shim.Success([]byte(traceInfo))
}
func (t *TraceChaincode) suicide(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success([]byte("success"))
}