Skip to content

Commit

Permalink
Add tastcase for softfork1, 2 and Fix variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsam committed May 5, 2021
1 parent 48ad49c commit 2a929b6
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 33 deletions.
10 changes: 5 additions & 5 deletions x/liquidity/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import (
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
//// SoftFork example
//if ctx.BlockHeight() == types.Airdrop1SoftForkTargetHeight {
// k.SoftForkAirdrop(ctx, types.Airdrop1ProviderAddr, types.Airdrop1TargetAddrs, types.Airdrop1DistributionCoin)
//if ctx.BlockHeight() == types.AirdropE1SoftForkTargetHeight {
// k.SoftForkAirdrop(ctx, types.AirdropE1ProviderAddr, types.AirdropE1TargetAddrs, types.AirdropE1DistributionCoin)
//}
//// SoftForkMultipleCoins example
//if ctx.BlockHeight() == types.Airdrop2SoftForkTargetHeight {
// err := k.SoftForkAirdropMultiCoins(ctx, types.Airdrop2ProviderAddr, types.Airdrop2Pairs)
//if ctx.BlockHeight() == types.AirdropE2SoftForkTargetHeight {
// err := k.SoftForkAirdropMultiCoins(ctx, types.AirdropE2ProviderAddr, types.AirdropE2Pairs)
// if err != nil {
// ctx.Logger().Error("#### softfork failed", err)
// }else {
// ctx.Logger().Info("#### softfork completed", types.Airdrop2Pairs)
// ctx.Logger().Info("#### softfork completed", types.AirdropE2Pairs)
// }
//}
if ctx.BlockHeight() == types.Airdrop1SoftForkTargetHeight {
Expand Down
1 change: 1 addition & 0 deletions x/liquidity/keeper/softfork.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (k Keeper) SoftForkAirdropMultiCoins(ctx sdk.Context, providerAddr string,
return fmt.Errorf("insufficient balances of provider account for softfork distribution")
}
for _, pair := range airdropPairs {
fmt.Println(pair)
err := k.bankKeeper.SendCoins(cachedCtx, providerAcc, pair.TargetAcc, pair.DistributionCoins)
if err != nil {
return err
Expand Down
231 changes: 218 additions & 13 deletions x/liquidity/keeper/softfork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ func TestSoftForkAirdrop(t *testing.T) {

providerAcc, err := sdk.AccAddressFromBech32(types.Airdrop1ProviderAddr)
require.Nil(t, err)
totalDistribution := sdk.NewCoin(types.Airdrop1DistributionCoin.Denom, types.Airdrop1DistributionCoin.Amount.MulRaw(int64(len(types.Airdrop1TargetAddrs))))
totalDistribution := sdk.NewCoin(types.AirdropE1DistributionCoin.Denom, types.AirdropE1DistributionCoin.Amount.MulRaw(int64(len(types.AirdropE1TargetAddrs))))
lapp.SaveAccount(simapp, ctx, providerAcc, sdk.Coins{totalDistribution})

// Airdrop1 Success Case
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceBeforeDistribution := simapp.BankKeeper.GetBalance(ctx, providerAcc, types.Airdrop1DistributionCoin.Denom)
err = simapp.LiquidityKeeper.SoftForkAirdrop(ctx, types.Airdrop1ProviderAddr, types.Airdrop1TargetAddrs, types.Airdrop1DistributionCoin)
providerBalanceBeforeDistribution := simapp.BankKeeper.GetBalance(ctx, providerAcc, types.AirdropE1DistributionCoin.Denom)
err = simapp.LiquidityKeeper.SoftForkAirdrop(ctx, types.Airdrop1ProviderAddr, types.AirdropE1TargetAddrs, types.AirdropE1DistributionCoin)
require.NoError(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceAfterDistribution := simapp.BankKeeper.GetBalance(ctx, providerAcc, types.Airdrop1DistributionCoin.Denom)
providerBalanceAfterDistribution := simapp.BankKeeper.GetBalance(ctx, providerAcc, types.AirdropE1DistributionCoin.Denom)
require.Equal(t, providerBalanceBeforeDistribution.Sub(providerBalanceAfterDistribution), totalDistribution)
for _, addr := range types.Airdrop1TargetAddrs {
for _, addr := range types.AirdropE1TargetAddrs {
acc, _ := sdk.AccAddressFromBech32(addr)
balance := simapp.BankKeeper.GetBalance(ctx, acc, types.Airdrop1DistributionCoin.Denom)
require.Equal(t, balance, types.Airdrop1DistributionCoin)
balance := simapp.BankKeeper.GetBalance(ctx, acc, types.AirdropE1DistributionCoin.Denom)
require.Equal(t, balance, types.AirdropE1DistributionCoin)
}

// Airdrop1 Fail Case, insufficient balances of provider account for softfork distribution
bankState := simapp.BankKeeper.ExportGenesis(ctx)
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceBeforeDistribution = simapp.BankKeeper.GetBalance(ctx, providerAcc, types.Airdrop1DistributionCoin.Denom)
err = simapp.LiquidityKeeper.SoftForkAirdrop(ctx, types.Airdrop1ProviderAddr, types.Airdrop1TargetAddrs, types.Airdrop1DistributionCoin)
providerBalanceBeforeDistribution = simapp.BankKeeper.GetBalance(ctx, providerAcc, types.AirdropE1DistributionCoin.Denom)
err = simapp.LiquidityKeeper.SoftForkAirdrop(ctx, types.Airdrop1ProviderAddr, types.AirdropE1TargetAddrs, types.AirdropE1DistributionCoin)
require.Error(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
// assert no changes
Expand All @@ -88,8 +88,8 @@ func TestSoftForkAirdrop(t *testing.T) {
lapp.SaveAccount(simapp, ctx, providerAcc, sdk.Coins{totalDistribution})
bankState = simapp.BankKeeper.ExportGenesis(ctx)
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceBeforeDistribution = simapp.BankKeeper.GetBalance(ctx, providerAcc, types.Airdrop1DistributionCoin.Denom)
err = simapp.LiquidityKeeper.SoftForkAirdrop(ctx, "cosmos1...wrongAddr", types.Airdrop1TargetAddrs, types.Airdrop1DistributionCoin)
providerBalanceBeforeDistribution = simapp.BankKeeper.GetBalance(ctx, providerAcc, types.AirdropE1DistributionCoin.Denom)
err = simapp.LiquidityKeeper.SoftForkAirdrop(ctx, "cosmos1...wrongAddr", types.AirdropE1TargetAddrs, types.AirdropE1DistributionCoin)
require.Error(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
// assert no changes
Expand All @@ -100,8 +100,8 @@ func TestSoftForkAirdrop(t *testing.T) {
lapp.SaveAccount(simapp, ctx, providerAcc, sdk.Coins{totalDistribution})
bankState = simapp.BankKeeper.ExportGenesis(ctx)
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceBeforeDistribution = simapp.BankKeeper.GetBalance(ctx, providerAcc, types.Airdrop1DistributionCoin.Denom)
err = simapp.LiquidityKeeper.SoftForkAirdrop(ctx, types.Airdrop1ProviderAddr, []string{""}, types.Airdrop1DistributionCoin)
providerBalanceBeforeDistribution = simapp.BankKeeper.GetBalance(ctx, providerAcc, types.AirdropE1DistributionCoin.Denom)
err = simapp.LiquidityKeeper.SoftForkAirdrop(ctx, types.Airdrop1ProviderAddr, []string{""}, types.AirdropE1DistributionCoin)
require.Error(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
// assert no changes
Expand Down Expand Up @@ -211,3 +211,208 @@ func TestSoftForkAirdropMultiCoins(t *testing.T) {
}
}

func TestSoftFork1AirdropMultiCoins(t *testing.T) {
simapp, ctx := createTestInput()
simapp.LiquidityKeeper.SetParams(ctx, types.DefaultParams())
params := simapp.LiquidityKeeper.GetParams(ctx)

poolTypeId := types.DefaultPoolTypeId
addrs := lapp.AddTestAddrs(simapp, ctx, 3, params.PoolCreationFee)

denomA := "uatom"
denomB := "utest"
denomA, denomB = types.AlphabeticalDenomPair(denomA, denomB)

deposit := sdk.NewCoins(sdk.NewCoin(denomA, sdk.NewInt(100*1000000)), sdk.NewCoin(denomB, sdk.NewInt(2000*1000000)))
lapp.SaveAccount(simapp, ctx, addrs[0], deposit)

depositA := simapp.BankKeeper.GetBalance(ctx, addrs[0], denomA)
depositB := simapp.BankKeeper.GetBalance(ctx, addrs[0], denomB)
depositBalance := sdk.NewCoins(depositA, depositB)

require.Equal(t, deposit, depositBalance)

msg := types.NewMsgCreatePool(addrs[0], poolTypeId, depositBalance)
_, err := simapp.LiquidityKeeper.CreatePool(ctx, msg)
require.NoError(t, err)

invalidMsg := types.NewMsgCreatePool(addrs[0], 0, depositBalance)
_, err = simapp.LiquidityKeeper.CreatePool(ctx, invalidMsg)
require.Error(t, err, types.ErrBadPoolTypeId)

pools := simapp.LiquidityKeeper.GetAllPools(ctx)
require.Equal(t, 1, len(pools))
require.Equal(t, uint64(1), pools[0].Id)
require.Equal(t, uint64(1), simapp.LiquidityKeeper.GetNextPoolId(ctx)-1)
require.Equal(t, denomA, pools[0].ReserveCoinDenoms[0])
require.Equal(t, denomB, pools[0].ReserveCoinDenoms[1])

poolCoin := simapp.LiquidityKeeper.GetPoolCoinTotalSupply(ctx, pools[0])
creatorBalance := simapp.BankKeeper.GetBalance(ctx, addrs[0], pools[0].PoolCoinDenom)
require.Equal(t, poolCoin, creatorBalance.Amount)

_, err = simapp.LiquidityKeeper.CreatePool(ctx, msg)
require.Error(t, err, types.ErrPoolAlreadyExists)

providerAcc, err := sdk.AccAddressFromBech32(types.Airdrop1ProviderAddr)
require.Nil(t, err)

totalDistributionCoins := sdk.NewCoins()
for _, pair := range types.Airdrop1Pairs {
totalDistributionCoins = totalDistributionCoins.Add(pair.DistributionCoins...)
}

// Airdrop1 Fail Case, insufficient balances of provider account for softfork distribution
bankState := simapp.BankKeeper.ExportGenesis(ctx)
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
err = simapp.LiquidityKeeper.SoftForkAirdropMultiCoins(ctx, types.Airdrop1ProviderAddr, types.Airdrop1Pairs)
require.Error(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
// assert no changes
bankStateAfterFail := simapp.BankKeeper.ExportGenesis(ctx)
require.Equal(t, bankState, bankStateAfterFail)

// Airdrop1 Fail Case, wrong address
lapp.SaveAccount(simapp, ctx, providerAcc, totalDistributionCoins)
bankState = simapp.BankKeeper.ExportGenesis(ctx)
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceBeforeDistribution := simapp.BankKeeper.GetAllBalances(ctx, providerAcc)
err = simapp.LiquidityKeeper.SoftForkAirdropMultiCoins(ctx, "cosmos1...wrongAddr", types.Airdrop1Pairs)
require.Error(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
// assert no changes
bankStateAfterFail = simapp.BankKeeper.ExportGenesis(ctx)
require.Equal(t, bankState, bankStateAfterFail)

// Airdrop1 Fail Case, wrong address, invalid coins
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceBeforeDistribution = simapp.BankKeeper.GetAllBalances(ctx, providerAcc)
Airdrop1PairsFailCase1 := []types.AirdropPair {
{"cosmos1w7xdwdllma6y2xhxwl3peurymx0tr95mk8urfp", nil, sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(100_000_000)), sdk.NewCoin("stake", sdk.NewInt(50_000_000)))},
{"cosmos1uu9twaqca5f28ltdzqjlnklys4wcv97ke4038j", nil, sdk.NewCoins()},
}
err = simapp.LiquidityKeeper.SoftForkAirdropMultiCoins(ctx, types.Airdrop1ProviderAddr, Airdrop1PairsFailCase1)
require.Error(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
// assert no changes
bankStateAfterFail = simapp.BankKeeper.ExportGenesis(ctx)
require.Equal(t, bankState, bankStateAfterFail)

// Airdrop1 Success Case
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceBeforeDistribution = simapp.BankKeeper.GetAllBalances(ctx, providerAcc)
err = simapp.LiquidityKeeper.SoftForkAirdropMultiCoins(ctx, types.Airdrop1ProviderAddr, types.Airdrop1Pairs)
require.NoError(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceAfterDistribution := simapp.BankKeeper.GetAllBalances(ctx, providerAcc)
require.Equal(t, providerBalanceBeforeDistribution.Sub(providerBalanceAfterDistribution), totalDistributionCoins)
for _, pair := range types.Airdrop1Pairs {
balances := simapp.BankKeeper.GetAllBalances(ctx, pair.TargetAcc)
require.NotNil(t, pair.TargetAcc)
require.True(t, balances.IsAllGTE(pair.DistributionCoins))
}
}


func TestSoftFork2AirdropMultiCoins(t *testing.T) {
simapp, ctx := createTestInput()
simapp.LiquidityKeeper.SetParams(ctx, types.DefaultParams())
params := simapp.LiquidityKeeper.GetParams(ctx)

poolTypeId := types.DefaultPoolTypeId
addrs := lapp.AddTestAddrs(simapp, ctx, 3, params.PoolCreationFee)

denomA := "uatom"
denomB := "utest"
denomA, denomB = types.AlphabeticalDenomPair(denomA, denomB)

deposit := sdk.NewCoins(sdk.NewCoin(denomA, sdk.NewInt(100*1000000)), sdk.NewCoin(denomB, sdk.NewInt(2000*1000000)))
lapp.SaveAccount(simapp, ctx, addrs[0], deposit)

depositA := simapp.BankKeeper.GetBalance(ctx, addrs[0], denomA)
depositB := simapp.BankKeeper.GetBalance(ctx, addrs[0], denomB)
depositBalance := sdk.NewCoins(depositA, depositB)

require.Equal(t, deposit, depositBalance)

msg := types.NewMsgCreatePool(addrs[0], poolTypeId, depositBalance)
_, err := simapp.LiquidityKeeper.CreatePool(ctx, msg)
require.NoError(t, err)

invalidMsg := types.NewMsgCreatePool(addrs[0], 0, depositBalance)
_, err = simapp.LiquidityKeeper.CreatePool(ctx, invalidMsg)
require.Error(t, err, types.ErrBadPoolTypeId)

pools := simapp.LiquidityKeeper.GetAllPools(ctx)
require.Equal(t, 1, len(pools))
require.Equal(t, uint64(1), pools[0].Id)
require.Equal(t, uint64(1), simapp.LiquidityKeeper.GetNextPoolId(ctx)-1)
require.Equal(t, denomA, pools[0].ReserveCoinDenoms[0])
require.Equal(t, denomB, pools[0].ReserveCoinDenoms[1])

poolCoin := simapp.LiquidityKeeper.GetPoolCoinTotalSupply(ctx, pools[0])
creatorBalance := simapp.BankKeeper.GetBalance(ctx, addrs[0], pools[0].PoolCoinDenom)
require.Equal(t, poolCoin, creatorBalance.Amount)

_, err = simapp.LiquidityKeeper.CreatePool(ctx, msg)
require.Error(t, err, types.ErrPoolAlreadyExists)

providerAcc, err := sdk.AccAddressFromBech32(types.Airdrop2ProviderAddr)
require.Nil(t, err)

totalDistributionCoins := sdk.NewCoins()
for _, pair := range types.Airdrop2Pairs {
totalDistributionCoins = totalDistributionCoins.Add(pair.DistributionCoins...)
}

// Airdrop2 Fail Case, insufficient balances of provider account for softfork distribution
bankState := simapp.BankKeeper.ExportGenesis(ctx)
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
err = simapp.LiquidityKeeper.SoftForkAirdropMultiCoins(ctx, types.Airdrop2ProviderAddr, types.Airdrop2Pairs)
require.Error(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
// assert no changes
bankStateAfterFail := simapp.BankKeeper.ExportGenesis(ctx)
require.Equal(t, bankState, bankStateAfterFail)

// Airdrop2 Fail Case, wrong address
lapp.SaveAccount(simapp, ctx, providerAcc, totalDistributionCoins)
bankState = simapp.BankKeeper.ExportGenesis(ctx)
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceBeforeDistribution := simapp.BankKeeper.GetAllBalances(ctx, providerAcc)
err = simapp.LiquidityKeeper.SoftForkAirdropMultiCoins(ctx, "cosmos1...wrongAddr", types.Airdrop2Pairs)
require.Error(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
// assert no changes
bankStateAfterFail = simapp.BankKeeper.ExportGenesis(ctx)
require.Equal(t, bankState, bankStateAfterFail)

// Airdrop2 Fail Case, wrong address, invalid coins
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceBeforeDistribution = simapp.BankKeeper.GetAllBalances(ctx, providerAcc)
airdrop2PairsFailCase1 := []types.AirdropPair {
{"cosmos1w7xdwdllma6y2xhxwl3peurymx0tr95mk8urfp", nil, sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(100_000_000)), sdk.NewCoin("stake", sdk.NewInt(50_000_000)))},
{"cosmos1uu9twaqca5f28ltdzqjlnklys4wcv97ke4038j", nil, sdk.NewCoins()},
}
err = simapp.LiquidityKeeper.SoftForkAirdropMultiCoins(ctx, types.Airdrop2ProviderAddr, airdrop2PairsFailCase1)
require.Error(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
// assert no changes
bankStateAfterFail = simapp.BankKeeper.ExportGenesis(ctx)
require.Equal(t, bankState, bankStateAfterFail)

// Airdrop2 Success Case
liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceBeforeDistribution = simapp.BankKeeper.GetAllBalances(ctx, providerAcc)
err = simapp.LiquidityKeeper.SoftForkAirdropMultiCoins(ctx, types.Airdrop2ProviderAddr, types.Airdrop2Pairs)
require.NoError(t, err)
liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
providerBalanceAfterDistribution := simapp.BankKeeper.GetAllBalances(ctx, providerAcc)
require.Equal(t, providerBalanceBeforeDistribution.Sub(providerBalanceAfterDistribution), totalDistributionCoins)
for _, pair := range types.Airdrop2Pairs {
balances := simapp.BankKeeper.GetAllBalances(ctx, pair.TargetAcc)
require.NotNil(t, pair.TargetAcc)
require.True(t, balances.IsAllGTE(pair.DistributionCoins))
}
}

30 changes: 15 additions & 15 deletions x/liquidity/types/softfork.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ type AirdropPair struct {

var (
//// variables of example case for airdrop softfork
//Airdrop1SoftForkTargetHeight = int64(20000)
//Airdrop1ProviderAddr = "cosmos1f8s3n4lmlrancdrnnaky0j464prdr58d835yx2"
//Airdrop1DistributionCoin = sdk.NewCoin("uatom", sdk.NewInt(100_000_000))
//Airdrop1TargetAddrs = []string {
// "cosmos1w7xdwdllma6y2xhxwl3peurymx0tr95mk8urfp",
// "cosmos1uu9twaqca5f28ltdzqjlnklys4wcv97ke4038j",
// //"cosmos1...",
//}
//
AirdropE1SoftForkTargetHeight = int64(20000)
AirdropE1ProviderAddr = "cosmos1f8s3n4lmlrancdrnnaky0j464prdr58d835yx2"
AirdropE1DistributionCoin = sdk.NewCoin("uatom", sdk.NewInt(100_000_000))
AirdropE1TargetAddrs = []string {
"cosmos1w7xdwdllma6y2xhxwl3peurymx0tr95mk8urfp",
"cosmos1uu9twaqca5f28ltdzqjlnklys4wcv97ke4038j",
//"cosmos1...",
}

//// variables of example case for softfork airdrop with multiCoins
//Airdrop2SoftForkTargetHeight = int64(30000)
//Airdrop2ProviderAddr = "cosmos1f8s3n4lmlrancdrnnaky0j464prdr58d835yx2"
//Airdrop2Pairs = []AirdropPair {
// {"cosmos1w7xdwdllma6y2xhxwl3peurymx0tr95mk8urfp", nil, sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(100_000_000)), sdk.NewCoin("utest", sdk.NewInt(50_000_000)))},
// {"cosmos1uu9twaqca5f28ltdzqjlnklys4wcv97ke4038j", nil, sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(50_000_000)), sdk.NewCoin("utest", sdk.NewInt(100_000_000)))},
//}
AirdropE2SoftForkTargetHeight = int64(30000)
AirdropE2ProviderAddr = "cosmos1f8s3n4lmlrancdrnnaky0j464prdr58d835yx2"
AirdropE2Pairs = []AirdropPair {
{"cosmos1w7xdwdllma6y2xhxwl3peurymx0tr95mk8urfp", nil, sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(100_000_000)), sdk.NewCoin("utest", sdk.NewInt(50_000_000)))},
{"cosmos1uu9twaqca5f28ltdzqjlnklys4wcv97ke4038j", nil, sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(50_000_000)), sdk.NewCoin("utest", sdk.NewInt(100_000_000)))},
}

// variables for softfork airdrop to add ommitted participants
Airdrop1SoftForkTargetHeight = int64(31100)
Expand Down

0 comments on commit 2a929b6

Please sign in to comment.