-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_contract.go
50 lines (43 loc) · 1.31 KB
/
manage_contract.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
package types
import (
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/common"
)
const (
ManageContractProtocolName = "brczero"
ManageCreateContract = "create"
ManageCallContract = "call"
)
type ManageContract struct {
Protocol string `json:"p" yaml:"p"`
Operation string `json:"op" yaml:"op"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Contract string `json:"contract,omitempty" yaml:"contract,omitempty"`
WaitInit uint8 `json:"wait_init" yaml:"waitInit"`
CallData string `json:"d" yaml:"d"`
}
func (mc ManageContract) ValidateBasic() error {
switch mc.Operation {
case ManageCreateContract:
if len(mc.Name) == 0 {
return ErrValidateBasic("name can not be empty when create contract")
}
case ManageCallContract:
if len(mc.Contract) == 0 {
return ErrValidateBasic("contract can not be empty when call contract")
}
if !common.IsHexAddress(mc.Contract) {
return ErrValidateBasic("contract must be hex address when call contract")
}
default:
return ErrUnknownOperationOfManageContract(mc.Operation)
}
if _, err := hex.DecodeString(mc.CallData); err != nil {
return ErrValidateBasic(fmt.Sprintf("decode callData error: %s", err.Error()))
}
return nil
}
func (mc ManageContract) GetCallData() ([]byte, error) {
return hex.DecodeString(mc.CallData)
}