Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OTE-693][OTE-691] Add keeper methods for affiliates #2141

Merged
merged 13 commits into from
Sep 5, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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>): 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;
}

};
13 changes: 13 additions & 0 deletions proto/dydxprotocol/stats/stats.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ message UserStats {
// Maker USDC in quantums
uint64 maker_notional = 2;
}

// CachedStakeAmount stores the last calculated total staked amount for address
message CachedStakeAmount {
// Last calculated total staked amount by the delegator (in coin amount).
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.
uint64 cached_at = 2;
}
teddyding marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ func New(
lib.GovModuleAddress.String(),
delaymsgmoduletypes.ModuleAddress.String(),
},
app.StakingKeeper,
)
statsModule := statsmodule.NewAppModule(appCodec, app.StatsKeeper)

Expand Down Expand Up @@ -1202,6 +1203,7 @@ func New(
[]string{
lib.GovModuleAddress.String(),
},
app.StatsKeeper,
)
affiliatesModule := affiliatesmodule.NewAppModule(appCodec, app.AffiliatesKeeper)

Expand Down
4 changes: 4 additions & 0 deletions protocol/lib/metrics/metric_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
ClobRateLimitPlaceOrderCount = "clob_rate_limit_place_order_count"
ClobRateLimitCancelOrderCount = "clob_rate_limit_cancel_order_count"
ClobRateLimitBatchCancelCount = "clob_rate_limit_batch_cancel_count"
StatsGetStakedAmountCacheHit = "stats_get_staked_amount_cache_hit"
StatsGetStakedAmountCacheMiss = "stats_get_staked_amount_cache_miss"

// Gauges
InsuranceFundBalance = "insurance_fund_balance"
Expand All @@ -36,6 +38,8 @@ const (
ClobSubaccountsRequiringDeleveragingCount = "clob_subaccounts_requiring_deleveraging_count"
SendingProcessDepositToSubaccount = "sending_process_deposit_to_subaccount"
RateLimitInsufficientWithdrawalAmount = "rate_limit_insufficient_withdrawal_amount"
StatsGetStakedAmountLatencyCacheHit = "stats_get_staked_amount_latency_cache_hit"
StatsGetStakedAmountLatencyCacheMiss = "stats_get_staked_amount_latency_cache_miss"

// Samples
ClobDeleverageSubaccountTotalQuoteQuantumsDistribution = "clob_deleverage_subaccount_total_quote_quantums_distribution"
Expand Down
3 changes: 3 additions & 0 deletions protocol/testutil/keeper/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
bridgetypes "github.com/dydxprotocol/v4-chain/protocol/x/bridge/types"
perpetualstypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
Expand Down Expand Up @@ -41,6 +42,8 @@ func createAccountKeeper(
types.FeeCollectorName: nil,
satypes.ModuleName: nil,
perpetualstypes.InsuranceFundName: nil,
stakingtypes.BondedPoolName: {types.Burner, types.Staking},
teddyding marked this conversation as resolved.
Show resolved Hide resolved
stakingtypes.NotBondedPoolName: {types.Burner, types.Staking},
}

k := keeper.NewAccountKeeper(
Expand Down
14 changes: 14 additions & 0 deletions protocol/testutil/keeper/clob.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,25 @@ func NewClobKeepersTestContextWithUninitializedMemStore(
true,
)
ks.BlockTimeKeeper, _ = createBlockTimeKeeper(stateStore, db, cdc)
accountsKeeper, _ := createAccountKeeper(
stateStore,
db,
cdc,
registry)

stakingKeeper, _ := createStakingKeeper(
stateStore,
db,
cdc,
accountsKeeper,
bankKeeper,
)
ks.StatsKeeper, _ = createStatsKeeper(
stateStore,
epochsKeeper,
db,
cdc,
stakingKeeper,
)
ks.VaultKeeper, _ = createVaultKeeper(
stateStore,
Expand Down
22 changes: 22 additions & 0 deletions protocol/testutil/keeper/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,34 @@ func ListingKeepers(
transientStoreKey,
true,
)
accountsKeeper, _ := createAccountKeeper(
stateStore,
db,
cdc,
registry)

bankKeeper, _ := createBankKeeper(
stateStore,
db,
cdc,
accountsKeeper,
)

stakingKeeper, _ := createStakingKeeper(
stateStore,
db,
cdc,
accountsKeeper,
bankKeeper,
)

blockTimeKeeper, _ := createBlockTimeKeeper(stateStore, db, cdc)
statsKeeper, _ := createStatsKeeper(
stateStore,
epochsKeeper,
db,
cdc,
stakingKeeper,
)
vaultKeeper, _ := createVaultKeeper(
stateStore,
Expand Down
22 changes: 22 additions & 0 deletions protocol/testutil/keeper/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,33 @@ func RewardsKeepers(
transientStoreKey,
true,
)
accountsKeeper, _ := createAccountKeeper(
stateStore,
db,
cdc,
registry)

bankKeeper, _ := createBankKeeper(
stateStore,
db,
cdc,
accountsKeeper,
)

stakingKeeper, _ := createStakingKeeper(
stateStore,
db,
cdc,
accountsKeeper,
bankKeeper,
)

statsKeeper, _ := createStatsKeeper(
stateStore,
epochsKeeper,
db,
cdc,
stakingKeeper,
)
vaultKeeper, _ := createVaultKeeper(
stateStore,
Expand Down
43 changes: 43 additions & 0 deletions protocol/testutil/keeper/staking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package keeper

import (
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"

storetypes "cosmossdk.io/store/types"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
sdk "github.com/cosmos/cosmos-sdk/types"
keeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
)

func createStakingKeeper(
stateStore storetypes.CommitMultiStore,
db *dbm.MemDB,
cdc *codec.ProtoCodec,
accountKeeper *authkeeper.AccountKeeper,
bankKeeper types.BankKeeper,
) (
*keeper.Keeper,
storetypes.StoreKey,
) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)

ss := runtime.NewKVStoreService(storeKey)

k := keeper.NewKeeper(
cdc,
ss,
accountKeeper,
bankKeeper,
lib.GovModuleAddress.String(),
addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()),
addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()),
)

return k, storeKey
}
3 changes: 3 additions & 0 deletions protocol/testutil/keeper/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
epochskeeper "github.com/dydxprotocol/v4-chain/protocol/x/epochs/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/stats/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/stats/types"
Expand All @@ -18,6 +19,7 @@ func createStatsKeeper(
epochsKeeper *epochskeeper.Keeper,
db *dbm.MemDB,
cdc *codec.ProtoCodec,
stakingKeeper *stakingkeeper.Keeper,
) (*keeper.Keeper, storetypes.StoreKey) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
transientStoreKey := storetypes.NewTransientStoreKey(types.TransientStoreKey)
Expand All @@ -38,6 +40,7 @@ func createStatsKeeper(
storeKey,
transientStoreKey,
authorities,
stakingKeeper,
)

return k, storeKey
Expand Down
Loading
Loading