/
allocation.go
97 lines (87 loc) · 1.7 KB
/
allocation.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
package main
/*
#include <stdlib.h>
*/
import (
"C"
)
import (
"encoding/json"
"github.com/0chain/gosdk/zboxapi"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zcncore"
)
// GetAllocation get allocation info
// ## Inputs
// - allocationID
//
// ## Output
//
// {
// "error":"",
// "result":"{}",
// }
//
//export GetAllocation
func GetAllocation(allocationID *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
allocID := C.GoString(allocationID)
return WithJSON(getAllocation(allocID))
}
// ListAllocations get allocation list
// ## Output
//
// {
// "error":"",
// "result":"[{},{}]",
// }
//
//export ListAllocations
func ListAllocations() *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
items, err := sdk.GetAllocations()
if err != nil {
log.Error("win: ", err)
return WithJSON("", err)
}
return WithJSON(items, nil)
}
// CreateFreeAllocation create a free allocation
// ## Inputs
// - freeMarker
// return
// {
// "error":"",
// "result":"id",
// }
//
//export CreateFreeAllocation
func CreateFreeAllocation(freemarker *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
marker := &zboxapi.FreeMarker{}
js := C.GoString(freemarker)
err := json.Unmarshal([]byte(js), marker)
if err != nil {
log.Error("win: ", err)
return WithJSON("", err)
}
lock := zcncore.ConvertToValue(marker.FreeTokens)
allocationID, _, err := sdk.CreateFreeAllocation(js, lock)
if err != nil {
log.Error("win: ", err, "lock: ", lock, " marker:", js)
return WithJSON("", err)
}
return WithJSON(allocationID, nil)
}