-
Notifications
You must be signed in to change notification settings - Fork 113
/
contract_handler.go
43 lines (36 loc) · 984 Bytes
/
contract_handler.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
package database
import (
"github.com/iost-official/go-iost/core/contract"
)
// ContractPrefix ...
const ContractPrefix = "c-"
// ContractHandler ...
type ContractHandler struct {
db database
}
// SetContract set contract to storage, will not do check
func (m *ContractHandler) SetContract(contract *contract.Contract) {
if contract != nil {
m.db.Put(ContractPrefix+contract.ID, contract.Encode())
} else {
panic("set a nil contract")
}
}
// Contract get contract by key
func (m *ContractHandler) Contract(key string) (c *contract.Contract) {
str := m.db.Get(ContractPrefix + key)
c = &contract.Contract{}
err := c.Decode(str)
if err != nil {
return nil
}
return
}
// HasContract determine if contract existed
func (m *ContractHandler) HasContract(key string) bool {
return m.db.Has(ContractPrefix + key)
}
// DelContract delete contract, if contract not exist, do nothing
func (m *ContractHandler) DelContract(key string) {
m.db.Del(ContractPrefix + key)
}