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

Dev. Remove resourcet #103

Open
wants to merge 21 commits into
base: dev-deposit-op-fix
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/ergolabs/cardano-dex-contracts
tag: 2fb44f444897d84e313ceb4d3d467441385802dd
tag: b4330de32e2d8be821a8a4fd3fd2d24508c280d7
subdir:
cardano-dex-contracts-offchain

Expand Down
1 change: 1 addition & 0 deletions dex-core/dex-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ library
either,
extra,
transformers,
submit-api,
cardano-api,
text,
serialise,
Expand Down
95 changes: 56 additions & 39 deletions dex-core/src/ErgoDex/Amm/Pool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Ledger
import Ledger.Value (assetClassValue, assetClassValueOf)
import PlutusTx.IsData.Class
import PlutusTx.Sqrt
import Plutus.V1.Ledger.Api (StakingCredential(..))
import PlutusTx.Numeric (AdditiveMonoid(zero))
import Ledger.Ada (lovelaceValueOf)
import Plutus.Script.Utils.V2.Address (mkValidatorAddress)
Expand Down Expand Up @@ -42,55 +43,65 @@ data PoolFee = PoolFee
} deriving (Show, Eq, Generic, FromJSON, ToJSON)

data Pool = Pool
{ poolId :: PoolId
, poolReservesX :: Amount X
, poolReservesY :: Amount Y
, poolLiquidity :: Amount Liquidity
, poolCoinX :: Coin X
, poolCoinY :: Coin Y
, poolCoinLq :: Coin Liquidity
, poolFee :: PoolFee
, outCollateral :: Amount Lovelace
{ poolId :: PoolId
, poolReservesX :: Amount X
, poolReservesY :: Amount Y
, poolLiquidity :: Amount Liquidity
, poolCoinX :: Coin X
, poolCoinY :: Coin Y
, poolCoinLq :: Coin Liquidity
, poolFee :: PoolFee
, outCollateral :: Amount Lovelace
, stakeAdminPolicy :: [CurrencySymbol]
, lqBound :: Amount X
, stakeCred :: Maybe StakingCredential
} deriving (Show, Eq, Generic, FromJSON, ToJSON)

feeDen :: Integer
feeDen = 1000

instance FromLedger Pool where
parseFromLedger fout@FullTxOut{fullTxOutDatum=(KnownDatum (Datum d)), ..} = --todo add also check for address
parseFromLedger fout@FullTxOut{fullTxOutDatum=(KnownDatum (Datum d)), fullTxOutAddress=Address{..}, ..} = --todo add also check for address
case fromBuiltinData d of
(Just PoolConfig{..}) -> do
let
rx = Amount $ assetClassValueOf fullTxOutValue poolX
ry = Amount $ assetClassValueOf fullTxOutValue poolY
rlq = Amount $ assetClassValueOf fullTxOutValue poolLq
nft = Amount $ assetClassValueOf fullTxOutValue poolNft
lq = maxLqCapAmount - rlq -- actual LQ emission
rx = Amount $ assetClassValueOf fullTxOutValue poolX
ry = Amount $ assetClassValueOf fullTxOutValue poolY
rlq = Amount $ assetClassValueOf fullTxOutValue poolLq
nft = Amount $ assetClassValueOf fullTxOutValue poolNft
lqBoundAmount = Amount lqBound
lq = maxLqCapAmount - rlq -- actual LQ emission
collateral = if W.isAda poolX || W.isAda poolY then zero else minSafeOutputAmount
when (rx == 0 || ry == 0 || rlq == 0 || nft /= 1) Nothing
Just $ OnChain fout Pool
{ poolId = PoolId $ Coin poolNft
, poolReservesX = rx
, poolReservesY = ry
, poolLiquidity = lq
, poolCoinX = Coin poolX
, poolCoinY = Coin poolY
, poolCoinLq = Coin poolLq
, poolFee = PoolFee poolFeeNum feeDen
, outCollateral = collateral
{ poolId = PoolId $ Coin poolNft
, poolReservesX = rx
, poolReservesY = ry
, poolLiquidity = lq
, poolCoinX = Coin poolX
, poolCoinY = Coin poolY
, poolCoinLq = Coin poolLq
, poolFee = PoolFee poolFeeNum feeDen
, outCollateral = collateral
, stakeAdminPolicy = stakeAdminPolicy
, lqBound = lqBoundAmount
, stakeCred = addressStakingCredential
}
_ -> Nothing
parseFromLedger _ = Nothing

instance ToLedger PoolValidatorV1 Pool where
toLedger (PoolValidator poolValidator) Pool{..} =
TxOutCandidate
{ txOutCandidateAddress = mkValidatorAddress poolValidator
{ txOutCandidateAddress = poolAddress
, txOutCandidateValue = poolValue
, txOutCandidateDatum = KnownDatum $ Datum $ toBuiltinData poolConf
, txOutCandidateRefScript = Nothing
}
where
poolAddress = (mkValidatorAddress poolValidator) {
addressStakingCredential = stakeCred
}
nft = unPoolId poolId
poolLqReserves = maxLqCapAmount - poolLiquidity
poolValue = assetClassValue (unCoin nft) 1 <>
Expand All @@ -100,18 +111,21 @@ instance ToLedger PoolValidatorV1 Pool where
lovelaceValueOf (unAmount outCollateral)

poolConf = PoolConfig
{ poolNft = unCoin nft
, poolX = unCoin poolCoinX
, poolY = unCoin poolCoinY
, poolLq = unCoin poolCoinLq
, poolFeeNum = poolFeeNum' poolFee
{ poolNft = unCoin nft
, poolX = unCoin poolCoinX
, poolY = unCoin poolCoinY
, poolLq = unCoin poolCoinLq
, poolFeeNum = poolFeeNum' poolFee
, stakeAdminPolicy = stakeAdminPolicy
, lqBound = unAmount lqBound
}

data PoolInitError
= InvalidLiquidity Integer
| InsufficientInitialLiqudity (Amount Liquidity)
deriving (Show, Eq)

-- todo: remove me
initPool
:: PoolValidator V1
-> S.PoolConfig
Expand All @@ -128,15 +142,18 @@ initPool poolValidator S.PoolConfig{..} burnLq (inX, inY) = do
then zero
else minSafeOutputAmount
pool = Pool
{ poolId = PoolId poolNft
, poolReservesX = inX
, poolReservesY = inY
, poolLiquidity = releasedLq
, poolCoinX = poolX
, poolCoinY = poolY
, poolCoinLq = poolLq
, poolFee = PoolFee poolFeeNum feeDen
, outCollateral = outCollateral
{ poolId = PoolId poolNft
, poolReservesX = inX
, poolReservesY = inY
, poolLiquidity = releasedLq
, poolCoinX = poolX
, poolCoinY = poolY
, poolCoinLq = poolLq
, poolFee = PoolFee poolFeeNum feeDen
, stakeAdminPolicy = []
, lqBound = 10000
, outCollateral = outCollateral
, stakeCred = Nothing
}
poolOut = toLedger poolValidator pool
pure (Predicted poolOut pool, releasedLq)
Expand Down
Loading