/
util.go
60 lines (54 loc) · 1.69 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
package bind
import (
"context"
"errors"
"github.com/entropyio/go-entropy"
"github.com/entropyio/go-entropy/blockchain/model"
"github.com/entropyio/go-entropy/common"
"time"
)
// WaitMined waits for tx to be mined on the blockchain.
// It stops waiting when the context is canceled.
func WaitMined(ctx context.Context, b DeployBackend, tx *model.Transaction) (*model.Receipt, error) {
queryTicker := time.NewTicker(time.Second)
defer queryTicker.Stop()
for {
receipt, err := b.TransactionReceipt(ctx, tx.Hash())
if err == nil {
return receipt, nil
}
if errors.Is(err, entropy.NotFound) {
log.Debug("Transaction not yet mined")
} else {
log.Debug("Receipt retrieval failed", "err", err)
}
// Wait for the next round.
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-queryTicker.C:
}
}
}
// WaitDeployed waits for a contract deployment transaction and returns the on-chain
// contract address when it is mined. It stops waiting when ctx is canceled.
func WaitDeployed(ctx context.Context, b DeployBackend, tx *model.Transaction) (common.Address, error) {
if tx.To() != nil {
return common.Address{}, errors.New("tx is not contract creation")
}
receipt, err := WaitMined(ctx, b, tx)
if err != nil {
return common.Address{}, err
}
if receipt.ContractAddress == (common.Address{}) {
return common.Address{}, errors.New("zero address")
}
// Check that code has indeed been deployed at the address.
// This matters on pre-Homestead chains: OOG in the constructor
// could leave an empty account behind.
code, err := b.CodeAt(ctx, receipt.ContractAddress, nil)
if err == nil && len(code) == 0 {
err = ErrNoCodeAfterDeploy
}
return receipt.ContractAddress, err
}