Commit 611f7399 authored by luxq's avatar luxq

add test code

parent 5f38eab7
Pipeline #915 canceled with stages
module code.wuban.net.cn/xueqianlu/txconfirm
go 1.24.9
require (
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/go-ethereum v1.10.24 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
)
github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k=
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
github.com/ethereum/go-ethereum v1.10.24 h1:16KV6vdc4T4VHn6UgGlTFs0S71YpbUpZd6BmGcxFXag=
github.com/ethereum/go-ethereum v1.10.24/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbLoNDdZlMt7kyKFg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
package main
import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"log"
"math/big"
"time"
)
var privhex = ""
var rpc = ""
func main() {
client, err := ethclient.Dial(rpc)
if err != nil {
panic(err)
}
defer client.Close()
pk, err := crypto.HexToECDSA(privhex)
if err != nil {
panic(err)
}
ctx := context.TODO()
chainId, err := client.ChainID(ctx)
if err != nil {
panic(err)
}
nonce, err := client.NonceAt(ctx, crypto.PubkeyToAddress(pk.PublicKey), nil)
if err != nil {
panic(err)
}
for i := 0; i < 10; i++ {
SendAndWait(client, ctx, pk, chainId, nonce)
nonce += 1
}
}
func SendAndWait(client *ethclient.Client, ctx context.Context, pk *ecdsa.PrivateKey, chainId *big.Int, nonce uint64) {
to := common.HexToAddress("0xAD4143Df8Fe9E31B1C4318e8dE555872085f481d")
tx := &types.LegacyTx{
Nonce: nonce,
GasPrice: big.NewInt(1000000000),
Gas: 21000,
To: &to,
Value: big.NewInt(1000),
}
signedTx, err := types.SignNewTx(pk, types.NewEIP155Signer(chainId), tx)
if err != nil {
log.Fatalf("failed to sign tx: %v", err)
}
err = client.SendTransaction(ctx, signedTx)
if err != nil {
log.Fatalln(fmt.Errorf("failed to send tx: %v", err))
}
t1 := time.Now()
tm := time.NewTicker(100 * time.Millisecond)
for {
select {
case <-tm.C:
receipt, _ := client.TransactionReceipt(ctx, signedTx.Hash())
if receipt != nil {
fmt.Println("tx mined in ", time.Since(t1).String())
return
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment