Skip to content

Commit

Permalink
remove old ics code that is unused
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Bowman authored and joe-bowman committed Jan 13, 2025
1 parent 9bcc8c9 commit 8051494
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 118 deletions.
37 changes: 5 additions & 32 deletions x/participationrewards/keeper/rewards_holdings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/quicksilver-zone/quicksilver/utils"
"github.com/quicksilver-zone/quicksilver/utils/addressutils"
airdroptypes "github.com/quicksilver-zone/quicksilver/x/airdrop/types"
cmtypes "github.com/quicksilver-zone/quicksilver/x/claimsmanager/types"
icstypes "github.com/quicksilver-zone/quicksilver/x/interchainstaking/types"
Expand All @@ -17,7 +16,7 @@ func (k Keeper) AllocateHoldingsRewards(ctx sdk.Context) error {
// obtain and iterate all claim records for each zone
k.icsKeeper.IterateZones(ctx, func(index int64, zone *icstypes.Zone) (stop bool) {
k.Logger(ctx).Info("zones", "zone", zone.ChainId)
userAllocations, remaining, _ := k.CalcUserHoldingsAllocations(ctx, zone)
userAllocations, remaining := k.CalcUserHoldingsAllocations(ctx, zone)

if err := k.DistributeToUsersFromModule(ctx, userAllocations); err != nil {
k.Logger(ctx).Error("failed to distribute to users", "ua", userAllocations, "err", err)
Expand All @@ -41,19 +40,16 @@ func (k Keeper) AllocateHoldingsRewards(ctx sdk.Context) error {
}

// CalcUserHoldingsAllocations calculates allocations per user for a given zone, based upon claims submitted and zone.
func (k Keeper) CalcUserHoldingsAllocations(ctx sdk.Context, zone *icstypes.Zone) ([]types.UserAllocation, math.Int, []types.UserAllocation) {
func (k Keeper) CalcUserHoldingsAllocations(ctx sdk.Context, zone *icstypes.Zone) ([]types.UserAllocation, math.Int) {
k.Logger(ctx).Info("CalcUserHoldingsAllocations", "zone", zone.ChainId, "allocations", zone.HoldingsAllocation)

userAllocations := make([]types.UserAllocation, 0)
icsRewardsAllocations := make([]types.UserAllocation, 0)
icsRewardsBalance := sdk.NewCoins()
icsRewardsPerAsset := make(map[string]sdk.Dec, 0)

supply := k.bankKeeper.GetSupply(ctx, zone.LocalDenom)

if zone.HoldingsAllocation == 0 || !supply.Amount.IsPositive() {
k.Logger(ctx).Info("holdings allocation is zero, nothing to allocate")
return userAllocations, math.NewIntFromUint64(zone.HoldingsAllocation), icsRewardsAllocations
return userAllocations, math.NewIntFromUint64(zone.HoldingsAllocation)
}

// calculate user totals and zone total (held assets)
Expand Down Expand Up @@ -83,26 +79,12 @@ func (k Keeper) CalcUserHoldingsAllocations(ctx sdk.Context, zone *icstypes.Zone

if !zoneAmount.IsPositive() {
k.Logger(ctx).Info("zero claims for zone", "zone", zone.ChainId)
return userAllocations, math.NewIntFromUint64(zone.HoldingsAllocation), icsRewardsAllocations
return userAllocations, math.NewIntFromUint64(zone.HoldingsAllocation)
}

zoneAllocation := math.NewIntFromUint64(zone.HoldingsAllocation)
tokensPerAsset := sdk.NewDecFromInt(zoneAllocation).Quo(sdk.NewDecFromInt(supply.Amount))

if zone.WithdrawalAddress != nil {
// determine ics rewards to be distributed per token.
icsRewardsAddr, err := addressutils.AddressFromBech32(zone.WithdrawalAddress.Address, zone.AccountPrefix)
if err != nil {
panic("unable to unmarshal withdrawal address")
}
icsRewardsBalance = k.bankKeeper.GetAllBalances(ctx, icsRewardsAddr)
icsRewardsPerAsset = make(map[string]sdk.Dec, len(icsRewardsBalance))
for _, rewardsAsset := range icsRewardsBalance {
icsRewardsPerAsset[rewardsAsset.Denom] = sdk.NewDecFromInt(rewardsAsset.Amount).Quo(sdk.NewDecFromInt(supply.Amount))
}

k.Logger(ctx).Info("ics rewards per asset", "zone", zone.ChainId, "icsrpa", icsRewardsPerAsset)
}
k.Logger(ctx).Info("tokens per asset", "zone", zone.ChainId, "tpa", tokensPerAsset)

for _, address := range utils.Keys(userAmountsMap) {
Expand All @@ -118,16 +100,7 @@ func (k Keeper) CalcUserHoldingsAllocations(ctx sdk.Context, zone *icstypes.Zone
panic("user allocation overflow")
}

// allocate ics rewards
for _, rewardsAsset := range icsRewardsBalance {
icsRewardsAllocation := types.UserAllocation{
Address: address,
Amount: sdk.NewCoin(rewardsAsset.Denom, sdk.NewDecFromInt(amount).Mul(icsRewardsPerAsset[rewardsAsset.Denom]).TruncateInt()),
}
icsRewardsAllocations = append(icsRewardsAllocations, icsRewardsAllocation)
}

}

return userAllocations, zoneAllocation, icsRewardsAllocations
return userAllocations, zoneAllocation
}
92 changes: 6 additions & 86 deletions x/participationrewards/keeper/rewards_holdings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ func (suite *KeeperTestSuite) TestCalcUserHoldingsAllocations() {
ctx := suite.chainA.GetContext()
bondDenom := appA.StakingKeeper.BondDenom(ctx)
tests := []struct {
name string
malleate func(ctx sdk.Context, appA *app.Quicksilver)
want []types.UserAllocation
icsWant []types.UserAllocation
remainder math.Int
icsRemainder sdk.Coins
wantErr string
name string
malleate func(ctx sdk.Context, appA *app.Quicksilver)
want []types.UserAllocation
remainder math.Int
wantErr string
}{
{
"zero claims; no allocation",
Expand All @@ -34,9 +32,7 @@ func (suite *KeeperTestSuite) TestCalcUserHoldingsAllocations() {
appA.InterchainstakingKeeper.SetZone(ctx, &zone)
},
[]types.UserAllocation{},
[]types.UserAllocation{},
sdk.ZeroInt(),
sdk.NewCoins(),
"",
},
{
Expand All @@ -49,9 +45,7 @@ func (suite *KeeperTestSuite) TestCalcUserHoldingsAllocations() {
appA.ClaimsManagerKeeper.SetLastEpochClaim(ctx, &cmtypes.Claim{UserAddress: user2.String(), ChainId: "otherchain-1", Module: cmtypes.ClaimTypeLiquidToken, SourceChainId: suite.chainA.ChainID, Amount: math.NewInt(1000)})
},
[]types.UserAllocation{},
[]types.UserAllocation{},
sdk.NewInt(64000),
sdk.NewCoins(),
"",
},
{
Expand All @@ -75,9 +69,7 @@ func (suite *KeeperTestSuite) TestCalcUserHoldingsAllocations() {
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(2500)),
},
},
[]types.UserAllocation{},
sdk.ZeroInt(),
sdk.NewCoins(),
"",
},
{
Expand All @@ -101,9 +93,7 @@ func (suite *KeeperTestSuite) TestCalcUserHoldingsAllocations() {
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(2000)), // 1000 / 2500 (0.4) * 5000 = 2000
},
},
[]types.UserAllocation{},
sdk.NewInt(2000),
sdk.NewCoins(),
"",
},
{
Expand All @@ -127,9 +117,7 @@ func (suite *KeeperTestSuite) TestCalcUserHoldingsAllocations() {
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(3333)), // 1000/1500 (0.66666) * 5000 = 3333
},
},
[]types.UserAllocation{},
sdk.OneInt(),
sdk.NewCoins(),
"",
},
{
Expand All @@ -155,18 +143,7 @@ func (suite *KeeperTestSuite) TestCalcUserHoldingsAllocations() {
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(3333)), // 1000/1500 (0.66666) * 5000 = 3333
},
},
[]types.UserAllocation{
{
Address: user1.String(),
Amount: sdk.NewCoin("testcoin", sdk.NewInt(300)), // 500/1500 (0.33333) * 900 == 300
},
{
Address: user2.String(),
Amount: sdk.NewCoin("testcoin", sdk.NewInt(600)), // 1000/1500 (0.66666) * 900 = 600
},
},
sdk.OneInt(),
sdk.NewCoins(),
"",
},
{
Expand Down Expand Up @@ -197,34 +174,7 @@ func (suite *KeeperTestSuite) TestCalcUserHoldingsAllocations() {
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(3333)), // 1000/1500 (0.66666) * 5000 = 3333
},
},
[]types.UserAllocation{
{
Address: user1.String(),
Amount: sdk.NewCoin("testcoin", sdk.NewInt(300)), // 500/1500 (0.33333) * 900 == 300
},
{
Address: user2.String(),
Amount: sdk.NewCoin("testcoin", sdk.NewInt(600)), // 1000/1500 (0.66666) * 900 = 600
},
{
Address: user1.String(),
Amount: sdk.NewCoin("testcoin2", sdk.NewInt(6000)), // 500/1500 (0.33333) * 18k == 6k
},
{
Address: user2.String(),
Amount: sdk.NewCoin("testcoin2", sdk.NewInt(12001)), // 1000/1500 (0.66666) * 18k = 12k
},
{
Address: user1.String(),
Amount: sdk.NewCoin("testcoin3", sdk.NewInt(50)), // 500/1500 (0.33333) * 150 == 50
},
{
Address: user2.String(),
Amount: sdk.NewCoin("testcoin3", sdk.NewInt(100)), // 1000/1500 (0.66666) * 150 = 100
},
},
sdk.OneInt(),
sdk.NewCoins(sdk.NewCoin("testcoin2", sdk.NewIntFromUint64(1))),
"",
},

Expand Down Expand Up @@ -254,29 +204,7 @@ func (suite *KeeperTestSuite) TestCalcUserHoldingsAllocations() {
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(2000)), // 1000 / 2500 (0.4) * 5000 = 2000
},
},
[]types.UserAllocation{
{
Address: user1.String(),
Amount: sdk.NewCoin("testcoin", sdk.NewInt(180)), // 500/1500 (0.33333) * 900 == 300
},
{
Address: user2.String(),
Amount: sdk.NewCoin("testcoin", sdk.NewInt(360)), // 1000/1500 (0.66666) * 900 = 600
},
{
Address: user1.String(),
Amount: sdk.NewCoin("testcoin2", sdk.NewInt(3600)), // 500/1500 (0.33333) * 18k == 6k
},
{
Address: user2.String(),
Amount: sdk.NewCoin("testcoin2", sdk.NewInt(7200)), // 1000/1500 (0.66666) * 18k = 12k
},
},
sdk.NewInt(2000),
sdk.NewCoins(
sdk.NewCoin("testcoin", sdk.NewInt(360)),
sdk.NewCoin("testcoin2", sdk.NewInt(7202)),
),
"",
},
}
Expand All @@ -299,17 +227,9 @@ func (suite *KeeperTestSuite) TestCalcUserHoldingsAllocations() {
suite.NoError(appA.BankKeeper.MintCoins(ctx, "mint", sdk.NewCoins(sdk.NewCoin(appA.StakingKeeper.BondDenom(ctx), sdk.NewIntFromUint64(zone.HoldingsAllocation)))))
suite.NoError(appA.BankKeeper.SendCoinsFromModuleToModule(ctx, "mint", types.ModuleName, sdk.NewCoins(sdk.NewCoin(appA.StakingKeeper.BondDenom(ctx), sdk.NewIntFromUint64(zone.HoldingsAllocation)))))

allocations, remainder, icsRewardsAllocations := appA.ParticipationRewardsKeeper.CalcUserHoldingsAllocations(ctx, &zone)
allocations, remainder := appA.ParticipationRewardsKeeper.CalcUserHoldingsAllocations(ctx, &zone)
suite.ElementsMatch(tt.want, allocations)
suite.ElementsMatch(tt.icsWant, icsRewardsAllocations)
suite.True(tt.remainder.Equal(remainder))

// distribute assets to users; check remainder (to be distributed next time!)
err := appA.ParticipationRewardsKeeper.DistributeToUsersFromAddress(ctx, icsRewardsAllocations, zone.WithdrawalAddress.Address)
suite.NoError(err)
icsAddress, _ := addressutils.AddressFromBech32(zone.WithdrawalAddress.Address, "")
icsBalance := appA.BankKeeper.GetAllBalances(ctx, icsAddress)
suite.ElementsMatch(tt.icsRemainder, icsBalance)
})
}
}
Expand Down

0 comments on commit 8051494

Please sign in to comment.