This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
454 lines (389 loc) · 14.4 KB
/
util.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package seth
import (
"context"
"database/sql/driver"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math/big"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
"github.com/pkg/errors"
network_debug_contract "github.com/smartcontractkit/seth/contracts/bind/debug"
network_sub_debug_contract "github.com/smartcontractkit/seth/contracts/bind/sub"
)
const (
ErrInsufficientRootKeyBalance = "insufficient root key balance: %s"
)
// FundingDetails funding details about shares we put into test keys
type FundingDetails struct {
RootBalance *big.Int
TotalFee *big.Int
FreeBalance *big.Int
AddrFunding *big.Int
NetworkTransferFee int64
}
// NewEphemeralKeys creates desired number of ephemeral keys, should be used only with ephemeral networks. Remember that they are not persisted anywhere, so you shouldn't use that option with live networks.
func NewEphemeralKeys(addrs int64) ([]string, error) {
privKeys := make([]string, 0)
for i := 0; i < int(addrs); i++ {
_, pKey, err := NewAddress()
if err != nil {
return nil, err
}
privKeys = append(privKeys, pKey)
}
return privKeys, nil
}
// CalculateSubKeyFunding calculates all required params to split funds from the root key to N test keys
func (m *Client) CalculateSubKeyFunding(addrs, gasPrice, rooKeyBuffer int64) (*FundingDetails, error) {
balance, err := m.Client.BalanceAt(context.Background(), m.Addresses[0], nil)
if err != nil {
return nil, err
}
gasLimit := m.Cfg.Network.TransferGasFee
newAddress, _, err := NewAddress()
if err == nil {
gasLimitRaw, err := m.EstimateGasLimitForFundTransfer(m.Addresses[0], common.HexToAddress(newAddress), big.NewInt(0).Quo(balance, big.NewInt(addrs)))
if err == nil {
gasLimit = int64(gasLimitRaw)
}
}
networkTransferFee := gasPrice * gasLimit
totalFee := new(big.Int).Mul(big.NewInt(networkTransferFee), big.NewInt(addrs))
rootKeyBuffer := new(big.Int).Mul(big.NewInt(rooKeyBuffer), big.NewInt(1_000_000_000_000_000_000))
freeBalance := new(big.Int).Sub(balance, big.NewInt(0).Add(totalFee, rootKeyBuffer))
L.Info().
Str("Balance (wei/ether)", fmt.Sprintf("%s/%s", balance.String(), WeiToEther(balance).Text('f', -1))).
Str("Total fee (wei/ether)", fmt.Sprintf("%s/%s", totalFee.String(), WeiToEther(totalFee).Text('f', -1))).
Str("Free Balance (wei/ether)", fmt.Sprintf("%s/%s", freeBalance.String(), WeiToEther(freeBalance).Text('f', -1))).
Str("Buffer (wei/ether)", fmt.Sprintf("%s/%s", rootKeyBuffer.String(), WeiToEther(rootKeyBuffer).Text('f', -1))).
Msg("Root key balance")
if freeBalance.Cmp(big.NewInt(0)) < 0 {
return nil, errors.New(fmt.Sprintf(ErrInsufficientRootKeyBalance, freeBalance.String()))
}
addrFunding := new(big.Int).Div(freeBalance, big.NewInt(addrs))
requiredBalance := big.NewInt(0).Mul(addrFunding, big.NewInt(addrs))
L.Debug().
Str("Funding per ephemeral key (wei/ether)", fmt.Sprintf("%s/%s", addrFunding.String(), WeiToEther(addrFunding).Text('f', -1))).
Str("Available balance (wei/ether)", fmt.Sprintf("%s/%s", freeBalance.String(), WeiToEther(freeBalance).Text('f', -1))).
Interface("Required balance (wei/ether)", fmt.Sprintf("%s/%s", requiredBalance.String(), WeiToEther(requiredBalance).Text('f', -1))).
Msg("Using hardcoded ephemeral funding")
if freeBalance.Cmp(requiredBalance) < 0 {
return nil, errors.New(fmt.Sprintf(ErrInsufficientRootKeyBalance, freeBalance.String()))
}
bd := &FundingDetails{
RootBalance: balance,
TotalFee: totalFee,
FreeBalance: freeBalance,
AddrFunding: addrFunding,
NetworkTransferFee: networkTransferFee,
}
L.Info().
Interface("RootBalance", bd.RootBalance.String()).
Interface("RootKeyBuffer", rootKeyBuffer.String()).
Interface("TransferFeesTotal", bd.TotalFee.String()).
Interface("NetworkTransferFee", bd.NetworkTransferFee).
Interface("FreeBalance", bd.FreeBalance.String()).
Interface("EachAddrGets", bd.AddrFunding.String()).
Msg("Splitting funds from the root account")
return bd, nil
}
func (m *Client) DeployDebugSubContract() (*network_sub_debug_contract.NetworkDebugSubContract, common.Address, error) {
address, tx, instance, err := network_sub_debug_contract.DeployNetworkDebugSubContract(m.NewTXOpts(), m.Client)
if err != nil {
return nil, common.Address{}, err
}
L.Info().
Str("Address", address.Hex()).
Str("TXHash", tx.Hash().Hex()).
Msg("Deploying sub-debug contract")
if _, err := bind.WaitDeployed(context.Background(), m.Client, tx); err != nil {
return nil, common.Address{}, err
}
L.Info().
Str("Address", address.Hex()).
Str("TXHash", tx.Hash().Hex()).
Msg("Sub-debug contract deployed")
return instance, address, nil
}
func (m *Client) DeployDebugContract(subDbgAddr common.Address) (*network_debug_contract.NetworkDebugContract, common.Address, error) {
address, tx, instance, err := network_debug_contract.DeployNetworkDebugContract(m.NewTXOpts(), m.Client, subDbgAddr)
if err != nil {
return nil, common.Address{}, err
}
L.Info().
Str("Address", address.Hex()).
Str("TXHash", tx.Hash().Hex()).
Msg("Deploying debug contract")
if _, err := bind.WaitDeployed(context.Background(), m.Client, tx); err != nil {
return nil, common.Address{}, err
}
L.Info().
Str("Address", address.Hex()).
Str("TXHash", tx.Hash().Hex()).
Msg("Debug contract deployed")
return instance, address, nil
}
// Duration is a non-negative time duration.
type Duration struct{ D time.Duration }
func MakeDuration(d time.Duration) (Duration, error) {
if d < time.Duration(0) {
return Duration{}, fmt.Errorf("cannot make negative time duration: %s", d)
}
return Duration{D: d}, nil
}
func ParseDuration(s string) (Duration, error) {
d, err := time.ParseDuration(s)
if err != nil {
return Duration{}, err
}
return MakeDuration(d)
}
func MustMakeDuration(d time.Duration) *Duration {
rv, err := MakeDuration(d)
if err != nil {
panic(err)
}
return &rv
}
// Duration returns the value as the standard time.Duration value.
func (d Duration) Duration() time.Duration {
return d.D
}
// Before returns the time d units before time t
func (d Duration) Before(t time.Time) time.Time {
return t.Add(-d.Duration())
}
// Shorter returns true if and only if d is shorter than od.
func (d Duration) Shorter(od Duration) bool { return d.D < od.D }
// IsInstant is true if and only if d is of duration 0
func (d Duration) IsInstant() bool { return d.D == 0 }
// String returns a string representing the duration in the form "72h3m0.5s".
// Leading zero units are omitted. As a special case, durations less than one
// second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure
// that the leading digit is non-zero. The zero duration formats as 0s.
func (d Duration) String() string {
return d.Duration().String()
}
// MarshalJSON implements the json.Marshaler interface.
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (d *Duration) UnmarshalJSON(input []byte) error {
var txt string
err := json.Unmarshal(input, &txt)
if err != nil {
return err
}
v, err := time.ParseDuration(string(txt))
if err != nil {
return err
}
*d, err = MakeDuration(v)
if err != nil {
return err
}
return nil
}
func (d *Duration) Scan(v interface{}) (err error) {
switch tv := v.(type) {
case int64:
*d, err = MakeDuration(time.Duration(tv))
return err
default:
return errors.Errorf(`don't know how to parse "%s" of type %T as a `+
`models.Duration`, tv, tv)
}
}
func (d Duration) Value() (driver.Value, error) {
return int64(d.D), nil
}
// MarshalText implements the text.Marshaler interface.
func (d Duration) MarshalText() ([]byte, error) {
return []byte(d.D.String()), nil
}
// UnmarshalText implements the text.Unmarshaler interface.
func (d *Duration) UnmarshalText(input []byte) error {
v, err := time.ParseDuration(string(input))
if err != nil {
return err
}
pd, err := MakeDuration(v)
if err != nil {
return err
}
*d = pd
return nil
}
func saveAsJson(v any, dirName, name string) (string, error) {
pwd, err := os.Getwd()
if err != nil {
return "", err
}
dir := filepath.Join(pwd, dirName)
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return "", err
}
}
confPath := filepath.Join(dir, fmt.Sprintf("%s.json", name))
f, _ := json.MarshalIndent(v, "", " ")
err = os.WriteFile(confPath, f, 0600)
return confPath, err
}
func OpenJsonFileAsStruct(path string, v any) error {
jsonFile, err := os.Open(path)
if err != nil {
return err
}
defer jsonFile.Close()
b, _ := io.ReadAll(jsonFile)
err = json.Unmarshal(b, v)
if err != nil {
return err
}
return nil
}
// CreateOrAppendToJsonArray appends to a JSON array in a file or creates a new JSON array if the file is empty or doesn't exist
func CreateOrAppendToJsonArray(filePath string, newItem any) error {
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
size, err := f.Seek(0, io.SeekEnd)
if err != nil {
return err
}
jsonBytes, err := json.Marshal(newItem)
if err != nil {
return err
}
jsonValue := string(jsonBytes)
if size == 0 {
_, err = f.WriteString(fmt.Sprintf("[%s]", jsonValue))
} else {
// Move cursor back by one character, so we can append data just before array end.
_, err = f.Seek(-1, io.SeekEnd)
if err != nil {
return err
}
_, err = f.WriteString(fmt.Sprintf(",\n%s]", jsonValue))
}
return err
}
// EtherToWei converts an ETH float amount to wei
func EtherToWei(eth *big.Float) *big.Int {
truncInt, _ := eth.Int(nil)
truncInt = new(big.Int).Mul(truncInt, big.NewInt(params.Ether))
fracStr := strings.Split(fmt.Sprintf("%.18f", eth), ".")[1]
fracStr += strings.Repeat("0", 18-len(fracStr))
fracInt, _ := new(big.Int).SetString(fracStr, 10)
wei := new(big.Int).Add(truncInt, fracInt)
return wei
}
// WeiToEther converts a wei amount to eth float
func WeiToEther(wei *big.Int) *big.Float {
f := new(big.Float)
f.SetPrec(236) // IEEE 754 octuple-precision binary floating-point format: binary256
f.SetMode(big.ToNearestEven)
fWei := new(big.Float)
fWei.SetPrec(236) // IEEE 754 octuple-precision binary floating-point format: binary256
fWei.SetMode(big.ToNearestEven)
return f.Quo(fWei.SetInt(wei), big.NewFloat(params.Ether))
}
const (
MetadataNotFoundErr = "metadata section not found"
InvalidMetadataLengthErr = "invalid metadata length"
FailedToDecodeMetadataErr = "failed to decode metadata"
NotCompiledWithSolcErr = "not compiled with solc"
)
// Pragma represents the version of the Solidity compiler used to compile the contract
type Pragma struct {
Minor uint64
Major uint64
Patch uint64
}
// String returns the string representation of the Pragma
func (p Pragma) String() string {
return fmt.Sprintf("%d.%d.%d", p.Major, p.Minor, p.Patch)
}
// DecodePragmaVersion extracts the pragma version from the bytecode or returns an error if it's not found or can't be decoded.
// Based on https://www.rareskills.io/post/solidity-metadata
func DecodePragmaVersion(bytecode string) (Pragma, error) {
metadataEndIndex := len(bytecode) - 4
metadataLengthHex := bytecode[metadataEndIndex:]
metadataLengthByte, err := hex.DecodeString(metadataLengthHex)
if err != nil {
return Pragma{}, fmt.Errorf("failed to decode metadata length: %v", err)
}
metadataByteLengthUint, err := strconv.ParseUint(hex.EncodeToString(metadataLengthByte), 16, 16)
if err != nil {
return Pragma{}, fmt.Errorf("failed to convert metadata length to int: %v", err)
}
// each byte is represented by 2 characters in hex
metadataLengthInt := int(metadataByteLengthUint) * 2
// if we get nonsensical metadata length, it means that metadata section is not present and last 2 bytes do not represent metadata length
if metadataLengthInt > len(bytecode) {
return Pragma{}, errors.New(MetadataNotFoundErr)
}
metadataStarIndex := metadataEndIndex - metadataLengthInt
maybeMetadata := bytecode[metadataStarIndex:metadataEndIndex]
if len(maybeMetadata) != metadataLengthInt {
return Pragma{}, fmt.Errorf("%s. expected: %d, actual: %d", InvalidMetadataLengthErr, metadataLengthInt, len(maybeMetadata))
}
// INVALID opcode is used as a marker for the start of the metadata section
metadataMarker := "fe"
maybeMarker := bytecode[metadataStarIndex-2 : metadataStarIndex]
if maybeMarker != metadataMarker {
return Pragma{}, errors.New(MetadataNotFoundErr)
}
// this is byte-encoded version of the string "solc"
solcMarker := "736f6c63"
if !strings.Contains(maybeMetadata, solcMarker) {
return Pragma{}, errors.New(NotCompiledWithSolcErr)
}
// now that we know that last section indeed contains metadata let's grab the version
maybePragma := bytecode[metadataEndIndex-6 : metadataEndIndex]
majorHex := maybePragma[0:2]
minorHex := maybePragma[2:4]
patchHex := maybePragma[4:6]
major, err := strconv.ParseUint(majorHex, 16, 16)
if err != nil {
return Pragma{}, fmt.Errorf("%s: %v", FailedToDecodeMetadataErr, err)
}
minor, err := strconv.ParseUint(minorHex, 16, 16)
if err != nil {
return Pragma{}, fmt.Errorf("%s: %v", FailedToDecodeMetadataErr, err)
}
patch, err := strconv.ParseUint(patchHex, 16, 16)
if err != nil {
return Pragma{}, fmt.Errorf("%s: %v", FailedToDecodeMetadataErr, err)
}
return Pragma{Major: major, Minor: minor, Patch: patch}, nil
}
// DoesPragmaSupportCustomRevert checks if the pragma version supports custom revert messages (must be >= 0.8.4)
func DoesPragmaSupportCustomRevert(pragma Pragma) bool {
return pragma.Minor > 8 || (pragma.Minor == 8 && pragma.Patch >= 4) || pragma.Major > 0
}
func wrapErrInMessageWithASuggestion(err error) error {
message := `
This error could be caused by several issues. Please try these steps to resolve it:
1. Make sure the address you are using has sufficient funds.
2. Use a different RPC node. The current one might be out of sync or malfunctioning.
3. Review the logs to see if automatic gas estimations were unsuccessful. If they were, check that the fallback gas prices are set correctly.
4. If a gas limit was manually set, try commenting it out to let the node estimate it instead and see if that resolves the issue.
5. Conversely, if a gas limit was set manually, try increasing it to a higher value. This adjustment is especially crucial for some Layer 2 solutions that have variable gas limits.
Original error:`
return fmt.Errorf("%s\n%s", message, err.Error())
}