Skip to content

Commit a2d4176

Browse files
committed
fix
1 parent 2a1b560 commit a2d4176

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

pkg/tx/transfer.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/ethereum/go-ethereum/ethclient"
1313
"log"
1414
"math/big"
15+
"time"
1516
)
1617

1718
func Transfer(pk string, to string, val string, data []byte, ec *ethclient.Client) (txHash common.Hash, err error) {
@@ -97,3 +98,32 @@ func Transfer(pk string, to string, val string, data []byte, ec *ethclient.Clien
9798
err = ec.SendTransaction(context.Background(), signedTx)
9899
return txn.Hash(), err
99100
}
101+
func WaitForTransactionConfirmation(client *ethclient.Client, txHash common.Hash) (*types.Receipt, error) {
102+
var receipt *types.Receipt
103+
var err error
104+
105+
// 设置查询上下文,可以设置超时
106+
ctx := context.Background()
107+
108+
// 设置轮询间隔
109+
110+
fmt.Println("waiting for tx confirmed")
111+
for {
112+
receipt, err = client.TransactionReceipt(ctx, txHash)
113+
if err != nil {
114+
if err == ethereum.NotFound {
115+
// 如果收据未找到,继续轮询
116+
time.Sleep(time.Second)
117+
continue
118+
}
119+
// 如果发生其他错误,则返回错误
120+
return nil, err
121+
}
122+
// 如果收据不为空,表示交易已被确认
123+
if receipt != nil {
124+
break
125+
}
126+
}
127+
128+
return receipt, nil
129+
}

transfer.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package main
22

33
import (
4-
"context"
54
"encoding/json"
65
"fmt"
76
_tx "github.com/NullpointerW/ethereum-wallet-tool/pkg/tx"
8-
"github.com/ethereum/go-ethereum"
9-
"github.com/ethereum/go-ethereum/common"
10-
"github.com/ethereum/go-ethereum/core/types"
117
"github.com/ethereum/go-ethereum/ethclient"
128
"os"
139
"strings"
14-
"time"
1510
)
1611

1712
type TxRecord struct {
@@ -74,39 +69,10 @@ func transfer(mpk string, w Wallet, val string, ec *ethclient.Client) TxRecord {
7469
if err != nil {
7570
txR.ErrMsg = err.Error()
7671
} else {
77-
_, err = WaitForTransactionConfirmation(ec, txHash)
72+
_, err = _tx.WaitForTransactionConfirmation(ec, txHash)
7873
if err != nil {
7974
txR.ErrMsg = err.Error()
8075
}
8176
}
8277
return txR
8378
}
84-
func WaitForTransactionConfirmation(client *ethclient.Client, txHash common.Hash) (*types.Receipt, error) {
85-
var receipt *types.Receipt
86-
var err error
87-
88-
// 设置查询上下文,可以设置超时
89-
ctx := context.Background()
90-
91-
// 设置轮询间隔
92-
93-
fmt.Println("waiting for tx confirmed")
94-
for {
95-
receipt, err = client.TransactionReceipt(ctx, txHash)
96-
if err != nil {
97-
if err == ethereum.NotFound {
98-
// 如果收据未找到,继续轮询
99-
time.Sleep(time.Second)
100-
continue
101-
}
102-
// 如果发生其他错误,则返回错误
103-
return nil, err
104-
}
105-
// 如果收据不为空,表示交易已被确认
106-
if receipt != nil {
107-
break
108-
}
109-
}
110-
111-
return receipt, nil
112-
}

0 commit comments

Comments
 (0)