-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kenta-elys
committed
Dec 4, 2023
1 parent
25f1a33
commit 949c66b
Showing
4 changed files
with
131 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
simapp "github.com/elys-network/elys/app" | ||
ammtypes "github.com/elys-network/elys/x/amm/types" | ||
"github.com/elys-network/elys/x/incentive/types" | ||
ptypes "github.com/elys-network/elys/x/parameter/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAPRCalculationPerPool(t *testing.T) { | ||
app := simapp.InitElysTestApp(initChain) | ||
ctx := app.BaseApp.NewContext(initChain, tmproto.Header{}) | ||
|
||
ik, amm, oracle := app.IncentiveKeeper, app.AmmKeeper, app.OracleKeeper | ||
|
||
// Setup coin prices | ||
SetupStableCoinPrices(ctx, oracle) | ||
|
||
// Generate 1 random account with 1000stake balanced | ||
addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000)) | ||
|
||
// Create a pool | ||
// Mint 100000USDC | ||
usdcToken := sdk.NewCoins(sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(100000))) | ||
|
||
err := app.BankKeeper.MintCoins(ctx, ammtypes.ModuleName, usdcToken) | ||
require.NoError(t, err) | ||
err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ammtypes.ModuleName, addr[0], usdcToken) | ||
require.NoError(t, err) | ||
|
||
poolAssets := []ammtypes.PoolAsset{ | ||
{ | ||
Weight: sdk.NewInt(50), | ||
Token: sdk.NewCoin(ptypes.Elys, sdk.NewInt(100000)), | ||
}, | ||
{ | ||
Weight: sdk.NewInt(50), | ||
Token: sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(10000)), | ||
}, | ||
} | ||
|
||
argSwapFee := sdk.MustNewDecFromStr("0.0") | ||
argExitFee := sdk.MustNewDecFromStr("0.0") | ||
|
||
poolParams := &ammtypes.PoolParams{ | ||
SwapFee: argSwapFee, | ||
ExitFee: argExitFee, | ||
} | ||
|
||
msg := ammtypes.NewMsgCreatePool( | ||
addr[0].String(), | ||
poolParams, | ||
poolAssets, | ||
) | ||
|
||
// Create a Elys+USDC pool | ||
poolId, err := amm.CreatePool(ctx, msg) | ||
require.NoError(t, err) | ||
require.Equal(t, poolId, uint64(1)) | ||
|
||
pools := amm.GetAllPool(ctx) | ||
|
||
// check length of pools | ||
require.Equal(t, len(pools), 1) | ||
|
||
// check block height | ||
require.Equal(t, int64(0), ctx.BlockHeight()) | ||
|
||
lpIncentive := types.IncentiveInfo{ | ||
// reward amount in eden for 1 year | ||
EdenAmountPerYear: sdk.NewInt(1000000000), | ||
// starting block height of the distribution | ||
DistributionStartBlock: sdk.NewInt(1), | ||
// distribution duration - block number per year | ||
TotalBlocksPerYear: sdk.NewInt(10000), | ||
// we set block numbers in 24 hrs | ||
AllocationEpochInBlocks: sdk.NewInt(100), | ||
// maximum eden allocation per day that won't exceed 30% apr | ||
MaxEdenPerAllocation: sdk.NewInt(100), | ||
// number of block intervals that distribute rewards. | ||
DistributionEpochInBlocks: sdk.NewInt(10), | ||
// current epoch in block number | ||
CurrentEpochInBlocks: sdk.NewInt(1), | ||
// eden boost apr (0-1) range | ||
EdenBoostApr: sdk.NewDec(1), | ||
} | ||
|
||
ctx = ctx.WithBlockHeight(lpIncentive.DistributionEpochInBlocks.Int64()) | ||
ik.UpdateLPRewardsUnclaimed(ctx, lpIncentive) | ||
|
||
// Get pool info from incentive param | ||
poolInfo, found := ik.GetPoolInfo(ctx, poolId) | ||
require.Equal(t, found, true) | ||
require.GreaterOrEqual(t, poolInfo.EdenApr.MulInt(sdk.NewInt(100)).TruncateInt().Int64(), int64(90)) | ||
|
||
// Get dex rewards per pool | ||
revenueAddress := ammtypes.NewPoolRevenueAddress(poolId) | ||
|
||
// Feed dex rewards | ||
usdcToken = sdk.NewCoins(sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(1000))) | ||
err = app.BankKeeper.MintCoins(ctx, ammtypes.ModuleName, usdcToken) | ||
require.NoError(t, err) | ||
err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ammtypes.ModuleName, revenueAddress, usdcToken) | ||
require.NoError(t, err) | ||
|
||
// 1 week later. | ||
ctx = ctx.WithBlockHeight(lpIncentive.AllocationEpochInBlocks.Mul(sdk.NewInt(ptypes.DaysPerWeek)).Int64()) | ||
poolInfo.NumBlocks = sdk.NewInt(ctx.BlockHeight()) | ||
ik.SetPoolInfo(ctx, poolId, poolInfo) | ||
|
||
ik.UpdateLPRewardsUnclaimed(ctx, lpIncentive) | ||
poolInfo, found = ik.GetPoolInfo(ctx, poolId) | ||
require.Equal(t, found, true) | ||
require.GreaterOrEqual(t, poolInfo.DexApr.MulInt(sdk.NewInt(100)).TruncateInt().Int64(), int64(90)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters