Skip to content

Commit

Permalink
Merge branch 'wxdefi-502-futures' of other.github.com:waves-exchange/…
Browse files Browse the repository at this point in the history
…contracts into wxdefi-502-futures
  • Loading branch information
bra1nsurfer committed Jul 31, 2024
2 parents b7c8b11 + fef32a2 commit cb37420
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
9 changes: 5 additions & 4 deletions ride/lp.ride
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,9 @@ func keySkipOrderValidation(poolAddress: String) = "%s%s__skipOrderValidation__"
func throwOrderError(
orderValid: Boolean,
orderValidInfo: String,
senderValid: Boolean,
matcherValid: Boolean
) = {
throw("order validation failed: orderValid=" + orderValid.toString() + " (" + orderValidInfo + ")" + " senderValid=" + senderValid.toString() + " matcherValid=" + matcherValid.toString())
throw("order validation failed: orderValid=" + orderValid.toString() + " (" + orderValidInfo + ")" + " matcherValid=" + matcherValid.toString())
}

#------------------------
Expand Down Expand Up @@ -1790,10 +1789,12 @@ func verify() = {
case order: Order =>
let matcherPub = getMatcherPubOrFail()
let (orderValid, orderValidInfo) = if (skipOrderValidation()) then (true, "") else validateMatcherOrderAllowed(order)
let senderValid = sigVerify(order.bodyBytes, order.proofs[0], order.senderPublicKey)
# Requested changes from matcher team
# Order should contain 2 proofs
# proof[0] is ignored
let matcherValid = sigVerify(order.bodyBytes, order.proofs[1], matcherPub)

(orderValid && senderValid && matcherValid) || throwOrderError(orderValid, orderValidInfo, senderValid, matcherValid)
(orderValid && matcherValid) || throwOrderError(orderValid, orderValidInfo, matcherValid)
case s: SetScriptTransaction =>
if sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) then {
true
Expand Down
9 changes: 5 additions & 4 deletions ride/lp_stable.ride
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,9 @@ func keySkipOrderValidation(poolAddress: String) = "%s%s__skipOrderValidation__"
func throwOrderError(
orderValid: Boolean,
orderValidInfo: String,
senderValid: Boolean,
matcherValid: Boolean
) = {
throw("order validation failed: orderValid=" + orderValid.toString() + " (" + orderValidInfo + ")" + " senderValid=" + senderValid.toString() + " matcherValid=" + matcherValid.toString())
throw("order validation failed: orderValid=" + orderValid.toString() + " (" + orderValidInfo + ")" + " matcherValid=" + matcherValid.toString())
}

#------------------------
Expand Down Expand Up @@ -1773,10 +1772,12 @@ func verify() = {
case order: Order =>
let matcherPub = mp()
let (orderValid, orderValidInfo) = if (skipOrderValidation()) then (true, "") else validateMatcherOrderAllowed(order)
let senderValid = sigVerify(order.bodyBytes, order.proofs[0], order.senderPublicKey)
# Requested changes from matcher team
# Order should contain 2 proofs
# proof[0] is ignored
let matcherValid = sigVerify(order.bodyBytes, order.proofs[1], matcherPub)

(orderValid && senderValid && matcherValid) || throwOrderError(orderValid, orderValidInfo, senderValid, matcherValid)
(orderValid && matcherValid) || throwOrderError(orderValid, orderValidInfo, matcherValid)
case s: SetScriptTransaction =>
if sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) then {
true
Expand Down
80 changes: 66 additions & 14 deletions ride/swap.ride
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func getAccBalance(assetId: String) = {
if(assetId == "WAVES") then wavesBalance(this).available else assetBalance(this, fromBase58String(assetId))
}

func stringToAssetId(assetIdString: String) = {
if(assetIdString == "WAVES") then unit else assetIdString.fromBase58String()
}

# %d%d__poolFee__protocolFee
func getSwapFees(poolAddressStr: String) = {
let poolFeeDefault = this.getInteger(poolFee()).value()
Expand Down Expand Up @@ -157,17 +161,40 @@ func swapCalculateREADONLY(
} else {
feePoolAmountCalc
}
let cleanAmountIn = amountIn.toBigInt() - feeProtocolAmount - feePoolAmount

let res = invoke(
# Protocol fee should always be extracted from Pool Price Asset
# isReverse == true -> amountIn is in priceAssetId
let cleanAmountIn = if (isReverse == true) then {
amountIn.toBigInt() - feeProtocolAmount - feePoolAmount
} else {
amountIn.toBigInt() - feePoolAmount
}

let totalGetRaw = invoke(
poolAdr,
"calculateAmountOutForSwapREADONLY",
[cleanAmountIn.toInt(), isReverse, feePoolAmount.toInt()],
[]
).exactAs[Int]

(nil,
res)
# Protocol fee should always be extracted from Pool Price Asset
let newTotalGet = if (isReverse == true) then {
totalGetRaw
} else {
let feeProtocolFromGetCalc = fraction(totalGetRaw.toBigInt(), prFee.toBigInt(), feeScale)
let feeProtocolAmountFromGet = if (feeProtocolFromGetCalc.toInt() == 0) then {
toBigInt(1).toInt()
} else {
feeProtocolFromGetCalc.toInt()
}

totalGetRaw - feeProtocolAmountFromGet
}

(
nil,
newTotalGet
)
}

@Callable(i)
Expand All @@ -176,6 +203,10 @@ func swap(
assetOutRaw: String,
addressTo: String
) = {
strict checks = [
i.payments.size() == 1 || "exactly 1 payment are expected".throw()
]

let assetOut = if (assetOutRaw == "") then "WAVES" else assetOutRaw
let pmt = i.payments[0].value()
let assetIn = if(pmt.assetId == unit) then "WAVES" else pmt.assetId.value().toBase58String()
Expand All @@ -184,6 +215,7 @@ func swap(
let (plFee, prFee) = getSwapFees(poolAdr.toString())
let feeProtocolAmountCalc = fraction(amountIn.toBigInt(), prFee.toBigInt(), feeScale)
let feePoolAmountCalc = fraction(amountIn.toBigInt(), plFee.toBigInt(), feeScale)
let addressTo_a = addressTo.addressFromStringValue()

let feeProtocolAmount = if (feeProtocolAmountCalc.toInt() == 0) then {
toBigInt(1)
Expand All @@ -195,24 +227,44 @@ func swap(
} else {
feePoolAmountCalc
}
let cleanAmountIn = amountIn.toBigInt() - feeProtocolAmount - feePoolAmount

strict checks = [
i.payments.size() == 1 || "exactly 1 payment are expected".throw()
]

let assetInAttachedPayment = if (assetIn == "WAVES") then unit else assetIn.fromBase58String()
# Protocol fee should always be extracted from Pool Price Asset
# isReverse == true -> amountIn is in priceAssetId
let cleanAmountIn = if (isReverse == true) then {
amountIn.toBigInt() - feeProtocolAmount - feePoolAmount
} else {
amountIn.toBigInt() - feePoolAmount
}

let assetInAttachedPayment = assetIn.stringToAssetId()
let totalGetRaw = invoke(
poolAdr,
"calculateAmountOutForSwapAndSendTokens",
[cleanAmountIn.toInt(), isReverse, amountOutMin, addressTo, feePoolAmount.toInt()],
[cleanAmountIn.toInt(), isReverse, amountOutMin, this.toString(), feePoolAmount.toInt()],
[AttachedPayment(assetInAttachedPayment, cleanAmountIn.toInt())]
).exactAs[Int]

([ScriptTransfer(feeCollectorAddress, feeProtocolAmount.toInt(), assetInAttachedPayment),
ScriptTransfer(poolAdr, feePoolAmount.toInt(), assetInAttachedPayment)],
totalGetRaw)
# Protocol fee should always be extracted from Pool Price Asset
let (protocolFeeActions, newTotalGet) = if (isReverse == true) then {
([ScriptTransfer(feeCollectorAddress, feeProtocolAmount.toInt(), assetInAttachedPayment)], totalGetRaw)
} else {
let feeProtocolFromGetCalc = fraction(totalGetRaw.toBigInt(), prFee.toBigInt(), feeScale)
let feeProtocolAmountFromGet = if (feeProtocolFromGetCalc.toInt() == 0) then {
toBigInt(1).toInt()
} else {
feeProtocolFromGetCalc.toInt()
}

([ScriptTransfer(feeCollectorAddress, feeProtocolAmountFromGet, assetOut.stringToAssetId())], totalGetRaw - feeProtocolAmountFromGet)
}

(
protocolFeeActions ++
[
ScriptTransfer(poolAdr, feePoolAmount.toInt(), assetInAttachedPayment),
ScriptTransfer(addressTo_a, newTotalGet, assetOut.stringToAssetId())
],
newTotalGet)
}

@Verifier(tx)
Expand Down

0 comments on commit cb37420

Please sign in to comment.