This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
deploy.go
135 lines (118 loc) · 2.61 KB
/
deploy.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"os"
"io/ioutil"
"context"
"crypto/ecdsa"
"fmt"
"log"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/common"
contract "./contracts"
)
type UserType struct {
Purse string
AddressHex string
AddressEth common.Address
PublicKey *ecdsa.PublicKey
PrivateKey *ecdsa.PrivateKey
}
var (
ClientETH = connectToETH("http://127.0.0.1:5555")
User *UserType
)
func init() {
if len(os.Args) < 2 {
panic("failed: len(os.Args) < 2")
}
var (
userLoadStr = ""
userLoadExist = false
)
for i := 1; i < len(os.Args); i++ {
arg := os.Args[i]
switch {
case strings.HasPrefix(arg, "-loaduser:"):
userLoadStr = strings.Replace(arg, "-loaduser:", "", 1)
userLoadExist = true
}
}
if !userLoadExist {
panic("failed: !userLoadExist")
}
if ClientETH == nil {
panic("failed: connect to ETH")
}
User = userLoad(userLoadStr)
if User == nil {
panic("failed: load user")
}
}
// Deploy contract and save address in file.
func main() {
auth := resetAuth(User)
address, tx, instance, err := contract.DeployContract(auth, ClientETH)
if err != nil {
log.Fatal(err)
}
fmt.Println(address.Hex())
fmt.Println(tx.Hash().Hex())
_ = instance
contractFile := "contract.address"
writeFile(contractFile, address.Hex())
}
func writeFile(filename string, data string) error {
return ioutil.WriteFile(filename, []byte(data), 0644)
}
func userLoad(purse string) *UserType {
priv, err := crypto.HexToECDSA(purse)
if err != nil {
return nil
}
pub, ok := priv.Public().(*ecdsa.PublicKey)
if !ok {
return nil
}
addressHex := crypto.PubkeyToAddress(*pub).Hex()
addressEth := common.HexToAddress(addressHex)
return &UserType{
Purse: purse,
AddressHex: addressHex,
AddressEth: addressEth,
PublicKey: pub,
PrivateKey: priv,
}
}
func connectToETH(address string) *ethclient.Client {
client, err := ethclient.Dial(address)
if err != nil {
return nil
}
return client
}
func resetAuth(user *UserType) *bind.TransactOpts {
nonce, err := ClientETH.PendingNonceAt(context.Background(), User.AddressEth)
if err != nil {
return nil
}
gasPrice, err := ClientETH.SuggestGasPrice(context.Background())
if err != nil {
return nil
}
auth := bind.NewKeyedTransactor(user.PrivateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0)
// auth.GasLimit = uint64(3000000)
auth.GasPrice = gasPrice
return auth
}
func fileIsExist(name string) bool {
if _, err := os.Stat(name); os.IsNotExist(err) {
return false
}
return true
}