-
Notifications
You must be signed in to change notification settings - Fork 0
/
contracts.go
60 lines (49 loc) · 1.51 KB
/
contracts.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
package ethereum
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/core/types"
)
// Contract is an interface defines how a vault interact with the smart contract
type Contract interface {
// Deploy deploys the smart contract to ethereum blockchain
Deploy(
wallet *Wallet,
arguments json.RawMessage) (address string, txID string, err error)
// Call calls a smart contract method
Call(
wallet *Wallet,
method,
fund string,
arguments json.RawMessage,
noSend bool,
gasLimit uint64,
gasPrice *int64,
nonce *uint64) (tx *types.Transaction, err error)
// Pack packs the method and arguments into a byte array representing
// the smart contract call data
Pack(
method string,
arguments json.RawMessage) ([]byte, error)
// Parse parses the arguments as JSON format into an array of smart
// contract function call arguments
Parse(
method string,
arguments json.RawMessage) ([]interface{}, error)
}
// ContractFactory is a function that takes an address and return a Contract instance
type ContractFactory func(string) Contract
// contracts is a singleton of all registered contract factory function
var contracts = map[string]ContractFactory{}
// GetContract returns
func GetContract(name string) ContractFactory {
return contracts[name]
}
// RegisterContract registers a contract
func RegisterContract(name string, contract ContractFactory) error {
if _, ok := contracts[name]; ok {
return fmt.Errorf("duplicated contract name has registered")
}
contracts[name] = contract
return nil
}