OSDN Git Service

Hulk did something
[bytom/vapor.git] / equity / equity / util / instance.go
1 package equity
2
3 import (
4         "encoding/hex"
5         "errors"
6         "strconv"
7
8         chainjson "github.com/vapor/encoding/json"
9
10         "github.com/vapor/equity/compiler"
11 )
12
13 // InstantiateContract instantiate contract parameters
14 func InstantiateContract(contract *compiler.Contract, args []compiler.ContractArg) ([]byte, error) {
15         program, err := compiler.Instantiate(contract.Body, contract.Params, contract.Recursive, args)
16         if err != nil {
17                 return nil, err
18         }
19
20         return program, nil
21 }
22
23 func ConvertArguments(contract *compiler.Contract, args []string) ([]compiler.ContractArg, error) {
24         var contractArgs []compiler.ContractArg
25         for i, p := range contract.Params {
26                 var argument compiler.ContractArg
27                 switch p.Type {
28                 case "Boolean":
29                         var boolValue bool
30                         if args[i] == "true" || args[i] == "1" {
31                                 boolValue = true
32                         } else if args[i] == "false" || args[i] == "0" {
33                                 boolValue = false
34                         } else {
35                                 return nil, errors.New("mismatch Boolean argument")
36                         }
37                         argument.B = &boolValue
38
39                 case "Amount":
40                         amount, err := strconv.ParseUint(args[i], 10, 64)
41                         if err != nil {
42                                 return nil, err
43                         }
44
45                         if amount > uint64(1<<uint(63)) {
46                                 return nil, errors.New("the Amount argument exceeds max int64")
47                         }
48                         amountValue := int64(amount)
49                         argument.I = &amountValue
50
51                 case "Integer":
52                         integerValue, err := strconv.ParseInt(args[i], 10, 64)
53                         if err != nil {
54                                 return nil, err
55                         }
56                         argument.I = &integerValue
57
58                 case "Asset", "Hash", "PublicKey":
59                         if len(args[i]) != 64 {
60                                 return nil, errors.New("mismatch length for Asset/Hash/PublicKey argument")
61                         }
62
63                         commonValue, err := hex.DecodeString(args[i])
64                         if err != nil {
65                                 return nil, err
66                         }
67                         argument.S = (*chainjson.HexBytes)(&commonValue)
68
69                 case "Program":
70                         program, err := hex.DecodeString(args[i])
71                         if err != nil {
72                                 return nil, err
73                         }
74                         argument.S = (*chainjson.HexBytes)(&program)
75
76                 case "String":
77                         strValue := []byte(args[i])
78                         argument.S = (*chainjson.HexBytes)(&strValue)
79
80                 }
81                 contractArgs = append(contractArgs, argument)
82         }
83
84         return contractArgs, nil
85 }