Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support EIP1559 #10

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions lib/run/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Generator struct {
ShouldPersist bool
Store *store.Store
limiter *RateLimiter
EIP1559 bool
}

func NewGenerator(rpcUrl, faucetPrivateKey string, senderCount, txCount int, shouldPersist bool, txStoreDir string, limiter *RateLimiter) (*Generator, error) {
Expand All @@ -34,6 +35,12 @@ func NewGenerator(rpcUrl, faucetPrivateKey string, senderCount, txCount int, sho
return &Generator{}, err
}

eip1559 := false
_, err = client.SuggestGasTipCap(context.Background())
if err == nil {
eip1559 = true
}

gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return &Generator{}, err
Expand Down Expand Up @@ -79,6 +86,7 @@ func NewGenerator(rpcUrl, faucetPrivateKey string, senderCount, txCount int, sho
ShouldPersist: shouldPersist,
Store: store.NewStore(txStoreDir),
limiter: limiter,
EIP1559: eip1559,
}, nil
}

Expand All @@ -99,7 +107,7 @@ func (g *Generator) GenerateSimple() (map[int]types.Transactions, error) {
go func(index int, sender *account.Account) {
txs := types.Transactions{}
for _, recipient := range g.Recipients {
tx, err := generateSimpleTx(sender.PrivateKey, recipient, sender.GetNonce(), g.ChainID, g.GasPrice, value)
tx, err := generateSimpleTx(sender.PrivateKey, recipient, sender.GetNonce(), g.ChainID, g.GasPrice, value, g.EIP1559)
if err != nil {
ch <- err
return
Expand Down Expand Up @@ -144,7 +152,7 @@ func (g *Generator) prepareSenders() error {
txs := types.Transactions{}

for _, recipient := range g.Senders {
signedTx, err := generateSimpleTx(g.FaucetAccount.PrivateKey, recipient.Address.Hex(), g.FaucetAccount.GetNonce(), g.ChainID, g.GasPrice, value)
signedTx, err := generateSimpleTx(g.FaucetAccount.PrivateKey, recipient.Address.Hex(), g.FaucetAccount.GetNonce(), g.ChainID, g.GasPrice, value, g.EIP1559)
if err != nil {
return err
}
Expand Down Expand Up @@ -177,13 +185,30 @@ func (g *Generator) prepareSenders() error {
return nil
}

func generateSimpleTx(privateKey *ecdsa.PrivateKey, recipient string, nonce uint64, chainID, gasPrice, value *big.Int) (*types.Transaction, error) {
func generateSimpleTx(privateKey *ecdsa.PrivateKey, recipient string, nonce uint64, chainID, gasPrice, value *big.Int, eip1559 bool) (*types.Transaction, error) {
gasLimit := uint64(21000) // Gas limit for ETH transfer

toAddress := common.HexToAddress(recipient)
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, nil)

signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
var signedTx *types.Transaction
var err error
if eip1559 {
tx := types.NewTx(&types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
To: &toAddress,
Value: value,
GasFeeCap: gasPrice,
GasTipCap: gasPrice,
Gas: gasLimit,
Data: nil,
})
signedTx, err = types.SignTx(tx, types.NewLondonSigner(chainID), privateKey)
} else {
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, nil)
signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
}

if err != nil {
return &types.Transaction{}, err
}
Expand Down
Loading