forked from Akachain/akc-go-sdk
/
balance_transfer.go
249 lines (207 loc) · 7.68 KB
/
balance_transfer.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* This chaincode is an example of how to use Akachain SDK high throughput in golang.
*
* @author Huan Le for Akachain
* @created 18 Jun 2019
*/
package example
import (
"encoding/json"
"fmt"
"strconv"
. "github.com/Akachain/akc-go-sdk/common"
"github.com/Akachain/akc-go-sdk/util"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/mitchellh/mapstructure"
akc "github.com/Akachain/akc-go-sdk/akc-htc"
)
var logger = shim.NewLogger("example_balance_transfer")
// Chaincode example simple Chaincode implementation
type Chaincode struct {
}
// USERTABLE - Table name in onchain
const USERTABLE = "User_"
// User - struct model
type User struct {
WalletAddress string `json:"WalletAddress"`
Amount float64 `json:"Amount"`
}
// AKC high throughput response data
type AkcResponseData struct {
Key string
Data []string
}
func (t *Chaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### example_balance_transfer Init ###########")
return shim.Success(nil)
}
// Invoke chaincode
func (t *Chaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### example_balance_transfer Invoke ###########")
function, args := stub.GetFunctionAndParameters()
switch function {
case "createUser":
return t.createUser(stub, args)
case "transferToken":
return t.transferToken(stub, args)
case "updateUserBalance":
return t.updateUserBalance(stub, args)
case "getUserToken":
return t.getUserToken(stub, args)
}
resErr := ResponseError{ERR5, "Unknown action, check the first argument"}
logger.Errorf("Unknown action, check the first argument. Got: %v", args[0])
return RespondError(resErr)
}
/**
* Create user and save to database onchain. All attributes of User defined on the model.
*
* - args[0] -> user walletAddress. Optional, if this argument is empty, wallet address = txID.
* - args[1] -> token Amount initialize.
*
* @param APIstub The chaincode shim
* @param args The arguments array for the update invocation
*
* @return a string of wallet address of User when success or message when error.
*/
func (t *Chaincode) createUser(stub shim.ChaincodeStubInterface, args []string) pb.Response {
util.CheckChaincodeFunctionCallWellFormedness(args, 2)
WalletAddress := args[0]
if WalletAddress == "" {
WalletAddress = stub.GetTxID()
}
Amount, _ := strconv.ParseFloat(args[1], 64)
// Create new user document and save to database onchain.
err := util.Createdata(stub, USERTABLE, []string{WalletAddress}, &User{WalletAddress: WalletAddress, Amount: Amount})
if err != nil {
resErr := ResponseError{ERR5, fmt.Sprintf("%s %s", ResCodeDict[ERR5], "")}
return RespondError(resErr)
} else {
// Update user data infor to AKC high throughput
akchtc := akc.AkcHighThroughput{}
akchtc.Insert(stub, []string{"User", WalletAddress, args[1], "OP_ADD"})
}
resSuc := ResponseSuccess{SUCCESS, ResCodeDict[SUCCESS], WalletAddress}
return RespondSuccess(resSuc)
}
/**
* Sending tokens from account A to account B. However, this transfer will not be done immediately,
* that it will push into high throughput. In this example, I use Akachain SDK: akc-highthroughput.
*
* - args[0] -> wallet address send
* - args[1] -> wallet address receive
* - args[2] -> token amount will send.
*
* @param APIstub The chaincode shim
* @param args The arguments array for the update invocation
*
* @return a string transaction ID response structure indicating success or failure with a message
*/
func (t *Chaincode) transferToken(stub shim.ChaincodeStubInterface, args []string) pb.Response {
util.CheckChaincodeFunctionCallWellFormedness(args, 3)
walletSend := args[0]
walletReceived := args[1]
amountToken := args[2]
// Perform the execution
_, err := strconv.Atoi(amountToken)
if err != nil {
resErr := ResponseError{ERR8, fmt.Sprintf("%s %s", ResCodeDict[ERR8], "Invalid transaction amount, expecting a integer value")}
return RespondError(resErr)
}
// Init AKC sdk high throughput
akchtc := akc.AkcHighThroughput{}
insertHTP := akchtc.Insert(stub, []string{"User", walletSend, amountToken, "OP_SUB"})
if insertHTP != nil {
// Insert High throughput failure
resErr := ResponseError{ERR8, fmt.Sprintf("%s %s", ResCodeDict[ERR8], insertHTP.Error())}
return RespondError(resErr)
} else {
akchtc.Insert(stub, []string{"User", walletReceived, amountToken, "OP_ADD"})
}
resSuc := ResponseSuccess{SUCCESS, ResCodeDict[SUCCESS], stub.GetTxID()}
return RespondSuccess(resSuc)
}
/**
* As mentioned above, that transfer will not be done immediately, it's pushed into
* AKC high throughput. And this function will perform that balance transfer via the AKC
* high throughput SDK.
*
* - args[0] -> type of prune <"PRUNE_SAFE" or "PRUNE_FAST">. (To perform calculation, AKC
* sdk will prune delta data, so need to know the prune type)
*
* @param APIstub The chaincode shim
* @param args The arguments array for the update invocation
*
* @return a string transaction ID response structure indicating success or failure with a message.
*/
func (t *Chaincode) updateUserBalance(stub shim.ChaincodeStubInterface, args []string) pb.Response {
util.CheckChaincodeFunctionCallWellFormedness(args, 1)
pruneType := args[0]
// Init AKC sdk high throughput
akchtc := akc.AkcHighThroughput{}
pruneBalance, akcErr := akchtc.Prune(stub, []string{"User", pruneType})
if akcErr != nil {
resErr := ResponseError{"AKC_ERR", akcErr.Error()}
return RespondError(resErr)
}
// Unmarshal get response
var responseData map[string]AkcResponseData
fmt.Printf("responseData: %s", responseData)
if err := json.Unmarshal(pruneBalance, &responseData); err != nil {
resErr := ResponseError{"AKC_ERR", err.Error()}
return RespondError(resErr)
}
for _, data := range responseData {
walletAddress := data.Key
tokenAmount, _ := strconv.ParseFloat(data.Data[0], 64)
var userData User
// Get user data in database by ID of document. In this, ID is wallet address of user.
user_rs, err := util.Getdatabyid(stub, walletAddress, USERTABLE)
if err != nil {
resErr := ResponseError{ERR4, fmt.Sprintf("%s %s", ResCodeDict[ERR4], err.Error())}
return RespondError(resErr)
}
mapstructure.Decode(user_rs, &userData)
userData.Amount = tokenAmount
// Overwrite user info.
err = util.Changeinfo(stub, USERTABLE, []string{walletAddress}, &User{WalletAddress: walletAddress, Amount: userData.Amount})
if err != nil {
// Overwrite fail
resErr := ResponseError{ERR5, fmt.Sprintf("%s %s", ResCodeDict[ERR5], err.Error())}
return RespondError(resErr)
}
}
resSuc := ResponseSuccess{SUCCESS, ResCodeDict[SUCCESS], stub.GetTxID()}
return RespondSuccess(resSuc)
}
/**
* Get token amount of user via wallet address.
*
* - args[0] -> wallet address.
*
* @param APIstub The chaincode shim
* @param args The arguments array for the update invocation
*
* @return a string token amount of user when success or failure with a message.
*/
func (t *Chaincode) getUserToken(stub shim.ChaincodeStubInterface, args []string) pb.Response {
util.CheckChaincodeFunctionCallWellFormedness(args, 1)
walletAddress := args[0]
var userData User
// Get user data in database by ID of document. In this, ID is wallet address of user.
user_rs, err := util.Getdatabyid(stub, walletAddress, USERTABLE)
if err != nil {
resErr := ResponseError{ERR4, fmt.Sprintf("%s %s", ResCodeDict[ERR4], err.Error())}
return RespondError(resErr)
}
mapstructure.Decode(user_rs, &userData)
resSuc := ResponseSuccess{SUCCESS, ResCodeDict[SUCCESS], fmt.Sprintf("%v", userData.Amount)}
return RespondSuccess(resSuc)
}
func main() {
err := shim.Start(new(Chaincode))
if err != nil {
logger.Errorf("Error starting chaincode: %s", err)
}
}