mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-03-30 15:08:28 +00:00
removed StakeDenom and it's related code (#299)
* removed StakeDenom and it's related code * fixed PoP, reissuance & distribution tests Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
2a9d3d4b47
commit
f6509cfa64
@ -17,7 +17,6 @@ asset-registry-scheme = "{{ .PlmntConfig.AssetRegistryScheme}}"
|
||||
asset-registry-domain = "{{ .PlmntConfig.AssetRegistryDomain }}"
|
||||
asset-registry-path = "{{ .PlmntConfig.AssetRegistryPath }}"
|
||||
token-denom = "{{ .PlmntConfig.TokenDenom }}"
|
||||
stake-denom = "{{ .PlmntConfig.StakeDenom }}"
|
||||
fee-denom = "{{ .PlmntConfig.FeeDenom }}"
|
||||
staged-denom = "{{ .PlmntConfig.StagedDenom }}"
|
||||
claim-denom = "{{ .PlmntConfig.ClaimDenom }}"
|
||||
@ -48,7 +47,6 @@ type Config struct {
|
||||
AssetRegistryDomain string `json:"asset-registry-domain" mapstructure:"asset-registry-domain"`
|
||||
AssetRegistryPath string `json:"asset-registry-path" mapstructure:"asset-registry-path"`
|
||||
TokenDenom string `json:"token-denom" mapstructure:"token-denom"`
|
||||
StakeDenom string `json:"stake-denom" mapstructure:"stake-denom"`
|
||||
FeeDenom string `json:"fee-denom" mapstructure:"fee-denom"`
|
||||
StagedDenom string `json:"staged-denom" mapstructure:"staged-denom"`
|
||||
ClaimDenom string `json:"claim-denom" mapstructure:"claim-denom"`
|
||||
@ -87,7 +85,6 @@ func DefaultConfig() *Config {
|
||||
AssetRegistryDomain: "testnet-assets.rddl.io",
|
||||
AssetRegistryPath: "register_asset",
|
||||
TokenDenom: "plmnt",
|
||||
StakeDenom: "plmntstake",
|
||||
FeeDenom: "plmnt",
|
||||
StagedDenom: "stagedcrddl",
|
||||
ClaimDenom: "crddl",
|
||||
|
@ -1,9 +1,12 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
bank "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
|
||||
"github.com/planetmint/planetmint-go/config"
|
||||
"github.com/planetmint/planetmint-go/testutil"
|
||||
clitestutil "github.com/planetmint/planetmint-go/testutil/cli"
|
||||
@ -11,8 +14,10 @@ import (
|
||||
"github.com/planetmint/planetmint-go/testutil/network"
|
||||
"github.com/planetmint/planetmint-go/testutil/sample"
|
||||
daocli "github.com/planetmint/planetmint-go/x/dao/client/cli"
|
||||
daotypes "github.com/planetmint/planetmint-go/x/dao/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var machines = []struct {
|
||||
@ -53,6 +58,8 @@ func (s *PopSelectionE2ETestSuite) SetupSuite() {
|
||||
|
||||
// trigger one participant selection per test
|
||||
conf.PopEpochs = 10
|
||||
conf.ReissuanceEpochs = 60
|
||||
conf.DistributionOffset = 2
|
||||
}
|
||||
|
||||
// TearDownSuite clean up after testing
|
||||
@ -84,11 +91,46 @@ func (s *PopSelectionE2ETestSuite) perpareLocalTest() testutil.BufferWriter {
|
||||
return out
|
||||
}
|
||||
|
||||
type yamlChallenge struct {
|
||||
Initiator string `yaml:"initiator"`
|
||||
Challenger string `yaml:"challenger"`
|
||||
Challengee string `yaml:"challengee"`
|
||||
Height string `yaml:"height"`
|
||||
Success bool `yaml:"success"`
|
||||
Finished bool `yaml:"finished"`
|
||||
}
|
||||
|
||||
func (s *PopSelectionE2ETestSuite) sendPoPResult(storedChallenge []byte, success bool) {
|
||||
val := s.network.Validators[0]
|
||||
var wrapper struct {
|
||||
Challenge yamlChallenge `yaml:"challenge"`
|
||||
}
|
||||
|
||||
err := yaml.Unmarshal(storedChallenge, &wrapper)
|
||||
if err != nil {
|
||||
log.Fatalf("error: %v", err)
|
||||
}
|
||||
tmpChallenge := wrapper.Challenge
|
||||
var challenge daotypes.Challenge
|
||||
challenge.Challengee = tmpChallenge.Challengee
|
||||
challenge.Challenger = tmpChallenge.Challenger
|
||||
challenge.Initiator = tmpChallenge.Initiator
|
||||
challenge.Height, err = strconv.ParseInt(tmpChallenge.Height, 10, 64)
|
||||
s.Require().NoError(err)
|
||||
challenge.Finished = true
|
||||
challenge.Success = success
|
||||
|
||||
msg := daotypes.NewMsgReportPopResult(val.Address.String(), &challenge)
|
||||
_, err = e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *PopSelectionE2ETestSuite) TestPopSelectionNoActors() {
|
||||
out := s.perpareLocalTest()
|
||||
|
||||
assert.NotContains(s.T(), out.String(), machines[0].address)
|
||||
assert.NotContains(s.T(), out.String(), machines[1].address)
|
||||
s.sendPoPResult(out.Bytes(), true)
|
||||
}
|
||||
|
||||
func (s *PopSelectionE2ETestSuite) TestPopSelectionOneActors() {
|
||||
@ -99,6 +141,7 @@ func (s *PopSelectionE2ETestSuite) TestPopSelectionOneActors() {
|
||||
|
||||
assert.NotContains(s.T(), out.String(), machines[0].address)
|
||||
assert.NotContains(s.T(), out.String(), machines[1].address)
|
||||
s.sendPoPResult(out.Bytes(), true)
|
||||
}
|
||||
|
||||
func (s *PopSelectionE2ETestSuite) TestPopSelectionTwoActors() {
|
||||
@ -109,4 +152,71 @@ func (s *PopSelectionE2ETestSuite) TestPopSelectionTwoActors() {
|
||||
|
||||
assert.Contains(s.T(), out.String(), machines[0].address)
|
||||
assert.Contains(s.T(), out.String(), machines[1].address)
|
||||
s.sendPoPResult(out.Bytes(), true)
|
||||
}
|
||||
|
||||
func (s *PopSelectionE2ETestSuite) VerifyTokens(token string) {
|
||||
val := s.network.Validators[0]
|
||||
conf := config.GetConfig()
|
||||
// check balance for crddl
|
||||
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetCmdQueryTotalSupply(), []string{
|
||||
fmt.Sprintf("--%s=%s", bank.FlagDenom, token),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
assert.Contains(s.T(), out.String(), conf.ClaimDenom)
|
||||
assert.Equal(s.T(), "amount: \"17979452050\"\ndenom: "+token+"\n", out.String()) // Total supply 2 * 7990867578 (total supply) + 1 * 1997716894 (challenger) = 17979452050
|
||||
|
||||
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{
|
||||
machines[0].address,
|
||||
fmt.Sprintf("--%s=%s", bank.FlagDenom, token),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
assert.Contains(s.T(), out.String(), token)
|
||||
assert.Equal(s.T(), "amount: \"5993150682\"\ndenom: "+token+"\n", out.String()) // 3 * 1997716894 = 5993150682
|
||||
|
||||
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{
|
||||
machines[1].address,
|
||||
fmt.Sprintf("--%s=%s", bank.FlagDenom, token),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
assert.Contains(s.T(), out.String(), token)
|
||||
assert.Equal(s.T(), "amount: \"11986301368\"\ndenom: "+token+"\n", out.String()) // 2 * 5993150684 = 11986301368
|
||||
}
|
||||
|
||||
func (s *PopSelectionE2ETestSuite) TestTokenDistribution1() {
|
||||
conf := config.GetConfig()
|
||||
|
||||
out := s.perpareLocalTest()
|
||||
|
||||
assert.Contains(s.T(), out.String(), machines[0].address)
|
||||
assert.Contains(s.T(), out.String(), machines[1].address)
|
||||
s.sendPoPResult(out.Bytes(), false)
|
||||
|
||||
out = s.perpareLocalTest()
|
||||
|
||||
assert.Contains(s.T(), out.String(), machines[0].address)
|
||||
assert.Contains(s.T(), out.String(), machines[1].address)
|
||||
s.sendPoPResult(out.Bytes(), true)
|
||||
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
|
||||
s.VerifyTokens(conf.StagedDenom)
|
||||
|
||||
// send Reissuance and DistributionResult implicitly
|
||||
latestHeight, err := s.network.LatestHeight()
|
||||
s.Require().NoError(err)
|
||||
for {
|
||||
latestHeight, err := s.network.WaitForHeight(latestHeight + 1)
|
||||
s.Require().NoError(err)
|
||||
// s.Require().NoError(s.network.WaitForNextBlock())
|
||||
if latestHeight%int64(conf.ReissuanceEpochs) == int64(conf.DistributionOffset) {
|
||||
break
|
||||
}
|
||||
}
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
|
||||
s.VerifyTokens(conf.ClaimDenom)
|
||||
}
|
||||
|
@ -3,11 +3,9 @@ package dao
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
@ -80,12 +78,10 @@ func (s *E2ETestSuite) SetupSuite() {
|
||||
|
||||
bbalances := sdk.NewCoins(
|
||||
sdk.NewCoin(conf.TokenDenom, math.NewInt(10000)),
|
||||
sdk.NewCoin(conf.StakeDenom, math.NewInt(10000)),
|
||||
)
|
||||
|
||||
abalances := sdk.NewCoins(
|
||||
sdk.NewCoin(conf.TokenDenom, math.NewInt(10000)),
|
||||
sdk.NewCoin(conf.StakeDenom, math.NewInt(5000)),
|
||||
)
|
||||
|
||||
accountBalances := []banktypes.Balance{
|
||||
@ -117,43 +113,6 @@ func (s *E2ETestSuite) TearDownSuite() {
|
||||
s.T().Log("tearing down e2e test suite")
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestDistributeCollectedFees() {
|
||||
val := s.network.Validators[0]
|
||||
|
||||
// sending funds to alice and pay some fees to be distributed
|
||||
coin := sdk.NewCoins(sdk.NewInt64Coin("stake", 1000))
|
||||
msg := banktypes.NewMsgSend(val.Address, aliceAddr, coin)
|
||||
|
||||
_, err := e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
err = s.network.WaitForNextBlock()
|
||||
s.Require().NoError(err)
|
||||
|
||||
_, err = e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
err = s.network.WaitForNextBlock()
|
||||
s.Require().NoError(err)
|
||||
|
||||
err = s.network.WaitForNextBlock()
|
||||
s.Require().NoError(err)
|
||||
|
||||
// assert that alice has 0 of 20 paid fee tokens based on 5000 stake of 15000 total stake
|
||||
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{
|
||||
aliceAddr.String(),
|
||||
})
|
||||
assert.False(s.T(), strings.Contains(out.String(), "node0token"))
|
||||
s.Require().NoError(err)
|
||||
|
||||
// assert that bob has 1 of 20 paid fee tokens based on 10000 stake of 15000 total stake
|
||||
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{
|
||||
bobAddr.String(),
|
||||
})
|
||||
assert.Contains(s.T(), out.String(), "amount: \"1\"\n denom: node0token")
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestMintToken() {
|
||||
val := s.network.Validators[0]
|
||||
|
||||
@ -254,121 +213,3 @@ func (s *E2ETestSuite) TestReissuance() {
|
||||
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, daocli.CmdGetReissuance(), []string{intValue})
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestPoPResult() {
|
||||
conf := config.GetConfig()
|
||||
conf.PopEpochs = 5
|
||||
val := s.network.Validators[0]
|
||||
|
||||
// send PoP results
|
||||
challenges := make([]daotypes.Challenge, 5)
|
||||
for i := range challenges {
|
||||
blockHeight := (i + 1) * config.GetConfig().PopEpochs
|
||||
challenges[i].Height = int64(blockHeight)
|
||||
challenges[i].Initiator = val.Address.String()
|
||||
challenges[i].Challenger = aliceAddr.String()
|
||||
challenges[i].Challengee = bobAddr.String()
|
||||
challenges[i].Success = blockHeight%2 == 0 // Need some successful and unsuccessful challenges for assertion of correct behavior
|
||||
challenges[i].Finished = true
|
||||
|
||||
msg := daotypes.NewMsgReportPopResult(val.Address.String(), &challenges[i])
|
||||
out, err := e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg)
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
s.Require().NoError(err)
|
||||
assert.Equal(s.T(), "[]", txResponse.RawLog)
|
||||
}
|
||||
|
||||
// check balance for stagedcrddl
|
||||
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetCmdQueryTotalSupply(), []string{
|
||||
fmt.Sprintf("--%s=%s", bank.FlagDenom, conf.StagedDenom),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
assert.Contains(s.T(), out.String(), conf.StagedDenom)
|
||||
assert.Equal(s.T(), "amount: \"21974885838\"\ndenom: stagedcrddl\n", out.String()) // Total supply 5 * 1997716894 + 2 * 5993150684 = 21974885838
|
||||
|
||||
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{
|
||||
aliceAddr.String(),
|
||||
fmt.Sprintf("--%s=%s", bank.FlagDenom, conf.StagedDenom),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
assert.Contains(s.T(), out.String(), conf.StagedDenom)
|
||||
assert.Equal(s.T(), "amount: \"9988584470\"\ndenom: stagedcrddl\n", out.String()) // 5 * 1997716894 = 9988584470
|
||||
|
||||
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{
|
||||
bobAddr.String(),
|
||||
fmt.Sprintf("--%s=%s", bank.FlagDenom, conf.StagedDenom),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
assert.Contains(s.T(), out.String(), conf.StagedDenom)
|
||||
assert.Equal(s.T(), "amount: \"11986301368\"\ndenom: stagedcrddl\n", out.String()) // 2 * 5993150684 = 11986301368
|
||||
|
||||
// send ReissuanceProposal
|
||||
msg1 := daotypes.NewMsgReissueRDDLProposal(val.Address.String(), hex.EncodeToString(val.PubKey.Address()),
|
||||
"reissueasset 7add40beb27df701e02ee85089c5bc0021bc813823fedb5f1dcb5debda7f3da9 2996.57534244",
|
||||
challenges[4].Height, challenges[0].Height, challenges[2].Height)
|
||||
output, err := e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg1)
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
|
||||
txResponse, err := lib.GetTxResponseFromOut(output)
|
||||
s.Require().NoError(err)
|
||||
assert.Equal(s.T(), "[]", txResponse.RawLog)
|
||||
|
||||
// send ReissuanceResult
|
||||
msg2 := daotypes.NewMsgReissueRDDLResult(val.Address.String(), aliceAddr.String(), "TxID", challenges[4].Height)
|
||||
output, err = e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg2)
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
|
||||
txResponse, err = lib.GetTxResponseFromOut(output)
|
||||
s.Require().NoError(err)
|
||||
assert.Equal(s.T(), "[]", txResponse.RawLog)
|
||||
|
||||
// send DistributionRequest
|
||||
distributionOrder := daotypes.DistributionOrder{
|
||||
Proposer: hex.EncodeToString(val.PubKey.Address()),
|
||||
FirstPop: challenges[0].Height,
|
||||
LastPop: challenges[2].Height,
|
||||
DaoTxID: "DaoTxID",
|
||||
PopTxID: "PoPTxID",
|
||||
InvestorTxID: "InvestorTxID",
|
||||
}
|
||||
msg3 := daotypes.NewMsgDistributionRequest(val.Address.String(), &distributionOrder)
|
||||
output, err = e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg3)
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
|
||||
txResponse, err = lib.GetTxResponseFromOut(output)
|
||||
s.Require().NoError(err)
|
||||
assert.Equal(s.T(), "[]", txResponse.RawLog)
|
||||
|
||||
// send DistributionResult implicitly
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
|
||||
// check balance for crddl
|
||||
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetCmdQueryTotalSupply(), []string{
|
||||
fmt.Sprintf("--%s=%s", bank.FlagDenom, conf.ClaimDenom),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
assert.Contains(s.T(), out.String(), conf.ClaimDenom)
|
||||
assert.Equal(s.T(), "amount: \"11986301366\"\ndenom: crddl\n", out.String()) // Total supply 1 * 5993150684 + 3 * 1997716894 = 11986301366
|
||||
|
||||
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{
|
||||
aliceAddr.String(),
|
||||
fmt.Sprintf("--%s=%s", bank.FlagDenom, conf.ClaimDenom),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
assert.Contains(s.T(), out.String(), conf.ClaimDenom)
|
||||
assert.Equal(s.T(), "amount: \"5993150682\"\ndenom: crddl\n", out.String()) // 3 * 1997716894 = 5993150682
|
||||
|
||||
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{
|
||||
bobAddr.String(),
|
||||
fmt.Sprintf("--%s=%s", bank.FlagDenom, conf.ClaimDenom),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
assert.Contains(s.T(), out.String(), conf.ClaimDenom)
|
||||
assert.Equal(s.T(), "amount: \"5993150684\"\ndenom: crddl\n", out.String()) // 1 * 5993150684 = 5993150684
|
||||
}
|
||||
|
@ -59,6 +59,7 @@ func New(t *testing.T, configs ...Config) *Network {
|
||||
conf.SetRoot(validatorTmpDir + "/node0/simd")
|
||||
|
||||
net, err := network.New(t, validatorTmpDir, cfg)
|
||||
require.NoError(t, err)
|
||||
|
||||
conf.ValidatorAddress = net.Validators[0].Address.String()
|
||||
// set missing validator client context values for sending txs
|
||||
|
@ -27,7 +27,7 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper)
|
||||
challenger, challengee := k.SelectPopParticipants(ctx)
|
||||
|
||||
// Init PoP - independent from challenger and challengee
|
||||
// The keeper will send MQTT the initializing message to challenger && challengee
|
||||
// The keeper will send the MQTT initializing message to challenger && challengee
|
||||
util.SendInitPoP(ctx, hexProposerAddress, challenger, challengee, currentBlockHeight)
|
||||
}
|
||||
|
||||
@ -72,6 +72,5 @@ func isDistributionHeight(height int64) bool {
|
||||
return height%int64(conf.ReissuanceEpochs) == int64(conf.DistributionOffset)
|
||||
}
|
||||
|
||||
func EndBlocker(ctx sdk.Context, _ abci.RequestEndBlock, k keeper.Keeper) {
|
||||
k.DistributeCollectedFees(ctx)
|
||||
func EndBlocker(_ sdk.Context, _ abci.RequestEndBlock, _ keeper.Keeper) {
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package keeper
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
db "github.com/cometbft/cometbft-db"
|
||||
"github.com/cometbft/cometbft/libs/log"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
@ -11,7 +10,6 @@ import (
|
||||
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
||||
|
||||
"github.com/planetmint/planetmint-go/config"
|
||||
@ -78,80 +76,6 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
||||
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
|
||||
}
|
||||
|
||||
func (k Keeper) DistributeCollectedFees(ctx sdk.Context) {
|
||||
ctx = sdk.UnwrapSDKContext(ctx)
|
||||
conf := config.GetConfig()
|
||||
|
||||
balances := make(map[string]math.Int)
|
||||
totalStake := math.ZeroInt()
|
||||
k.accountKeeper.IterateAccounts(ctx, func(acc authtypes.AccountI) bool {
|
||||
addr := acc.GetAddress()
|
||||
balance := k.bankKeeper.SpendableCoins(ctx, addr)
|
||||
found, stake := balance.Find(conf.StakeDenom)
|
||||
if found {
|
||||
totalStake = totalStake.Add(stake.Amount)
|
||||
balances[addr.String()] = stake.Amount
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
distAddr := k.accountKeeper.GetModuleAddress(disttypes.ModuleName)
|
||||
distSpendableCoins := k.bankKeeper.SpendableCoins(ctx, distAddr)
|
||||
found, coinToDistribute := distSpendableCoins.Find(conf.FeeDenom)
|
||||
|
||||
if found {
|
||||
err := k.processBalances(ctx, balances, totalStake, coinToDistribute)
|
||||
if err != nil {
|
||||
util.GetAppLogger().Error(ctx, "Error processing balances:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the address is blocked
|
||||
func (k Keeper) isAddressBlocked(accAddress sdk.AccAddress) bool {
|
||||
return k.bankKeeper.BlockedAddr(accAddress)
|
||||
}
|
||||
|
||||
// Send coins from the module to the account
|
||||
func (k Keeper) sendCoinsFromModuleToAccount(ctx sdk.Context, accAddress sdk.AccAddress, coinClaim sdk.Coin) error {
|
||||
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, disttypes.ModuleName, accAddress, sdk.NewCoins(coinClaim))
|
||||
}
|
||||
|
||||
// Calculate the claim for an address
|
||||
func calculateClaimForAddress(stake math.Int, totalStake math.Int, coinToDistribute sdk.Coin) sdk.Dec {
|
||||
decTotalAmountToDistribute := sdk.NewDecFromInt(coinToDistribute.Amount)
|
||||
decTotalStake := sdk.NewDecFromInt(totalStake)
|
||||
decStake := sdk.NewDecFromInt(stake)
|
||||
|
||||
share := decStake.Quo(decTotalStake)
|
||||
return decTotalAmountToDistribute.Mul(share)
|
||||
}
|
||||
|
||||
func (k Keeper) processBalances(ctx sdk.Context, balances map[string]math.Int, totalStake math.Int, coinToDistribute sdk.Coin) error {
|
||||
conf := config.GetConfig()
|
||||
for addr, stake := range balances {
|
||||
claim := calculateClaimForAddress(stake, totalStake, coinToDistribute)
|
||||
|
||||
if claim.GTE(sdk.OneDec()) {
|
||||
intClaim := claim.TruncateInt()
|
||||
coinClaim := sdk.NewCoin(conf.FeeDenom, intClaim)
|
||||
|
||||
accAddress, err := sdk.AccAddressFromBech32(addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !k.isAddressBlocked(accAddress) {
|
||||
err = k.sendCoinsFromModuleToAccount(ctx, accAddress, coinClaim)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k Keeper) SelectPopParticipants(ctx sdk.Context) (challenger string, challengee string) {
|
||||
conf := config.GetConfig()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user