Skip to content

Commit 6d424fd

Browse files
authored
Dev 254. Debug off (#59)
1 parent 491e48b commit 6d424fd

File tree

7 files changed

+40
-11
lines changed

7 files changed

+40
-11
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

cardano-dex-contracts-onchain/ErgoDex/PContracts/PSwap.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,14 @@ swapValidatorT = plam $ \conf' redeemer' ctx' -> unTermCont $ do
118118
(pIsAda # quote)
119119
(pdiv # (quoteDelta * exFeePerTokenDen) # (exFeePerTokenDen - exFeePerTokenNum))
120120
quoteDelta
121-
121+
fairExFee <-
122+
tlet $
123+
(pIsAda # quote) #|| (validExFee # rewardValue # selfValue # base # baseAmount # quoteAmount # exFeePerTokenNum # exFeePerTokenDen)
122124
let
123125
strictInputs =
124126
let inputsLength = plength # inputs
125127
in inputsLength #== 2 -- address double satisfaction attack
126128
minSatisfaction = minOutput #<= quoteAmount
127-
fairExFee = validExFee # rewardValue # selfValue # base # baseAmount # quote # quoteAmount # exFeePerTokenNum # exFeePerTokenDen
128129
fairPrice = validPrice # quoteAmount # poolValue # base # quote # baseAmount # feeNum
129130

130131
pure $
@@ -141,28 +142,26 @@ validExFee ::
141142
:--> PValue _ _
142143
:--> PAssetClass
143144
:--> PInteger
144-
:--> PAssetClass
145145
:--> PInteger
146146
:--> PInteger
147147
:--> PInteger
148148
:--> PBool
149149
)
150150
validExFee =
151-
plam $ \rewardValue selfValue base baseAmount quote quoteAmount exFeePerTokenNum exFeePerTokenDen ->
151+
plam $ \rewardValue selfValue base baseAmount quoteAmount exFeePerTokenNum exFeePerTokenDen ->
152152
unTermCont $ do
153153
zeroAsData' <- tlet zeroAsData
154154
bqAda <-
155155
tlet $
156156
pif
157157
(pIsAda # base)
158-
(ptuple # pdata baseAmount # zeroAsData')
159-
(pif (pIsAda # quote) (ptuple # zeroAsData' # pdata quoteAmount) (ptuple # zeroAsData' # zeroAsData'))
160-
let baseAda = pfromData $ pfield @"_0" # bqAda
161-
quoteAda = pfromData $ pfield @"_1" # bqAda
158+
(pdata baseAmount)
159+
(zeroAsData')
160+
let baseAda = pfromData $ bqAda
162161
outAda = plovelaceValueOf # rewardValue
163162
inAda = plovelaceValueOf # selfValue
164163
exFee = pdiv # (quoteAmount * exFeePerTokenNum) # exFeePerTokenDen
165-
pure $ (inAda - baseAda - exFee) #<= (outAda - quoteAda)
164+
pure $ (inAda - baseAda - exFee) #<= outAda
166165

167166
validPrice ::
168167
Term
@@ -180,4 +179,5 @@ validPrice =
180179
let relaxedOut = quoteAmount + 1
181180
reservesBase = assetClassValueOf # poolValue # base
182181
reservesQuote = assetClassValueOf # poolValue # quote
183-
in reservesQuote * baseAmount * feeNum #<= relaxedOut * (reservesBase * feeDen + baseAmount * feeNum)
182+
correctOut = pdiv # (reservesQuote * baseAmount * feeNum) # (reservesBase * feeDen + baseAmount * feeNum)
183+
in correctOut #<= relaxedOut

cardano-dex-contracts-onchain/ErgoDex/PValidators.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Plutarch.Unsafe (punsafeCoerce)
2323
import Plutarch.Internal
2424

2525
cfgForValidator :: Config
26-
cfgForValidator = Config DoTracingAndBinds
26+
cfgForValidator = Config NoTracing
2727

2828
wrapValidator ::
2929
(PIsData dt, PIsData rdmr) =>

cardano-dex-contracts-onchain/test/Tests/Swap.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Gen.SwapGen
2323
checkSwap = testGroup "CheckSwap"
2424
[ HH.testProperty "correct_swap" successSwap
2525
, HH.testProperty "correct_swap_x_is_ada" successSwapWithXIsAda
26+
, HH.testProperty "correct_swap_y_is_ada" successSwapWithYIsAda
2627
, HH.testProperty "swap_invalid_ex_fee" invalidExFee
2728
, HH.testProperty "swap_invalid_fair_price" invalidFairPrice
2829
]
@@ -93,6 +94,34 @@ successSwapWithXIsAda = property $ do
9394

9495
result === Right ()
9596

97+
successSwapWithYIsAda :: Property
98+
successSwapWithYIsAda = property $ do
99+
let (x, _, nft, lq) = genAssetClasses
100+
pkh <- forAll genPkh
101+
orderTxRef <- forAll genTxOutRef
102+
let
103+
y = mkAdaAssetClass
104+
(cfgData, dh) = genSConfig x y nft 995 4160772239327619 100000000000000 pkh 49405 48068
105+
orderTxIn = genSTxIn orderTxRef dh x 49405 3489838
106+
orderTxOut = genSTxOut dh y 0 1479634 pkh
107+
108+
poolTxRef <- forAll genTxOutRef
109+
let
110+
(pcfg, pdh) = genPConfig x y nft lq 1
111+
poolTxIn = genPTxIn poolTxRef pdh x 9940655 y 0 lq 9223372036844775807 nft 1 10060000
112+
poolTxOut = genPTxOut pdh x 9990060 y 0 lq 9223372036854775787 nft 1 10010497
113+
114+
let
115+
txInfo = mkTxInfo poolTxIn orderTxIn poolTxOut orderTxOut
116+
purpose = mkPurpose orderTxRef
117+
118+
cxtToData = toData $ mkContext txInfo purpose
119+
orderRedeemToData = toData $ mkOrderRedeemer 1 0 1
120+
121+
result = eraseRight $ evalWithArgs (wrapValidator PSwap.swapValidatorT) [cfgData, orderRedeemToData, cxtToData]
122+
123+
result === Right ()
124+
96125
invalidFairPrice :: Property
97126
invalidFairPrice = property $ do
98127
let (x, y, nft, lq) = genAssetClasses

0 commit comments

Comments
 (0)