Skip to content

Commit

Permalink
feat: add new CLP tx to add liquidity to rewards bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
snobbee committed Nov 7, 2023
1 parent 40bb2c3 commit 2e0180f
Show file tree
Hide file tree
Showing 30 changed files with 1,643 additions and 147 deletions.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ tests:
feature-tests:
@GOFLAGS=$(GOFLAGS) go test -v ./test/bdd --godog.format=pretty --godog.random -race -coverprofile=.coverage.txt

mocks:
@echo "Generating mocks"

# Check if mockery is available in $PATH, install it if not.
@if ! which mockery > /dev/null; then \
echo "mockery not found, installing version v2..."; \
go install github.com/vektra/mockery/v2; \
fi

# Check if mockgen is available in $PATH, install it if not.
@if ! which mockgen > /dev/null; then \
echo "mockgen not found, installing latest version..."; \
go install go.uber.org/mock/mockgen@latest; \
fi

# Check again if mockery is installed, fail if not found.
@if ! which mockery > /dev/null; then \
echo "Error: mockery could not be found or installed"; \
exit 1; \
fi

# Check again if mockgen is installed, fail if not found.
@if ! which mockgen > /dev/null; then \
echo "Error: mockgen could not be found or installed"; \
exit 1; \
fi

# Run go generate on all packages.
@go generate ./...

run:
GOFLAGS=$(GOFLAGS) go run ./cmd/sifnoded start

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ require (
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand Down
9 changes: 9 additions & 0 deletions proto/sifnode/clp/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package sifnode.clp.v1;

import "gogoproto/gogo.proto";
import "cosmos/base/coin.proto";
import "sifnode/clp/v1/types.proto";
import "sifnode/clp/v1/params.proto";

Expand Down Expand Up @@ -34,6 +35,7 @@ service Msg {
rpc ModifyLiquidityProtectionRates(MsgModifyLiquidityProtectionRates) returns (MsgModifyLiquidityProtectionRatesResponse);
rpc AddProviderDistributionPeriod(MsgAddProviderDistributionPeriodRequest) returns (MsgAddProviderDistributionPeriodResponse);
rpc UpdateSwapFeeParams(MsgUpdateSwapFeeParamsRequest) returns (MsgUpdateSwapFeeParamsResponse);
rpc AddLiquidityToRewardsBucket (MsgAddLiquidityToRewardsBucketRequest) returns (MsgAddLiquidityToRewardsBucketResponse);
}

// message MsgUpdateStakingRewardParams{
Expand Down Expand Up @@ -281,3 +283,10 @@ message MsgUpdateSwapFeeParamsRequest {
}

message MsgUpdateSwapFeeParamsResponse {}

message MsgAddLiquidityToRewardsBucketRequest {
string signer = 1;
repeated cosmos.base.v1beta1.Coin amount = 2 [(gogoproto.nullable) = false];
}

message MsgAddLiquidityToRewardsBucketResponse {}
10 changes: 7 additions & 3 deletions testutil/keeper/clp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Sifchain/sifnode/x/clp/keeper"
"github.com/Sifchain/sifnode/x/clp/types"

"github.com/Sifchain/sifnode/x/clp/types/mocks"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
Expand All @@ -19,7 +20,7 @@ import (
tmdb "github.com/tendermint/tm-db"
)

func ClpKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
func ClpKeeper(t testing.TB) (*keeper.Keeper, sdk.Context, *mocks.BankKeeper) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

Expand All @@ -38,10 +39,13 @@ func ClpKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
memStoreKey,
"ClpParams",
)

bankKeeper := mocks.NewBankKeeper(t)

k := keeper.NewKeeper(
cdc,
storeKey,
nil,
bankKeeper,
nil,
nil,
nil,
Expand All @@ -55,5 +59,5 @@ func ClpKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
// Initialize params
k.SetParams(ctx, types.DefaultParams())

return &k, ctx
return &k, ctx, bankKeeper
}
13 changes: 13 additions & 0 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sample

import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// AccAddress returns a sample account address
func AccAddress() string {
pk := ed25519.GenPrivKey().PubKey()
addr := pk.Address()
return sdk.AccAddress(addr).String()
}
1 change: 1 addition & 0 deletions x/clp/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func GetTxCmd() *cobra.Command {
GetCmdModifyLiquidityProtectionRates(),
GetCmdSetProviderDistributionPeriods(),
GetCmdSetSwapFeeParams(),
CmdAddLiquidityToRewardsBucket(),
)

return clpTxCmd
Expand Down
47 changes: 47 additions & 0 deletions x/clp/client/cli/tx_add_liquidity_to_rewards_bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cli

import (
"strconv"

"github.com/Sifchain/sifnode/x/clp/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
)

var _ = strconv.Itoa(0)

func CmdAddLiquidityToRewardsBucket() *cobra.Command {
cmd := &cobra.Command{
Use: "add-liquidity-to-rewards-bucket [amount]",
Example: "sifnodecli tx clp add-liquidity-to-rewards-bucket 100000000000000000000000000rowan,100000000000000000000000000ceth,100000000cusdc",
Short: "To add liquidity to the rewards bucket",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argAmount, err := sdk.ParseCoinsNormalized(args[0])
if err != nil {
return err
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgAddLiquidityToRewardsBucketRequest(
clientCtx.GetFromAddress().String(),
argAmount,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
45 changes: 42 additions & 3 deletions x/clp/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func registerTxRoutes(cliCtx client.Context, r *mux.Router) {
"/clp/decommissionPool",
decommissionPoolHandler(cliCtx),
).Methods("POST")
r.HandleFunc(
"/clp/addLiquidityToRewardsBucket",
addLiquidityToRewardsBucketHandler(cliCtx),
).Methods("POST")
}

type (
Expand Down Expand Up @@ -84,11 +88,16 @@ type (
SentAmount sdk.Uint `json:"sent_amount"` // Amount of SentAsset being sent
MinReceivingAmount sdk.Uint `json:"min_receiving_amount"` // Min amount specified by the user m the swap will not go through if the receiving amount drops below this value
}
AddLiquidityToRewardsBucketReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Signer string `json:"signer"` // User who is trying to add liquidity to the pool
Amounts sdk.Coins `json:"amounts"` // Amounts of liquidity to add to rewards bucket
}
)

// wallet < - > abci <-mempool-> tendermint
// storage > tx
// /tx hash= []
// wallet < - > abci <-mempool-> tendermint
// storage > tx
// /tx hash= []
func createPoolHandler(cliCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req CreatePoolReq
Expand Down Expand Up @@ -269,3 +278,33 @@ func swapHandler(cliCtx client.Context) http.HandlerFunc {
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, &msg)
}
}

func addLiquidityToRewardsBucketHandler(cliCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req AddLiquidityToRewardsBucketReq
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
return
}

req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}

signer, err := sdk.AccAddressFromBech32(req.Signer)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

msg := types.NewMsgAddLiquidityToRewardsBucketRequest(signer.String(), req.Amounts)
err = msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}
2 changes: 1 addition & 1 deletion x/clp/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestGenesis(t *testing.T) {
},
}

k, ctx := keepertest.ClpKeeper(t)
k, ctx, _ := keepertest.ClpKeeper(t)
clp.InitGenesis(ctx, *k, genesisState)
got := clp.ExportGenesis(ctx, *k)
require.NotNil(t, got)
Expand Down
3 changes: 3 additions & 0 deletions x/clp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgUpdateSwapFeeParamsRequest:
res, err := msgServer.UpdateSwapFeeParams(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgAddLiquidityToRewardsBucketRequest:
res, err := msgServer.AddLiquidityToRewardsBucket(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, errors.Wrap(errors.ErrUnknownRequest, errMsg)
Expand Down
29 changes: 29 additions & 0 deletions x/clp/keeper/add_liquidity_to_rewards_bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package keeper

import (
"github.com/Sifchain/sifnode/x/clp/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) AddLiquidityToRewardsBucket(ctx sdk.Context, signer string, amounts sdk.Coins) (sdk.Coins, error) {
// check that the sender has all the coins in the wallet
for _, coin := range amounts {
if !k.bankKeeper.HasBalance(ctx, sdk.AccAddress(signer), coin) {
return nil, types.ErrBalanceNotAvailable
}
}

// send from user to module
err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sdk.AccAddress(signer), types.ModuleName, amounts)
if err != nil {
return nil, err
}

// add multiple coins to rewards buckets
addedCoins, err := k.AddMultipleCoinsToRewardsBuckets(ctx, amounts)
if err != nil {
return nil, err
}

return addedCoins, nil
}
Loading

0 comments on commit 2e0180f

Please sign in to comment.