Skip to content

add GetEstimateFee #17751

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

Merged
merged 16 commits into from
May 19, 2025
62 changes: 62 additions & 0 deletions core/capabilities/targets/mocks/contract_writer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions core/scripts/ccip/manual-execution/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module manual-execution

go 1.24.1

toolchain go1.24.2
go 1.24.2

require (
github.com/ethereum/go-ethereum v1.15.3
Expand Down
4 changes: 2 additions & 2 deletions core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/shopspring/decimal v1.4.0
github.com/smartcontractkit/chainlink-automation v0.8.1
github.com/smartcontractkit/chainlink-ccip v0.0.0-20250515091132-6c08936b29ab
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250515101002-90b1d1b66ce4
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250519161208-80bc8b13c0e7
github.com/smartcontractkit/chainlink-data-streams v0.1.1-0.20250417193446-eeb0a7d1e049
github.com/smartcontractkit/chainlink-deployments-framework v0.1.2
github.com/smartcontractkit/chainlink-evm v0.0.0-20250515125821-cae192cd18bb
Expand Down Expand Up @@ -395,7 +395,7 @@ require (
github.com/smartcontractkit/chainlink-protos/rmn/v1.6/go v0.0.0-20250131130834-15e0d4cde2a6 // indirect
github.com/smartcontractkit/chainlink-protos/svr v1.1.0 // indirect
github.com/smartcontractkit/chainlink-protos/workflows/go v0.0.0-20250501150903-3e93089d9ad5 // indirect
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250516194721-92fae45fbb27 // indirect
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250519165144-2167692b318c // indirect
github.com/smartcontractkit/chainlink-testing-framework/framework v0.8.1 // indirect
github.com/smartcontractkit/chainlink-testing-framework/parrot v0.6.2 // indirect
github.com/smartcontractkit/chainlink-testing-framework/seth v1.50.10 // indirect
Expand Down
8 changes: 4 additions & 4 deletions core/scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1265,8 +1265,8 @@ github.com/smartcontractkit/chainlink-ccip v0.0.0-20250515091132-6c08936b29ab h1
github.com/smartcontractkit/chainlink-ccip v0.0.0-20250515091132-6c08936b29ab/go.mod h1:Jb05WL6lj5H89XGcaaOinxTf4Gdj+vXO4TcUhqTgqIM=
github.com/smartcontractkit/chainlink-ccip/chains/solana v0.0.0-20250515132731-ad40fab9b75e h1:4Hx+m9MovqsayvKdYjnj6OQ5pxXkpU2LDz0UNRuqoA8=
github.com/smartcontractkit/chainlink-ccip/chains/solana v0.0.0-20250515132731-ad40fab9b75e/go.mod h1:k3/Z6AvwurPUlfuDFEonRbkkiTSgNSrtVNhJEWNlUZA=
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250515101002-90b1d1b66ce4 h1:kWAHWz0JFWzJ9pw5mFGRWSeudqHJOy1ttDTv72WsdG4=
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250515101002-90b1d1b66ce4/go.mod h1:uNF6+noody47ZdmRwymDZAnQ7eKTXLzMKvl41LA63lo=
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250519161208-80bc8b13c0e7 h1:yeDKMQSKJQwe5SdfPmbzjzgyZIeq1QlcR93K3e7tRCU=
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250519161208-80bc8b13c0e7/go.mod h1:uNF6+noody47ZdmRwymDZAnQ7eKTXLzMKvl41LA63lo=
github.com/smartcontractkit/chainlink-common/pkg/monitoring v0.0.0-20250415235644-8703639403c7 h1:9wh1G+WbXwPVqf0cfSRSgwIcaXTQgvYezylEAfwmrbw=
github.com/smartcontractkit/chainlink-common/pkg/monitoring v0.0.0-20250415235644-8703639403c7/go.mod h1:yaDOAZF6MNB+NGYpxGCUc+owIdKrjvFW0JODdTcQ3V0=
github.com/smartcontractkit/chainlink-data-streams v0.1.1-0.20250417193446-eeb0a7d1e049 h1:7HwYt8rDz1ehTcB28oNipdTZUtV17F2sfkLTLtMJC4c=
Expand Down Expand Up @@ -1295,8 +1295,8 @@ github.com/smartcontractkit/chainlink-protos/svr v1.1.0 h1:79Z9N9dMbMVRGaLoDPAQ+
github.com/smartcontractkit/chainlink-protos/svr v1.1.0/go.mod h1:TcOliTQU6r59DwG4lo3U+mFM9WWyBHGuFkkxQpvSujo=
github.com/smartcontractkit/chainlink-protos/workflows/go v0.0.0-20250501150903-3e93089d9ad5 h1:iF7WucN9ZxJLbDuyB2co0mGPytFY9NdEBeQHY1+bJnc=
github.com/smartcontractkit/chainlink-protos/workflows/go v0.0.0-20250501150903-3e93089d9ad5/go.mod h1:HIpGvF6nKCdtZ30xhdkKWGM9+4Z4CVqJH8ZBL1FTEiY=
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250516194721-92fae45fbb27 h1:Dxpmsm2i6gV5bYw5exlnjK7GBPgKOCJgRh6H/78dY8w=
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250516194721-92fae45fbb27/go.mod h1:gLFSnWHGOiFLiUr6PIJ08q2ZvhUdKsjYY5+UxR4SHlg=
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250519165144-2167692b318c h1:9fc0vfCgRl8zHDAVQPxcTeYafF8Y/g2eLVxC96Mhwc0=
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250519165144-2167692b318c/go.mod h1:2Ctb1oLBWOjX+VukbmY4dSpRIF9DBxqp8SxyaBSgOwA=
github.com/smartcontractkit/chainlink-testing-framework/framework v0.8.1 h1:xDfmixWYefrECJkWpnSDx+WOdnJe/wNADsTZ2B68jcw=
github.com/smartcontractkit/chainlink-testing-framework/framework v0.8.1/go.mod h1:JJlLI8lxMi6nMF+Tl56qHJcAybgNcupQwltnF5Y6Oi0=
github.com/smartcontractkit/chainlink-testing-framework/lib v1.52.4 h1:+kwLuO9kcq1+ZbRUQjxX1SQmzlL2M6ZP6+L0xQMtmkU=
Expand Down
65 changes: 65 additions & 0 deletions core/services/relay/evm/chain_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"
"strings"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"

Expand Down Expand Up @@ -216,6 +217,70 @@ func (w *chainWriter) GetFeeComponents(ctx context.Context) (*commontypes.ChainF
}, nil
}

// GetEstimateFee returns total cost of TX execution in the underlying chain's currency.
// The value (val) is included in the fee calculation.
func (w *chainWriter) GetEstimateFee(ctx context.Context, contract, method string, args any, toAddress string, meta *commontypes.TxMeta, val *big.Int) (commontypes.EstimateFee, error) {
calldata, err := w.encoder.Encode(ctx, args, codec.WrapItemType(contract, method, true))
if err != nil {
return commontypes.EstimateFee{}, fmt.Errorf("%w: failed to encode args", err)
}

to := common.HexToAddress(toAddress)
var v assets.Eth
if val != nil {
v = assets.Eth(*val)
}

contractConfig, ok := w.contracts[contract]
if !ok {
return commontypes.EstimateFee{}, fmt.Errorf("contract config not found: %v", contract)
}

methodConfig, ok := contractConfig.Configs[method]
if !ok {
return commontypes.EstimateFee{}, fmt.Errorf("method config not found: %v", method)
}

gasLimit := methodConfig.GasLimit
if meta != nil && meta.GasLimit != nil {
gasLimit = meta.GasLimit.Uint64()
}

from := common.Address{}
cost, err := w.getMaxCost(ctx, v, calldata, gasLimit, w.maxGasPrice, &from, &to)
if err != nil {
return commontypes.EstimateFee{}, err
}

return commontypes.EstimateFee{
Fee: cost,
Decimals: 18,
}, nil
}

func (w *chainWriter) getMaxCost(ctx context.Context, amount assets.Eth, calldata []byte,
gasLimit uint64, maxGasPrice *assets.Wei, fromAddress, toAddress *common.Address) (*big.Int, error) {
fee, err := w.GetFeeComponents(ctx)
var gasPrice *big.Int
if err != nil {
w.logger.Warnf("%w: GetFeeComponents failed; use maxFeePrice instead", err)
gasPrice = fee.ExecutionFee
} else {
gasPrice = (*big.Int)(maxGasPrice)
}

estimateGas, err := w.client.EstimateGas(ctx, ethereum.CallMsg{To: toAddress, Data: calldata})
if err != nil {
w.logger.Warnf("%w: EstimateGas failed; use gasLimit instead", err)
estimateGas = gasLimit
}

totalFee := new(big.Int).Mul(gasPrice, big.NewInt(int64(estimateGas)))
amountWithFees := new(big.Int).Add(amount.ToInt(), totalFee)

return amountWithFees, nil
}

func (w *chainWriter) Close() error {
return w.StopOnce(w.Name(), func() error {
return nil
Expand Down
4 changes: 2 additions & 2 deletions deployment/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ require (
github.com/smartcontractkit/chainlink-aptos v0.0.0-20250502091650-484cfa7ccddf
github.com/smartcontractkit/chainlink-ccip v0.0.0-20250515091132-6c08936b29ab
github.com/smartcontractkit/chainlink-ccip/chains/solana v0.0.0-20250515132731-ad40fab9b75e
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250515101002-90b1d1b66ce4
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250519161208-80bc8b13c0e7
github.com/smartcontractkit/chainlink-deployments-framework v0.1.2
github.com/smartcontractkit/chainlink-evm v0.0.0-20250515125821-cae192cd18bb
github.com/smartcontractkit/chainlink-framework/multinode v0.0.0-20250514200342-5169fbe9e28d
github.com/smartcontractkit/chainlink-protos/job-distributor v0.10.0
github.com/smartcontractkit/chainlink-protos/orchestrator v0.6.0
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250516194721-92fae45fbb27
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250519165144-2167692b318c
github.com/smartcontractkit/chainlink-testing-framework/framework v0.8.1
github.com/smartcontractkit/chainlink-testing-framework/lib v1.52.4
github.com/smartcontractkit/freeport v0.1.0
Expand Down
8 changes: 4 additions & 4 deletions deployment/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1243,8 +1243,8 @@ github.com/smartcontractkit/chainlink-ccip v0.0.0-20250515091132-6c08936b29ab h1
github.com/smartcontractkit/chainlink-ccip v0.0.0-20250515091132-6c08936b29ab/go.mod h1:Jb05WL6lj5H89XGcaaOinxTf4Gdj+vXO4TcUhqTgqIM=
github.com/smartcontractkit/chainlink-ccip/chains/solana v0.0.0-20250515132731-ad40fab9b75e h1:4Hx+m9MovqsayvKdYjnj6OQ5pxXkpU2LDz0UNRuqoA8=
github.com/smartcontractkit/chainlink-ccip/chains/solana v0.0.0-20250515132731-ad40fab9b75e/go.mod h1:k3/Z6AvwurPUlfuDFEonRbkkiTSgNSrtVNhJEWNlUZA=
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250515101002-90b1d1b66ce4 h1:kWAHWz0JFWzJ9pw5mFGRWSeudqHJOy1ttDTv72WsdG4=
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250515101002-90b1d1b66ce4/go.mod h1:uNF6+noody47ZdmRwymDZAnQ7eKTXLzMKvl41LA63lo=
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250519161208-80bc8b13c0e7 h1:yeDKMQSKJQwe5SdfPmbzjzgyZIeq1QlcR93K3e7tRCU=
github.com/smartcontractkit/chainlink-common v0.7.1-0.20250519161208-80bc8b13c0e7/go.mod h1:uNF6+noody47ZdmRwymDZAnQ7eKTXLzMKvl41LA63lo=
github.com/smartcontractkit/chainlink-common/pkg/monitoring v0.0.0-20250415235644-8703639403c7 h1:9wh1G+WbXwPVqf0cfSRSgwIcaXTQgvYezylEAfwmrbw=
github.com/smartcontractkit/chainlink-common/pkg/monitoring v0.0.0-20250415235644-8703639403c7/go.mod h1:yaDOAZF6MNB+NGYpxGCUc+owIdKrjvFW0JODdTcQ3V0=
github.com/smartcontractkit/chainlink-data-streams v0.1.1-0.20250417193446-eeb0a7d1e049 h1:7HwYt8rDz1ehTcB28oNipdTZUtV17F2sfkLTLtMJC4c=
Expand Down Expand Up @@ -1273,8 +1273,8 @@ github.com/smartcontractkit/chainlink-protos/svr v1.1.0 h1:79Z9N9dMbMVRGaLoDPAQ+
github.com/smartcontractkit/chainlink-protos/svr v1.1.0/go.mod h1:TcOliTQU6r59DwG4lo3U+mFM9WWyBHGuFkkxQpvSujo=
github.com/smartcontractkit/chainlink-protos/workflows/go v0.0.0-20250501150903-3e93089d9ad5 h1:iF7WucN9ZxJLbDuyB2co0mGPytFY9NdEBeQHY1+bJnc=
github.com/smartcontractkit/chainlink-protos/workflows/go v0.0.0-20250501150903-3e93089d9ad5/go.mod h1:HIpGvF6nKCdtZ30xhdkKWGM9+4Z4CVqJH8ZBL1FTEiY=
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250516194721-92fae45fbb27 h1:Dxpmsm2i6gV5bYw5exlnjK7GBPgKOCJgRh6H/78dY8w=
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250516194721-92fae45fbb27/go.mod h1:gLFSnWHGOiFLiUr6PIJ08q2ZvhUdKsjYY5+UxR4SHlg=
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250519165144-2167692b318c h1:9fc0vfCgRl8zHDAVQPxcTeYafF8Y/g2eLVxC96Mhwc0=
github.com/smartcontractkit/chainlink-solana v1.1.2-0.20250519165144-2167692b318c/go.mod h1:2Ctb1oLBWOjX+VukbmY4dSpRIF9DBxqp8SxyaBSgOwA=
github.com/smartcontractkit/chainlink-testing-framework/framework v0.8.1 h1:xDfmixWYefrECJkWpnSDx+WOdnJe/wNADsTZ2B68jcw=
github.com/smartcontractkit/chainlink-testing-framework/framework v0.8.1/go.mod h1:JJlLI8lxMi6nMF+Tl56qHJcAybgNcupQwltnF5Y6Oi0=
github.com/smartcontractkit/chainlink-testing-framework/lib v1.52.4 h1:+kwLuO9kcq1+ZbRUQjxX1SQmzlL2M6ZP6+L0xQMtmkU=
Expand Down
Loading
Loading