forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Community pool spend proposal (cosmos#4329)
Implement the "CommunityPoolSpendProposal" as described in Cosmos Hub proposal 7. Also a useful test of Git flow for merging features passed in governance proposals.
- Loading branch information
Showing
19 changed files
with
383 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Community pool spend proposal per Cosmos Hub governance proposal #7 "Activate the Community Pool" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cli | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
type ( | ||
// CommunityPoolSpendProposalJSON defines a CommunityPoolSpendProposal with a deposit | ||
CommunityPoolSpendProposalJSON struct { | ||
Title string `json:"title"` | ||
Description string `json:"description"` | ||
Recipient sdk.AccAddress `json:"recipient"` | ||
Amount sdk.Coins `json:"amount"` | ||
Deposit sdk.Coins `json:"deposit"` | ||
} | ||
) | ||
|
||
// ParseCommunityPoolSpendProposalJSON reads and parses a CommunityPoolSpendProposalJSON from a file. | ||
func ParseCommunityPoolSpendProposalJSON(cdc *codec.Codec, proposalFile string) (CommunityPoolSpendProposalJSON, error) { | ||
proposal := CommunityPoolSpendProposalJSON{} | ||
|
||
contents, err := ioutil.ReadFile(proposalFile) | ||
if err != nil { | ||
return proposal, err | ||
} | ||
|
||
if err := cdc.UnmarshalJSON(contents, &proposal); err != nil { | ||
return proposal, err | ||
} | ||
|
||
return proposal, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package rest | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/rest" | ||
) | ||
|
||
type ( | ||
// CommunityPoolSpendProposalReq defines a community pool spend proposal request body. | ||
CommunityPoolSpendProposalReq struct { | ||
BaseReq rest.BaseReq `json:"base_req"` | ||
|
||
Title string `json:"title"` | ||
Description string `json:"description"` | ||
Recipient sdk.AccAddress `json:"recipient"` | ||
Amount sdk.Coins `json:"amount"` | ||
Proposer sdk.AccAddress `json:"proposer"` | ||
Deposit sdk.Coins `json:"deposit"` | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/distribution/types" | ||
) | ||
|
||
func HandleCommunityPoolSpendProposal(ctx sdk.Context, k Keeper, p types.CommunityPoolSpendProposal) sdk.Error { | ||
feePool := k.GetFeePool(ctx) | ||
newPool, negative := feePool.CommunityPool.SafeSub(sdk.NewDecCoins(p.Amount)) | ||
if negative { | ||
return types.ErrBadDistribution(k.codespace) | ||
} | ||
feePool.CommunityPool = newPool | ||
k.SetFeePool(ctx, feePool) | ||
_, err := k.bankKeeper.AddCoins(ctx, p.Recipient, p.Amount) | ||
if err != nil { | ||
return err | ||
} | ||
logger := k.Logger(ctx) | ||
logger.Info(fmt.Sprintf("Spent %s coins from the community pool to recipient %s", p.Amount, p.Recipient)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package distribution | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/tendermint/tendermint/crypto/ed25519" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/distribution/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
delPk1 = ed25519.GenPrivKey().PubKey() | ||
delAddr1 = sdk.AccAddress(delPk1.Address()) | ||
) | ||
|
||
func testProposal(recipient sdk.AccAddress, amount sdk.Coins) types.CommunityPoolSpendProposal { | ||
return types.NewCommunityPoolSpendProposal( | ||
"Test", | ||
"description", | ||
recipient, | ||
amount, | ||
) | ||
} | ||
|
||
func TestProposalHandlerPassed(t *testing.T) { | ||
ctx, accountKeeper, keeper, _, _ := CreateTestInputDefault(t, false, 10) | ||
recipient := delAddr1 | ||
amount := sdk.NewCoin("stake", sdk.NewInt(1)) | ||
|
||
account := accountKeeper.NewAccountWithAddress(ctx, recipient) | ||
require.True(t, account.GetCoins().IsZero()) | ||
accountKeeper.SetAccount(ctx, account) | ||
|
||
feePool := keeper.GetFeePool(ctx) | ||
feePool.CommunityPool = sdk.DecCoins{sdk.NewDecCoinFromCoin(amount)} | ||
keeper.SetFeePool(ctx, feePool) | ||
|
||
tp := testProposal(recipient, sdk.NewCoins(amount)) | ||
hdlr := NewCommunityPoolSpendProposalHandler(keeper) | ||
require.NoError(t, hdlr(ctx, tp)) | ||
require.Equal(t, accountKeeper.GetAccount(ctx, recipient).GetCoins(), sdk.NewCoins(amount)) | ||
} | ||
|
||
func TestProposalHandlerFailed(t *testing.T) { | ||
ctx, accountKeeper, keeper, _, _ := CreateTestInputDefault(t, false, 10) | ||
recipient := delAddr1 | ||
amount := sdk.NewCoin("stake", sdk.NewInt(1)) | ||
|
||
account := accountKeeper.NewAccountWithAddress(ctx, recipient) | ||
require.True(t, account.GetCoins().IsZero()) | ||
accountKeeper.SetAccount(ctx, account) | ||
|
||
tp := testProposal(recipient, sdk.NewCoins(amount)) | ||
hdlr := NewCommunityPoolSpendProposalHandler(keeper) | ||
require.Error(t, hdlr(ctx, tp)) | ||
require.True(t, accountKeeper.GetAccount(ctx, recipient).GetCoins().IsZero()) | ||
} |
Oops, something went wrong.