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: implement some gamm related methods #3

Merged
merged 4 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
55 changes: 55 additions & 0 deletions proto/osmosis/gamm/v1beta1/pool.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
syntax = "proto3";
package osmosis.gamm.v1beta1;

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/c-osmosis/osmosis/x/gamm/types";

message Record {
string denormalizedWeight = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.moretags) = "yaml:\"denormalized_weight\"",
(gogoproto.nullable) = false
];
string balance = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"balance\"",
(gogoproto.nullable) = false
];
}

message LP {
string name = 1 [
(gogoproto.moretags) = "yaml:\"name\""
];
string totalSupply = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"total_supply\"",
(gogoproto.nullable) = false
];
}

message Pool {
uint64 id = 1 [
(gogoproto.moretags) = "yaml:\"id\""
];
string swapFee = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.moretags) = "yaml:\"swap_fee\"",
(gogoproto.nullable) = false
];
string totalWeight = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"total_weight\"",
(gogoproto.nullable) = false
];
LP token = 4 [
(gogoproto.moretags) = "yaml:\"token\"",
(gogoproto.nullable) = false
];
map<string, Record> records = 5 [
(gogoproto.moretags) = "yaml:\"records\"",
(gogoproto.nullable) = false
];
}
24 changes: 8 additions & 16 deletions proto/osmosis/gamm/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ option go_package = "github.com/c-osmosis/osmosis/x/gamm/types";

// ===================== MsgJoinPool
message MaxAmountIn {
cosmos.base.v1beta1.Coin token = 1 [
(gogoproto.moretags) = "yaml:\"token\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.nullable) = false
string denom = 1 [
(gogoproto.moretags) = "yaml:\"denom\""
];
string maxAmount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
Expand All @@ -26,8 +24,7 @@ message MsgJoinPool {
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress",
(gogoproto.moretags) = "yaml:\"sender\""
];
bytes targetPool = 2 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress",
uint64 targetPool = 2 [
(gogoproto.moretags) = "yaml:\"target_pool\""
];
string poolAmountOut = 3 [
Expand All @@ -44,10 +41,8 @@ message MsgJoinPool {

// ===================== MsgExitPool
message MinAmountOut {
cosmos.base.v1beta1.Coin token = 1 [
(gogoproto.moretags) = "yaml:\"token\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.nullable) = false
string denom = 1 [
(gogoproto.moretags) = "yaml:\"denom\""
];
string minAmount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
Expand All @@ -61,8 +56,7 @@ message MsgExitPool {
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress",
(gogoproto.moretags) = "yaml:\"sender\""
];
bytes targetPool = 2 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress",
uint64 targetPool = 2 [
(gogoproto.moretags) = "yaml:\"target_pool\""
];
string poolAmountIn = 3 [
Expand All @@ -79,10 +73,8 @@ message MsgExitPool {

// ===================== MsgCreatePool
message TokenInfo {
cosmos.base.v1beta1.Coin token = 1 [
(gogoproto.moretags) = "yaml:\"token\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.nullable) = false
string denom = 1 [
(gogoproto.moretags) = "yaml:\"denom\""
];
string ratio = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
Expand Down
24 changes: 17 additions & 7 deletions x/gamm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,35 @@ import (

var _ Keeper = (*keeper)(nil)

// type alias
type poolService = pool.Service

type Keeper interface {
pool.Service
poolService
}

type keeper struct {
pool.Service
poolService

// stores
poolStore pool.Store

cdc codec.BinaryMarshaler
storeKey sdk.StoreKey
// keepers
accountKeeper types.AccountKeeper
bankKeeper bankkeeper.Keeper
}

func NewKeeper(cdc codec.BinaryMarshaler, storeKey sdk.StoreKey, accountKeeper types.AccountKeeper, bankKeeper bankkeeper.Keeper) Keeper {
var (
poolStore = pool.NewStore(cdc, storeKey)
poolService = pool.NewService(poolStore, accountKeeper, bankKeeper)
)
return keeper{
Service: pool.NewService(cdc, storeKey, accountKeeper, bankKeeper),
// pool
poolService: poolService,
poolStore: poolStore,

cdc: cdc,
storeKey: storeKey,
// keepers
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
}
Expand Down
32 changes: 32 additions & 0 deletions x/gamm/keeper/pool/lp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pool

import (
"github.com/c-osmosis/osmosis/x/gamm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
)

type lp struct {
denom string
bankKeeper bankkeeper.Keeper
}

func (p lp) pushPoolShare(ctx sdk.Context, to sdk.AccAddress, amount sdk.Int) error {
lp := sdk.Coin{Denom: p.denom, Amount: amount}
return p.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, to, sdk.Coins{lp})
}

func (p lp) pullPoolShare(ctx sdk.Context, from sdk.AccAddress, amount sdk.Int) error {
lp := sdk.Coin{Denom: p.denom, Amount: amount}
return p.bankKeeper.SendCoinsFromAccountToModule(ctx, from, types.ModuleName, sdk.Coins{lp})
}

func (p lp) mintPoolShare(ctx sdk.Context, amount sdk.Int) error {
lp := sdk.Coin{Denom: p.denom, Amount: amount}
return p.bankKeeper.MintCoins(ctx, types.ModuleName, sdk.Coins{lp})
}

func (p lp) burnPoolShare(ctx sdk.Context, amount sdk.Int) error {
lp := sdk.Coin{Denom: p.denom, Amount: amount}
return p.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.Coins{lp})
}
14 changes: 0 additions & 14 deletions x/gamm/keeper/pool/pool.go

This file was deleted.

99 changes: 87 additions & 12 deletions x/gamm/keeper/pool/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,30 @@ package pool

import (
"github.com/c-osmosis/osmosis/x/gamm/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
)

type Service interface {
CreatePool(sdk.Context, sdk.AccAddress, sdk.Dec, []types.TokenInfo) error
JoinPool(sdk.Context, sdk.AccAddress, sdk.AccAddress, sdk.Int, []types.MaxAmountIn) error
ExitPool(sdk.Context, sdk.AccAddress, sdk.AccAddress, sdk.Int, []types.MinAmountOut) error
JoinPool(sdk.Context, sdk.AccAddress, uint64, sdk.Int, []types.MaxAmountIn) error
ExitPool(sdk.Context, sdk.AccAddress, uint64, sdk.Int, []types.MinAmountOut) error
}

type poolService struct {
cdc codec.BinaryMarshaler
storeKey sdk.StoreKey
store Store
accountKeeper types.AccountKeeper
bankKeeper bankkeeper.Keeper
}

func NewService(
cdc codec.BinaryMarshaler,
storeKey sdk.StoreKey,
store Store,
accountKeeper types.AccountKeeper,
bankKeeper bankkeeper.Keeper,
) Service {
return poolService{
cdc: cdc,
storeKey: storeKey,
store: store,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
}
Expand All @@ -40,23 +37,101 @@ func (p poolService) CreatePool(
swapFee sdk.Dec,
tokenInfo []types.TokenInfo,
) error {
return nil
records := make(map[string]types.Record, len(tokenInfo))
for _, info := range tokenInfo {
records[info.Denom] = types.Record{
DenormalizedWeight: info.Ratio,
Balance: info.Amount,
}
}

pool := types.Pool{
Id: p.store.GetNextPoolNumber(ctx),
SwapFee: swapFee,
TotalWeight: sdk.NewInt(0),
Records: records,
}

p.store.StorePool(ctx, pool)

var coins sdk.Coins
for denom, record := range records {
coins = append(coins, sdk.Coin{
Denom: denom,
Amount: record.Balance,
})
}

return p.bankKeeper.SendCoinsFromAccountToModule(
ctx,
sender,
types.ModuleName,
coins,
)
}

func (p poolService) JoinPool(
ctx sdk.Context,
sender sdk.AccAddress,
targetPool sdk.AccAddress,
targetPoolId uint64,
poolAmountOut sdk.Int,
maxAmountsIn []types.MaxAmountIn,
) error {
pool, err := p.store.FetchPool(ctx, targetPoolId)
if err != nil {
return err
}

poolTotal := pool.Token.TotalSupply.ToDec()
poolRatio := poolAmountOut.ToDec().Quo(poolTotal)
if poolRatio.Equal(sdk.NewDec(0)) {
return sdkerrors.Wrapf(types.ErrMathApprox, "calc poolRatio")
}

var sendTargets sdk.Coins
for _, maxAmountIn := range maxAmountsIn {
record := pool.Records[maxAmountIn.Denom]
tokenAmountIn := poolRatio.Mul(record.Balance.ToDec()).TruncateInt()
if tokenAmountIn.Equal(sdk.NewInt(0)) {
return sdkerrors.Wrapf(types.ErrMathApprox, "calc tokenAmountIn")
}
if tokenAmountIn.GT(maxAmountIn.MaxAmount) {
return sdkerrors.Wrapf(types.ErrLimitExceed, "limit exceeded")
}
record.Balance = record.Balance.Add(tokenAmountIn)
sendTargets = append(sendTargets, sdk.Coin{
Denom: maxAmountIn.Denom,
Amount: tokenAmountIn,
})
}

err = p.bankKeeper.SendCoinsFromAccountToModule(
ctx,
sender,
types.ModuleName,
sendTargets,
)
if err != nil {
return err
}

poolShare := lp{
denom: pool.Token.Name,
bankKeeper: p.bankKeeper,
}
if err := poolShare.mintPoolShare(ctx, poolAmountOut); err != nil {
return err
}
if err := poolShare.pushPoolShare(ctx, sender, poolAmountOut); err != nil {
return err
}
return nil
}

func (p poolService) ExitPool(
ctx sdk.Context,
sender sdk.AccAddress,
targetPool sdk.AccAddress,
targetPoolId uint64,
poolAmountIn sdk.Int,
minAmountsOut []types.MinAmountOut,
) error {
Expand Down
Loading