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 fix swap orders parsing #106

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
fcac32d
Add path delimiter
Bromel777 Jun 5, 2023
4d895f2
Fix incorrect balancing check
Bromel777 Jun 12, 2023
9d750e9
remove resourceT
Bromel777 Jun 19, 2023
0cdf6ab
fix pool parsing
Bromel777 Jul 20, 2023
48a7ab0
add debug
Bromel777 Jul 20, 2023
c7d67ab
fix swap op operation
Bromel777 Jul 20, 2023
5efbb44
update pool uplc
Bromel777 Jul 23, 2023
988719b
fix deps
Bromel777 Jul 23, 2023
c86d2dc
add debug
Bromel777 Jul 25, 2023
f70b66a
add unsafe order execution
Bromel777 Jul 29, 2023
7d926a4
add unsafe finalizeTx
Bromel777 Jul 29, 2023
07cecc4
fix Transactions
Bromel777 Jul 29, 2023
326adea
update unsafe collateral estimation logic
Bromel777 Jul 30, 2023
a849d6c
add debug
Bromel777 Jul 30, 2023
aca7102
change return collateral
Bromel777 Jul 30, 2023
936a286
remove debug
Bromel777 Jul 30, 2023
e91e1b2
increase fee
Bromel777 Jul 30, 2023
d7a8acc
debug for exUnits
Bromel777 Jul 30, 2023
2c5a6ed
add substituteExecutionUnitsUnsafe
Bromel777 Jul 30, 2023
fef1fda
update exunits
Bromel777 Jul 30, 2023
5126336
add UnsafeEvalConfig
Bromel777 Jul 30, 2023
d67876e
add cfg to substituteExecutionUnitsUnsafe
Bromel777 Jul 30, 2023
fcadfb4
fix reward addr
Bromel777 Jul 30, 2023
f5687fd
add v2
Bromel777 Aug 29, 2023
6369658
cleanup
Bromel777 Aug 29, 2023
3ecac97
add json instances for v1|v2
Bromel777 Aug 29, 2023
bb3299d
PoolValidator V2
Bromel777 Sep 1, 2023
5984bae
lbsp tx balancer. wip
Bromel777 Sep 15, 2023
d292467
http submit api draft
Bromel777 Sep 15, 2023
b0981e0
cleanuo
Bromel777 Sep 15, 2023
b3cf256
add fromDhall instance for settings
Bromel777 Sep 15, 2023
12bb33e
add content type
Bromel777 Sep 16, 2023
f9253ed
add errors
Bromel777 Sep 16, 2023
7155790
change splitBetween logic
Bromel777 Sep 18, 2023
14a4237
add debug
Bromel777 Sep 21, 2023
c5d03ba
add debug
Bromel777 Sep 21, 2023
20714ae
remove explicit txOuts in txBody2 balancing process
Bromel777 Sep 21, 2023
41e4e45
fix chargeBox merging
Bromel777 Sep 21, 2023
d180e1b
fix filtering utxos in utxoStore
Bromel777 Sep 21, 2023
858b6e3
add debug
Bromel777 Sep 21, 2023
7d627d4
add finalizeTxWithExplFeePolicy
Bromel777 Sep 21, 2023
c940cbc
change tx balancing
Bromel777 Sep 21, 2023
cb4c9f1
remove utxoStore usage for lbsp purposes
Bromel777 Sep 21, 2023
97154a1
fix filterSpentedUtxos
Bromel777 Sep 21, 2023
6f447da
mkSafeGetRequest
Bromel777 Sep 22, 2023
051a2a0
add debug
Bromel777 Sep 22, 2023
8fa2cba
add selectUtxosStrictLbsp
Bromel777 Sep 22, 2023
f50c30a
change logic of selectUtxosLbsp
Bromel777 Sep 25, 2023
39fe9de
remove condition of incorrect pool parsing
Bromel777 Sep 28, 2023
ea1fd6c
add swap debug
Bromel777 Oct 31, 2023
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: c25c8ab7daf52871d29efd00d3dac236cc9d6a36
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
16 changes: 14 additions & 2 deletions dex-core/src/ErgoDex/Amm/Orders.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import ErgoDex.Contracts.Types
import ErgoDex.Contracts.Proxy.Swap
import ErgoDex.Contracts.Proxy.Deposit
import ErgoDex.Contracts.Proxy.Redeem
import Debug.Trace

data Swap = Swap
{ swapPoolId :: PoolId
Expand All @@ -37,8 +38,10 @@ data Swap = Swap
, swapRewardSPkh :: Maybe StakePubKeyHash
} deriving (Show, Eq, Generic, ToJSON, FromJSON)

-- 10 000 000 000 000 000

instance FromLedger Swap where
parseFromLedger fout@FullTxOut{fullTxOutDatum=(KnownDatum (Datum d)), ..} =
parseFromLedger fout@FullTxOut{fullTxOutDatum=(KnownDatum (Datum d)), fullTxOutRef, ..} =
case fromBuiltinData d of
(Just SwapConfig{..}) -> do
let
Expand All @@ -48,6 +51,15 @@ instance FromLedger Swap where
if isAda swapBase
then baseAmount + divide (minQuoteAmount * exFeePerTokenNum) exFeePerTokenDen
else baseAmount
traceM $ "Found swap config for " ++ show (PoolId $ Coin poolNft)
traceM $ "baseIn " ++ show baseIn
traceM $ "baseAmount" ++ show baseAmount
traceM $ "minQuoteAmount" ++ show minQuoteAmount
traceM $ "exFeePerTokenNum" ++ show exFeePerTokenNum
traceM $ "exFeePerTokenDen" ++ show exFeePerTokenDen
traceM $ "divide (minQuoteAmount * exFeePerTokenNum) exFeePerTokenDen" ++ show (divide (minQuoteAmount * exFeePerTokenNum) exFeePerTokenDen)
traceM $ "minBase " ++ show minBase
traceM $ "(unAmount baseIn < minBase) " ++ show (unAmount baseIn < minBase)
when (unAmount baseIn < minBase) Nothing
Just $ OnChain fout Swap
{ swapPoolId = PoolId $ Coin poolNft
Expand Down Expand Up @@ -78,7 +90,7 @@ instance FromLedger Deposit where
(Just DepositConfig{..}) -> do
let adaIn = Ada.getLovelace $ Ada.fromValue fullTxOutValue
adaDeclared = exFee + collateralAda
when (adaIn < adaDeclared) Nothing
-- when (adaIn < adaDeclared) Nothing
case extractPairValue fullTxOutValue of
[assetX, assetY] ->
Just $ OnChain fout Deposit
Expand Down
112 changes: 67 additions & 45 deletions dex-core/src/ErgoDex/Amm/Pool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ 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)
import Debug.Trace

import CardanoTx.Models
( FullTxOut(FullTxOut, fullTxOutDatum, fullTxOutValue,
Expand All @@ -29,8 +31,12 @@ import ErgoDex.Validators
import qualified ErgoDex.Contracts.Typed as S
import ErgoDex.Contracts.Types
import qualified ErgoDex.Contracts.Proxy.Order as W
import ErgoDex.Contracts.Pool
import ErgoDex.Contracts.Pool
( PoolConfig(PoolConfig, lqBound, stakeAdminPolicy, poolFeeNum,
poolLq, poolY, poolX, poolNft),
maxLqCapAmount )
import ErgoDex.Amm.Constants (minSafeOutputAmount)
import Debug.Trace

newtype PoolId = PoolId { unPoolId :: Coin Nft }
deriving (Show, Eq, Generic)
Expand All @@ -42,55 +48,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
instance ToLedger (PoolValidator ver) 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 +116,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,21 +147,24 @@ 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)


applyDeposit :: PoolValidator V1 -> Pool -> (Amount X, Amount Y) -> Predicted Pool
applyDeposit :: PoolValidator ver -> Pool -> (Amount X, Amount Y) -> Predicted Pool
applyDeposit poolValidator p@Pool{..} (inX, inY) =
Predicted nextPoolOut nextPool
where
Expand Down Expand Up @@ -170,7 +192,7 @@ rewardLp p@Pool{poolLiquidity=(Amount lq), poolReservesX=(Amount poolX), poolRes
else (Amount $ (minByX - minByY) * poolX `div` lq, Amount 0)
unlockedLq = Amount (min minByX minByY)

applyRedeem :: PoolValidator V1 -> Pool -> Amount Liquidity -> Predicted Pool
applyRedeem :: PoolValidator ver -> Pool -> Amount Liquidity -> Predicted Pool
applyRedeem poolValidator p@Pool{..} burnedLq =
Predicted nextPoolOut nextPool
where
Expand All @@ -183,8 +205,8 @@ applyRedeem poolValidator p@Pool{..} burnedLq =
}
nextPoolOut = toLedger poolValidator nextPool

applySwap :: PoolValidator V1 -> Pool -> AssetAmount Base -> Predicted Pool
applySwap poolValidator p@Pool{..} base =
applySwap :: PoolValidator ver -> Pool -> AssetAmount Base -> Predicted Pool
applySwap poolValidator p@Pool{..} base = do
Predicted nextPoolOut nextPool
where
xy = unCoin (getAsset base) == unCoin poolCoinX
Expand Down
Loading