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

refactor: service params migration #363

Merged
merged 4 commits into from
Jun 15, 2023
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
10 changes: 5 additions & 5 deletions modules/coinswap/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"

v1 "github.com/irisnet/irismod/modules/coinswap/migrations/v1"
v2 "github.com/irisnet/irismod/modules/coinswap/migrations/v2"
v3 "github.com/irisnet/irismod/modules/coinswap/migrations/v3"
v4 "github.com/irisnet/irismod/modules/coinswap/migrations/v4"
v5 "github.com/irisnet/irismod/modules/coinswap/migrations/v5"
"github.com/irisnet/irismod/types/exported"
)

Expand All @@ -23,20 +23,20 @@ func NewMigrator(k Keeper, legacySubspace exported.Subspace) Migrator {

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v1.Migrate(ctx, m.k, m.k.bk, m.k.ak)
return v2.Migrate(ctx, m.k, m.k.bk, m.k.ak)
}

// Migrate1to2 migrates from version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v2.Migrate(ctx, m.k, m.legacySubspace)
return v3.Migrate(ctx, m.k, m.legacySubspace)
}

// Migrate1to2 migrates from version 3 to 4.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v3.Migrate(ctx, m.k, m.legacySubspace)
return v4.Migrate(ctx, m.k, m.legacySubspace)
}

// Migrate1to2 migrates from version 4 to 5.
func (m Migrator) Migrate4to5(ctx sdk.Context) error {
return v4.Migrate(ctx, m.k, m.legacySubspace)
return v5.Migrate(ctx, m.k, m.legacySubspace)
}
110 changes: 0 additions & 110 deletions modules/coinswap/migrations/v1/migrate.go

This file was deleted.

125 changes: 92 additions & 33 deletions modules/coinswap/migrations/v2/migrate.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,110 @@
package v2

import (
sdkmath "cosmossdk.io/math"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/irisnet/irismod/modules/coinswap/types"
"github.com/irisnet/irismod/types/exported"
coinswaptypes "github.com/irisnet/irismod/modules/coinswap/types"
)

// Parameter store keys
var (
KeyFee = []byte("Fee") // fee key
DefaultPoolCreationFee = sdk.NewCoin("uiris", sdkmath.NewIntWithDecimal(5000, 6))
DefaultTaxRate = sdk.NewDecWithPrec(4, 1)
)
type CoinswapKeeper interface {
GetStandardDenom(ctx sdk.Context) string
CreatePool(ctx sdk.Context, counterpartyDenom string) coinswaptypes.Pool
}

type (
CoinswapKeeper interface {
GetParams(ctx sdk.Context) types.Params
SetParams(ctx sdk.Context, params types.Params) error
func Migrate(ctx sdk.Context,
k CoinswapKeeper,
bk coinswaptypes.BankKeeper,
ak coinswaptypes.AccountKeeper,
) error {
// 1. Query all current liquidity tokens
var lptDenoms []string
bk.IterateTotalSupply(ctx, func(coin sdk.Coin) bool {
if strings.HasPrefix(coin.GetDenom(), FormatUniABSPrefix) {
lptDenoms = append(lptDenoms, coin.GetDenom())
}
return false
})

// 2. Create a new liquidity pool based on the results of the first step
standardDenom := k.GetStandardDenom(ctx)
var pools = make(map[string]coinswaptypes.Pool, len(lptDenoms))
for _, ltpDenom := range lptDenoms {
counterpartyDenom := strings.TrimPrefix(ltpDenom, FormatUniABSPrefix)
pools[ltpDenom] = k.CreatePool(ctx, counterpartyDenom)
//3. Transfer tokens from the old liquidity to the newly created liquidity pool
if err := migratePool(ctx, bk, pools[ltpDenom], ltpDenom, standardDenom); err != nil {
return err
}
}

Params struct {
Fee sdk.Dec `protobuf:"bytes,1,opt,name=fee,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"fee"`
// 4. Traverse all accounts and modify the old liquidity token to the new liquidity token
ak.IterateAccounts(ctx, func(account authtypes.AccountI) (stop bool) {
balances := bk.GetAllBalances(ctx, account.GetAddress())
for _, ltpDenom := range lptDenoms {
amount := balances.AmountOf(ltpDenom)
if amount.IsZero() {
continue
}
originLptCoin := sdk.NewCoin(ltpDenom, amount)
err := migrateProvider(ctx, originLptCoin, bk, pools[ltpDenom], account.GetAddress())
if err != nil {
panic(err)
}
}
return false
})
return nil
}

func migrateProvider(ctx sdk.Context,
originLptCoin sdk.Coin,
bk coinswaptypes.BankKeeper,
pool coinswaptypes.Pool,
provider sdk.AccAddress,
) error {
//1. Burn the old liquidity tokens
burnCoins := sdk.NewCoins(originLptCoin)
// send liquidity vouchers to be burned from sender account to module account
if err := bk.SendCoinsFromAccountToModule(ctx, provider, coinswaptypes.ModuleName, burnCoins); err != nil {
return err
}
// burn liquidity vouchers of reserve pool from module account
if err := bk.BurnCoins(ctx, coinswaptypes.ModuleName, burnCoins); err != nil {
return err
}
)

func Migrate(ctx sdk.Context, k CoinswapKeeper, paramSpace exported.Subspace) error {
params := GetLegacyParams(ctx, paramSpace)
newParams := types.Params{
Fee: params.Fee,
PoolCreationFee: DefaultPoolCreationFee,
TaxRate: DefaultTaxRate,
//2. Issue new liquidity tokens
mintToken := sdk.NewCoin(pool.LptDenom, originLptCoin.Amount)
mintTokens := sdk.NewCoins(mintToken)
if err := bk.MintCoins(ctx, coinswaptypes.ModuleName, mintTokens); err != nil {
return err
}
return k.SetParams(ctx, newParams)
}

// GetLegacyParams gets the parameters for the coinswap module.
func GetLegacyParams(ctx sdk.Context, paramSpace exported.Subspace) Params {
var swapParams Params
paramSpace.GetParamSet(ctx, &swapParams)
return swapParams
return bk.SendCoinsFromModuleToAccount(ctx, coinswaptypes.ModuleName, provider, mintTokens)
}

// ParamSetPairs implements paramtypes.KeyValuePairs
func (p *Params) ParamSetPairs() exported.ParamSetPairs {
return exported.ParamSetPairs{
exported.NewParamSetPair(KeyFee, &p.Fee, nil),
func migratePool(ctx sdk.Context,
bk coinswaptypes.BankKeeper,
pool coinswaptypes.Pool,
ltpDenom, standardDenom string,
) error {
counterpartyDenom := strings.TrimPrefix(ltpDenom, FormatUniABSPrefix)
originPoolAddress := GetReservePoolAddr(ltpDenom)

//Query the amount of the original liquidity pool account
originPoolBalances := bk.GetAllBalances(ctx, originPoolAddress)
transferCoins := sdk.NewCoins(
sdk.NewCoin(standardDenom, originPoolBalances.AmountOf(standardDenom)),
sdk.NewCoin(counterpartyDenom, originPoolBalances.AmountOf(counterpartyDenom)),
)

dstPoolAddress, err := sdk.AccAddressFromBech32(pool.EscrowAddress)
if err != nil {
return err
}

return bk.SendCoins(ctx, originPoolAddress, dstPoolAddress, transferCoins)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1_test
package v2_test

import (
"testing"
Expand All @@ -12,7 +12,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

v1 "github.com/irisnet/irismod/modules/coinswap/migrations/v1"
v2 "github.com/irisnet/irismod/modules/coinswap/migrations/v2"
coinswaptypes "github.com/irisnet/irismod/modules/coinswap/types"
"github.com/irisnet/irismod/simapp"
)
Expand All @@ -27,8 +27,8 @@ const (
var (
addrSender1 = sdk.AccAddress(tmhash.SumTruncated([]byte("addrSender1")))
addrSender2 = sdk.AccAddress(tmhash.SumTruncated([]byte("addrSender2")))
poolAddrBTC = v1.GetReservePoolAddr(denomLptBTC)
poolAddrETH = v1.GetReservePoolAddr(denomLptETH)
poolAddrBTC = v2.GetReservePoolAddr(denomLptBTC)
poolAddrETH = v2.GetReservePoolAddr(denomLptETH)
denomStandard = sdk.DefaultBondDenom
)

Expand All @@ -42,7 +42,7 @@ func TestMigrate(t *testing.T) {
})
app, verify := setupWithGenesisAccounts(t)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
err := v1.Migrate(ctx, app.CoinswapKeeper, app.BankKeeper, app.AccountKeeper)
err := v2.Migrate(ctx, app.CoinswapKeeper, app.BankKeeper, app.AccountKeeper)
assert.NoError(t, err)

//app.BaseApp.Commit()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package v2

import (
"fmt"
Expand Down
22 changes: 9 additions & 13 deletions modules/coinswap/migrations/v3/migrate.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package v3

import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/irisnet/irismod/modules/coinswap/types"
"github.com/irisnet/irismod/types/exported"
)

// Parameter store keys
var (
KeyFee = []byte("Fee")
KeyPoolCreationFee = []byte("PoolCreationFee")
KeyTaxRate = []byte("TaxRate")
UnilateralLiquidityFee = sdk.NewDecWithPrec(2, 3)
KeyFee = []byte("Fee") // fee key
DefaultPoolCreationFee = sdk.NewCoin("uiris", sdkmath.NewIntWithDecimal(5000, 6))
DefaultTaxRate = sdk.NewDecWithPrec(4, 1)
)

type (
Expand All @@ -21,19 +22,16 @@ type (
}

Params struct {
Fee sdk.Dec `protobuf:"bytes,1,opt,name=fee,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"fee"`
PoolCreationFee sdk.Coin `protobuf:"bytes,2,opt,name=pool_creation_fee,json=poolCreationFee,proto3" json:"pool_creation_fee"`
TaxRate sdk.Dec `protobuf:"bytes,3,opt,name=tax_rate,json=taxRate,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"tax_rate"`
Fee sdk.Dec `protobuf:"bytes,1,opt,name=fee,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"fee"`
}
)

func Migrate(ctx sdk.Context, k CoinswapKeeper, paramSpace exported.Subspace) error {
params := GetLegacyParams(ctx, paramSpace)
newParams := types.Params{
Fee: params.Fee,
PoolCreationFee: params.PoolCreationFee,
TaxRate: params.TaxRate,
UnilateralLiquidityFee: UnilateralLiquidityFee,
Fee: params.Fee,
PoolCreationFee: DefaultPoolCreationFee,
TaxRate: DefaultTaxRate,
}
return k.SetParams(ctx, newParams)
}
Expand All @@ -49,7 +47,5 @@ func GetLegacyParams(ctx sdk.Context, paramSpace exported.Subspace) Params {
func (p *Params) ParamSetPairs() exported.ParamSetPairs {
return exported.ParamSetPairs{
exported.NewParamSetPair(KeyFee, &p.Fee, nil),
exported.NewParamSetPair(KeyPoolCreationFee, &p.PoolCreationFee, nil),
exported.NewParamSetPair(KeyTaxRate, &p.TaxRate, nil),
}
}
Loading