-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters