-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethdial.go
251 lines (229 loc) · 5.59 KB
/
ethdial.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package ethdial
import (
"context"
"crypto/ecdsa"
"math/big"
"time"
ethereum "github.com/ethereum/go-ethereum"
"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"
)
type FuncTran func(*EthDial)
type EthDial struct {
EthAddr common.Address
EthCall string
EthCancel context.CancelFunc
EthChainId *big.Int
EthClient *ethclient.Client
EthCoin *big.Int
EthContext context.Context
EthData string
Error error
EthFrom common.Address
EthLimit uint64
EthPrice *big.Int
EthNonce uint64
Result *big.Int
EthSign *ecdsa.PrivateKey
EthTran common.Hash
EthTranFunc FuncTran
EthTranStatus string
EthTranString string
EthTranWait int
EthWait int
Transaction string
}
// initialize struct with some defaults
func New() *EthDial {
return &EthDial{
EthCoin: big.NewInt(0),
EthWait: 10}
}
// make a connection to the endpoint with a context timeout
func (e *EthDial) Dial(a string) *EthDial {
e.EthClient, e.Error = ethclient.Dial(a)
if e.Error == nil {
e.EthContext, e.EthCancel = context.WithDeadline(context.Background(), time.Now().Add(time.Duration(e.EthWait)*time.Second))
if e.EthChainId == nil {
e.EthChainId, e.Error = e.EthClient.NetworkID(e.EthContext)
}
if e.Error == nil {
if e.EthCall == "" {
e = e.TxSend()
} else {
if e.EthSign == nil {
e = e.TxCallFree()
} else {
e = e.TxCallPaid()
}
}
}
e.EthCancel()
}
if e.EthTranFunc != nil || e.EthTranWait > 0 {
go e.TranWait()
}
return e
}
func (e *EthDial) TranWait() {
e.EthTranStatus = "unknown"
ctx := context.Background()
e.EthTranWait = int(time.Now().Unix()) + e.EthTranWait
for {
time.Sleep(10 * time.Second)
receipt, _ := e.EthClient.TransactionReceipt(ctx, e.EthTran)
if receipt == nil {
e.EthTranStatus = "pending"
} else {
switch receipt.Status {
case 0:
e.EthTranStatus = "failure"
case 1:
e.EthTranStatus = "success"
default:
e.EthTranStatus = "unknown"
}
break
}
if int(time.Now().Unix()) > e.EthTranWait {
e.EthTranStatus = "timeout"
}
}
if e.EthTranFunc != nil {
e.EthTranFunc(e)
}
}
func (e *EthDial) TxCallFree() *EthDial {
msg := ethereum.CallMsg{
From: e.EthFrom,
To: &e.EthAddr,
Gas: e.EthLimit,
GasPrice: e.EthPrice,
Value: e.EthCoin,
Data: common.Hex2Bytes(e.EthCall + e.EthData)}
result, err := e.EthClient.CallContract(e.EthContext, msg, nil)
e.Error = err
if e.Error == nil {
e.Result = new(big.Int).SetBytes(result)
}
return e
}
func (e *EthDial) TxCallPaid() *EthDial {
e.EthFrom = crypto.PubkeyToAddress(e.EthSign.PublicKey)
e.EthNonce, e.Error = e.EthClient.NonceAt(e.EthContext, e.EthFrom, nil)
if e.Error == nil {
utx := types.NewTransaction(e.EthNonce, e.EthAddr, e.EthCoin, e.EthLimit, e.EthPrice, common.Hex2Bytes(e.EthCall+e.EthData))
stx, err := types.SignTx(utx, types.NewEIP155Signer(e.EthChainId), e.EthSign)
e.Error = err
if e.Error == nil {
e.Error = e.EthClient.SendTransaction(e.EthContext, stx)
if e.Error == nil {
e.EthTran = stx.Hash()
e.EthTranString = stx.Hash().String()
}
}
}
return e
}
func (e *EthDial) TxSend() *EthDial {
e.EthFrom = crypto.PubkeyToAddress(e.EthSign.PublicKey)
e.EthNonce, e.Error = e.EthClient.NonceAt(e.EthContext, e.EthFrom, nil)
if e.Error == nil {
utx := types.NewTransaction(e.EthNonce, e.EthAddr, e.EthCoin, e.EthLimit, e.EthPrice, nil)
stx, err := types.SignTx(utx, types.NewEIP155Signer(e.EthChainId), e.EthSign)
e.Error = err
if e.Error == nil {
e.Error = e.EthClient.SendTransaction(e.EthContext, stx)
if e.Error == nil {
e.EthTran = stx.Hash()
e.EthTranString = stx.Hash().String()
}
}
}
return e
}
// set the 'signing' (private) address
func (e *EthDial) Sign(a string) *EthDial {
if e.Error == nil {
e.EthSign, e.Error = crypto.HexToECDSA(a)
}
return e
}
// set the ethereum network chainId
func (e *EthDial) Chain(a *big.Int) *EthDial {
if e.Error == nil {
e.EthChainId = a
}
return e
}
// set how long for the context to timeout
func (e *EthDial) Wait(a int) *EthDial {
if e.Error == nil {
e.EthWait = a
}
return e
}
// set the gasLimit
func (e *EthDial) Limit(a uint64) *EthDial {
if e.Error == nil {
e.EthLimit = a
}
return e
}
// set the amount of coin to be sent
func (e *EthDial) Coin(a *big.Int) *EthDial {
if e.Error == nil {
e.EthCoin = a
}
return e
}
// set the gasPrice
func (e *EthDial) Price(a *big.Int) *EthDial {
if e.Error == nil {
e.EthPrice = a
}
return e
}
// set the 'from' address
// TODO this can be calculated from the 'sign' address
func (e *EthDial) From(a string) *EthDial {
if e.Error == nil {
e.EthFrom = common.HexToAddress(a)
}
return e
}
// set the 'to' address
func (e *EthDial) Addr(a string) *EthDial {
if e.Error == nil {
e.EthAddr = common.HexToAddress(a)
}
return e
}
// set the called function in the 'addr' contract
func (e *EthDial) Call(a string) *EthDial {
if e.Error == nil {
e.EthCall = crypto.Keccak256Hash([]byte(a)).String()[2:10]
}
return e
}
// build a 'data' string as part of calling a contract function
func (e *EthDial) DataInt(a int) *EthDial {
if e.Error == nil {
e.EthData += leftPadHex(unHex(big.NewInt(int64(a)).Text(16)), 64)
}
return e
}
func (e *EthDial) DataBigInt(a *big.Int) *EthDial {
if e.Error == nil {
e.EthData += leftPadHex(unHex(a.Text(16)), 64)
}
return e
}
//
func (e *EthDial) Tran(wait int, fTran FuncTran) *EthDial {
e.EthTranWait = wait
e.EthTranFunc = fTran
return e
}