Skip to content

Commit a94957d

Browse files
committed
add check tx confirmed
1 parent 47f45dc commit a94957d

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

cmd/tx/deploy-contract.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/ethereum/go-ethereum/ethclient"
13+
1214
"github.com/ethereum/go-ethereum/common"
1315
"github.com/ethereum/go-ethereum/crypto"
1416

@@ -77,27 +79,29 @@ func deployEvmContract(bytecode string, cmd *cobra.Command) {
7779
err = signedTx.EncodeRLP(&buf)
7880
utils.ExitOnErr(err, "failed to encode tx")
7981

82+
fmt.Println("Tx hash", signedTx.Hash())
83+
8084
err = ethClient8545.SendTransaction(context.Background(), signedTx)
8185
utils.ExitOnErr(err, "failed to send tx")
8286

83-
fmt.Println("Tx hash", signedTx.Hash())
87+
if tx := waitForEthTx(ethClient8545, signedTx.Hash()); tx != nil {
88+
fmt.Println("New contract deployed at:")
89+
} else {
90+
fmt.Println("Timed-out waiting for tx to be mined, contract may have been deployed.")
91+
fmt.Println("Expected contract address:")
92+
}
93+
fmt.Println(newContractAddress)
94+
}
8495

85-
var found bool
96+
func waitForEthTx(ethClient8545 *ethclient.Client, txHash common.Hash) *ethtypes.Transaction {
8697
for try := 1; try <= 6; try++ {
87-
txByHash, pending, err := ethClient8545.TransactionByHash(context.Background(), signedTx.Hash())
88-
if err == nil && !pending && txByHash != nil {
89-
found = true
90-
break
98+
tx, _, err := ethClient8545.TransactionByHash(context.Background(), txHash)
99+
if err == nil && tx != nil {
100+
return tx
91101
}
92102

93103
time.Sleep(time.Second)
94104
}
95105

96-
if found {
97-
fmt.Println("New contract deployed at:")
98-
} else {
99-
fmt.Println("Timed-out waiting for tx to be mined, contract may have been deployed.")
100-
fmt.Println("Expected contract address:")
101-
}
102-
fmt.Println(newContractAddress)
106+
return nil
103107
}

cmd/tx/send.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,16 @@ func GetSendEvmTxCommand() *cobra.Command {
106106
rawTxRLPHex := hex.EncodeToString(buf.Bytes())
107107
fmt.Printf("RawTx: 0x%s\n", rawTxRLPHex)
108108

109+
fmt.Println("Tx hash", signedTx.Hash())
110+
109111
err = ethClient8545.SendTransaction(context.Background(), signedTx)
110112
utils.ExitOnErr(err, "failed to send tx")
113+
114+
if tx := waitForEthTx(ethClient8545, signedTx.Hash()); tx != nil {
115+
fmt.Println("Tx executed successfully")
116+
} else {
117+
fmt.Println("Timed out waiting for tx to be mined")
118+
}
111119
},
112120
}
113121

0 commit comments

Comments
 (0)