/
helpers.go
51 lines (40 loc) · 1.23 KB
/
helpers.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
// Copyright (c) 2018 Clearmatics Technologies Ltd
package utils
import (
"context"
"encoding/hex"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
)
func GetNonce(client *ethclient.Client, auth *bind.TransactOpts) {
// Find the correct tx nonce
nonce, err := client.PendingNonceAt(context.Background(), auth.From)
if err != nil {
log.Fatalf("Failed to calculate nonce: %v", err)
}
auth.Nonce = big.NewInt(int64(nonce))
}
// Needed to convert transaction strings into the correct format
func StringToBytes32(input string) (output [32]byte, err error) {
// Check string length is correct 64
if len(input) == 64 {
inputBytes, err := hex.DecodeString(input)
if err != nil {
log.Fatalf("Failed to encode string as bytes: %v", err)
}
copy(output[:], inputBytes[:len(output)])
return output, nil
} else if len(input) == 66 && input[:2] == "0x" {
inputBytes, err := hex.DecodeString(input[2:])
if err != nil {
log.Fatalf("Failed to encode string as bytes: %v", err)
}
copy(output[:], inputBytes[:len(output)])
return output, nil
} else {
return [32]byte{}, fmt.Errorf("Failed to encode string as bytes32, incorrect string input")
}
}