Skip to content

Commit

Permalink
[OTE-693][OTE-691] Add keeper methods for affiliates (#2141)
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 authored Sep 5, 2024
1 parent 0788d29 commit 3faadf9
Show file tree
Hide file tree
Showing 25 changed files with 1,231 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ export interface AffiliateTiersSDKType {
/** Tier defines an affiliate tier. */

export interface AffiliateTiers_Tier {
/** Level of the tier */
level: number;
/** Required all-time referred volume in quote quantums. */

reqReferredVolume: Long;
reqReferredVolumeQuoteQuantums: Long;
/** Required currently staked native tokens (in whole coins). */

reqStakedWholeCoins: number;
Expand All @@ -30,11 +27,8 @@ export interface AffiliateTiers_Tier {
/** Tier defines an affiliate tier. */

export interface AffiliateTiers_TierSDKType {
/** Level of the tier */
level: number;
/** Required all-time referred volume in quote quantums. */

req_referred_volume: Long;
req_referred_volume_quote_quantums: Long;
/** Required currently staked native tokens (in whole coins). */

req_staked_whole_coins: number;
Expand Down Expand Up @@ -90,29 +84,24 @@ export const AffiliateTiers = {

function createBaseAffiliateTiers_Tier(): AffiliateTiers_Tier {
return {
level: 0,
reqReferredVolume: Long.UZERO,
reqReferredVolumeQuoteQuantums: Long.UZERO,
reqStakedWholeCoins: 0,
takerFeeSharePpm: 0
};
}

export const AffiliateTiers_Tier = {
encode(message: AffiliateTiers_Tier, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.level !== 0) {
writer.uint32(8).uint32(message.level);
}

if (!message.reqReferredVolume.isZero()) {
writer.uint32(16).uint64(message.reqReferredVolume);
if (!message.reqReferredVolumeQuoteQuantums.isZero()) {
writer.uint32(8).uint64(message.reqReferredVolumeQuoteQuantums);
}

if (message.reqStakedWholeCoins !== 0) {
writer.uint32(24).uint32(message.reqStakedWholeCoins);
writer.uint32(16).uint32(message.reqStakedWholeCoins);
}

if (message.takerFeeSharePpm !== 0) {
writer.uint32(32).uint32(message.takerFeeSharePpm);
writer.uint32(24).uint32(message.takerFeeSharePpm);
}

return writer;
Expand All @@ -128,18 +117,14 @@ export const AffiliateTiers_Tier = {

switch (tag >>> 3) {
case 1:
message.level = reader.uint32();
message.reqReferredVolumeQuoteQuantums = (reader.uint64() as Long);
break;

case 2:
message.reqReferredVolume = (reader.uint64() as Long);
break;

case 3:
message.reqStakedWholeCoins = reader.uint32();
break;

case 4:
case 3:
message.takerFeeSharePpm = reader.uint32();
break;

Expand All @@ -154,8 +139,7 @@ export const AffiliateTiers_Tier = {

fromPartial(object: DeepPartial<AffiliateTiers_Tier>): AffiliateTiers_Tier {
const message = createBaseAffiliateTiers_Tier();
message.level = object.level ?? 0;
message.reqReferredVolume = object.reqReferredVolume !== undefined && object.reqReferredVolume !== null ? Long.fromValue(object.reqReferredVolume) : Long.UZERO;
message.reqReferredVolumeQuoteQuantums = object.reqReferredVolumeQuoteQuantums !== undefined && object.reqReferredVolumeQuoteQuantums !== null ? Long.fromValue(object.reqReferredVolumeQuoteQuantums) : Long.UZERO;
message.reqStakedWholeCoins = object.reqStakedWholeCoins ?? 0;
message.takerFeeSharePpm = object.takerFeeSharePpm ?? 0;
return message;
Expand Down
79 changes: 79 additions & 0 deletions indexer/packages/v4-protos/src/codegen/dydxprotocol/stats/stats.ts
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.ZERO
};
}

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).int64(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.int64() 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.ZERO;
return message;
}

};
8 changes: 3 additions & 5 deletions proto/dydxprotocol/affiliates/affiliates.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ option go_package = "github.com/dydxprotocol/v4-chain/protocol/x/affiliates/type
message AffiliateTiers {
// Tier defines an affiliate tier.
message Tier {
// Level of the tier
uint32 level = 1;
// Required all-time referred volume in quote quantums.
uint64 req_referred_volume = 2;
uint64 req_referred_volume_quote_quantums = 1;
// Required currently staked native tokens (in whole coins).
uint32 req_staked_whole_coins = 3;
uint32 req_staked_whole_coins = 2;
// Taker fee share in parts-per-million.
uint32 taker_fee_share_ppm = 4;
uint32 taker_fee_share_ppm = 3;
}
// All affiliate tiers
repeated Tier tiers = 1 [ (gogoproto.nullable) = false ];
Expand Down
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.
int64 cached_at = 2;
}
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 @@ -1203,6 +1204,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},
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
Loading

0 comments on commit 3faadf9

Please sign in to comment.