Skip to content

Commit ea5a810

Browse files
committed
update pool contract. StakeAdmins and lqLock support
1 parent 45c7af9 commit ea5a810

File tree

6 files changed

+47
-23
lines changed

6 files changed

+47
-23
lines changed

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ source-repository-package
313313
source-repository-package
314314
type: git
315315
location: https://github.com/ergolabs/cardano-dex-contracts
316-
tag: 2fb44f444897d84e313ceb4d3d467441385802dd
316+
tag: 0f53e485b2310cb83a946bbd29a5c5454a757b22
317317
subdir:
318318
cardano-dex-contracts-offchain
319319

dex-core/src/ErgoDex/Amm/Pool.hs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Ledger
1111
import Ledger.Value (assetClassValue, assetClassValueOf)
1212
import PlutusTx.IsData.Class
1313
import PlutusTx.Sqrt
14+
import Plutus.V1.Ledger.Api (StakingCredential(..))
1415
import PlutusTx.Numeric (AdditiveMonoid(zero))
1516
import Ledger.Ada (lovelaceValueOf)
1617
import Plutus.Script.Utils.V2.Address (mkValidatorAddress)
@@ -51,25 +52,29 @@ data Pool = Pool
5152
, poolCoinLq :: Coin Liquidity
5253
, poolFee :: PoolFee
5354
, outCollateral :: Amount Lovelace
55+
, stakeAdmins :: [PubKeyHash]
56+
, lqBound :: Amount X
57+
, stakeCred :: Maybe StakingCredential
5458
} deriving (Show, Eq, Generic, FromJSON, ToJSON)
5559

5660
feeDen :: Integer
5761
feeDen = 1000
5862

5963
instance FromLedger Pool where
60-
parseFromLedger fout@FullTxOut{fullTxOutDatum=(KnownDatum (Datum d)), ..} = --todo add also check for address
64+
parseFromLedger fout@FullTxOut{fullTxOutDatum=(KnownDatum (Datum d)), fullTxOutAddress=Address{..}, ..} = --todo add also check for address
6165
case fromBuiltinData d of
6266
(Just PoolConfig{..}) -> do
6367
let
64-
rx = Amount $ assetClassValueOf fullTxOutValue poolX
65-
ry = Amount $ assetClassValueOf fullTxOutValue poolY
66-
rlq = Amount $ assetClassValueOf fullTxOutValue poolLq
67-
nft = Amount $ assetClassValueOf fullTxOutValue poolNft
68-
lq = maxLqCapAmount - rlq -- actual LQ emission
68+
rx = Amount $ assetClassValueOf fullTxOutValue poolX
69+
ry = Amount $ assetClassValueOf fullTxOutValue poolY
70+
rlq = Amount $ assetClassValueOf fullTxOutValue poolLq
71+
nft = Amount $ assetClassValueOf fullTxOutValue poolNft
72+
lqBoundAmount = Amount lqBound
73+
lq = maxLqCapAmount - rlq -- actual LQ emission
6974
collateral = if W.isAda poolX || W.isAda poolY then zero else minSafeOutputAmount
7075
when (rx == 0 || ry == 0 || rlq == 0 || nft /= 1) Nothing
7176
Just $ OnChain fout Pool
72-
{ poolId = PoolId $ Coin poolNft
77+
{ poolId = PoolId $ Coin poolNft
7378
, poolReservesX = rx
7479
, poolReservesY = ry
7580
, poolLiquidity = lq
@@ -78,19 +83,25 @@ instance FromLedger Pool where
7883
, poolCoinLq = Coin poolLq
7984
, poolFee = PoolFee poolFeeNum feeDen
8085
, outCollateral = collateral
86+
, stakeAdmins = stakeAdmins
87+
, lqBound = lqBoundAmount
88+
, stakeCred = addressStakingCredential
8189
}
8290
_ -> Nothing
8391
parseFromLedger _ = Nothing
8492

8593
instance ToLedger PoolValidatorV1 Pool where
8694
toLedger (PoolValidator poolValidator) Pool{..} =
8795
TxOutCandidate
88-
{ txOutCandidateAddress = mkValidatorAddress poolValidator
96+
{ txOutCandidateAddress = poolAddress
8997
, txOutCandidateValue = poolValue
9098
, txOutCandidateDatum = KnownDatum $ Datum $ toBuiltinData poolConf
9199
, txOutCandidateRefScript = Nothing
92100
}
93101
where
102+
poolAddress = (mkValidatorAddress poolValidator) {
103+
addressStakingCredential = stakeCred
104+
}
94105
nft = unPoolId poolId
95106
poolLqReserves = maxLqCapAmount - poolLiquidity
96107
poolValue = assetClassValue (unCoin nft) 1 <>
@@ -100,18 +111,21 @@ instance ToLedger PoolValidatorV1 Pool where
100111
lovelaceValueOf (unAmount outCollateral)
101112

102113
poolConf = PoolConfig
103-
{ poolNft = unCoin nft
104-
, poolX = unCoin poolCoinX
105-
, poolY = unCoin poolCoinY
106-
, poolLq = unCoin poolCoinLq
107-
, poolFeeNum = poolFeeNum' poolFee
114+
{ poolNft = unCoin nft
115+
, poolX = unCoin poolCoinX
116+
, poolY = unCoin poolCoinY
117+
, poolLq = unCoin poolCoinLq
118+
, poolFeeNum = poolFeeNum' poolFee
119+
, stakeAdmins = stakeAdmins
120+
, lqBound = unAmount lqBound
108121
}
109122

110123
data PoolInitError
111124
= InvalidLiquidity Integer
112125
| InsufficientInitialLiqudity (Amount Liquidity)
113126
deriving (Show, Eq)
114127

128+
-- todo: remove me
115129
initPool
116130
:: PoolValidator V1
117131
-> S.PoolConfig
@@ -136,6 +150,8 @@ initPool poolValidator S.PoolConfig{..} burnLq (inX, inY) = do
136150
, poolCoinY = poolY
137151
, poolCoinLq = poolLq
138152
, poolFee = PoolFee poolFeeNum feeDen
153+
, stakeAdmins = []
154+
, lqBound = 10000
139155
, outCollateral = outCollateral
140156
}
141157
poolOut = toLedger poolValidator pool

dex-core/src/ErgoDex/Amm/PoolActions.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ data OrderExecErr
3434
| PoolMismatch PoolId PoolId
3535
| EmptyPool PoolId
3636
| PoolNotFoundInFinalTx PoolId
37+
| InsufficientPoolLqForSwap PoolId
3738
deriving (Show)
3839

3940
instance Exception OrderExecErr
@@ -102,6 +103,7 @@ runSwap' executorPkh pv sv refInputs (OnChain swapOut Swap{swapExFee=ExFeePerTok
102103

103104
when (swapPoolId /= poolId pool) (Left $ PoolMismatch swapPoolId (poolId pool))
104105
when (getAmount quoteOutput < swapMinQuoteOut) (Left PriceTooHigh)
106+
when (lqBound pool <= poolReservesX pool * 2) (Left $ InsufficientPoolLqForSwap (poolId pool))
105107

106108
let
107109
exFee = assetAmountRawValue quoteOutput * exFeePerTokenNum `div` exFeePerTokenDen
@@ -121,7 +123,7 @@ runSwap' executorPkh pv sv refInputs (OnChain swapOut Swap{swapExFee=ExFeePerTok
121123
<> Ada.lovelaceValueOf (negate exFee) -- Remove Batcher Fee
122124

123125
rewardValue = assetAmountValue quoteOutput <> residualValue
124-
126+
125127
txCandidate = TxCandidate
126128
{ txCandidateInputs = inputs
127129
, txCandidateRefIns = refInputs
@@ -218,7 +220,7 @@ runRedeem' executorPkh pv rv refInputs (OnChain redeemOut Redeem{..}) (poolOut,
218220
pp@(Predicted nextPoolOut _) = applyRedeem pv pool redeemLqIn
219221

220222
burnLqValue = assetClassValue (unCoin redeemLq) (negate $ unAmount redeemLqIn)
221-
223+
222224
exFee = unAmount $ unExFee redeemExFee
223225

224226
rewardAddr = pubKeyHashAddress (PaymentPubKeyHash redeemRewardPkh) redeemRewardSPkh
@@ -233,13 +235,13 @@ runRedeem' executorPkh pv rv refInputs (OnChain redeemOut Redeem{..}) (poolOut,
233235
(outX, outY) = sharesAmount pool redeemLqIn
234236
initValue = fullTxOutValue redeemOut
235237
negatedExFe = Ada.lovelaceValueOf . negate $ exFee
236-
residualValue =
237-
initValue
238-
<> burnLqValue
238+
residualValue =
239+
initValue
240+
<> burnLqValue
239241
<> negatedExFe -- Remove LQ input and ExFee
240242

241243
rewardValue = assetAmountValue outX <> assetAmountValue outY <> residualValue
242-
244+
243245
txCandidate = TxCandidate
244246
{ txCandidateInputs = inputs
245247
, txCandidateRefIns = refInputs

dex-core/src/ErgoDex/Amm/PoolSetup.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mkPoolSetup pv changeAddr = PoolSetup
4444
{ poolDeploy = poolDeploy' pv burnLqInitial changeAddr
4545
}
4646

47+
-- todo: remove me
4748
poolDeploy'
4849
:: PoolValidatorV1
4950
-> Amount Liquidity

dex-core/test/Spec/Pool.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,16 @@ nativePool = Pool
100100
, poolCoinLq = poolLq
101101
, poolFee = PoolFee poolFeeNum feeDen
102102
, outCollateral = minSafeOutputAmount
103+
, stakeAdmins = []
104+
, lqBound = Amount 0
105+
, stakeCred = Nothing
103106
}
104107

108+
-- todo: remove me
105109
initPoolTests = testGroup "NonNativePoolInit"
106-
[ HH.testProperty "init_non_native_pool_sufficient_liquidity" initNonNativePoolSufficientLiquidity
107-
, HH.testProperty "init_non_native_pool_insufficient_liquidity" initNonNativePoolInsufficientLiquidity
110+
[
111+
-- HH.testProperty "init_non_native_pool_sufficient_liquidity" initNonNativePoolSufficientLiquidity
112+
-- , HH.testProperty "init_non_native_pool_insufficient_liquidity" initNonNativePoolInsufficientLiquidity
108113
]
109114

110115
initNonNativePoolInsufficientLiquidity :: Property

nix/pkgs/haskell/haskell.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let
4343
"https://github.com/input-output-hk/cardano-ledger"."c7c63dabdb215ebdaed8b63274965966f2bf408f" = "zTQbMOGPD1Oodv6VUsfF6NUiXkbN8SWI98W3Atv4wbI=";
4444
"https://github.com/input-output-hk/plutus-apps"."593ffafa59dd30ad28cfaf144c526c66328595d2" = "CIuI/Nz7O67ljOHDg7UBbXgWuIE7VPRdPX4VK0/DI3A=";
4545
"https://github.com/input-output-hk/hedgehog-extras"."714ee03a5a786a05fc57ac5d2f1c2edce4660d85" = "6KQFEzb9g2a0soVvwLKESEbA+a8ygpROcMr6bkatROE=";
46-
"https://github.com/ergolabs/cardano-dex-contracts"."2fb44f444897d84e313ceb4d3d467441385802dd" = "Kih0IS6Ty3EnXlgqAyF04nWIWJAnHOEVfraebh5RsNI=";
46+
"https://github.com/ergolabs/cardano-dex-contracts"."0f53e485b2310cb83a946bbd29a5c5454a757b22" = "ksJ0ni8bFUbmoVyG4USSPzNu4+oasa84ijeY/pRDYew=";
4747
"https://github.com/ergolabs/hlog"."19dfa3a6e696a3f63fc3539cd6b7a3fc4d999853" = "Lvmj1oLuXmktrboXh/BrXqLPf8FxSCXIf99GnBXu0Bk=";
4848
"https://github.com/daleiz/rocksdb-haskell"."109af08f95b40f458d4933e3725ecb3e59337c39" = "1i1ya491fapa0g96527krarv0w0iybizqcz518741iw06hhpikiy";
4949
};

0 commit comments

Comments
 (0)