-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
executable file
·106 lines (99 loc) · 2.83 KB
/
account.go
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
103
104
105
106
package control
import (
`encoding/json`
`fmt`
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/peer"
`github.com/hyperledger/fabric/smart_contract/common`
"github.com/hyperledger/fabric/smart_contract/log"
`github.com/hyperledger/fabric/smart_contract/module`
`strings`
)
/**
* Account Info
**/
func (t *CTKChainCode) AccountInfo(stub shim.ChaincodeStubInterface, args []string) peer.Response {
log.Logger.Info("##############account info###############")
if len(args) < 1 {
return shim.Error("params error!")
}
var param module.AccountInfo
err := json.Unmarshal([]byte(args[0]), ¶m)
if err != nil {
return shim.Error(" params err:" + err.Error())
}
account := t.GetTokenAccountInfo(stub,param.Token,param.Account)
if account == nil{
return shim.Success(common.FormatSuccessResult(map[string]interface{}{
"account":map[string]interface{}{
"address":param.Account,
"balance":"0",
},
}))
}
return shim.Success(common.FormatSuccessResult(map[string]interface{}{
"account":account,
}))
}
/**
* Account Txs
**/
func (t *CTKChainCode) Transactions(stub shim.ChaincodeStubInterface, args []string) peer.Response {
log.Logger.Info("################# search transaction list ############################")
if len(args) < 1 {
log.Logger.Error("QueryTransferByType params error。")
return shim.Error("params error")
}
var param module.TransactionsParam
err := json.Unmarshal([]byte(args[0]), ¶m)
if err != nil {
log.Logger.Error("args json unmarshal err")
return shim.Error("unmarshal error")
}
param.Account = strings.ToLower(param.Account)
key := module.DB_TRANS_TO+module.DB_TRANSACTIONS+param.Token+param.Account
fmt.Println(key)
historyIter, err := stub.GetHistoryForKey(key)
var list []map[string]interface{}
i := 0
if err == nil {
fmt.Println("=======",historyIter.HasNext(),err)
for historyIter.HasNext(){
modification, err := historyIter.Next()
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("history receive tx", string(modification.Value))
var v map[string]interface{}
_ = json.Unmarshal(modification.Value,&v)
list = append(list,v)
i++
if i > 1000{
break
}
}
}
i = 0
key = module.DB_TRANS_FROM+module.DB_TRANSACTIONS+param.Token+param.Account
fmt.Println(key)
historyIter, err = stub.GetHistoryForKey(key)
if err != nil {
return shim.Success(common.FormatSuccessResult(list))
}
for historyIter.HasNext(){
modification, err := historyIter.Next()
fmt.Println("=======",modification)
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("history spend tx", string(modification.Value))
var v map[string]interface{}
_ = json.Unmarshal(modification.Value,&v)
list = append(list,v)
i++
if i > 1000{
break
}
}
return shim.Success(common.FormatSuccessResult(list))
}