-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathethutils.go
More file actions
116 lines (99 loc) · 3.39 KB
/
ethutils.go
File metadata and controls
116 lines (99 loc) · 3.39 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ethcontract
import (
"encoding/json"
"io/ioutil"
"reflect"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
// ETransact allows complex transaction info to be assesible if needed, so we dont
// need to constantly return it
type ETransact struct {
Address *common.Address
Contract *bind.BoundContract
TxHash *common.Hash
Tx *types.Transaction
}
type EClient struct {
conn bind.ContractBackend //*ethclient.Client
auth *bind.TransactOpts
LastTranasction *ETransact
}
func NewEthUtil(dialpath string) (*EClient, error) {
// Create an IPC based RPC connection to a remote node and an authorized transactor
conn, err := ethclient.Dial(dialpath)
if err != nil {
return nil, err
}
return &EClient{conn: conn, LastTranasction: &ETransact{}}, err
}
// SetWalletPrivateKey takes in a key string
func (e *EClient) SetWalletPrivateKey(keydata string) {
keyBytes := common.FromHex(keydata)
key := crypto.ToECDSAUnsafe(keyBytes)
e.auth = bind.NewKeyedTransactor(key)
}
// DeployContractSimple deploys a simple contract with an abi and bin data. If the contract
// has contructor arguments, we will automatically generate zero values for them
func (e *EClient) DeployContractSimple(contractAbi string, contractBin string) (*common.Address, error) {
// Deploy a new awesome contract for the binding demo
parsed, err := abi.JSON(strings.NewReader(contractAbi))
if err != nil {
return nil, err
}
return e.deployContractSimple(parsed, contractBin)
}
func (e *EClient) deployContractSimple(parsed abi.ABI, contractBin string) (*common.Address, error) {
inputs := parsed.Constructor.Inputs
var dataInputs []interface{} = make([]interface{}, 0)
//Default all the parameters
for _, i := range inputs {
t := reflect.Zero(i.Type.Type)
v := t.Interface()
if t.Kind() == reflect.Ptr && t.IsNil() {
elem := reflect.TypeOf(v).Elem()
v2 := reflect.New(elem)
v = v2.Interface()
}
dataInputs = append(dataInputs, v)
}
address, tx, contract, err := bind.DeployContract(e.auth, parsed, common.FromHex(contractBin), e.conn, dataInputs...)
if err != nil {
return nil, err
}
// fmt.Printf("Contract pending deploy: 0x%x\n", address)
// fmt.Printf("Transaction waiting to be mined: 0x%x\n\n", tx.Hash())
// fmt.Printf("Contract Object-%v", contract)
e.LastTranasction.Address = &address
e.LastTranasction.Contract = contract
h := tx.Hash()
e.LastTranasction.TxHash = &h
e.LastTranasction.Tx = tx
return &address, nil
}
type TruffleContract struct {
Abi abi.ABI `json:"abi"`
BinaryData string `json:"unlinked_binary"`
}
func (e *EClient) DeployContractTruffle(truffleJsonData string) (*common.Address, error) {
var truffleContract TruffleContract
err := json.Unmarshal([]byte(truffleJsonData), &truffleContract)
if err != nil {
return nil, err
}
return e.deployContractSimple(truffleContract.Abi, truffleContract.BinaryData)
}
func (e *EClient) DeployContractTruffleFromFile(filename string) (*common.Address, error) {
// read file into string
data, err := ioutil.ReadFile(filename) // just pass the file name
if err != nil {
return nil, err
}
//Pass to deploy truffle contract
return e.DeployContractTruffle(string(data))
}