Skip to content

Commit d5321d8

Browse files
committed
fixed taker fee to be chraged from the user
1 parent 63850d5 commit d5321d8

File tree

7 files changed

+45
-25
lines changed

7 files changed

+45
-25
lines changed

ibctesting/genesis_transfer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (s *transferGenesisSuite) TestIRO() {
108108
apptesting.FundAccount(s.rollappApp(), s.rollappCtx(), s.rollappChain().SenderAccount.GetAddress(), sdk.NewCoins(coin))
109109

110110
// create IRO plan
111-
_, err := s.hubApp().IROKeeper.CreatePlan(s.hubCtx(), amt, time.Time{}, time.Time{}, rollapp, irotypes.DefaultBondingCurve(), irotypes.DefaultIncentivePlanParams())
111+
_, err := s.hubApp().IROKeeper.CreatePlan(s.hubCtx(), amt, time.Now(), time.Now().Add(time.Hour), rollapp, irotypes.DefaultBondingCurve(), irotypes.DefaultIncentivePlanParams())
112112
s.Require().NoError(err)
113113

114114
// non-genesis transfer should fail, as the bridge is not open

x/iro/keeper/iro.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ func (k Keeper) MustGetPlan(ctx sdk.Context, planId string) types.Plan {
5353
return plan
5454
}
5555

56+
// MustGetPlanByRollapp returns a plan from its rollapp ID
57+
// It will panic if the plan is not found
58+
func (k Keeper) MustGetPlanByRollapp(ctx sdk.Context, rollappId string) types.Plan {
59+
plan, found := k.GetPlanByRollapp(ctx, rollappId)
60+
if !found {
61+
panic(fmt.Sprintf("plan not found for rollapp ID: %s", rollappId))
62+
}
63+
return plan
64+
}
65+
5666
// GetAllPlans returns all plans
5767
func (k Keeper) GetAllPlans(ctx sdk.Context) (list []types.Plan) {
5868
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PlanKeyPrefix)

x/iro/keeper/keeper_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,10 @@ func (suite *KeeperTestSuite) SetupTest() {
4545
suite.FundAcc(sdk.MustAccAddressFromBech32(rollapp.Owner), sdk.NewCoins(sdk.NewCoin(appparams.BaseDenom, funds)))
4646
}
4747

48-
// buyer := sample.Acc()
49-
//
50-
// buyersFunds := sdk.NewCoins(sdk.NewCoin("adym", sdk.NewInt(100_000)))
51-
// s.FundAcc(buyer, buyersFunds)
5248
// BuySomeTokens buys some tokens from the plan
5349
func (suite *KeeperTestSuite) BuySomeTokens(planId string, buyer sdk.AccAddress, amt math.Int) {
5450
maxAmt := sdk.NewInt(1_000_000_000)
55-
suite.FundAcc(buyer, sdk.NewCoins(sdk.NewCoin("adym", amt)))
51+
suite.FundAcc(buyer, sdk.NewCoins(sdk.NewCoin("adym", amt.MulRaw(10)))) // 10 times the amount to buy, for buffer and fees
5652
err := suite.App.IROKeeper.Buy(suite.Ctx, planId, buyer, amt, maxAmt)
5753
suite.Require().NoError(err)
5854
}

x/iro/keeper/settle_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (s *KeeperTestSuite) TestSettle() {
3434

3535
// buy some tokens
3636
s.Ctx = s.Ctx.WithBlockTime(startTime.Add(time.Minute))
37-
soldAmt := sdk.NewInt(100)
37+
soldAmt := sdk.NewInt(1_000)
3838
s.BuySomeTokens(planId, sample.Acc(), soldAmt)
3939

4040
// settle should fail as no rollappDenom balance available
@@ -79,7 +79,7 @@ func (s *KeeperTestSuite) TestClaim() {
7979
claimer := sample.Acc()
8080
// buy some tokens
8181
s.Ctx = s.Ctx.WithBlockTime(startTime.Add(time.Minute))
82-
soldAmt := sdk.NewInt(100)
82+
soldAmt := sdk.NewInt(1_000)
8383
s.BuySomeTokens(planId, claimer, soldAmt)
8484

8585
// claim should fail as not settled

x/iro/keeper/trade.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ func (k Keeper) Buy(ctx sdk.Context, planId string, buyer sdk.AccAddress, amount
6868
if !cost.IsPositive() {
6969
return errorsmod.Wrapf(types.ErrInvalidCost, "cost: %s", cost.String())
7070
}
71+
costCoin := sdk.NewCoin(appparams.BaseDenom, cost)
7172

72-
totalCost, takerFeeCoin := k.AddTakerFee(sdk.NewCoin(appparams.BaseDenom, cost), k.GetParams(ctx).TakerFee)
73+
totalCost, takerFeeCoin := k.AddTakerFee(costCoin, k.GetParams(ctx).TakerFee)
7374
if !totalCost.IsPositive() || !takerFeeCoin.IsPositive() {
7475
return errorsmod.Wrapf(types.ErrInvalidCost, "totalCost: %s, takerFeeCoin: %s", totalCost.String(), takerFeeCoin.String())
7576
}
@@ -86,7 +87,7 @@ func (k Keeper) Buy(ctx sdk.Context, planId string, buyer sdk.AccAddress, amount
8687
}
8788

8889
// send DYM from buyer to the plan. DYM sent directly to the plan's module account
89-
err = k.BK.SendCoins(ctx, buyer, plan.GetAddress(), sdk.NewCoins(totalCost))
90+
err = k.BK.SendCoins(ctx, buyer, plan.GetAddress(), sdk.NewCoins(costCoin))
9091
if err != nil {
9192
return err
9293
}
@@ -129,8 +130,12 @@ func (k Keeper) Sell(ctx sdk.Context, planId string, seller sdk.AccAddress, amou
129130

130131
// Calculate cost over fixed price curve
131132
cost := plan.BondingCurve.Cost(plan.SoldAmt.Sub(amountTokensToSell), plan.SoldAmt)
133+
if !cost.IsPositive() {
134+
return errorsmod.Wrapf(types.ErrInvalidCost, "cost: %s", cost.String())
135+
}
136+
costCoin := sdk.NewCoin(appparams.BaseDenom, cost)
132137

133-
totalCost, takerFeeCoin := k.SubtractTakerFee(sdk.NewCoin(appparams.BaseDenom, cost), k.GetParams(ctx).TakerFee)
138+
totalCost, takerFeeCoin := k.SubtractTakerFee(costCoin, k.GetParams(ctx).TakerFee)
134139
if !totalCost.IsPositive() || !takerFeeCoin.IsPositive() {
135140
return errorsmod.Wrapf(types.ErrInvalidCost, "totalCost: %s, takerFeeCoin: %s", totalCost.String(), takerFeeCoin.String())
136141
}
@@ -153,7 +158,7 @@ func (k Keeper) Sell(ctx sdk.Context, planId string, seller sdk.AccAddress, amou
153158
}
154159

155160
// send DYM from the plan to the seller. DYM managed by the plan's module account
156-
err = k.BK.SendCoins(ctx, plan.GetAddress(), seller, sdk.NewCoins(totalCost))
161+
err = k.BK.SendCoins(ctx, plan.GetAddress(), seller, sdk.NewCoins(costCoin))
157162
if err != nil {
158163
return err
159164
}

x/iro/keeper/trade_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import (
66

77
"cosmossdk.io/math"
88
sdk "github.com/cosmos/cosmos-sdk/types"
9+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
910
"github.com/dymensionxyz/dymension/v3/testutil/sample"
1011
"github.com/dymensionxyz/dymension/v3/x/iro/keeper"
1112
"github.com/dymensionxyz/dymension/v3/x/iro/types"
13+
"github.com/osmosis-labs/osmosis/v15/x/txfees"
1214
)
1315

1416
// FIXME: test trade after settled
1517

16-
// FIXME: test taker fee
18+
// FIXME: test taker fee (+low values should fail)
1719

1820
// FIXME: add sell test
1921

@@ -30,19 +32,16 @@ func (s *KeeperTestSuite) TestBuy() {
3032
rollapp, _ := s.App.RollappKeeper.GetRollapp(s.Ctx, rollappId)
3133
planId, err := k.CreatePlan(s.Ctx, totalAllocation, startTime, startTime.Add(time.Hour), rollapp, curve, incentives)
3234
s.Require().NoError(err)
35+
s.Ctx = s.Ctx.WithBlockTime(startTime.Add(time.Minute))
3336

3437
buyer := sample.Acc()
3538
buyersFunds := sdk.NewCoins(sdk.NewCoin("adym", sdk.NewInt(100_000)))
3639
s.FundAcc(buyer, buyersFunds)
3740

3841
// buy before plan start - should fail
39-
s.Ctx = s.Ctx.WithBlockTime(startTime.Add(-time.Minute))
40-
err = k.Buy(s.Ctx, planId, buyer, sdk.NewInt(1_000), maxAmt)
42+
err = k.Buy(s.Ctx.WithBlockTime(startTime.Add(-time.Minute)), planId, buyer, sdk.NewInt(1_000), maxAmt)
4143
s.Require().Error(err)
4244

43-
// plan start
44-
s.Ctx = s.Ctx.WithBlockTime(startTime.Add(time.Minute))
45-
4645
// cost is higher than maxCost specified - should fail
4746
err = k.Buy(s.Ctx, planId, buyer, sdk.NewInt(1_000), sdk.NewInt(10))
4847
s.Require().Error(err)
@@ -51,11 +50,14 @@ func (s *KeeperTestSuite) TestBuy() {
5150
err = k.Buy(s.Ctx, planId, buyer, sdk.NewInt(900_000), maxAmt)
5251
s.Require().Error(err)
5352

54-
// successful buy
53+
// assert nothing sold
5554
plan, _ := k.GetPlan(s.Ctx, planId)
5655
s.Assert().Equal(sdk.NewInt(0), plan.SoldAmt)
56+
buyerBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, buyer).AmountOf("adym")
57+
s.Assert().Equal(buyersFunds.AmountOf("adym"), buyerBalance)
5758

58-
amountTokensToBuy := sdk.NewInt(100)
59+
// successful buy
60+
amountTokensToBuy := sdk.NewInt(1_000)
5961
expectedCost := curve.Cost(plan.SoldAmt, plan.SoldAmt.Add(amountTokensToBuy))
6062
err = k.Buy(s.Ctx, planId, buyer, amountTokensToBuy, maxAmt)
6163
s.Require().NoError(err)
@@ -74,7 +76,9 @@ func (s *KeeperTestSuite) TestBuy() {
7476

7577
// assert balance
7678
balances := s.App.BankKeeper.GetAllBalances(s.Ctx, buyer)
77-
s.Require().Equal(buyersFunds.AmountOf("adym").Sub(expectedCost).Sub(expectedCost2), balances.AmountOf("adym"))
79+
takerFee := s.App.BankKeeper.GetAllBalances(s.Ctx, authtypes.NewModuleAddress(txfees.ModuleName))
80+
expectedBalance := buyersFunds.AmountOf("adym").Sub(expectedCost).Sub(expectedCost2).Sub(takerFee.AmountOf("adym"))
81+
s.Require().Equal(expectedBalance, balances.AmountOf("adym"))
7882

7983
expectedBaseDenom := fmt.Sprintf("%s_%s", types.IROTokenPrefix, rollappId)
8084
s.Require().Equal(amountTokensToBuy.MulRaw(2), balances.AmountOf(expectedBaseDenom))

x/rollapp/transfergenesis/ibc_module.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type TransferKeeper interface {
4848
}
4949

5050
type IROKeeper interface {
51+
MustGetPlanByRollapp(ctx sdk.Context, rollappID string) irotypes.Plan
5152
GetPlanByRollapp(ctx sdk.Context, rollappID string) (irotypes.Plan, bool)
5253
GetModuleAccountAddress() string
5354
}
@@ -164,7 +165,8 @@ func (w IBCModule) OnRecvPacket(
164165
}
165166

166167
// handle genesis transfer by the IRO keeper
167-
plan, _ := w.iroKeeper.GetPlanByRollapp(ctx, ra.RollappId)
168+
plan := w.iroKeeper.MustGetPlanByRollapp(ctx, ra.RollappId)
169+
168170
// validate the transfer against the IRO plan
169171
err = w.validateGenesisTransfer(plan, transfer, memo.Denom)
170172
if err != nil {
@@ -182,11 +184,14 @@ func (w IBCModule) OnRecvPacket(
182184
ack = w.IBCModule.OnRecvPacket(commontypes.SkipRollappMiddlewareContext(ctx), packet, relayer)
183185
// if the ack is nil, we return an error as we expect immediate ack
184186
if ack == nil {
187+
l.Error("Expected immediate ack for genesis transfer.")
185188
return channeltypes.NewErrorAcknowledgement(errorsmod.Wrap(gerrc.ErrInternal, "transfer genesis: OnRecvPacket"))
186189
}
187-
l.Info("Received valid genesis transfer. Registered denom data.")
188-
189-
w.EnableTransfers(ctx, ra.RollappId, transfer.Denom)
190+
err = w.EnableTransfers(ctx, ra.RollappId, transfer.Denom)
191+
if err != nil {
192+
l.Error("Enable transfers.", "err", err)
193+
return channeltypes.NewErrorAcknowledgement(errorsmod.Wrap(err, "transfer genesis: enable transfers"))
194+
}
190195
return ack
191196
}
192197

0 commit comments

Comments
 (0)