Skip to content

Commit e9e7a2d

Browse files
authored
Merge pull request #7 from bcdevtools/imp/minor-improvement-to-tx-send
imp: minor improvement to `devd tx send`
2 parents 0855ecf + 9f3e692 commit e9e7a2d

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,19 @@ devd query debug_traceTransaction [0xHash] [--tracer callTracer] [--rpc http://l
7676
#### Send EVM transaction
7777

7878
```bash
79-
devd tx send [to] [amount] [--rpc http://localhost:8545]
79+
# Transfer native coin
80+
devd tx send [to] [amount]
81+
# Transfer ERC-20 token
82+
devd tx send [to] [amount] [--erc20 contract_address]
83+
```
84+
85+
#### Deploy EVM contract
86+
87+
```bash
88+
# Deploy contract with deployment bytecode
89+
devd tx deploy-contract [deployment bytecode]
90+
# Deploy ERC-20 contract with pre-defined bytecode
91+
devd tx deploy-contract erc20
8092
```
8193

8294
### Convert tools

cmd/tx/deploy-contract.go

Lines changed: 18 additions & 14 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

@@ -59,7 +61,7 @@ func deployEvmContract(bytecode string, cmd *cobra.Command) {
5961
txData := ethtypes.LegacyTx{
6062
Nonce: nonce,
6163
GasPrice: big.NewInt(20_000_000_000),
62-
Gas: 2_000_000,
64+
Gas: 4_000_000,
6365
To: nil,
6466
Data: deploymentBytes,
6567
Value: common.Big0,
@@ -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)