Skip to content

Commit

Permalink
add check for token outflows with InflowOutflowQuotaTokenBase
Browse files Browse the repository at this point in the history
  • Loading branch information
gsk967 committed Nov 7, 2023
1 parent 6868b11 commit 3f28e31
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 47 deletions.
6 changes: 6 additions & 0 deletions proto/umee/uibc/v1/quota.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ message Params {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// inflow_outflow_quota_token_base defines the inflow outflow quota base for token
string inflow_outflow_quota_token_base = 7 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

// IBCTransferStatus status of ibc-transfer quota check for inflow and outflow
Expand Down
16 changes: 10 additions & 6 deletions x/uibc/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
// DefaultParams returns default genesis params
func DefaultParams() Params {
return Params{
IbcStatus: IBCTransferStatus_IBC_TRANSFER_STATUS_QUOTA_ENABLED,
TotalQuota: sdk.NewDec(1_600_000), // $1.6M
TokenQuota: sdk.NewDec(900_000), // $900K
QuotaDuration: time.Second * 60 * 60 * 24, // 24h
InflowOutflowQuotaBase: sdk.NewDec(1_000_000), // 1M
InflowOutflowQuotaRate: sdk.MustNewDecFromStr("0.25"),
IbcStatus: IBCTransferStatus_IBC_TRANSFER_STATUS_QUOTA_ENABLED,
TotalQuota: sdk.NewDec(1_600_000), // $1.6M
TokenQuota: sdk.NewDec(1_200_000), // $1.2M
QuotaDuration: time.Second * 60 * 60 * 24, // 24h
InflowOutflowQuotaBase: sdk.NewDec(1_000_000), // 1M
InflowOutflowQuotaRate: sdk.MustNewDecFromStr("0.25"),
InflowOutflowQuotaTokenBase: sdk.NewDec(900_000), // $0.9M
}
}

Expand All @@ -38,6 +39,9 @@ func (p Params) Validate() error {
if err := validateQuota(p.InflowOutflowQuotaRate, "total inflow outflow quota rate"); err != nil {
return err
}
if err := validateQuota(p.InflowOutflowQuotaTokenBase, "total inflow outflow quota token base"); err != nil {
return err
}
if p.TotalQuota.LT(p.TokenQuota) {
return fmt.Errorf("token quota shouldn't be less than quota per denom")
}
Expand Down
121 changes: 85 additions & 36 deletions x/uibc/quota.pb.go

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

1 change: 1 addition & 0 deletions x/uibc/quota/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestUnitParams(t *testing.T) {
params.TotalQuota = sdk.MustNewDecFromStr("3.4321")
params.InflowOutflowQuotaBase = sdk.MustNewDecFromStr("3.4321")
params.InflowOutflowQuotaRate = sdk.MustNewDecFromStr("0.2")
params.InflowOutflowQuotaTokenBase = sdk.MustNewDecFromStr("0.2")

err := k.SetParams(params)
require.NoError(err)
Expand Down
9 changes: 7 additions & 2 deletions x/uibc/quota/keeper/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,14 @@ func (k Keeper) CheckAndUpdateQuota(denom string, newOutflow sdkmath.Int) error

o := k.GetTokenOutflows(denom)
o.Amount = o.Amount.Add(exchangePrice)
if !params.TokenQuota.IsZero() && o.Amount.GT(params.TokenQuota) {
return uibc.ErrQuotaExceeded
inToken := k.GetTokenInflow(denom)
if !params.TokenQuota.IsZero() {
if o.Amount.GT(params.TokenQuota) ||
o.Amount.GT(params.InflowOutflowQuotaTokenBase.Add((sdk.MustNewDecFromStr("0.25").Mul(inToken.Amount)))) {
return uibc.ErrQuotaExceeded
}
}

// Allow outflow either of two conditions
// 1. Total Outflow Sum <= Total Outflow Quota
// or
Expand Down
1 change: 1 addition & 0 deletions x/uibc/quota/keeper/quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestUnitCheckAndUpdateQuota(t *testing.T) {
k.SetTokenInflow(sdk.NewInt64DecCoin(umee, 6))
k.SetTotalOutflowSum(sdk.NewDec(50))
k.SetTotalInflow(sdk.NewDec(50))
k.SetTokenInflow(sdk.NewDecCoin(umee, math.NewInt(50)))

err := k.CheckAndUpdateQuota(umee, sdk.NewInt(1))
assert.NilError(t, err)
Expand Down
7 changes: 4 additions & 3 deletions x/uibc/quota/keeper/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ func (k TestKeeper) checkOutflows(denom string, perToken, total int64) {

func (k TestKeeper) setQuotaParams(perToken, total int64) {
err := k.SetParams(uibc.Params{
TokenQuota: sdk.NewDec(perToken),
TotalQuota: sdk.NewDec(total),
InflowOutflowQuotaBase: sdk.NewDec(1_000_000),
TokenQuota: sdk.NewDec(perToken),
TotalQuota: sdk.NewDec(total),
InflowOutflowQuotaBase: sdk.NewDec(1_000_000),
InflowOutflowQuotaTokenBase: sdk.NewDec(9_00_000),
})
require.NoError(k.t, err)
}

0 comments on commit 3f28e31

Please sign in to comment.