-
Notifications
You must be signed in to change notification settings - Fork 9
/
instance.go
85 lines (70 loc) · 2.01 KB
/
instance.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
package equity
import (
"encoding/hex"
"errors"
"strconv"
chainjson "github.com/bytom/vapor/encoding/json"
"github.com/bytom/vapor/equity/compiler"
)
// InstantiateContract instantiate contract parameters
func InstantiateContract(contract *compiler.Contract, args []compiler.ContractArg) ([]byte, error) {
program, err := compiler.Instantiate(contract.Body, contract.Params, contract.Recursive, args)
if err != nil {
return nil, err
}
return program, nil
}
func ConvertArguments(contract *compiler.Contract, args []string) ([]compiler.ContractArg, error) {
var contractArgs []compiler.ContractArg
for i, p := range contract.Params {
var argument compiler.ContractArg
switch p.Type {
case "Boolean":
var boolValue bool
if args[i] == "true" || args[i] == "1" {
boolValue = true
} else if args[i] == "false" || args[i] == "0" {
boolValue = false
} else {
return nil, errors.New("mismatch Boolean argument")
}
argument.B = &boolValue
case "Amount":
amount, err := strconv.ParseUint(args[i], 10, 64)
if err != nil {
return nil, err
}
if amount > uint64(1<<uint(63)) {
return nil, errors.New("the Amount argument exceeds max int64")
}
amountValue := int64(amount)
argument.I = &amountValue
case "Integer":
integerValue, err := strconv.ParseInt(args[i], 10, 64)
if err != nil {
return nil, err
}
argument.I = &integerValue
case "Asset", "Hash", "PublicKey":
if len(args[i]) != 64 {
return nil, errors.New("mismatch length for Asset/Hash/PublicKey argument")
}
commonValue, err := hex.DecodeString(args[i])
if err != nil {
return nil, err
}
argument.S = (*chainjson.HexBytes)(&commonValue)
case "Program":
program, err := hex.DecodeString(args[i])
if err != nil {
return nil, err
}
argument.S = (*chainjson.HexBytes)(&program)
case "String":
strValue := []byte(args[i])
argument.S = (*chainjson.HexBytes)(&strValue)
}
contractArgs = append(contractArgs, argument)
}
return contractArgs, nil
}