Skip to content

Commit

Permalink
fix!: use a slice instead of map for payout coin
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency authored Apr 23, 2024
1 parent 23d4e55 commit 6a7913e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
15 changes: 10 additions & 5 deletions interchaintest/mainfest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func TestManifestModule(t *testing.T) {
}

users := interchaintest.GetAndFundTestUsers(t, ctx, "default", DefaultGenesisAmt, appChain, appChain, appChain)
user1, user2 := users[0], users[1]
uaddr, addr2 := user1.FormattedAddress(), user2.FormattedAddress()
user1, user2, user3 := users[0], users[1], users[2]
uaddr, addr2, addr3 := user1.FormattedAddress(), user2.FormattedAddress(), user3.FormattedAddress()

node := appChain.GetNode()

Expand Down Expand Up @@ -113,7 +113,7 @@ func TestManifestModule(t *testing.T) {
t.Run("success; disable auto inflation. Set new stakeholders", func(t *testing.T) {
txRes, _ := helpers.ManifestUpdateParams(
t, ctx, appChain, poaAdmin,
fmt.Sprintf("%s:1_000_000,%s:99_000_000", uaddr, addr2),
fmt.Sprintf("%s:1_000_000,%s:98_000_000,%s:1_000_000", uaddr, addr2, addr3),
"false",
sdk.NewCoin(Denom, sdkmath.NewIntFromUint64(p.Inflation.YearlyAmount)).String(), // it's off, this just matches genesis
)
Expand All @@ -122,13 +122,14 @@ func TestManifestModule(t *testing.T) {
p, err = helpers.ManifestQueryParams(ctx, node)
require.NoError(t, err)
require.False(t, p.Inflation.AutomaticEnabled)
require.Len(t, p.StakeHolders, 2)
require.Len(t, p.StakeHolders, 3)
})

t.Run("success; Perform a manual distribution payout from the PoA admin", func(t *testing.T) {

beforeBal1, _ := appChain.GetBalance(ctx, uaddr, Denom)
beforeBal2, _ := appChain.GetBalance(ctx, addr2, Denom)
beforeBal3, _ := appChain.GetBalance(ctx, addr3, Denom)

c := sdk.NewCoin(Denom, sdkmath.NewInt(100_000000))
txRes, _ := helpers.ManifestStakeholderPayout(t, ctx, appChain, poaAdmin, c.String())
Expand All @@ -140,7 +141,11 @@ func TestManifestModule(t *testing.T) {

user2bal, err := appChain.GetBalance(ctx, addr2, Denom)
require.NoError(t, err)
require.EqualValues(t, user2bal.Uint64(), beforeBal2.Uint64()+99_000_000)
require.EqualValues(t, user2bal.Uint64(), beforeBal2.Uint64()+98_000_000)

user3bal, err := appChain.GetBalance(ctx, addr3, Denom)
require.NoError(t, err)
require.EqualValues(t, user3bal.Uint64(), beforeBal3.Uint64()+1_000_000)

})

Expand Down
2 changes: 1 addition & 1 deletion interchaintest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var (

CosmosGovModuleAcc = "manifest10d07y265gmmuvt4z0w9aw880jnsr700jmq3jzm"

vals = 1
vals = 2
fullNodes = 0

DefaultGenesis = []cosmos.GenesisKV{
Expand Down
23 changes: 15 additions & 8 deletions x/manifest/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ func (k Keeper) IsManualMintingEnabled(ctx context.Context) bool {
return !params.Inflation.AutomaticEnabled
}

type StakeHolderPayout struct {
Address string
Coin sdk.Coin
}

// Returns the amount of coins to be distributed to the holders
func (k Keeper) CalculateShareHolderTokenPayout(ctx context.Context, c sdk.Coin) map[string]sdk.Coin {
func (k Keeper) CalculateShareHolderTokenPayout(ctx context.Context, c sdk.Coin) []StakeHolderPayout {
sh := k.GetShareHolders(ctx)

pairs := make(map[string]sdk.Coin, len(sh))
pairs := make([]StakeHolderPayout, 0, len(sh))

// iter each stakeholder, get their percent of the total 100%, and then split up their amount of coin cost
for _, s := range sh {
s := s
pct := sdkmath.NewInt(int64(s.Percentage)).ToLegacyDec().QuoInt64(types.MaxPercentShare)
Expand All @@ -123,7 +127,11 @@ func (k Keeper) CalculateShareHolderTokenPayout(ctx context.Context, c sdk.Coin)
continue
}

pairs[s.Address] = sdk.NewCoin(c.Denom, coinAmt)
pairs = append(pairs, StakeHolderPayout{
Address: s.Address,
Coin: sdk.NewCoin(c.Denom, coinAmt),
})

}

return pairs
Expand All @@ -140,14 +148,13 @@ func (k Keeper) PayoutStakeholders(ctx context.Context, c sdk.Coin) error {
return err
}

for addr, coin := range pairs {
accAddr, err := sdk.AccAddressFromBech32(addr)
for _, p := range pairs {
accAddr, err := sdk.AccAddressFromBech32(p.Address)
if err != nil {
return err
}

// send from the mintKeeper -> the stakeholder
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, accAddr, sdk.NewCoins(coin)); err != nil {
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, accAddr, sdk.NewCoins(p.Coin)); err != nil {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions x/manifest/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ func TestCalculatePayoutLogic(t *testing.T) {
// validate the full payout of 100 tokens got split up between all fractional shares as expected
res := k.CalculateShareHolderTokenPayout(f.Ctx, sdk.NewCoin("stake", sdkmath.NewInt(100_000_000)))
for _, s := range sh {
for w, coin := range res {
if s.Address == w {
require.EqualValues(t, s.Percentage, coin.Amount.Int64())
for w, shp := range res {
if s.Address == shp.Address {
require.EqualValues(t, s.Percentage, shp.Coin.Amount.Int64(), "stakeholder %d", w)
}
}
}
Expand Down

0 comments on commit 6a7913e

Please sign in to comment.