Skip to content

Commit

Permalink
add: eth sendTx
Browse files Browse the repository at this point in the history
  • Loading branch information
hthuz committed Jul 25, 2024
1 parent 6291285 commit 7b49cbe
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
106 changes: 106 additions & 0 deletions go/eth/cmd/sendtx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"context"
"eth/config"
"fmt"
"log"
"math/big"
"time"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"
"github.com/spf13/cobra"
)

// sendtxCmd represents the sendtx command
var sendtxCmd = &cobra.Command{
Use: "sendtx",
Short: "eth send tx",
Run: func(cmd *cobra.Command, args []string) {

// Get keystore
ks := keystore.NewKeyStore(config.Account1KeyStorePath, keystore.StandardScryptN, keystore.StandardScryptP)
// manager := accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: false}, ks)
account1 := ks.Accounts()[0]

client, err := ethclient.Dial(config.TestNode)
if err != nil {
log.Fatal(err)
}

chainid, err := client.ChainID(context.Background())
if err != nil {
log.Fatal(err)
}

nonce, err := client.PendingNonceAt(context.Background(), common.HexToAddress(config.TestAccount1))
if err != nil {
log.Fatal(err)
}

tipCap, _ := client.SuggestGasTipCap(context.Background())
feeCap, _ := client.SuggestGasPrice(context.Background())

var (
to = common.HexToAddress(config.TestAccount2)
value = new(big.Int).Mul(big.NewInt(2), big.NewInt(params.Wei))
gasLimit = uint64(21111) // Minimum gasUsed for a ETH transfer is 21000, tx will fail if gasLimit < 21000
)
// Create a new transaction
tx := types.NewTx(
&types.DynamicFeeTx{
ChainID: chainid,
Nonce: nonce,
GasTipCap: tipCap,
GasFeeCap: feeCap,
Gas: gasLimit,
To: &to,
Value: value,
Data: nil,
})

// signedTX, err := types.SignTx(tx, types.NewLondonSigner(chainid), sk)
signedTx, err := ks.SignTxWithPassphrase(account1, "123456", tx, chainid)
if err != nil {
log.Fatal(err)
}

// send transaction
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatal(err)
}

fmt.Println("Waiting")
time.Sleep(6 * time.Second)
// get transaction receipt
receipt, err := client.TransactionReceipt(context.Background(), signedTx.Hash())
if err != nil {
log.Println(signedTx.Hash(), " pending")
log.Fatal(err)
}
fmt.Printf("%+v\n", receipt)

},
}

func init() {
rootCmd.AddCommand(sendtxCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// sendtxCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// sendtxCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
11 changes: 7 additions & 4 deletions go/eth/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package config

var (
PublicNode = "https://bsc-rpc.publicnode.com"
TestNode = "http://localhost:8545"
TestMiner = "0x6A62503BE31643a98c5E5c7a584238e6Fc815793"
TestContract = "60a060405234801561000f575f80fd5b506040516107c03803806107c0833981810160405281019061003191906100b7565b8060808181525050805f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550506100e2565b5f80fd5b5f819050919050565b61009681610084565b81146100a0575f80fd5b50565b5f815190506100b18161008d565b92915050565b5f602082840312156100cc576100cb610080565b5b5f6100d9848285016100a3565b91505092915050565b6080516106c66100fa5f395f61017701526106c65ff3fe608060405234801561000f575f80fd5b5060043610610060575f3560e01c806306fdde031461006457806318160ddd14610082578063313ce567146100a057806370a08231146100be57806395d89b41146100ee578063a9059cbb1461010c575b5f80fd5b61006c61013c565b6040516100799190610454565b60405180910390f35b61008a610175565b604051610097919061048c565b60405180910390f35b6100a8610199565b6040516100b591906104c0565b60405180910390f35b6100d860048036038101906100d39190610537565b61019e565b6040516100e5919061048c565b60405180910390f35b6100f66101e3565b6040516101039190610454565b60405180910390f35b6101266004803603810190610121919061058c565b61021c565b60405161013391906105e4565b60405180910390f35b6040518060400160405280600b81526020017f53696d706c65455243323000000000000000000000000000000000000000000081525081565b7f000000000000000000000000000000000000000000000000000000000000000081565b600481565b5f805f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050919050565b6040518060400160405280600481526020017f534552430000000000000000000000000000000000000000000000000000000081525081565b5f815f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20541015610265575f80fd5b815f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20546102ad919061062a565b5f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550815f808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2054610335919061065d565b5f808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040516103d2919061048c565b60405180910390a36001905092915050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f601f19601f8301169050919050565b5f610426826103e4565b61043081856103ee565b93506104408185602086016103fe565b6104498161040c565b840191505092915050565b5f6020820190508181035f83015261046c818461041c565b905092915050565b5f819050919050565b61048681610474565b82525050565b5f60208201905061049f5f83018461047d565b92915050565b5f60ff82169050919050565b6104ba816104a5565b82525050565b5f6020820190506104d35f8301846104b1565b92915050565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610506826104dd565b9050919050565b610516816104fc565b8114610520575f80fd5b50565b5f813590506105318161050d565b92915050565b5f6020828403121561054c5761054b6104d9565b5b5f61055984828501610523565b91505092915050565b61056b81610474565b8114610575575f80fd5b50565b5f8135905061058681610562565b92915050565b5f80604083850312156105a2576105a16104d9565b5b5f6105af85828601610523565b92505060206105c085828601610578565b9150509250929050565b5f8115159050919050565b6105de816105ca565b82525050565b5f6020820190506105f75f8301846105d5565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f61063482610474565b915061063f83610474565b9250828203905081811115610657576106566105fd565b5b92915050565b5f61066782610474565b915061067283610474565b925082820190508082111561068a576106896105fd565b5b9291505056fea2646970667358221220fd05db9e8758a827aa8d709883c1cb5aec932650b07c59f091d57b68412d737c64736f6c634300081a0033"
PublicNode = "https://bsc-rpc.publicnode.com"
TestNode = "http://localhost:8545"
TestAccount1 = "0x6A62503BE31643a98c5E5c7a584238e6Fc815793"
Account1KeyStorePath = "~/bsc-testnet/node1/keystore"
Account2KeyStorePath = "~/bsc-testnet/node2/keystore"
TestAccount2 = "0xdf2b323C0f2E029a2b8E61006815e60269a303Be"
TestContract = "60a060405234801561000f575f80fd5b506040516107c03803806107c0833981810160405281019061003191906100b7565b8060808181525050805f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550506100e2565b5f80fd5b5f819050919050565b61009681610084565b81146100a0575f80fd5b50565b5f815190506100b18161008d565b92915050565b5f602082840312156100cc576100cb610080565b5b5f6100d9848285016100a3565b91505092915050565b6080516106c66100fa5f395f61017701526106c65ff3fe608060405234801561000f575f80fd5b5060043610610060575f3560e01c806306fdde031461006457806318160ddd14610082578063313ce567146100a057806370a08231146100be57806395d89b41146100ee578063a9059cbb1461010c575b5f80fd5b61006c61013c565b6040516100799190610454565b60405180910390f35b61008a610175565b604051610097919061048c565b60405180910390f35b6100a8610199565b6040516100b591906104c0565b60405180910390f35b6100d860048036038101906100d39190610537565b61019e565b6040516100e5919061048c565b60405180910390f35b6100f66101e3565b6040516101039190610454565b60405180910390f35b6101266004803603810190610121919061058c565b61021c565b60405161013391906105e4565b60405180910390f35b6040518060400160405280600b81526020017f53696d706c65455243323000000000000000000000000000000000000000000081525081565b7f000000000000000000000000000000000000000000000000000000000000000081565b600481565b5f805f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050919050565b6040518060400160405280600481526020017f534552430000000000000000000000000000000000000000000000000000000081525081565b5f815f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20541015610265575f80fd5b815f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20546102ad919061062a565b5f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550815f808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2054610335919061065d565b5f808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040516103d2919061048c565b60405180910390a36001905092915050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f601f19601f8301169050919050565b5f610426826103e4565b61043081856103ee565b93506104408185602086016103fe565b6104498161040c565b840191505092915050565b5f6020820190508181035f83015261046c818461041c565b905092915050565b5f819050919050565b61048681610474565b82525050565b5f60208201905061049f5f83018461047d565b92915050565b5f60ff82169050919050565b6104ba816104a5565b82525050565b5f6020820190506104d35f8301846104b1565b92915050565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610506826104dd565b9050919050565b610516816104fc565b8114610520575f80fd5b50565b5f813590506105318161050d565b92915050565b5f6020828403121561054c5761054b6104d9565b5b5f61055984828501610523565b91505092915050565b61056b81610474565b8114610575575f80fd5b50565b5f8135905061058681610562565b92915050565b5f80604083850312156105a2576105a16104d9565b5b5f6105af85828601610523565b92505060206105c085828601610578565b9150509250929050565b5f8115159050919050565b6105de816105ca565b82525050565b5f6020820190506105f75f8301846105d5565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f61063482610474565b915061063f83610474565b9250828203905081811115610657576106566105fd565b5b92915050565b5f61066782610474565b915061067283610474565b925082820190508082111561068a576106896105fd565b5b9291505056fea2646970667358221220fd05db9e8758a827aa8d709883c1cb5aec932650b07c59f091d57b68412d737c64736f6c634300081a0033"
)
2 changes: 2 additions & 0 deletions go/eth/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ require (
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/holiman/uint256 v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go/eth/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPx
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk=
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I=
Expand Down Expand Up @@ -211,6 +213,7 @@ golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit 7b49cbe

Please sign in to comment.