forked from fiatjaf/etleneum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontract_functions.go
62 lines (48 loc) · 1023 Bytes
/
contract_functions.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
package main
import (
"encoding/json"
"strings"
"time"
"github.com/aarzilli/golua/lua"
"github.com/fiatjaf/etleneum/data"
)
func contractFromRedis(ctid string) (ct *data.Contract, err error) {
var jct []byte
ct = &data.Contract{}
jct, err = rds.Get("contract:" + ctid).Bytes()
if err != nil {
return
}
err = json.Unmarshal(jct, ct)
if err != nil {
return
}
return
}
func checkContractCode(code string) (ok bool) {
if strings.Index(code, "function __init__") == -1 {
return false
}
L := lua.NewState()
defer L.Close()
lerr := L.LoadString(code)
if lerr != 0 {
return false
}
return true
}
func getContractCost(ct data.Contract) int64 {
words := int64(len(wordMatcher.FindAllString(ct.Code, -1)))
return 1000*s.InitialContractCostSatoshis + 1000*words
}
func saveContractOnRedis(ct data.Contract) (jct []byte, err error) {
jct, err = json.Marshal(ct)
if err != nil {
return
}
err = rds.Set("contract:"+ct.Id, jct, time.Hour*20).Err()
if err != nil {
return
}
return
}