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

fix(iro): first sell tokens, than charge fee #1717

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions x/iro/keeper/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,6 @@ func (k Keeper) Sell(ctx sdk.Context, planId string, seller sdk.AccAddress, amou
return errorsmod.Wrapf(types.ErrInvalidMinCost, "minCost: %s, cost: %s, fee: %s", minIncomeAmt.String(), costAmt.String(), takerFeeAmt.String())
}

// Charge taker fee
takerFee := sdk.NewCoin(appparams.BaseDenom, takerFeeAmt)
owner := k.rk.MustGetRollappOwner(ctx, plan.RollappId)
err = k.chargeTakerFee(ctx, takerFee, seller, &owner)
if err != nil {
return err
}

// send allocated tokens from seller to the plan
err = k.BK.SendCoinsFromAccountToModule(ctx, seller, types.ModuleName, sdk.NewCoins(sdk.NewCoin(plan.TotalAllocation.Denom, amountTokensToSell)))
if err != nil {
Expand All @@ -240,6 +232,14 @@ func (k Keeper) Sell(ctx sdk.Context, planId string, seller sdk.AccAddress, amou
plan.SoldAmt = plan.SoldAmt.Sub(amountTokensToSell)
k.SetPlan(ctx, *plan)

// Charge taker fee
takerFee := sdk.NewCoin(appparams.BaseDenom, takerFeeAmt)
owner := k.rk.MustGetRollappOwner(ctx, plan.RollappId)
err = k.chargeTakerFee(ctx, takerFee, seller, &owner)
if err != nil {
return err
}

// Emit event
err = uevent.EmitTypedEvent(ctx, &types.EventSell{
Seller: seller.String(),
Expand Down
4 changes: 2 additions & 2 deletions x/iro/types/bonding_curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ func (lbc BondingCurve) TokensApproximation(startingX, spendTokens math.LegacyDe
// Newton-Raphson iteration
epsilonDec := math.LegacyNewDecWithPrec(1, epsilonPrecision)
for i := 0; i < maxIterations; i++ {
fx := f(x)
fx := f(x) // diff between spendTokens and the actual cost to get to x
// If the function converges, return the result
if fx.Abs().LT(epsilonDec) {
return x, i, nil
}
prevX := x
fPrimex := fPrime(x)
fPrimex := fPrime(x) // price for new X

// defensive check to avoid division by zero
// not supposed to happen, as spotPriceInternal should never return 0
Expand Down
Loading