Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
feat(utils): replace mathutils (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp authored Mar 4, 2024
1 parent 0f15192 commit 514869d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ require (
github.com/stretchr/testify v1.8.4
github.com/swaggo/swag v1.16.2
github.com/urfave/cli/v2 v2.25.7
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611
golang.org/x/sync v0.5.0
modernc.org/mathutil v1.6.0
)

require (
Expand Down Expand Up @@ -167,7 +167,6 @@ require (
github.com/quic-go/quic-go v0.39.3 // indirect
github.com/quic-go/webtransport-go v0.6.0 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/rs/cors v1.7.0 // indirect
Expand Down Expand Up @@ -197,7 +196,6 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -891,8 +891,6 @@ github.com/raulk/go-watchdog v1.3.0 h1:oUmdlHxdkXRJlwfG0O9omj8ukerm8MEQavSiDTEtB
github.com/raulk/go-watchdog v1.3.0/go.mod h1:fIvOnLbF0b0ZwkB9YU4mOW9Did//4vPZtDqv66NfsMU=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down Expand Up @@ -1654,8 +1652,6 @@ k8s.io/utils v0.0.0-20201110183641-67b214c5f920 h1:CbnUZsM497iRC5QMVkHwyl8s2tB3g
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
lukechampine.com/blake3 v1.2.1 h1:YuqqRuaqsGV71BV/nm9xlI0MKUv4QC54jQnBChWbGnI=
lukechampine.com/blake3 v1.2.1/go.mod h1:0OFRp7fBtAylGVCO40o87sbupkyIGgbpv1+M1k1LM6k=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
13 changes: 7 additions & 6 deletions internal/sender/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/holiman/uint256"
"modernc.org/mathutil"

"github.com/taikoxyz/taiko-client/internal/utils"
)

// adjustGas adjusts the gas fee cap and gas tip cap of the given transaction with the configured
Expand All @@ -20,27 +21,27 @@ func (s *Sender) adjustGas(txData types.TxData) {
case *types.DynamicFeeTx:
gasFeeCap := baseTx.GasFeeCap.Int64()
gasFeeCap = gasFeeCap / 100 * int64(rate)
gasFeeCap = mathutil.MinInt64(gasFeeCap, int64(s.MaxGasFee))
gasFeeCap = utils.Min(gasFeeCap, int64(s.MaxGasFee))
baseTx.GasFeeCap = big.NewInt(gasFeeCap)

gasTipCap := baseTx.GasTipCap.Int64()
gasTipCap = gasTipCap / 100 * int64(rate)
gasTipCap = mathutil.MinInt64(gasFeeCap, mathutil.MinInt64(gasTipCap, int64(s.MaxGasFee)))
gasTipCap = utils.Min(gasFeeCap, utils.Min(gasTipCap, int64(s.MaxGasFee)))
baseTx.GasTipCap = big.NewInt(gasTipCap)
case *types.BlobTx:
gasFeeCap := baseTx.GasFeeCap.Uint64()
gasFeeCap = gasFeeCap / 100 * rate
gasFeeCap = mathutil.MinUint64(gasFeeCap, s.MaxGasFee)
gasFeeCap = utils.Min(gasFeeCap, s.MaxGasFee)
baseTx.GasFeeCap = uint256.NewInt(gasFeeCap)

gasTipCap := baseTx.GasTipCap.Uint64()
gasTipCap = gasTipCap / 100 * rate
gasTipCap = mathutil.MinUint64(gasFeeCap, mathutil.MinUint64(gasTipCap, s.MaxGasFee))
gasTipCap = utils.Min(gasFeeCap, utils.Min(gasTipCap, s.MaxGasFee))
baseTx.GasTipCap = uint256.NewInt(gasTipCap)

blobFeeCap := baseTx.BlobFeeCap.Uint64()
blobFeeCap = blobFeeCap / 100 * rate
blobFeeCap = mathutil.MinUint64(blobFeeCap, s.MaxBlobFee)
blobFeeCap = utils.Min(blobFeeCap, s.MaxBlobFee)
baseTx.BlobFeeCap = uint256.NewInt(blobFeeCap)
default:
log.Warn("Unsupported transaction type when adjust gas fee", "from", s.Opts.From)
Expand Down
17 changes: 17 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/joho/godotenv"
"github.com/modern-go/reflect2"
"golang.org/x/exp/constraints"
)

func LoadEnv() {
Expand Down Expand Up @@ -52,3 +53,19 @@ func RandUint32(max *big.Int) uint32 {
func IsNil(i interface{}) bool {
return reflect2.IsNil(i)
}

// Min return the minimum value of two integers.
func Min[T constraints.Integer](a, b T) T {
if a < b {
return a
}
return b
}

// Max return the maximum value of two integers.
func Max[T constraints.Integer](a, b T) T {
if a > b {
return a
}
return b
}

0 comments on commit 514869d

Please sign in to comment.