@@ -11,6 +11,7 @@ import Ledger
1111import Ledger.Value (assetClassValue , assetClassValueOf )
1212import PlutusTx.IsData.Class
1313import PlutusTx.Sqrt
14+ import Plutus.V1.Ledger.Api (StakingCredential (.. ))
1415import PlutusTx.Numeric (AdditiveMonoid (zero ))
1516import Ledger.Ada (lovelaceValueOf )
1617import 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
5660feeDen :: Integer
5761feeDen = 1000
5862
5963instance 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
8593instance 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
110123data PoolInitError
111124 = InvalidLiquidity Integer
112125 | InsufficientInitialLiqudity (Amount Liquidity )
113126 deriving (Show , Eq )
114127
128+ -- todo: remove me
115129initPool
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
0 commit comments