Skip to content

Commit

Permalink
update Seth, estimate gas for funds sending
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofel committed May 7, 2024
1 parent daaf9d3 commit 897dd96
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 24 deletions.
25 changes: 16 additions & 9 deletions integration-tests/actions/seth/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,27 @@ func SendFunds(logger zerolog.Logger, client *seth.Client, payload FundsToSendPa
return nil, err
}

gasLimit := uint64(client.Cfg.Network.TransferGasFee)
var gasLimit int64
gasLimitRaw, err := client.EstimateGasLimitForFundTransfer(fromAddress, payload.ToAddress, payload.Amount)
if err != nil {
gasLimit = client.Cfg.Network.TransferGasFee
} else {
gasLimit = int64(gasLimitRaw)
}

gasPrice := big.NewInt(0)
gasFeeCap := big.NewInt(0)
gasTipCap := big.NewInt(0)

if payload.GasLimit != nil {
gasLimit = uint64(*payload.GasLimit)
gasLimit = *payload.GasLimit
}

if client.Cfg.Network.EIP1559DynamicFees {
// if any of the dynamic fees are not set, we need to either estimate them or read them from config
if payload.GasFeeCap == nil || payload.GasTipCap == nil {
// estimatior or config reading happens here
txOptions := client.NewTXOpts(seth.WithGasLimit(gasLimit))
// estimation or config reading happens here
txOptions := client.NewTXOpts(seth.WithGasLimit(uint64(gasLimit)))
gasFeeCap = txOptions.GasFeeCap
gasTipCap = txOptions.GasTipCap
}
Expand All @@ -215,7 +222,7 @@ func SendFunds(logger zerolog.Logger, client *seth.Client, payload FundsToSendPa
}
} else {
if payload.GasPrice == nil {
txOptions := client.NewTXOpts((seth.WithGasLimit(gasLimit)))
txOptions := client.NewTXOpts(seth.WithGasLimit(uint64(gasLimit)))
gasPrice = txOptions.GasPrice
} else {
gasPrice = payload.GasPrice
Expand All @@ -229,7 +236,7 @@ func SendFunds(logger zerolog.Logger, client *seth.Client, payload FundsToSendPa
Nonce: nonce,
To: &payload.ToAddress,
Value: payload.Amount,
Gas: gasLimit,
Gas: uint64(gasLimit),
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
}
Expand All @@ -238,7 +245,7 @@ func SendFunds(logger zerolog.Logger, client *seth.Client, payload FundsToSendPa
Nonce: nonce,
To: &payload.ToAddress,
Value: payload.Amount,
Gas: gasLimit,
Gas: uint64(gasLimit),
GasPrice: gasPrice,
}
}
Expand All @@ -259,7 +266,7 @@ func SendFunds(logger zerolog.Logger, client *seth.Client, payload FundsToSendPa
Str("To", payload.ToAddress.Hex()).
Str("Amount (wei/ether)", fmt.Sprintf("%s/%s", payload.Amount, conversions.WeiToEther(payload.Amount).Text('f', -1))).
Uint64("Nonce", nonce).
Uint64("Gas Limit", gasLimit).
Int64("Gas Limit", gasLimit).
Str("Gas Price", gasPrice.String()).
Str("Gas Fee Cap", gasFeeCap.String()).
Str("Gas Tip Cap", gasTipCap.String()).
Expand All @@ -279,7 +286,7 @@ func SendFunds(logger zerolog.Logger, client *seth.Client, payload FundsToSendPa
Str("TxHash", signedTx.Hash().String()).
Str("Amount (wei/ether)", fmt.Sprintf("%s/%s", payload.Amount, conversions.WeiToEther(payload.Amount).Text('f', -1))).
Uint64("Nonce", nonce).
Uint64("Gas Limit", gasLimit).
Int64("Gas Limit", gasLimit).
Str("Gas Price", gasPrice.String()).
Str("Gas Fee Cap", gasFeeCap.String()).
Str("Gas Tip Cap", gasTipCap.String()).
Expand Down
20 changes: 14 additions & 6 deletions integration-tests/actions/seth/refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func ReturnFundsFromNodes(log zerolog.Logger, client *seth.Client, chainlinkNode
return err
}

err = sendAllFundsIfPossible(log, client, decryptedKey.PrivateKey)
err = returnAllFundsIfPossible(log, client, decryptedKey.PrivateKey)
if err != nil {
log.Error().Err(err).Msg("Failed to return funds from Chainlink node to default network wallet")
publicKey := decryptedKey.PrivateKey.Public()
Expand All @@ -284,7 +284,7 @@ func ReturnFundsFromNodes(log zerolog.Logger, client *seth.Client, chainlinkNode
return nil
}

func sendAllFundsIfPossible(log zerolog.Logger, sethClient *seth.Client, fromPrivateKey *ecdsa.PrivateKey) error {
func returnAllFundsIfPossible(log zerolog.Logger, sethClient *seth.Client, fromPrivateKey *ecdsa.PrivateKey) error {
publicKey := fromPrivateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
Expand Down Expand Up @@ -321,11 +321,19 @@ func sendAllFundsIfPossible(log zerolog.Logger, sethClient *seth.Client, fromPri
Priority: txPriority,
})

var gasLimit int64
gasLimitRaw, err := sethClient.EstimateGasLimitForFundTransfer(fromAddress, sethClient.Addresses[0], balance)
if err != nil {
gasLimit = sethClient.Cfg.Network.TransferGasFee
} else {
gasLimit = int64(gasLimitRaw)
}

var maxTotalGasCost *big.Int
if sethClient.Cfg.Network.EIP1559DynamicFees {
maxTotalGasCost = new(big.Int).Mul(big.NewInt(0).SetInt64(sethClient.Cfg.Network.TransferGasFee), estimations.GasFeeCap)
maxTotalGasCost = new(big.Int).Mul(big.NewInt(0).SetInt64(gasLimit), estimations.GasFeeCap)
} else {
maxTotalGasCost = new(big.Int).Mul(big.NewInt(0).SetInt64(sethClient.Cfg.Network.TransferGasFee), estimations.GasPrice)
maxTotalGasCost = new(big.Int).Mul(big.NewInt(0).SetInt64(gasLimit), estimations.GasPrice)
}

toSend := new(big.Int).Sub(balance, maxTotalGasCost)
Expand All @@ -345,7 +353,7 @@ func sendAllFundsIfPossible(log zerolog.Logger, sethClient *seth.Client, fromPri
ToAddress: sethClient.Addresses[0],
Amount: toSend,
PrivateKey: fromPrivateKey,
GasLimit: &sethClient.Cfg.Network.TransferGasFee,
GasLimit: &gasLimit,
GasPrice: estimations.GasPrice,
GasFeeCap: estimations.GasFeeCap,
GasTipCap: estimations.GasTipCap,
Expand All @@ -354,7 +362,7 @@ func sendAllFundsIfPossible(log zerolog.Logger, sethClient *seth.Client, fromPri

_, err = SendFunds(log, sethClient, payload)
if err != nil {
handler := OvershotTransferRetrier{maxRetries: 10, nextRetrier: &InsufficientFundTransferRetrier{maxRetries: 10, nextRetrier: &GasTooLowTransferRetrier{maxGasLimit: sethClient.Cfg.Network.TransferGasFee * 10}}}
handler := OvershotTransferRetrier{maxRetries: 10, nextRetrier: &InsufficientFundTransferRetrier{maxRetries: 10, nextRetrier: &GasTooLowTransferRetrier{maxGasLimit: gasLimit * 10}}}
err = handler.Retry(context.Background(), log, sethClient, err, payload, 0)
if err != nil {
log.Error().
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/smartcontractkit/chainlink-vrf v0.0.0-20231120191722-fef03814f868
github.com/smartcontractkit/chainlink/v2 v2.0.0-00010101000000-000000000000
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c
github.com/smartcontractkit/seth v0.1.7-0.20240506111032-35a1950da7e6
github.com/smartcontractkit/seth v0.1.7-0.20240506143142-19b341728a52
github.com/smartcontractkit/wasp v0.4.7
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1535,8 +1535,8 @@ github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f h1:hgJ
github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f/go.mod h1:MvMXoufZAtqExNexqi4cjrNYE9MefKddKylxjS+//n0=
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c h1:lIyMbTaF2H0Q71vkwZHX/Ew4KF2BxiKhqEXwF8rn+KI=
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c/go.mod h1:fb1ZDVXACvu4frX3APHZaEBp0xi1DIm34DcA0CwTsZM=
github.com/smartcontractkit/seth v0.1.7-0.20240506111032-35a1950da7e6 h1:etn6EfK8AllunLRr0y6fyYgv10/cUaW5tcBzLnTIy3g=
github.com/smartcontractkit/seth v0.1.7-0.20240506111032-35a1950da7e6/go.mod h1:2TMOZQ8WTAw7rR1YBbXpnad6VmT/+xDd/nXLmB7Eero=
github.com/smartcontractkit/seth v0.1.7-0.20240506143142-19b341728a52 h1:SGOACHnCChcNd4ADj4/f8rWxr3ELFKP+ba9lgFIh24Q=
github.com/smartcontractkit/seth v0.1.7-0.20240506143142-19b341728a52/go.mod h1:2TMOZQ8WTAw7rR1YBbXpnad6VmT/+xDd/nXLmB7Eero=
github.com/smartcontractkit/tdh2/go/ocr2/decryptionplugin v0.0.0-20230906073235-9e478e5e19f1 h1:yiKnypAqP8l0OX0P3klzZ7SCcBUxy5KqTAKZmQOvSQE=
github.com/smartcontractkit/tdh2/go/ocr2/decryptionplugin v0.0.0-20230906073235-9e478e5e19f1/go.mod h1:q6f4fe39oZPdsh1i57WznEZgxd8siidMaSFq3wdPmVg=
github.com/smartcontractkit/tdh2/go/tdh2 v0.0.0-20230906073235-9e478e5e19f1 h1:Dai1bn+Q5cpeGMQwRdjOdVjG8mmFFROVkSKuUgBErRQ=
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/load/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/smartcontractkit/chainlink/integration-tests v0.0.0-20240214231432-4ad5eb95178c
github.com/smartcontractkit/chainlink/v2 v2.9.0-beta0.0.20240216210048-da02459ddad8
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c
github.com/smartcontractkit/seth v0.1.7-0.20240506111032-35a1950da7e6
github.com/smartcontractkit/seth v0.1.7-0.20240506143142-19b341728a52
github.com/smartcontractkit/tdh2/go/tdh2 v0.0.0-20230906073235-9e478e5e19f1
github.com/smartcontractkit/wasp v0.4.7
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/load/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1526,8 +1526,8 @@ github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f h1:hgJ
github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f/go.mod h1:MvMXoufZAtqExNexqi4cjrNYE9MefKddKylxjS+//n0=
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c h1:lIyMbTaF2H0Q71vkwZHX/Ew4KF2BxiKhqEXwF8rn+KI=
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c/go.mod h1:fb1ZDVXACvu4frX3APHZaEBp0xi1DIm34DcA0CwTsZM=
github.com/smartcontractkit/seth v0.1.7-0.20240506111032-35a1950da7e6 h1:etn6EfK8AllunLRr0y6fyYgv10/cUaW5tcBzLnTIy3g=
github.com/smartcontractkit/seth v0.1.7-0.20240506111032-35a1950da7e6/go.mod h1:2TMOZQ8WTAw7rR1YBbXpnad6VmT/+xDd/nXLmB7Eero=
github.com/smartcontractkit/seth v0.1.7-0.20240506143142-19b341728a52 h1:SGOACHnCChcNd4ADj4/f8rWxr3ELFKP+ba9lgFIh24Q=
github.com/smartcontractkit/seth v0.1.7-0.20240506143142-19b341728a52/go.mod h1:2TMOZQ8WTAw7rR1YBbXpnad6VmT/+xDd/nXLmB7Eero=
github.com/smartcontractkit/tdh2/go/ocr2/decryptionplugin v0.0.0-20230906073235-9e478e5e19f1 h1:yiKnypAqP8l0OX0P3klzZ7SCcBUxy5KqTAKZmQOvSQE=
github.com/smartcontractkit/tdh2/go/ocr2/decryptionplugin v0.0.0-20230906073235-9e478e5e19f1/go.mod h1:q6f4fe39oZPdsh1i57WznEZgxd8siidMaSFq3wdPmVg=
github.com/smartcontractkit/tdh2/go/tdh2 v0.0.0-20230906073235-9e478e5e19f1 h1:Dai1bn+Q5cpeGMQwRdjOdVjG8mmFFROVkSKuUgBErRQ=
Expand Down
3 changes: 0 additions & 3 deletions integration-tests/wrappers/contract_caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,6 @@ func (w *WrappedContractBackend) getErrorFromContext(ctx context.Context) error
if v, ok := ctxErr.(error); ok {
return v
}
if v, ok := ctxErr.(seth.ContextErrorValue); ok {
return v.Error
}
return errors.Wrapf(errors.New("unknown error type"), "error in context: %v", ctxErr)
}

Expand Down

0 comments on commit 897dd96

Please sign in to comment.