Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add x/farm module #362

Merged
merged 34 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6d8d3e5
feat: add farm module
hallazzang Sep 15, 2022
92476a7
feat: allocate rewards
hallazzang Sep 16, 2022
4c5a831
feat: implement remaining methods
hallazzang Sep 17, 2022
40bd9cc
fix: distribute rewards based on reward weight
hallazzang Sep 18, 2022
9b9332f
fix: add gRPC query definitions and organize code structure
hallazzang Sep 19, 2022
5fbcd22
feat: implement validation logics
hallazzang Sep 19, 2022
7190295
test: add tests for types package
hallazzang Sep 20, 2022
6dc3a47
fix: remove underscores in ParseDec
hallazzang Sep 20, 2022
dc8e404
feat: terminate ended plas and change pool reward weight function
hallazzang Sep 20, 2022
ed2f5ba
refactor!: `Rewards` returns current withdrawable rewards
hallazzang Sep 21, 2022
12ff990
fix: wrap `PoolRewardWeight` with `SafeMath` to prevent overflow panic
hallazzang Sep 21, 2022
e153841
test: add genesis tests
hallazzang Sep 21, 2022
0752dd1
fix: fix typo in time comparison
hallazzang Sep 21, 2022
9f272f1
fix: withdraw rewards before updating total farming amount
hallazzang Sep 21, 2022
168f939
refactor: do not send rewards when there's no farm
hallazzang Sep 21, 2022
d35b13c
feat: implement query cmds
hallazzang Sep 21, 2022
2de5a71
fix: fix `CachedKeeper.SpendableCoins` to set cache
hallazzang Sep 21, 2022
e209faa
test: fix app tests to include farm module
hallazzang Sep 22, 2022
00749c2
feat: add `FarmingPlanProposal` and write sim operations
hallazzang Sep 22, 2022
1be39d9
test: fix operations test
hallazzang Sep 22, 2022
25f8176
fix: fix bugs in `AllocateRewrds` and `Farm`
hallazzang Sep 23, 2022
41e4143
fix: copy `strings.Cut` implementation from the future
hallazzang Sep 23, 2022
3181cb2
test: add operations tests
hallazzang Sep 23, 2022
fefadef
test: add unit tests
hallazzang Sep 26, 2022
6f3de7c
feat: add invariant checks
hallazzang Sep 26, 2022
3bd1c94
feat: add `TotalFarmingAmountInvariant` check
hallazzang Sep 27, 2022
c3a4ff1
test: add more tests
hallazzang Sep 27, 2022
61a0026
ci: update labeler configuration
hallazzang Sep 27, 2022
5cbbe06
fix: resolve TODO comments and emit typed events
hallazzang Sep 28, 2022
c1328ae
refactor: add counter for non-terminated private plans
hallazzang Sep 28, 2022
3447839
fix: update app.go
hallazzang Sep 28, 2022
25fbdf4
fix: add `NumPrivatePlans` to genesis state
hallazzang Sep 29, 2022
643182a
fix: call `RegisterProposalTypeCodec` for `FarmingPlanProposal`
hallazzang Sep 30, 2022
5585f1e
refactor: update docs and rename `withdrawn_coins`
hallazzang Oct 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
# commit msg check
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v1.0.0
rev: v2.0.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
Expand Down
21 changes: 21 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ import (
"github.com/cosmosquad-labs/squad/v3/x/claim"
claimkeeper "github.com/cosmosquad-labs/squad/v3/x/claim/keeper"
claimtypes "github.com/cosmosquad-labs/squad/v3/x/claim/types"
"github.com/cosmosquad-labs/squad/v3/x/farm"
farmkeeper "github.com/cosmosquad-labs/squad/v3/x/farm/keeper"
farmtypes "github.com/cosmosquad-labs/squad/v3/x/farm/types"
"github.com/cosmosquad-labs/squad/v3/x/farming"
farmingclient "github.com/cosmosquad-labs/squad/v3/x/farming/client"
farmingkeeper "github.com/cosmosquad-labs/squad/v3/x/farming/keeper"
Expand Down Expand Up @@ -164,6 +167,7 @@ var (
liquidstaking.AppModuleBasic{},
claim.AppModuleBasic{},
marketmaker.AppModuleBasic{},
farm.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -229,6 +233,7 @@ type App struct {
LiquidStakingKeeper liquidstakingkeeper.Keeper
ClaimKeeper claimkeeper.Keeper
MarketMakerKeeper marketmakerkeeper.Keeper
FarmKeeper farmkeeper.Keeper

ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
Expand Down Expand Up @@ -297,6 +302,7 @@ func NewApp(
liquidstakingtypes.StoreKey,
claimtypes.StoreKey,
marketmakertypes.StoreKey,
farmtypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -444,6 +450,14 @@ func NewApp(
app.AccountKeeper,
app.BankKeeper,
)
app.FarmKeeper = farmkeeper.NewKeeper(
appCodec,
keys[farmtypes.StoreKey],
app.GetSubspace(farmtypes.ModuleName),
app.AccountKeeper,
app.BankKeeper,
app.LiquidityKeeper,
)

// register the proposal types
govRouter := govtypes.NewRouter()
Expand All @@ -455,6 +469,7 @@ func NewApp(
AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper)).
AddRoute(farmingtypes.RouterKey, farming.NewPublicPlanProposalHandler(app.FarmingKeeper)).
AddRoute(marketmakertypes.RouterKey, marketmaker.NewMarketMakerProposalHandler(app.MarketMakerKeeper))
// TODO: add x/farm proposal handler

app.GovKeeper = govkeeper.NewKeeper(
appCodec,
Expand Down Expand Up @@ -555,6 +570,7 @@ func NewApp(
liquidstaking.NewAppModule(appCodec, app.LiquidStakingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GovKeeper),
claim.NewAppModule(appCodec, app.ClaimKeeper, app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.GovKeeper, app.LiquidityKeeper, app.LiquidStakingKeeper),
marketmaker.NewAppModule(appCodec, app.MarketMakerKeeper, app.AccountKeeper, app.BankKeeper),
farm.NewAppModule(appCodec, app.FarmKeeper, app.AccountKeeper, app.BankKeeper, app.LiquidityKeeper),
app.transferModule,
)

Expand All @@ -574,6 +590,7 @@ func NewApp(
liquidstakingtypes.ModuleName,
liquiditytypes.ModuleName,
ibchost.ModuleName,
farmtypes.ModuleName,

// empty logic modules
authtypes.ModuleName,
Expand Down Expand Up @@ -618,6 +635,7 @@ func NewApp(
claimtypes.ModuleName,
budgettypes.ModuleName,
marketmakertypes.ModuleName,
farmtypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -646,6 +664,7 @@ func NewApp(
liquidstakingtypes.ModuleName,
claimtypes.ModuleName,
marketmakertypes.ModuleName,
farmtypes.ModuleName,

// empty logic modules
paramstypes.ModuleName,
Expand Down Expand Up @@ -686,6 +705,7 @@ func NewApp(
liquidstaking.NewAppModule(appCodec, app.LiquidStakingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GovKeeper),
claim.NewAppModule(appCodec, app.ClaimKeeper, app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.GovKeeper, app.LiquidityKeeper, app.LiquidStakingKeeper),
marketmaker.NewAppModule(appCodec, app.MarketMakerKeeper, app.AccountKeeper, app.BankKeeper),
farm.NewAppModule(appCodec, app.FarmKeeper, app.AccountKeeper, app.BankKeeper, app.LiquidityKeeper),
ibc.NewAppModule(app.IBCKeeper),
app.transferModule,
)
Expand Down Expand Up @@ -891,6 +911,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(liquiditytypes.ModuleName)
paramsKeeper.Subspace(liquidstakingtypes.ModuleName)
paramsKeeper.Subspace(marketmakertypes.ModuleName)
paramsKeeper.Subspace(farmtypes.ModuleName)

return paramsKeeper
}
Expand Down
3 changes: 3 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
dbm "github.com/tendermint/tm-db"

"github.com/cosmosquad-labs/squad/v3/x/claim"
"github.com/cosmosquad-labs/squad/v3/x/farm"
"github.com/cosmosquad-labs/squad/v3/x/farming"
"github.com/cosmosquad-labs/squad/v3/x/liquidity"
"github.com/cosmosquad-labs/squad/v3/x/liquidstaking"
Expand Down Expand Up @@ -194,6 +195,7 @@ func TestRunMigrations(t *testing.T) {
"liquidstaking": liquidstaking.AppModule{}.ConsensusVersion(),
"claim": claim.AppModule{}.ConsensusVersion(),
"marketmaker": marketmaker.AppModule{}.ConsensusVersion(),
"farm": farm.AppModule{}.ConsensusVersion(),
"ibc": ibc.AppModule{}.ConsensusVersion(),
"transfer": transfer.AppModule{}.ConsensusVersion(),
},
Expand Down Expand Up @@ -254,6 +256,7 @@ func TestInitGenesisOnMigration(t *testing.T) {
"liquidstaking": liquidstaking.AppModule{}.ConsensusVersion(),
"claim": claim.AppModule{}.ConsensusVersion(),
"marketmaker": marketmaker.AppModule{}.ConsensusVersion(),
"farm": farm.AppModule{}.ConsensusVersion(),
"ibc": ibc.AppModule{}.ConsensusVersion(),
"transfer": transfer.AppModule{}.ConsensusVersion(),
},
Expand Down
2 changes: 2 additions & 0 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
dbm "github.com/tendermint/tm-db"

claimtypes "github.com/cosmosquad-labs/squad/v3/x/claim/types"
farmtypes "github.com/cosmosquad-labs/squad/v3/x/farm/types"
farmingtypes "github.com/cosmosquad-labs/squad/v3/x/farming/types"
liquiditytypes "github.com/cosmosquad-labs/squad/v3/x/liquidity/types"
liquidstakingtypes "github.com/cosmosquad-labs/squad/v3/x/liquidstaking/types"
Expand Down Expand Up @@ -194,6 +195,7 @@ func TestAppImportExport(t *testing.T) {
{app.keys[liquiditytypes.StoreKey], newApp.keys[liquiditytypes.StoreKey], [][]byte{}},
{app.keys[claimtypes.StoreKey], newApp.keys[claimtypes.StoreKey], [][]byte{}},
{app.keys[marketmakertypes.StoreKey], newApp.keys[marketmakertypes.StoreKey], [][]byte{}},
{app.keys[farmtypes.StoreKey], newApp.keys[farmtypes.StoreKey], [][]byte{}},
{app.keys[ibchost.StoreKey], newApp.keys[ibchost.StoreKey], [][]byte{}},
{app.keys[ibctransfertypes.StoreKey], newApp.keys[ibctransfertypes.StoreKey], [][]byte{}},
}
Expand Down
62 changes: 62 additions & 0 deletions proto/squad/farm/v1beta1/farm.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
syntax = "proto3";

package squad.farm.v1beta1;

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";

option go_package = "github.com/cosmosquad-labs/squad/x/farm/types";
option (gogoproto.goproto_getters_all) = false;

message Params {
repeated cosmos.base.v1beta1.Coin private_plan_creation_fee = 1
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins", (gogoproto.nullable) = false];
string fee_collector = 2;
uint32 max_num_private_plans = 3;
google.protobuf.Duration max_block_duration = 4 [(gogoproto.stdduration) = true, (gogoproto.nullable) = false];
}

message Plan {
uint64 id = 1;
string description = 2;
string farming_pool_address = 3;
string termination_address = 4;
repeated RewardAllocation reward_allocations = 5 [(gogoproto.nullable) = false];
google.protobuf.Timestamp start_time = 6 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
google.protobuf.Timestamp end_time = 7 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
bool is_private = 8;
bool is_terminated = 9;
}

message RewardAllocation {
uint64 pair_id = 1;
repeated cosmos.base.v1beta1.Coin rewards_per_day = 2
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins", (gogoproto.nullable) = false];
}

message Farm {
string total_farming_amount = 1
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
repeated cosmos.base.v1beta1.DecCoin current_rewards = 2
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins", (gogoproto.nullable) = false];
repeated cosmos.base.v1beta1.DecCoin outstanding_rewards = 3
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins", (gogoproto.nullable) = false];
uint64 period = 4;
}

message Position {
string farmer = 1;
string denom = 2;
string farming_amount = 3
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
uint64 previous_period = 4;
int64 starting_block_height = 5;
}

message HistoricalRewards {
repeated cosmos.base.v1beta1.DecCoin cumulative_unit_rewards = 1
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins", (gogoproto.nullable) = false];
uint32 reference_count = 2;
}
31 changes: 31 additions & 0 deletions proto/squad/farm/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
syntax = "proto3";

package squad.farm.v1beta1;

import "gogoproto/gogo.proto";
import "squad/farm/v1beta1/farm.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/cosmosquad-labs/squad/x/farm/types";
option (gogoproto.goproto_getters_all) = false;

message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
google.protobuf.Timestamp last_block_time = 2 [(gogoproto.stdtime) = true];
uint64 last_plan_id = 3;
repeated Plan plans = 4 [(gogoproto.nullable) = false];
repeated FarmRecord farms = 5 [(gogoproto.nullable) = false];
repeated Position positions = 6 [(gogoproto.nullable) = false];
repeated HistoricalRewardsRecord historical_rewards = 7 [(gogoproto.nullable) = false];
}

message FarmRecord {
string denom = 1;
Farm farm = 2 [(gogoproto.nullable) = false];
}

message HistoricalRewardsRecord {
string denom = 1;
uint64 period = 2;
HistoricalRewards historical_rewards = 3 [(gogoproto.nullable) = false];
}
31 changes: 31 additions & 0 deletions proto/squad/farm/v1beta1/proposal.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
syntax = "proto3";

package squad.farm.v1beta1;

import "gogoproto/gogo.proto";
import "squad/farm/v1beta1/farm.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/cosmosquad-labs/squad/x/farm/types";
option (gogoproto.goproto_getters_all) = false;

message FarmingPlanProposal {
option (gogoproto.goproto_stringer) = false;
string title = 1;
string description = 2;
repeated CreatePlanRequest create_plan_requests = 3 [(gogoproto.nullable) = false];
repeated TerminatePlanRequest terminate_plan_requests = 4 [(gogoproto.nullable) = false];
}

message CreatePlanRequest {
string description = 1;
string farming_pool_address = 2;
string termination_address = 3;
repeated RewardAllocation reward_allocations = 4 [(gogoproto.nullable) = false];
google.protobuf.Timestamp start_time = 5 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
google.protobuf.Timestamp end_time = 6 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
}

message TerminatePlanRequest {
uint64 plan_id = 1;
}
Loading