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

fixed fees #450

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 13 additions & 16 deletions ride/lp.ride
Original file line number Diff line number Diff line change
Expand Up @@ -1029,34 +1029,31 @@ func calculateAmountOutForSwapAndSendTokens(cleanAmountIn: Int, isReverse: Boole
} else {
getStringOrFail(this, aa())
}
let poolAssetInBalance = getAccBalance(assetIn) - i.payments[0].value().amount - feePoolAmount
let poolAssetInBalance = getAccBalance(assetIn) - i.payments[0].value().amount
let poolAssetOutBalance = getAccBalance(assetOut)
let amountOut = fraction(poolAssetOutBalance, cleanAmountIn, poolAssetInBalance + cleanAmountIn)

let oldK = poolAssetInBalance.toBigInt() * poolAssetOutBalance.toBigInt()
let newK = (getAccBalance(assetIn).toBigInt() + feePoolAmount.toBigInt()) * (getAccBalance(assetOut).toBigInt() - amountOut.toBigInt())
let newK = (getAccBalance(assetIn).toBigInt()) * (getAccBalance(assetOut).toBigInt() - amountOut.toBigInt())

strict checkK = newK >= oldK || "new K is fewer error".throw()
strict checkMin = amountOutMin <= amountOut || "Exchange result is fewer coins than expected".throw()

let amountAssetBalanceDelta = if (isReverse) then {
-amountOut
} else {
feePoolAmount
}
let priceAssetBalanceDelta = if (isReverse) then {
feePoolAmount
} else {
-amountOut
}
strict refreshKLpActions = refreshKLpInternal(amountAssetBalanceDelta, priceAssetBalanceDelta, 0)._1
let priceAssetId = getStringOrFail(this, pa())
let feeAssetId = priceAssetId.fromBase58String()
let feeAmount = fraction(cleanAmountIn, feePoolAmount, 100)

strict rebalanceState = rebalanceAsset(assetIn)
strict withdrawState = withdrawAndRebalanceAsset(assetOut, amountOut)
( withdrawState ++
strict withdrawState = withdrawAndRebalanceAsset(assetOut, amountOut + feeAmount)

let feeTransfer = ScriptTransfer(feeCollectorAddress, feeAmount, feeAssetId)

(
withdrawState ++
rebalanceState ++
[
ScriptTransfer(addressTo.addressFromStringValue(), amountOut, assetOut.parseAssetId())
ScriptTransfer(addressTo.addressFromStringValue(), amountOut - feeAmount, assetOut.parseAssetId()),
feeTransfer
],
amountOut
)
Expand Down
Loading