-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
69 lines (61 loc) · 1.8 KB
/
utils.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
package metax
import (
"context"
"encoding/json"
"errors"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"log"
"math/big"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
// WaitMined waits for tx to be mined on the blockchain.
// It stops waiting when the context is canceled.
func WaitMined(ctx context.Context, b bind.DeployBackend, txHash common.Hash) (*types.Receipt, error) {
queryTicker := time.NewTicker(time.Second)
defer queryTicker.Stop()
for {
receipt, err := b.TransactionReceipt(ctx, txHash)
if err == nil {
return receipt, nil
}
if errors.Is(err, ethereum.NotFound) {
log.Printf("Transaction %s not yet mined\n", txHash)
} else {
log.Printf("Receipt retrieval failed: %v\n", err)
}
// Wait for the next round.
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-queryTicker.C:
}
}
}
func ConvertToJsonStr(obj interface{}) string {
jsonStr, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return ""
} else {
return string(jsonStr)
}
}
func GetDomainSeparator(forwarderAddress common.Address, chainId *big.Int) (common.Hash, error) {
typedData := apitypes.TypedData{
Types: SignedTypes,
PrimaryType: EIP712DomainType,
Domain: apitypes.TypedDataDomain{
Name: ForwardRequestName,
Version: Version,
VerifyingContract: forwarderAddress.Hex(),
Salt: hexutil.Encode(common.LeftPadBytes(chainId.Bytes(), 32)),
},
Message: make(map[string]interface{}),
}
domainSeparator, err := typedData.HashStruct(EIP712DomainType, typedData.Domain.Map())
return common.HexToHash(hexutil.Encode(domainSeparator)), err
}