Skip to content

Commit

Permalink
ai suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Nov 20, 2023
1 parent 1f5c098 commit 3d6aef2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
4 changes: 2 additions & 2 deletions x/uibc/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func (gs GenesisState) Validate() error {
}

if gs.OutflowSum.IsNegative() {
return fmt.Errorf("total outflow sum cannot be negative : %s ", gs.OutflowSum.String())
return fmt.Errorf("outflow sum cannot be negative : %s ", gs.OutflowSum.String())
}

if gs.InflowSum.IsNegative() {
return fmt.Errorf("total inflow sum cannot be negative : %s ", gs.InflowSum.String())
return fmt.Errorf("inflow sum cannot be negative : %s ", gs.InflowSum.String())
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions x/uibc/quota/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (kb Builder) InitGenesis(ctx sdk.Context, genState uibc.GenesisState) {
k.SetTokenOutflows(genState.Outflows)
k.SetTokenInflows(genState.Inflows)
k.SetOutflowSum(genState.OutflowSum)
k.SetTotalInflow(genState.InflowSum)
k.SetInflowSum(genState.InflowSum)

err = k.SetExpire(genState.QuotaExpires)
util.Panic(err)
Expand All @@ -38,6 +38,6 @@ func (kb Builder) ExportGenesis(ctx sdk.Context) *uibc.GenesisState {
OutflowSum: k.GetTotalOutflow(),
QuotaExpires: *quotaExpires,
Inflows: inflows,
InflowSum: k.GetTotalInflow(),
InflowSum: k.GetInflowSum(),
}
}
29 changes: 14 additions & 15 deletions x/uibc/quota/keeper/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ func (k Keeper) GetTokenInflow(denom string) sdk.DecCoin {
return sdk.NewDecCoinFromDec(denom, amount)
}

// GetTotalInflow returns the total inflow of ibc-transfer amount.
func (k Keeper) GetTotalInflow() sdk.Dec {
// GetInflowSum returns the total inflow of ibc-transfer amount.
func (k Keeper) GetInflowSum() sdk.Dec {
// When total inflow is not stored in store it will return 0
amount, _ := store.GetDec(k.store, keyTotalInflows, "total_inflows")
return amount
}

// SetTotalInflow save the total inflow of ibc-transfer amount.
func (k Keeper) SetTotalInflow(amount sdk.Dec) {
// SetInflowSum save the total inflow of ibc-transfer amount.
func (k Keeper) SetInflowSum(amount sdk.Dec) {
err := store.SetDec(k.store, keyTotalInflows, amount, "total_inflows")
util.Panic(err)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func (k Keeper) ResetAllQuotas() error {
store.DeleteByPrefixStore(ps)

// inflows
k.SetTotalInflow(zero)
k.SetInflowSum(zero)
ps = k.PrefixStore(keyPrefixDenomInflows)
store.DeleteByPrefixStore(ps)
return nil
Expand Down Expand Up @@ -156,19 +156,18 @@ func (k Keeper) CheckAndUpdateQuota(denom string, newOutflow sdkmath.Int) error
}

// Allow outflow either of two conditions
// 1. Total Outflow Sum <= Total Outflow Quota
// or
// 2. Total Outflow Sum <= params.InflowOutflowQuotaBase + (params.InflowOutflowQuotaRate * sum of all inflows)
totalOutflowSum := k.GetTotalOutflow().Add(exchangePrice)
ttlInSum := k.GetTotalInflow()
// 1. Outflow Sum <= Total Outflow Quota
// 2. OR Outflow Sum <= params.InflowOutflowQuotaBase + (params.InflowOutflowQuotaRate * sum of all inflows)
outflowSum := k.GetTotalOutflow().Add(exchangePrice)
inflowSum := k.GetInflowSum()
if !params.TotalQuota.IsZero() {
if totalOutflowSum.GT(params.TotalQuota) ||
totalOutflowSum.GT(params.InflowOutflowQuotaBase.Add(ttlInSum.Mul(params.InflowOutflowQuotaRate))) {
if outflowSum.GT(params.TotalQuota) ||
outflowSum.GT(params.InflowOutflowQuotaBase.Add(inflowSum.Mul(params.InflowOutflowQuotaRate))) {
return uibc.ErrQuotaExceeded
}
}
k.SetTokenOutflow(o)
k.SetOutflowSum(totalOutflowSum)
k.SetOutflowSum(outflowSum)

return nil
}
Expand Down Expand Up @@ -261,8 +260,8 @@ func (k Keeper) RecordIBCInflow(ctx sdk.Context,

tokenInflow := sdk.NewDecCoinFromDec(ibcDenom, inflowInUSD)
k.SetTokenInflow(tokenInflow)
totalInflowSum := k.GetTotalInflow()
k.SetTotalInflow(totalInflowSum.Add(inflowInUSD))
totalInflowSum := k.GetInflowSum()
k.SetInflowSum(totalInflowSum.Add(inflowInUSD))
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions x/uibc/quota/keeper/quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestUnitCheckAndUpdateQuota(t *testing.T) {
k.SetTokenOutflow(sdk.NewInt64DecCoin(umee, 6))
k.SetTokenInflow(sdk.NewInt64DecCoin(umee, 6))
k.SetOutflowSum(sdk.NewDec(50))
k.SetTotalInflow(sdk.NewDec(50))
k.SetInflowSum(sdk.NewDec(50))
k.SetTokenInflow(sdk.NewDecCoin(umee, math.NewInt(50)))

err := k.CheckAndUpdateQuota(umee, sdk.NewInt(1))
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestUnitCheckAndUpdateQuota(t *testing.T) {
err = k.SetParams(dp)
k.SetOutflowSum(sdk.NewDec(80))
// 80+(20*2) > Total Outflow Quota Limit (100)
err = k.CheckAndUpdateQuota(umee, sdk.NewInt(20)) // exceeds token quota
err = k.CheckAndUpdateQuota(umee, sdk.NewInt(20))
assert.ErrorContains(t, err, "quota")
err = k.CheckAndUpdateQuota(umee, sdk.NewInt(10))
assert.NilError(t, err)
Expand All @@ -134,7 +134,7 @@ func TestUnitCheckAndUpdateQuota(t *testing.T) {

err = k.SetParams(dp)
assert.NilError(t, err)
k.SetTotalInflow(sdk.NewDec(100))
k.SetInflowSum(sdk.NewDec(100))
// 80+(80*2) > InflowOutflowQuotaBase(100) + 25% of Total Inflow Sum (100) = 240 > 100+25
err = k.CheckAndUpdateQuota(umee, sdk.NewInt(80)) // exceeds token quota
assert.ErrorContains(t, err, "quota")
Expand All @@ -161,9 +161,9 @@ func TestUnitGetExchangePrice(t *testing.T) {
func TestSetAndGetIBCInflows(t *testing.T) {
k := initKeeperSimpleMock(t)
inflowSum := sdk.MustNewDecFromStr("123123")
k.SetTotalInflow(inflowSum)
k.SetInflowSum(inflowSum)

rv := k.GetTotalInflow()
rv := k.GetInflowSum()
assert.DeepEqual(t, inflowSum, rv)

// inflow of token
Expand Down

0 comments on commit 3d6aef2

Please sign in to comment.