fix: add gaspremium check when less than maxfee

This commit is contained in:
hunjixin 2022-10-17 11:56:50 +08:00
parent b6d36436c3
commit e8aacf325c
2 changed files with 38 additions and 0 deletions

View File

@ -218,6 +218,7 @@ func CapGasFee(mff dtypes.DefaultMaxFeeFunc, msg *types.Message, sendSpec *api.M
totalFee := types.BigMul(msg.GasFeeCap, gl)
if totalFee.LessThanEqual(maxFee) {
msg.GasPremium = big.Min(msg.GasFeeCap, msg.GasPremium) // cap premium at FeeCap
return
}

View File

@ -1075,3 +1075,40 @@ func TestRemoveMessage(t *testing.T) {
assert.Len(t, msgs, 0)
}
}
func TestCapGasFee(t *testing.T) {
t.Run("use default maxfee", func(t *testing.T) {
msg := &types.Message{
GasLimit: 100_000_000,
GasFeeCap: abi.NewTokenAmount(100_000_000),
GasPremium: abi.NewTokenAmount(100_000),
}
CapGasFee(func() (abi.TokenAmount, error) {
return abi.NewTokenAmount(100_000_000_000), nil
}, msg, nil)
assert.Equal(t, msg.GasFeeCap.Int64(), int64(1000))
assert.Equal(t, msg.GasPremium.Int.Int64(), int64(1000))
})
t.Run("use spec maxfee", func(t *testing.T) {
msg := &types.Message{
GasLimit: 100_000_000,
GasFeeCap: abi.NewTokenAmount(100_000_000),
GasPremium: abi.NewTokenAmount(100_000),
}
CapGasFee(nil, msg, &api.MessageSendSpec{MaxFee: abi.NewTokenAmount(100_000_000_000)})
assert.Equal(t, msg.GasFeeCap.Int64(), int64(1000))
assert.Equal(t, msg.GasPremium.Int.Int64(), int64(1000))
})
t.Run("use smaller feecap value when fee is enough", func(t *testing.T) {
msg := &types.Message{
GasLimit: 100_000_000,
GasFeeCap: abi.NewTokenAmount(100_000),
GasPremium: abi.NewTokenAmount(100_000_000),
}
CapGasFee(nil, msg, &api.MessageSendSpec{MaxFee: abi.NewTokenAmount(100_000_000_000_000)})
assert.Equal(t, msg.GasFeeCap.Int64(), int64(100_000))
assert.Equal(t, msg.GasPremium.Int.Int64(), int64(100_000))
})
}