Skip to content

Commit

Permalink
Merge pull request #7 from bcdevtools/imp/minor-improvement-to-tx-send
Browse files Browse the repository at this point in the history
imp: minor improvement to `devd tx send`
  • Loading branch information
0xbcdev authored Dec 19, 2024
2 parents 0855ecf + 9f3e692 commit e9e7a2d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,19 @@ devd query debug_traceTransaction [0xHash] [--tracer callTracer] [--rpc http://l
#### Send EVM transaction

```bash
devd tx send [to] [amount] [--rpc http://localhost:8545]
# Transfer native coin
devd tx send [to] [amount]
# Transfer ERC-20 token
devd tx send [to] [amount] [--erc20 contract_address]
```

#### Deploy EVM contract

```bash
# Deploy contract with deployment bytecode
devd tx deploy-contract [deployment bytecode]
# Deploy ERC-20 contract with pre-defined bytecode
devd tx deploy-contract erc20
```

### Convert tools
Expand Down
32 changes: 18 additions & 14 deletions cmd/tx/deploy-contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"time"

"github.com/ethereum/go-ethereum/ethclient"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"

Expand Down Expand Up @@ -59,7 +61,7 @@ func deployEvmContract(bytecode string, cmd *cobra.Command) {
txData := ethtypes.LegacyTx{
Nonce: nonce,
GasPrice: big.NewInt(20_000_000_000),
Gas: 2_000_000,
Gas: 4_000_000,
To: nil,
Data: deploymentBytes,
Value: common.Big0,
Expand All @@ -77,27 +79,29 @@ func deployEvmContract(bytecode string, cmd *cobra.Command) {
err = signedTx.EncodeRLP(&buf)
utils.ExitOnErr(err, "failed to encode tx")

fmt.Println("Tx hash", signedTx.Hash())

err = ethClient8545.SendTransaction(context.Background(), signedTx)
utils.ExitOnErr(err, "failed to send tx")

fmt.Println("Tx hash", signedTx.Hash())
if tx := waitForEthTx(ethClient8545, signedTx.Hash()); tx != nil {
fmt.Println("New contract deployed at:")
} else {
fmt.Println("Timed-out waiting for tx to be mined, contract may have been deployed.")
fmt.Println("Expected contract address:")
}
fmt.Println(newContractAddress)
}

var found bool
func waitForEthTx(ethClient8545 *ethclient.Client, txHash common.Hash) *ethtypes.Transaction {
for try := 1; try <= 6; try++ {
txByHash, pending, err := ethClient8545.TransactionByHash(context.Background(), signedTx.Hash())
if err == nil && !pending && txByHash != nil {
found = true
break
tx, _, err := ethClient8545.TransactionByHash(context.Background(), txHash)
if err == nil && tx != nil {
return tx
}

time.Sleep(time.Second)
}

if found {
fmt.Println("New contract deployed at:")
} else {
fmt.Println("Timed-out waiting for tx to be mined, contract may have been deployed.")
fmt.Println("Expected contract address:")
}
fmt.Println(newContractAddress)
return nil
}
8 changes: 8 additions & 0 deletions cmd/tx/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,16 @@ func GetSendEvmTxCommand() *cobra.Command {
rawTxRLPHex := hex.EncodeToString(buf.Bytes())
fmt.Printf("RawTx: 0x%s\n", rawTxRLPHex)

fmt.Println("Tx hash", signedTx.Hash())

err = ethClient8545.SendTransaction(context.Background(), signedTx)
utils.ExitOnErr(err, "failed to send tx")

if tx := waitForEthTx(ethClient8545, signedTx.Hash()); tx != nil {
fmt.Println("Tx executed successfully")
} else {
fmt.Println("Timed out waiting for tx to be mined")
}
},
}

Expand Down

0 comments on commit e9e7a2d

Please sign in to comment.