-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
278 lines (217 loc) · 7.48 KB
/
main.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"context"
"fmt"
"log"
// Importing the general purpose Cosmos blockchain client
"github.com/ignite/cli/ignite/pkg/cosmosclient"
// Importing the types package of blockchain
"mitoblockchaindev/x/mitoblockchaindev/types"
)
// middleware to allow incoming requests from all ports
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT")
// we do not allow options
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
// forward request to next handler
c.Next()
}
}
var addressPrefix = "mito"
var cosmos, err = cosmosclient.New(
context.Background(),
cosmosclient.WithAddressPrefix(addressPrefix),
)
var accountName = "bob"
var account, accounterr = cosmos.Account(accountName)
var addr, addrerr = account.Address(addressPrefix)
// discount token from angular
type discountToken struct {
Timestamp string `json:"timestamp"`
ActivityName string `json:"activity_name"`
Score string `json:"score"`
Message string `json:"message"`
DiscountValue string `json:"discount_value"`
EligibleCompanies string `json:"eligible_companies"`
ItemType string `json:"item_type"`
ExpiryDate string `json:"expiry_date"`
}
// discount token status from angular
type DiscountTokenStatus struct {
TokenID uint64 `json:"token_id"`
ID uint64 `json:"id"`
Timestamp string `json:"timestamp"`
Status string `json:"status"`
}
// membership token from angular
type membershipToken struct {
Timestamp string `json:"timestamp"`
ActivityName string `json:"activity_name"`
Score string `json:"score"`
Message string `json:"message"`
MembershipDuration string `json:"membership_duration"`
EligibleCompanies string `json:"eligible_companies"`
ExpiryDate string `json:"expiry_date"`
}
// membership token status from angular
type MembershipTokenStatus struct {
TokenID uint64 `json:"token_id"`
ID uint64 `json:"id"`
Timestamp string `json:"timestamp"`
Status string `json:"status"`
}
func createMembershipToken(c *gin.Context) {
var newMembershipToken membershipToken
if err := c.BindJSON(&newMembershipToken); err != nil {
return
}
msg := &types.MsgCreateMembershipToken{
Creator: addr,
Timestamp: newMembershipToken.Timestamp,
ActivityName: newMembershipToken.ActivityName,
Score: newMembershipToken.Score,
Message: newMembershipToken.Message,
MembershipDuration: newMembershipToken.MembershipDuration,
EligibleCompanies: newMembershipToken.EligibleCompanies,
ExpiryDate: newMembershipToken.ExpiryDate,
}
txResp, transerr := cosmos.BroadcastTx(account, msg)
if transerr != nil {
log.Fatal(transerr)
}
fmt.Print("MsgCreateMembershipToken:\n\n")
fmt.Println(txResp)
msgStatus := &types.MsgCreateMembershipTokenStatus{
Creator: addr,
TokenID: 0,
Timestamp: newMembershipToken.Timestamp,
Status: "Valid",
}
txRespStatus, transerrStatus := cosmos.BroadcastTx(account, msgStatus)
if transerrStatus != nil {
log.Fatal(transerrStatus)
}
fmt.Print("MsgCreateMembershipTokenStatus:\n\n")
fmt.Println(txRespStatus)
c.IndentedJSON(http.StatusOK, txResp)
}
func createDiscountToken(c *gin.Context) {
var newDiscountToken discountToken
if err := c.BindJSON(&newDiscountToken); err != nil {
return
}
// Define a message to create a discount token
msg := &types.MsgCreateDiscountToken{
Creator: addr,
Timestamp: newDiscountToken.Timestamp,
ActivityName: newDiscountToken.ActivityName,
Score: newDiscountToken.Score,
Message: newDiscountToken.Message,
DiscountValue: newDiscountToken.DiscountValue,
EligibleCompanies: newDiscountToken.EligibleCompanies,
ItemType: newDiscountToken.ItemType,
ExpiryDate: newDiscountToken.ExpiryDate,
}
txResp, transerr := cosmos.BroadcastTx(account, msg)
if transerr != nil {
log.Fatal(transerr)
}
// Print response from broadcasting a transaction
fmt.Print("MsgCreateDiscountToken:\n\n")
fmt.Println(txResp)
msgStatus := &types.MsgCreateDiscountTokenStatus{
Creator: addr,
TokenID: 0,
Timestamp: newDiscountToken.Timestamp,
Status: "Valid",
}
txRespStatus, transerrStatus := cosmos.BroadcastTx(account, msgStatus)
if transerrStatus != nil {
log.Fatal(transerrStatus)
}
fmt.Print("MsgCreateDiscountTokenStatus:\n\n")
fmt.Println(txRespStatus)
c.IndentedJSON(http.StatusOK, txResp)
}
func deleteDiscountTokenStatus(c *gin.Context) {
var newDiscountTokenStatus DiscountTokenStatus
if err := c.BindJSON(&newDiscountTokenStatus); err != nil {
return
}
msgStatus := &types.MsgDeleteDiscountTokenStatus{
Creator: addr,
DiscountTokenStatusID: newDiscountTokenStatus.ID,
TokenID: newDiscountTokenStatus.TokenID,
}
txRespStatus, transerrStatus := cosmos.BroadcastTx(account, msgStatus)
if transerrStatus != nil {
log.Fatal(transerrStatus)
}
fmt.Print("MsgDeleteDiscountTokenStatus:\n\n")
fmt.Println(txRespStatus)
msgNewStatus := &types.MsgCreateDiscountTokenStatus{
Creator: addr,
TokenID: newDiscountTokenStatus.TokenID,
Timestamp: newDiscountTokenStatus.Timestamp,
Status: newDiscountTokenStatus.Status,
}
txRespNewStatus, transerrNewStatus := cosmos.BroadcastTx(account, msgNewStatus)
if transerrNewStatus != nil {
log.Fatal(transerrStatus)
}
fmt.Print("MsgCreateDiscountTokenStatus:\n\n")
fmt.Println(txRespNewStatus)
c.IndentedJSON(http.StatusOK, txRespStatus)
}
func deleteMembershipTokenStatus(c *gin.Context) {
var newMembershipTokenStatus MembershipTokenStatus
if err := c.BindJSON(&newMembershipTokenStatus); err != nil {
return
}
msgStatus := &types.MsgDeleteMembershipTokenStatus{
Creator: addr,
MembershipTokenStatusID: newMembershipTokenStatus.ID,
TokenID: newMembershipTokenStatus.TokenID,
}
txRespStatus, transerrStatus := cosmos.BroadcastTx(account, msgStatus)
if transerrStatus != nil {
log.Fatal(transerrStatus)
}
fmt.Print("MsgDeleteMembershipTokenStatus:\n\n")
fmt.Println(txRespStatus)
msgNewStatus := &types.MsgCreateMembershipTokenStatus{
Creator: addr,
TokenID: newMembershipTokenStatus.TokenID,
Timestamp: newMembershipTokenStatus.Timestamp,
Status: newMembershipTokenStatus.Status,
}
txRespNewStatus, transerrNewStatus := cosmos.BroadcastTx(account, msgNewStatus)
if transerrNewStatus != nil {
log.Fatal(transerrStatus)
}
fmt.Print("MsgCreateMembershipTokenStatus:\n\n")
fmt.Println(txRespNewStatus)
c.IndentedJSON(http.StatusOK, txRespStatus)
}
func getAddr(c *gin.Context) {
c.IndentedJSON(http.StatusOK, addr)
}
func main() {
router := gin.Default()
router.Use(CORSMiddleware())
router.GET("/addr", getAddr)
router.POST("/discountToken", createDiscountToken)
router.POST("/membershipToken", createMembershipToken)
router.POST("/discountTokenStatus", deleteDiscountTokenStatus)
router.POST("/membershipTokenStatus", deleteMembershipTokenStatus)
router.Run("localhost:8080")
}