From 82ea79dcdc490349db5233d030884265c8953d45 Mon Sep 17 00:00:00 2001 From: affan Date: Thu, 22 Aug 2024 15:35:23 -0400 Subject: [PATCH] fixes and lints --- .../src/codegen/dydxprotocol/stats/stats.ts | 79 +++++++++++++++++++ proto/dydxprotocol/stats/stats.proto | 6 +- protocol/testutil/keeper/clob.go | 7 -- protocol/testutil/keeper/staking.go | 3 +- protocol/x/affiliates/keeper/keeper.go | 10 ++- protocol/x/affiliates/keeper/keeper_test.go | 10 ++- protocol/x/stats/keeper/keeper_test.go | 7 +- protocol/x/stats/types/expected_keepers.go | 3 +- 8 files changed, 104 insertions(+), 21 deletions(-) diff --git a/indexer/packages/v4-protos/src/codegen/dydxprotocol/stats/stats.ts b/indexer/packages/v4-protos/src/codegen/dydxprotocol/stats/stats.ts index 1aba60a6bb..6dc0de8065 100644 --- a/indexer/packages/v4-protos/src/codegen/dydxprotocol/stats/stats.ts +++ b/indexer/packages/v4-protos/src/codegen/dydxprotocol/stats/stats.ts @@ -115,6 +115,30 @@ export interface UserStatsSDKType { maker_notional: Long; } +/** CachedStakeAmount stores the last calculated total staked amount for address */ + +export interface CachedStakeAmount { + /** Last calculated total staked amount by the delegator (in coin amount). */ + stakedAmount: Uint8Array; + /** + * Block time at which the calculation is cached (in Unix Epoch seconds) + * Rounded down to nearest second. + */ + + cachedAt: Long; +} +/** CachedStakeAmount stores the last calculated total staked amount for address */ + +export interface CachedStakeAmountSDKType { + /** Last calculated total staked amount by the delegator (in coin amount). */ + staked_amount: Uint8Array; + /** + * Block time at which the calculation is cached (in Unix Epoch seconds) + * Rounded down to nearest second. + */ + + cached_at: Long; +} function createBaseBlockStats(): BlockStats { return { @@ -479,4 +503,59 @@ export const UserStats = { return message; } +}; + +function createBaseCachedStakeAmount(): CachedStakeAmount { + return { + stakedAmount: new Uint8Array(), + cachedAt: Long.UZERO + }; +} + +export const CachedStakeAmount = { + encode(message: CachedStakeAmount, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { + if (message.stakedAmount.length !== 0) { + writer.uint32(10).bytes(message.stakedAmount); + } + + if (!message.cachedAt.isZero()) { + writer.uint32(16).uint64(message.cachedAt); + } + + return writer; + }, + + decode(input: _m0.Reader | Uint8Array, length?: number): CachedStakeAmount { + const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseCachedStakeAmount(); + + while (reader.pos < end) { + const tag = reader.uint32(); + + switch (tag >>> 3) { + case 1: + message.stakedAmount = reader.bytes(); + break; + + case 2: + message.cachedAt = (reader.uint64() as Long); + break; + + default: + reader.skipType(tag & 7); + break; + } + } + + return message; + }, + + fromPartial(object: DeepPartial): CachedStakeAmount { + const message = createBaseCachedStakeAmount(); + message.stakedAmount = object.stakedAmount ?? new Uint8Array(); + message.cachedAt = object.cachedAt !== undefined && object.cachedAt !== null ? Long.fromValue(object.cachedAt) : Long.UZERO; + return message; + } + }; \ No newline at end of file diff --git a/proto/dydxprotocol/stats/stats.proto b/proto/dydxprotocol/stats/stats.proto index bd080434f5..a16cfa84c7 100644 --- a/proto/dydxprotocol/stats/stats.proto +++ b/proto/dydxprotocol/stats/stats.proto @@ -63,14 +63,14 @@ message UserStats { } // CachedStakeAmount stores the last calculated total staked amount for address -message CachedStakeAmount {; +message CachedStakeAmount { // Last calculated total staked amount by the delegator (in coin amount). - bytes staked_amount = 1 [ + bytes staked_amount = 1 [ (gogoproto.customtype) = "github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt", (gogoproto.nullable) = false ]; // Block time at which the calculation is cached (in Unix Epoch seconds) - // Rounded down to nearest second. + // Rounded down to nearest second. uint64 cached_at = 2; } \ No newline at end of file diff --git a/protocol/testutil/keeper/clob.go b/protocol/testutil/keeper/clob.go index 29aca7fdd2..01374590ba 100644 --- a/protocol/testutil/keeper/clob.go +++ b/protocol/testutil/keeper/clob.go @@ -119,13 +119,6 @@ func NewClobKeepersTestContextWithUninitializedMemStore( cdc, registry) - bankKeeper, _ := createBankKeeper( - stateStore, - db, - cdc, - accountsKeeper, - ) - stakingKeeper, _ := createStakingKeeper( stateStore, db, diff --git a/protocol/testutil/keeper/staking.go b/protocol/testutil/keeper/staking.go index 08e4fd37c9..689f91f190 100644 --- a/protocol/testutil/keeper/staking.go +++ b/protocol/testutil/keeper/staking.go @@ -5,7 +5,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/runtime" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/dydxprotocol/v4-chain/protocol/lib" @@ -20,7 +19,7 @@ func createStakingKeeper( db *dbm.MemDB, cdc *codec.ProtoCodec, accountKeeper *authkeeper.AccountKeeper, - bankKeeper *bankkeeper.BaseKeeper, + bankKeeper types.BankKeeper, ) ( *keeper.Keeper, storetypes.StoreKey, diff --git a/protocol/x/affiliates/keeper/keeper.go b/protocol/x/affiliates/keeper/keeper.go index 9936fddd0c..2dfaef6401 100644 --- a/protocol/x/affiliates/keeper/keeper.go +++ b/protocol/x/affiliates/keeper/keeper.go @@ -81,7 +81,10 @@ func (k Keeper) AddReferredVolume( if !affiliateReferredVolumePrefixStore.Has([]byte(affiliateAddr)) { referredVolume = dtypes.NewInt(0) } else { - referredVolume.Unmarshal(affiliateReferredVolumePrefixStore.Get([]byte(affiliateAddr))) + err := referredVolume.Unmarshal(affiliateReferredVolumePrefixStore.Get([]byte(affiliateAddr))) + if err != nil { + return err + } } referredVolumeBigInt := referredVolume.BigInt() referredVolumeBigInt.Add(referredVolumeBigInt, referredVolumeFromBlock.BigInt()) @@ -101,7 +104,10 @@ func (k Keeper) GetReferredVolume(ctx sdk.Context, affiliateAddr string) (dtypes return dtypes.NewInt(0), false } var referredVolume dtypes.SerializableInt - referredVolume.Unmarshal(affiliateReferredVolumePrefixStore.Get([]byte(affiliateAddr))) + err := referredVolume.Unmarshal(affiliateReferredVolumePrefixStore.Get([]byte(affiliateAddr))) + if err != nil { + return dtypes.NewInt(0), false + } return referredVolume, true } diff --git a/protocol/x/affiliates/keeper/keeper_test.go b/protocol/x/affiliates/keeper/keeper_test.go index 2573c33867..9dbe63a544 100644 --- a/protocol/x/affiliates/keeper/keeper_test.go +++ b/protocol/x/affiliates/keeper/keeper_test.go @@ -104,9 +104,10 @@ func TestGetTakerFeeShareViaReferredVolume(t *testing.T) { affiliate := constants.AliceAccAddress.String() referee := constants.BobAccAddress.String() stakingKeeper := tApp.App.StakingKeeper - stakingKeeper.SetDelegation(ctx, + err = stakingKeeper.SetDelegation(ctx, stakingtypes.NewDelegation(constants.AliceAccAddress.String(), constants.AliceValAddress.String(), math.LegacyNewDecFromBigInt(big.NewInt(100)))) + require.NoError(t, err) err = k.RegisterAffiliate(ctx, referee, affiliate) require.NoError(t, err) @@ -152,9 +153,10 @@ func TestGetTakerFeeShareViaStakedAmount(t *testing.T) { affiliate := constants.AliceAccAddress.String() referee := constants.BobAccAddress.String() stakingKeeper := tApp.App.StakingKeeper - stakingKeeper.SetDelegation(ctx, + err = stakingKeeper.SetDelegation(ctx, stakingtypes.NewDelegation(constants.AliceAccAddress.String(), constants.AliceValAddress.String(), math.LegacyNewDecFromBigInt(big.NewInt(1000)))) + require.NoError(t, err) err = k.RegisterAffiliate(ctx, referee, affiliate) require.NoError(t, err) @@ -166,10 +168,10 @@ func TestGetTakerFeeShareViaStakedAmount(t *testing.T) { require.Equal(t, uint32(2000), feeSharePpm) // Add more staked amount to upgrade tier - stakingKeeper.SetDelegation(ctx, + err = stakingKeeper.SetDelegation(ctx, stakingtypes.NewDelegation(constants.AliceAccAddress.String(), constants.AliceValAddress.String(), math.LegacyNewDecFromBigInt(big.NewInt(2000)))) - + require.NoError(t, err) // Get updated taker fee share for referee affiliateAddr, feeSharePpm, exists, err = k.GetTakerFeeShare(ctx, referee) require.NoError(t, err) diff --git a/protocol/x/stats/keeper/keeper_test.go b/protocol/x/stats/keeper/keeper_test.go index 5f3b351f3d..fd8f07c9b0 100644 --- a/protocol/x/stats/keeper/keeper_test.go +++ b/protocol/x/stats/keeper/keeper_test.go @@ -300,8 +300,11 @@ func TestGetStakedAmount(t *testing.T) { statsKeeper := tApp.App.StatsKeeper stakingKeeper := tApp.App.StakingKeeper expectedCoinsStaked := big.NewInt(100) - delegation := stakingtypes.NewDelegation(constants.AliceAccAddress.String(), constants.AliceValAddress.String(), math.LegacyNewDecFromBigInt(expectedCoinsStaked)) - stakingKeeper.SetDelegation(ctx, delegation) + delegation := stakingtypes.NewDelegation( + constants.AliceAccAddress.String(), constants.AliceValAddress.String(), + math.LegacyNewDecFromBigInt(expectedCoinsStaked)) + err := stakingKeeper.SetDelegation(ctx, delegation) + require.NoError(t, err) receivedCoins := statsKeeper.GetStakedAmount(ctx, constants.AliceAccAddress.String()) require.Equal(t, expectedCoinsStaked, &receivedCoins) diff --git a/protocol/x/stats/types/expected_keepers.go b/protocol/x/stats/types/expected_keepers.go index 86478f5eb1..a33ee70d27 100644 --- a/protocol/x/stats/types/expected_keepers.go +++ b/protocol/x/stats/types/expected_keepers.go @@ -14,5 +14,6 @@ type EpochsKeeper interface { } type StakingKeeper interface { - GetDelegatorDelegations(ctx context.Context, delegator sdk.AccAddress, maxRetrieve uint16) ([]stakingtypes.Delegation, error) + GetDelegatorDelegations(ctx context.Context, + delegator sdk.AccAddress, maxRetrieve uint16) ([]stakingtypes.Delegation, error) }