From b1382bf95b3389c63ff7f10f147fc3822f7b4770 Mon Sep 17 00:00:00 2001 From: Sai Kumar Date: Tue, 31 Oct 2023 17:13:30 +0530 Subject: [PATCH] address the review comments --- proto/umee/uibc/v1/genesis.proto | 2 +- proto/umee/uibc/v1/quota.proto | 10 +- x/uibc/genesis.pb.go | 2 +- x/uibc/params.go | 16 +-- x/uibc/quota.pb.go | 132 ++++++++++++++++------- x/uibc/quota/ibc_module.go | 2 +- x/uibc/quota/keeper/genesis.go | 4 +- x/uibc/quota/keeper/intest/quota_test.go | 6 ++ x/uibc/quota/keeper/keys.go | 2 +- x/uibc/quota/keeper/params_test.go | 4 +- x/uibc/quota/keeper/quota.go | 104 +++++++++--------- x/uibc/quota/keeper/quota_test.go | 24 ++++- 12 files changed, 201 insertions(+), 107 deletions(-) diff --git a/proto/umee/uibc/v1/genesis.proto b/proto/umee/uibc/v1/genesis.proto index f9e50cf2c4..942c48b5a0 100644 --- a/proto/umee/uibc/v1/genesis.proto +++ b/proto/umee/uibc/v1/genesis.proto @@ -30,7 +30,7 @@ message GenesisState { (gogoproto.jsontag) = "quota_duration,omitempty", (gogoproto.moretags) = "yaml:\"quota_expires\"" ]; - // inflows defines inflow amount of denoms + // inflows defines inflow amount per denom denoms repeated cosmos.base.v1beta1.DecCoin inflows = 5 [ (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins", (gogoproto.nullable) = false diff --git a/proto/umee/uibc/v1/quota.proto b/proto/umee/uibc/v1/quota.proto index a72d50f8d0..bef592e45e 100644 --- a/proto/umee/uibc/v1/quota.proto +++ b/proto/umee/uibc/v1/quota.proto @@ -30,8 +30,14 @@ message Params { (gogoproto.jsontag) = "quota_duration,omitempty", (gogoproto.moretags) = "yaml:\"quota_duration\"" ]; - // total_inflow_quota defines the total inflow limit of ibc-transfer in USD - string total_inflow_quota = 5 [ + // inflow_outflow_quota_base defines the inflow outflow quota base of ibc-transfer in USD + string inflow_outflow_quota_base = 5 [ + (cosmos_proto.scalar) = "cosmos.Dec", + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false + ]; + // inflow_outflow_quota_rate defines the rate of total inflows + string inflow_outflow_quota_rate = 6 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false diff --git a/x/uibc/genesis.pb.go b/x/uibc/genesis.pb.go index 5965541ded..acf0468b89 100644 --- a/x/uibc/genesis.pb.go +++ b/x/uibc/genesis.pb.go @@ -38,7 +38,7 @@ type GenesisState struct { TotalOutflowSum github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=total_outflow_sum,json=totalOutflowSum,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"total_outflow_sum"` // quota_expires defines quota expire time (as unix timestamp) for ibc-transfer denom. QuotaExpires time.Time `protobuf:"bytes,4,opt,name=quota_expires,json=quotaExpires,proto3,stdtime" json:"quota_duration,omitempty" yaml:"quota_expires"` - // inflows defines inflow amount of denoms + // inflows defines inflow amount per denom denoms Inflows github_com_cosmos_cosmos_sdk_types.DecCoins `protobuf:"bytes,5,rep,name=inflows,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.DecCoins" json:"inflows"` // total_inflow_sum defines the total inflow sum of ibc-transfer in USD. TotalInflowSum github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,6,opt,name=total_inflow_sum,json=totalInflowSum,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"total_inflow_sum"` diff --git a/x/uibc/params.go b/x/uibc/params.go index 9f4726e258..8bd3499341 100644 --- a/x/uibc/params.go +++ b/x/uibc/params.go @@ -10,11 +10,12 @@ import ( // DefaultParams returns default genesis params func DefaultParams() Params { return Params{ - IbcStatus: IBCTransferStatus_IBC_TRANSFER_STATUS_QUOTA_ENABLED, - TotalQuota: sdk.NewDec(1_000_000), - TokenQuota: sdk.NewDec(600_000), - QuotaDuration: time.Second * 60 * 60 * 24, // 24h - TotalInflowQuota: sdk.NewDec(1_000_000), + 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.1"), } } @@ -31,7 +32,10 @@ func (p Params) Validate() error { if err := validateQuota(p.TokenQuota, "quota per token"); err != nil { return err } - if err := validateQuota(p.TotalInflowQuota, "total inflow quota"); err != nil { + if err := validateQuota(p.InflowOutflowQuotaBase, "total inflow outflow quota base"); err != nil { + return err + } + if err := validateQuota(p.InflowOutflowQuotaRate, "total inflow outflow quota rate"); err != nil { return err } if p.TotalQuota.LT(p.TokenQuota) { diff --git a/x/uibc/quota.pb.go b/x/uibc/quota.pb.go index 767037249d..0d73164d21 100644 --- a/x/uibc/quota.pb.go +++ b/x/uibc/quota.pb.go @@ -83,8 +83,10 @@ type Params struct { TokenQuota github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=token_quota,json=tokenQuota,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"token_quota"` // quota_duration defines quota expires for each ibc-transfer denom in seconds QuotaDuration time.Duration `protobuf:"bytes,4,opt,name=quota_duration,json=quotaDuration,proto3,stdduration" json:"quota_duration,omitempty" yaml:"quota_duration"` - // total_inflow_quota defines the total inflow limit of ibc-transfer in USD - TotalInflowQuota github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,5,opt,name=total_inflow_quota,json=totalInflowQuota,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"total_inflow_quota"` + // inflow_outflow_quota_base defines the inflow outflow quota base of ibc-transfer in USD + InflowOutflowQuotaBase github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,5,opt,name=inflow_outflow_quota_base,json=inflowOutflowQuotaBase,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflow_outflow_quota_base"` + // inflow_outflow_quota_rate defines the rate of total inflows + InflowOutflowQuotaRate github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,6,opt,name=inflow_outflow_quota_rate,json=inflowOutflowQuotaRate,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflow_outflow_quota_rate"` } func (m *Params) Reset() { *m = Params{} } @@ -142,40 +144,42 @@ func init() { func init() { proto.RegisterFile("umee/uibc/v1/quota.proto", fileDescriptor_651be1a0280abcb6) } var fileDescriptor_651be1a0280abcb6 = []byte{ - // 524 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x93, 0x31, 0x6f, 0xd3, 0x40, - 0x14, 0x80, 0xe3, 0xa4, 0xad, 0xd4, 0x2b, 0x54, 0xe1, 0x04, 0x52, 0xda, 0xc1, 0x0e, 0x81, 0x46, - 0xa1, 0x22, 0x67, 0xb5, 0x48, 0x0c, 0x08, 0x10, 0x71, 0xec, 0x4a, 0x96, 0x50, 0x9a, 0xc6, 0xf6, - 0x82, 0x84, 0x2c, 0xdb, 0xbd, 0x18, 0x93, 0xd8, 0x17, 0xec, 0x73, 0x4a, 0x26, 0x56, 0x46, 0x46, - 0x7e, 0x08, 0x3f, 0xa2, 0x63, 0xc5, 0x84, 0x18, 0x02, 0x4a, 0x36, 0x46, 0xf8, 0x03, 0xc8, 0x77, - 0x8e, 0xda, 0xaa, 0xcd, 0xd6, 0xc9, 0xf7, 0xfc, 0xbe, 0xfb, 0xee, 0xbd, 0x67, 0x1f, 0xa8, 0xa4, - 0x21, 0xc6, 0x72, 0x1a, 0xb8, 0x9e, 0x3c, 0xde, 0x93, 0x3f, 0xa4, 0x84, 0x3a, 0x68, 0x14, 0x13, - 0x4a, 0xe0, 0xad, 0x2c, 0x83, 0x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 0x8f, 0xb9, 0x58, 0x78, 0x73, 0x71, 0xd8, 0xd9, 0x2d, 0x68, 0x46, 0x98, 0x9e, 0x90, 0x78, - 0xc0, 0x02, 0x79, 0xfc, 0x54, 0xfe, 0xc8, 0x2e, 0xac, 0xbb, 0xc6, 0xfe, 0x8c, 0x27, 0xff, 0x03, - 0x00, 0x00, 0xff, 0xff, 0x88, 0xbe, 0x35, 0x75, 0xc4, 0x03, 0x00, 0x00, + // 546 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x94, 0x31, 0x6f, 0xd3, 0x40, + 0x14, 0x80, 0xe3, 0x34, 0x8d, 0xd4, 0x2b, 0x54, 0xc1, 0x02, 0xe4, 0x74, 0xb0, 0x43, 0xa0, 0x51, + 0xa8, 0x88, 0xad, 0x16, 0x89, 0x01, 0x01, 0x22, 0x8e, 0x5d, 0xc9, 0x12, 0x4a, 0x52, 0xdb, 0x59, + 0x90, 0x90, 0x75, 0x76, 0x2f, 0xc1, 0x4a, 0xec, 0x0b, 0xf6, 0x39, 0x21, 0x13, 0x2b, 0x23, 0x23, + 0x3f, 0x84, 0x1f, 0xd1, 0xb1, 0x62, 0x42, 0x0c, 0x01, 0x25, 0x1b, 0x23, 0x1b, 0x1b, 0xf2, 0x9d, + 0xa3, 0xb6, 0xa2, 0xd9, 0xca, 0x74, 0xf7, 0xf2, 0xbe, 0xfb, 0xde, 0xbb, 0xa7, 0x8b, 0x81, 0x90, + 0x04, 0x08, 0x29, 0x89, 0xef, 0x7a, 0xca, 0xe4, 0x40, 0x79, 0x97, 0x60, 0x02, 0xe5, 0x71, 0x84, + 0x09, 0xe6, 0x6f, 0xa4, 0x19, 0x39, 0xcd, 0xc8, 0x93, 0x83, 0xdd, 0xdb, 0x03, 0x3c, 0xc0, 0x34, + 0xa1, 0xa4, 0x3b, 0xc6, 0xec, 0x8a, 0x03, 0x8c, 0x07, 0x23, 0xa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xa5, 0x98, 0xfb, 0xb6, 0x14, 0x73, 0xaf, 0x2f, 0x8e, 0x3c, 0xfd, 0x2f, + 0x36, 0x42, 0x44, 0xa6, 0x38, 0x1a, 0xd2, 0x40, 0x99, 0x3c, 0x51, 0xde, 0xd3, 0xcf, 0x86, 0x5b, + 0xa4, 0xef, 0xf3, 0xf1, 0xdf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xd7, 0x73, 0x20, 0x82, 0x4a, 0x04, + 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -199,9 +203,19 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size := m.TotalInflowQuota.Size() + size := m.InflowOutflowQuotaRate.Size() i -= size - if _, err := m.TotalInflowQuota.MarshalTo(dAtA[i:]); err != nil { + if _, err := m.InflowOutflowQuotaRate.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintQuota(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x32 + { + size := m.InflowOutflowQuotaBase.Size() + i -= size + if _, err := m.InflowOutflowQuotaBase.MarshalTo(dAtA[i:]); err != nil { return 0, err } i = encodeVarintQuota(dAtA, i, uint64(size)) @@ -270,7 +284,9 @@ func (m *Params) Size() (n int) { n += 1 + l + sovQuota(uint64(l)) l = github_com_cosmos_gogoproto_types.SizeOfStdDuration(m.QuotaDuration) n += 1 + l + sovQuota(uint64(l)) - l = m.TotalInflowQuota.Size() + l = m.InflowOutflowQuotaBase.Size() + n += 1 + l + sovQuota(uint64(l)) + l = m.InflowOutflowQuotaRate.Size() n += 1 + l + sovQuota(uint64(l)) return n } @@ -432,7 +448,41 @@ func (m *Params) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 5: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field TotalInflowQuota", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field InflowOutflowQuotaBase", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuota + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthQuota + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQuota + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.InflowOutflowQuotaBase.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 6: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field InflowOutflowQuotaRate", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -460,7 +510,7 @@ func (m *Params) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.TotalInflowQuota.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.InflowOutflowQuotaRate.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/uibc/quota/ibc_module.go b/x/uibc/quota/ibc_module.go index ff2c1af290..bf87008e6a 100644 --- a/x/uibc/quota/ibc_module.go +++ b/x/uibc/quota/ibc_module.go @@ -55,7 +55,7 @@ func (im ICS20Module) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, } isSourceChain := transfertypes.SenderChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), data.Denom) - ackErr := im.kb.Keeper(&ctx).CheckIBCInflow(ctx, packet, data.Denom, data.Amount, isSourceChain) + ackErr := im.kb.Keeper(&ctx).RecordIBCInflow(ctx, packet, data.Denom, data.Amount, isSourceChain) if ackErr != nil { return ackErr } diff --git a/x/uibc/quota/keeper/genesis.go b/x/uibc/quota/keeper/genesis.go index da94643824..2fd86cb0d4 100644 --- a/x/uibc/quota/keeper/genesis.go +++ b/x/uibc/quota/keeper/genesis.go @@ -14,9 +14,9 @@ func (kb Builder) InitGenesis(ctx sdk.Context, genState uibc.GenesisState) { util.Panic(err) k.SetTokenOutflows(genState.Outflows) - k.SetTokenOutflows(genState.Inflows) + k.SetTokenInflows(genState.Inflows) k.SetTotalOutflowSum(genState.TotalOutflowSum) - k.SetTotalInflowSum(genState.TotalInflowSum) + k.SetTotalInflow(genState.TotalInflowSum) err = k.SetExpire(genState.QuotaExpires) util.Panic(err) diff --git a/x/uibc/quota/keeper/intest/quota_test.go b/x/uibc/quota/keeper/intest/quota_test.go index ec8beca1eb..63789caab2 100644 --- a/x/uibc/quota/keeper/intest/quota_test.go +++ b/x/uibc/quota/keeper/intest/quota_test.go @@ -25,12 +25,18 @@ func TestResetQuota(t *testing.T) { q := k.GetTokenOutflows(umeeQuota.Denom) assert.DeepEqual(t, q, umeeQuota) + k.SetTokenInflow(umeeQuota) + i := k.GetTokenInflow(umeeQuota.Denom) + assert.DeepEqual(t, i, umeeQuota) + // reset the quota k.ResetAllQuotas() // check the quota after reset q = k.GetTokenOutflows(umeeQuota.Denom) assert.DeepEqual(t, q.Amount, sdk.NewDec(0)) + i = k.GetTokenInflow(umeeQuota.Denom) + assert.DeepEqual(t, i.Amount, sdk.NewDec(0)) } func TestKeeper_CheckAndUpdateQuota(t *testing.T) { diff --git a/x/uibc/quota/keeper/keys.go b/x/uibc/quota/keeper/keys.go index 0d644c9d4e..82f5ed4cf6 100644 --- a/x/uibc/quota/keeper/keys.go +++ b/x/uibc/quota/keeper/keys.go @@ -18,7 +18,7 @@ func KeyTotalOutflows(ibcDenom string) []byte { return util.ConcatBytes(0, keyPrefixDenomOutflows, []byte(ibcDenom)) } -func KeyTotalInflows(ibcDenom string) []byte { +func KeyTokenInflow(ibcDenom string) []byte { // keyPrefixDenomInflows | denom return util.ConcatBytes(0, keyPrefixDenomInflows, []byte(ibcDenom)) } diff --git a/x/uibc/quota/keeper/params_test.go b/x/uibc/quota/keeper/params_test.go index 092bb900fb..6eb7734ccb 100644 --- a/x/uibc/quota/keeper/params_test.go +++ b/x/uibc/quota/keeper/params_test.go @@ -22,7 +22,9 @@ func TestUnitParams(t *testing.T) { params.IbcStatus = uibc.IBCTransferStatus_IBC_TRANSFER_STATUS_QUOTA_DISABLED params.TokenQuota = sdk.MustNewDecFromStr("12.23") params.TotalQuota = sdk.MustNewDecFromStr("3.4321") - params.TotalInflowQuota = sdk.MustNewDecFromStr("3.4321") + params.InflowOutflowQuotaBase = sdk.MustNewDecFromStr("3.4321") + params.InflowOutflowQuotaRate = sdk.MustNewDecFromStr("0.2") + err := k.SetParams(params) require.NoError(err) // check the updated params diff --git a/x/uibc/quota/keeper/quota.go b/x/uibc/quota/keeper/quota.go index b02cdf0512..3c2061abf0 100644 --- a/x/uibc/quota/keeper/quota.go +++ b/x/uibc/quota/keeper/quota.go @@ -38,6 +38,39 @@ func (k Keeper) GetAllOutflows() (sdk.DecCoins, error) { return outflows, nil } +// GetTokenOutflows returns sum of denom outflows in USD value in the DecCoin structure. +func (k Keeper) GetTokenOutflows(denom string) sdk.DecCoin { + amount, _ := store.GetDec(k.store, KeyTotalOutflows(denom), "total_outflow") + return sdk.NewDecCoinFromDec(denom, amount) +} + +// SetTokenOutflows saves provided updated IBC outflows as a pair: USD value, denom name in the +// DecCoin structure. +func (k Keeper) SetTokenOutflows(outflows sdk.DecCoins) { + for _, q := range outflows { + k.SetTokenOutflow(q) + } +} + +// SetTotalOutflowSum save the total outflow of ibc-transfer amount. +func (k Keeper) SetTotalOutflowSum(amount sdk.Dec) { + err := store.SetDec(k.store, keyTotalOutflows, amount, "total_outflow_sum") + util.Panic(err) +} + +// GetTotalOutflow returns the total outflow of ibc-transfer amount. +func (k Keeper) GetTotalOutflow() sdk.Dec { + amount, _ := store.GetDec(k.store, keyTotalOutflows, "total_outflow") + return amount +} + +// SetTokenOutflow save the outflows of denom into store. +func (k Keeper) SetTokenOutflow(outflow sdk.DecCoin) { + key := KeyTotalOutflows(outflow.Denom) + err := store.SetDec(k.store, key, outflow.Amount, "total_outflow") + util.Panic(err) +} + // GetAllInflows returns inflows of all registered tokens in USD value. func (k Keeper) GetAllInflows() (sdk.DecCoins, error) { var inflows sdk.DecCoins @@ -57,20 +90,6 @@ func (k Keeper) GetAllInflows() (sdk.DecCoins, error) { return inflows, nil } -// GetTokenOutflows returns sum of denom outflows in USD value in the DecCoin structure. -func (k Keeper) GetTokenOutflows(denom string) sdk.DecCoin { - amount, _ := store.GetDec(k.store, KeyTotalOutflows(denom), "total_outflow") - return sdk.NewDecCoinFromDec(denom, amount) -} - -// SetTokenOutflows saves provided updated IBC outflows as a pair: USD value, denom name in the -// DecCoin structure. -func (k Keeper) SetTokenOutflows(outflows sdk.DecCoins) { - for _, q := range outflows { - k.SetTokenOutflow(q) - } -} - // SetTokenInflows saves provided updated IBC inflows as a pair: USD value, denom name in the // DecCoin structure. func (k Keeper) SetTokenInflows(inflows sdk.DecCoins) { @@ -79,42 +98,30 @@ func (k Keeper) SetTokenInflows(inflows sdk.DecCoins) { } } -// SetTokenOutflow save the outflows of denom into store. -func (k Keeper) SetTokenOutflow(outflow sdk.DecCoin) { - key := KeyTotalOutflows(outflow.Denom) - err := store.SetDec(k.store, key, outflow.Amount, "total_outflow") - util.Panic(err) -} - // SetTokenInflow save the inflow of denom into store. func (k Keeper) SetTokenInflow(inflow sdk.DecCoin) { - key := KeyTotalInflows(inflow.Denom) - err := store.SetDec(k.store, key, inflow.Amount, "total_inflow") + key := KeyTokenInflow(inflow.Denom) + err := store.SetDec(k.store, key, inflow.Amount, "token_inflow") util.Panic(err) } -// GetTotalOutflow returns the total outflow of ibc-transfer amount. -func (k Keeper) GetTotalOutflow() sdk.Dec { - // TODO: use store.Get/SetDec - bz := k.store.Get(keyTotalOutflows) - return sdk.MustNewDecFromStr(string(bz)) +// GetTokenInflow returns the inflow of denom from store. +func (k Keeper) GetTokenInflow(denom string) sdk.DecCoin { + key := KeyTokenInflow(denom) + amount, _ := store.GetDec(k.store, key, "token_inflow") + return sdk.NewDecCoinFromDec(denom, amount) } // GetTotalInflow returns the total inflow of ibc-transfer amount. func (k Keeper) GetTotalInflow() sdk.Dec { - // TODO: use store.Get/SetDec - bz := k.store.Get(keyTotalInflows) - return sdk.MustNewDecFromStr(string(bz)) + amount, _ := store.GetDec(k.store, keyTotalInflows, "total_inflows") + return amount } -// SetTotalOutflowSum save the total outflow of ibc-transfer amount. -func (k Keeper) SetTotalOutflowSum(amount sdk.Dec) { - k.store.Set(keyTotalOutflows, []byte(amount.String())) -} - -// SetTotalInflowSum save the total inflow of ibc-transfer amount. -func (k Keeper) SetTotalInflowSum(amount sdk.Dec) { - k.store.Set(keyTotalInflows, []byte(amount.String())) +// SetTotalInflow save the total inflow of ibc-transfer amount. +func (k Keeper) SetTotalInflow(amount sdk.Dec) { + err := store.SetDec(k.store, keyTotalInflows, amount, "total_inflows") + util.Panic(err) } // SetExpire save the quota expire time of ibc denom into. @@ -135,26 +142,22 @@ func (k Keeper) ResetAllQuotas() error { return err } zero := sdk.NewDec(0) - zeroBz, err := zero.Marshal() - if err != nil { - return err - } // outflows k.SetTotalOutflowSum(zero) store := k.PrefixStore(keyPrefixDenomOutflows) iter := sdk.KVStorePrefixIterator(store, nil) defer iter.Close() for ; iter.Valid(); iter.Next() { - store.Set(iter.Key(), zeroBz) + store.Delete(iter.Key()) } // inflows - k.SetTotalInflowSum(zero) + k.SetTotalInflow(zero) store = k.PrefixStore(keyPrefixDenomInflows) iter = sdk.KVStorePrefixIterator(store, nil) defer iter.Close() for ; iter.Valid(); iter.Next() { - store.Set(iter.Key(), zeroBz) + store.Delete(iter.Key()) } return nil } @@ -184,7 +187,8 @@ func (k Keeper) CheckAndUpdateQuota(denom string, newOutflow sdkmath.Int) error totalOutflowSum := k.GetTotalOutflow().Add(exchangePrice) ttlInSum := k.GetTotalInflow() if !(!params.TotalQuota.IsZero() && totalOutflowSum.LTE(params.TotalQuota) || - !ttlInSum.IsZero() && totalOutflowSum.LTE(sdk.NewDec(10_00_000).Mul(ttlInSum).Add(params.TotalInflowQuota))) { + !ttlInSum.IsZero() && + totalOutflowSum.LTE(params.InflowOutflowQuotaBase.Add(ttlInSum.Mul(params.InflowOutflowQuotaRate)))) { return uibc.ErrQuotaExceeded } k.SetTokenOutflow(o) @@ -249,8 +253,8 @@ func (k Keeper) UndoUpdateQuota(denom string, amount sdkmath.Int) error { return nil } -// CheckIBCInflow validates if inflow token is registered in x/leverage -func (k Keeper) CheckIBCInflow(ctx sdk.Context, +// RecordIBCInflow validates if inflow token is registered in x/leverage +func (k Keeper) RecordIBCInflow(ctx sdk.Context, packet channeltypes.Packet, dataDenom, dataAmount string, isSourceChain bool, ) exported.Acknowledgement { // if chain is recevier and sender chain is source then we need create ibc_denom (ibc/hash(channel,denom)) to @@ -282,7 +286,7 @@ func (k Keeper) CheckIBCInflow(ctx sdk.Context, tokenInflow := sdk.NewDecCoinFromDec(ibcDenom, inflowinUSD) k.SetTokenInflow(tokenInflow) totalInflowSum := k.GetTotalInflow() - k.SetTotalInflowSum(totalInflowSum.Add(inflowinUSD)) + k.SetTotalInflow(totalInflowSum.Add(inflowinUSD)) } return nil diff --git a/x/uibc/quota/keeper/quota_test.go b/x/uibc/quota/keeper/quota_test.go index e80d78d4cd..18115608d6 100644 --- a/x/uibc/quota/keeper/quota_test.go +++ b/x/uibc/quota/keeper/quota_test.go @@ -3,8 +3,10 @@ package keeper import ( "testing" + "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + "gotest.tools/v3/assert" ibcutil "github.com/umee-network/umee/v6/util/ibc" ) @@ -45,7 +47,7 @@ func TestUnitCheckAndUpdateQuota(t *testing.T) { k.SetTokenOutflow(sdk.NewInt64DecCoin(umee, 6)) k.SetTokenInflow(sdk.NewInt64DecCoin(umee, 6)) k.SetTotalOutflowSum(sdk.NewDec(50)) - k.SetTotalInflowSum(sdk.NewDec(50)) + k.SetTotalInflow(sdk.NewDec(50)) err := k.CheckAndUpdateQuota(umee, sdk.NewInt(1)) require.NoError(t, err) @@ -113,3 +115,23 @@ func TestUnitGetExchangePrice(t *testing.T) { _, err = k.getExchangePrice("notexisting", sdk.NewInt(10)) require.ErrorContains(t, err, "not found") } + +func TestSetAndGetIBCInflows(t *testing.T) { + k := initKeeperSimpleMock(t) + inflowSum := sdk.MustNewDecFromStr("123123") + k.SetTotalInflow(inflowSum) + + rv := k.GetTotalInflow() + assert.DeepEqual(t, inflowSum, rv) + + // inflow of token + inflowOfToken := sdk.NewDecCoin("abcd", math.NewInt(1000000)) + k.SetTokenInflow(inflowOfToken) + + val := k.GetTokenInflow(inflowOfToken.Denom) + assert.DeepEqual(t, val, inflowOfToken) + + inflows, err := k.GetAllInflows() + assert.NilError(t, err) + assert.DeepEqual(t, inflows[0], inflowOfToken) +}