-
Notifications
You must be signed in to change notification settings - Fork 113
/
contract.go
99 lines (86 loc) · 1.58 KB
/
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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package itest
import (
"io/ioutil"
"github.com/iost-official/go-iost/core/contract"
)
// Constant of Contract
const (
DefaultCode = `
class Test {
init() {
//Execute once when contract is packed into a block
}
transfer(from, to, amount, memo) {
//Function called by other
blockchain.transfer(from, to, amount, memo)
}
};
module.exports = Test;
`
DefaultABI = `
{
"lang": "javascript",
"version": "1.0.0",
"abi": [
{
"name": "transfer",
"args": [
"string",
"string",
"string",
"string"
],
"amountLimit": [
{
"token": "iost",
"val": "100"
}
]
}
]
}
`
)
// Contract is the contract object
type Contract struct {
*contract.Contract
}
// NewContract will return a new contract
func NewContract(code, abi string) (*Contract, error) {
c, err := (&contract.Compiler{}).Parse("", code, abi)
if err != nil {
return nil, err
}
contract := &Contract{
Contract: c,
}
return contract, nil
}
// LoadContract will load a contract from file
func LoadContract(codepath, abipath string) (*Contract, error) {
code := DefaultCode
abi := DefaultABI
if codepath != "" {
data, err := ioutil.ReadFile(codepath)
if err != nil {
return nil, err
}
code = string(data)
}
if abipath != "" {
data, err := ioutil.ReadFile(abipath)
if err != nil {
return nil, err
}
abi = string(data)
}
contract, err := NewContract(code, abi)
if err != nil {
return nil, err
}
return contract, nil
}
// String will return the string of contract
func (c *Contract) String() string {
return c.B64Encode()
}