Skip to content

Commit

Permalink
Updates for the removal of minFill
Browse files Browse the repository at this point in the history
  • Loading branch information
Micrograx committed Nov 2, 2023
1 parent 7d0429e commit 4aca2ef
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 194 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ that is completely placed on the blockchain. That is the validator and minting p
{
"validatorRefs":{
"refAddr":"addr_test1wpgexmeunzsykesf42d4eqet5yvzeap6trjnflxqtkcf66g0kpnxt",
"refNftAC":"fae686ea8f21d567841d703dea4d4221c2af071a6f2b433ff07c0af2.e6a295bb83d06f53fcf91151f54acec0a63fbd6f0d924206d5d012e6da3b72af",
"refNftUtxoRef":"39f987a6beb9cc4c45bba149a21c28068f640f3593f15f8157f0b6022b431977#0",
"scriptRef":"39f987a6beb9cc4c45bba149a21c28068f640f3593f15f8157f0b6022b431977#1",
"nftPolicyRef":"39f987a6beb9cc4c45bba149a21c28068f640f3593f15f8157f0b6022b431977#0"
"refNftAC":"fae686ea8f21d567841d703dea4d4221c2af071a6f2b433ff07c0af2.6af3807634905a04be64a8dbe0ddb9da6ce52eed23cb428c7be5d6114eacc189",
"refNftUtxoRef":"aaaae7e568aab31db6f74faaf550867d58b2361868324567470862ecaaac7646#0",
"scriptRef":"aaaae7e568aab31db6f74faaf550867d58b2361868324567470862ecaaac7646#1",
"nftPolicyRef":"aaaae7e568aab31db6f74faaf550867d58b2361868324567470862ecaaac7646#0"
}
}
```
Expand Down
8 changes: 4 additions & 4 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ package strict-containers
source-repository-package
type: git
location: https://github.com/maestro-org/haskell-sdk
tag: 3c4efccb00df8e9fe0313dfb5228e294caa1807d
--sha256: sha256-H7SsK0ri7cmE9d9cUhFZu24s1FWrvMuVlKzplFfBe+4=
tag: 3865f4465f05323a378dbed257c34450923e8c99
--sha256: sha256-nQnvml8eelTw7HpzByC5Rv5XUD74oLJucRRg8OFz71s=

-- Unfortunately, cardano-node 8.1.2 is constrained with plutus-ledger-api 1.5.0.0 and we would like at least 1.6.0.0.
-- This is done in accordance with changes in https://github.com/input-output-hk/cardano-ledger/pull/3430/files.
Expand Down Expand Up @@ -64,8 +64,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/geniusyield/atlas
tag: v0.3.0
--sha256: sha256-55nIF4S6TyrI1onWH6bu7WDynqHophMZ+QCyw8IFsfQ=
tag: 014f8b4ab5b21b1c54165716f3cb6e800b6defb9
--sha256: sha256-1QhPygU0MyXO9QvJkJD4awNmWEhj79jSFbQBj0FQNfM=

source-repository-package
type: git
Expand Down
4 changes: 2 additions & 2 deletions compiled-scripts/minting-policy

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions compiled-scripts/partial-order

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions config-files/bot-config.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"signingKeyFP":"bot.skey",
"nftMintingPolicyFP":"compiled-scripts/minting-policy",
"orderValidatorFP":"compiled-scripts/partial-order",
"validatorRefs":{
"refAddr":"addr_test1wpgexmeunzsykesf42d4eqet5yvzeap6trjnflxqtkcf66g0kpnxt",
"refNftAC":"fae686ea8f21d567841d703dea4d4221c2af071a6f2b433ff07c0af2.e6a295bb83d06f53fcf91151f54acec0a63fbd6f0d924206d5d012e6da3b72af",
"refNftUtxoRef":"39f987a6beb9cc4c45bba149a21c28068f640f3593f15f8157f0b6022b431977#0",
"scriptRef":"39f987a6beb9cc4c45bba149a21c28068f640f3593f15f8157f0b6022b431977#1",
"nftPolicyRef":"39f987a6beb9cc4c45bba149a21c28068f640f3593f15f8157f0b6022b431977#0"
},
"strategy":"OneSellToManyBuy",
"scanDelay":40000000,
"maxOrderMatches":5,
"maxTxsPerIteration":5,
"randomizeMatchesFound":true,
"scanTokens":[
{
"commodityAsset":"c6e65ba7878b2f8ea0ad39287d3e2fd256dc5c4160fc19bdf4c4d87e.7447454e53",
"currencyAsset":"lovelace"
}
]
"signingKeyFP": "bot.skey",
"nftMintingPolicyFP": "compiled-scripts/minting-policy",
"orderValidatorFP": "compiled-scripts/partial-order",
"validatorRefs": {
"refAddr": "addr_test1wpgexmeunzsykesf42d4eqet5yvzeap6trjnflxqtkcf66g0kpnxt",
"refNftAC": "fae686ea8f21d567841d703dea4d4221c2af071a6f2b433ff07c0af2.6af3807634905a04be64a8dbe0ddb9da6ce52eed23cb428c7be5d6114eacc189",
"refNftUtxoRef": "aaaae7e568aab31db6f74faaf550867d58b2361868324567470862ecaaac7646#0",
"scriptRef": "aaaae7e568aab31db6f74faaf550867d58b2361868324567470862ecaaac7646#1",
"nftPolicyRef": "aaaae7e568aab31db6f74faaf550867d58b2361868324567470862ecaaac7646#0"
},
"strategy": "OneSellToManyBuy",
"scanDelay": 40000000,
"maxOrderMatches": 5,
"maxTxsPerIteration": 5,
"randomizeMatchesFound": true,
"scanTokens": [
{
"commodityAsset": "c6e65ba7878b2f8ea0ad39287d3e2fd256dc5c4160fc19bdf4c4d87e.7447454e53",
"currencyAsset": "lovelace"
}
]
}
2 changes: 1 addition & 1 deletion geniusyield-dex-api/src/GeniusYield/DEX/Api/Constants.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ import GeniusYield.Imports ( Natural )
-- | Altering this constant will result in a modification of the
-- validator address.
minDeposit :: Natural
minDeposit = 2_000_000
minDeposit = 2_100_000
96 changes: 36 additions & 60 deletions geniusyield-dex-api/src/GeniusYield/DEX/Api/PartialOrder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,24 @@ data PartialOrderDatum = PartialOrderDatum
-- ^ The asset being offered.
, podOfferedOriginalAmount :: !Integer
-- ^ Original number of units being offered. Initially, this would be same as `podOfferedAmount`.
, podOfferedAmount :: !Integer
, podOfferedAmount :: !Integer
-- ^ The number of units being offered.
, podAskedAsset :: !Plutus.AssetClass
, podAskedAsset :: !Plutus.AssetClass
-- ^ The asset being asked for as payment.
, podPrice :: !PlutusTx.Rational
, podPrice :: !PlutusTx.Rational
-- ^ The price for one unit of the offered asset.
, podMinFilling :: !Integer
-- ^ Minimal number of units of the offered asset that must be paid for in a partial filling.
, podNFT :: !Plutus.TokenName
, podNFT :: !Plutus.TokenName
-- ^ Token name of the NFT identifying this order.
, podStart :: !(Maybe Plutus.POSIXTime)
, podStart :: !(Maybe Plutus.POSIXTime)
-- ^ The time when the order can earliest be filled (optional).
, podEnd :: !(Maybe Plutus.POSIXTime)
, podEnd :: !(Maybe Plutus.POSIXTime)
-- ^ The time when the order can latest be filled (optional).
, podFee :: Integer
-- ^ Fee the filler is entitled to take.
, podPartialFills :: Integer
, podPartialFills :: !Integer
-- ^ Number of partial fills order has undergone, initially would be 0.
}
deriving stock (Show)

PlutusTx.makeIsDataIndexed ''PartialOrderDatum [ ('PartialOrderDatum, 0) ]
PlutusTx.makeIsDataIndexed ''PartialOrderDatum [('PartialOrderDatum, 0)]

-- | Exceptions raised while (partially) filling (partial) orders.
data FillOrderException
Expand All @@ -83,16 +79,15 @@ data FillOrderException
data PodException = PodNftNotAvailable
| PodNonPositiveAmount !Integer
| PodNonPositivePrice !GYRational
| PodNonPositiveMinFilling !Integer
| PodRequestedAmountGreaterOrEqualToOfferedAmount
{ poeReqAmt:: !Natural
, poeOfferedAmount :: !Natural
}
| PodRequestedAmountLessThanMinFilling
{ poeReqAmt:: !Natural
, poeMinFilling :: !Natural
}
| PodFeeNotEnough !Integer
| PodNonDifferentAssets !GYAssetClass
-- ^ Offered asset is same as asked asset.
| PodEndEarlierThanStart
!GYTime -- ^ Start time.
!GYTime -- ^ End time.
deriving stock Show
deriving anyclass (Exception, IsGYApiError)

Expand Down Expand Up @@ -132,20 +127,18 @@ data PartialOrderInfo = PartialOrderInfo
-- ^ The asset being asked for as payment.
, poiPrice :: !GYRational
-- ^ The price for one unit of the offered asset.
, poiMinFilling :: !Natural
-- ^ Minimal number of units of the asked-for asset that must be paid in a partial filling.
, poiNFT :: !GYTokenName
-- ^ Token name of the NFT identifying this partial order.
, poiFeesDeposits :: !Natural
-- ^ Number of lovelace included for fees and deposits.
, poiStart :: !(Maybe GYTime)
-- ^ The time when the order can earliest be filled (optional).
, poiEnd :: !(Maybe GYTime)
-- ^ The time when the order can latest be filled (optional).
, poiFee :: !Natural
-- ^ The fee for each filling.
, poiPartialFills :: !Natural
-- ^ The number of past partial fills.
, poiUTxOValue :: !GYValue
-- ^ Total value in the UTxO.
, poiNFTCS :: !GYMintingPolicyId
-- ^ Caching the CS to avoid recalculating for it.
} deriving stock (Show, Eq, Generic)

-------------------------------------------------------------------------------
Expand Down Expand Up @@ -199,17 +192,13 @@ completelyFillPartialOrder poiSource = do
Left orderRef -> getPartialOrderInfo orderRef
Right poi -> return poi

let price = partialOrderPrice oi poiOfferedAmount
feesAndDeposits = valueFromLovelace
$ max 0
$ toInteger poiFeesDeposits - toInteger poiFee
refScript = maybe mempty mustHaveRefInput (porValidatorRef dexPORefs)
let refScript = maybe mempty mustHaveRefInput (porValidatorRef dexPORefs)
refMinting = maybe mempty mustHaveRefInput (porNftPolicyRef dexPORefs)

cs <- validFillRangeConstraints poiStart poiEnd
return $ mconcat
[ mustHaveInput (partialOrderInfoToIn oi CompleteFill di)
, mustHaveOutput (partialOrderInfoToPayment oi $ price <> feesAndDeposits)
, mustHaveOutput (partialOrderInfoToPayment oi $ expectedValueOut oi poiOfferedAmount)
, mustMint (mintingScript di) nothingRedeemer poiNFT (-1)
, cs
, refScript
Expand All @@ -226,7 +215,7 @@ partiallyFillPartialOrder
-- ^ The amount of offered tokens to buy.
-> m (GYTxSkeleton PlutusV2)
partiallyFillPartialOrder poiSource amt = do
di@DEXInfo{dexNftPolicy, dexPartialOrderValidator, dexPORefs} <- ask
di@DEXInfo{dexPartialOrderValidator, dexPORefs} <- ask

oi@PartialOrderInfo {..} <- case poiSource of
Left orderRef -> getPartialOrderInfo orderRef
Expand All @@ -238,33 +227,18 @@ partiallyFillPartialOrder poiSource amt = do
$ PodNonPositiveAmount $ toInteger amt
when (amt >= poiOfferedAmount) . throwAppError
$ PodRequestedAmountGreaterOrEqualToOfferedAmount amt poiOfferedAmount
when (amt < poiMinFilling) . throwAppError
$ PodRequestedAmountLessThanMinFilling amt poiMinFilling

let od = partialOrderInfoToPartialOrderDatum oi
{ poiOfferedAmount = poiOfferedAmount - amt
, poiPartialFills = poiPartialFills + 1
}
price = partialOrderPrice oi amt
feesAndDeposits = valueFromLovelace
$ max 0
$ toInteger poiFeesDeposits -
toInteger (poiFee + minDeposit)
v = mconcat
[ valueSingleton poiOfferedAsset (toInteger $ poiOfferedAmount - amt)
, feesAndDeposits
, valueSingleton (GYToken (mintingPolicyId dexNftPolicy) poiNFT) 1
]
payment = price <> valueFromLovelace (toInteger minDeposit)

o = mkGYTxOut outAddr v (datumFromPlutusData od)
o = mkGYTxOut outAddr (expectedValueOut oi amt) (datumFromPlutusData od)
refScript = maybe mempty mustHaveRefInput (porValidatorRef dexPORefs)

cs <- validFillRangeConstraints poiStart poiEnd
return $ mconcat
[ mustHaveInput (partialOrderInfoToIn oi (PartialFill $ toInteger amt) di)
, mustHaveOutput o
, mustHaveOutput (partialOrderInfoToPayment oi payment)
, cs
, refScript
, mustHaveRefInput (porRefNftRef dexPORefs)
Expand Down Expand Up @@ -330,11 +304,9 @@ partialOrderInfoToPartialOrderDatum PartialOrderInfo {..} =
, podOfferedAmount = fromIntegral poiOfferedAmount
, podAskedAsset = assetClassToPlutus poiAskedAsset
, podPrice = PlutusTx.fromGHC $ toRational poiPrice
, podMinFilling = fromIntegral poiMinFilling
, podNFT = tokenNameToPlutus poiNFT
, podStart = timeToPlutus <$> poiStart
, podEnd = timeToPlutus <$> poiEnd
, podFee = fromIntegral poiFee
, podPartialFills = fromIntegral poiPartialFills
}

Expand All @@ -347,26 +319,22 @@ makePartialOrderInfo
-> ExceptT GYTxMonadException m PartialOrderInfo
makePartialOrderInfo orderRef (_, v, PartialOrderDatum {..}) = do
DEXInfo{dexNftPolicy} <- ask
addr <- addressFromPlutus' podOwnerAddr
key <- pubKeyHashFromPlutus' podOwnerKey
addr <- addressFromPlutus' podOwnerAddr

key <- pubKeyHashFromPlutus' podOwnerKey

offeredAsset <- assetClassFromPlutus' podOfferedAsset
nft <- tokenNameFromPlutus' podNFT
askedAsset <- assetClassFromPlutus' podAskedAsset

let price = rationalFromPlutus podPrice
feesDeposits = flip valueAssetClass GYLovelace $ v `valueMinus`
valueSingleton offeredAsset podOfferedAmount
let price = rationalFromPlutus podPrice

when (price <= 0) $
throwAppError (PodNonPositivePrice price)

when (valueAssetClass v (GYToken (mintingPolicyId dexNftPolicy) nft) /= 1) $
throwAppError PodNftNotAvailable

when (podFee < 200_000) $
throwAppError (PodFeeNotEnough podFee)

return PartialOrderInfo
{ poiRef = orderRef
, poiOwnerKey = key
Expand All @@ -376,13 +344,12 @@ makePartialOrderInfo orderRef (_, v, PartialOrderDatum {..}) = do
, poiOfferedAmount = fromInteger podOfferedAmount
, poiAskedAsset = askedAsset
, poiPrice = price
, poiMinFilling = fromInteger podMinFilling
, poiNFT = nft
, poiFeesDeposits = fromInteger feesDeposits
, poiStart = timeFromPlutus <$> podStart
, poiEnd = timeFromPlutus <$> podEnd
, poiFee = fromInteger podFee
, poiPartialFills = fromInteger podPartialFills
, poiUTxOValue = v
, poiNFTCS = mintingPolicyId dexNftPolicy
}

validFillRangeConstraints
Expand Down Expand Up @@ -411,3 +378,12 @@ validFillRangeConstraints mstart mend = do
if now <= endSlot
then return $ isInvalidAfter $ min endSlot $ unsafeAdvanceSlot now 120
else throwAppError $ TooLateFill {foeEnd = endSlot, foeNow = now}

expectedValueOut :: PartialOrderInfo -> Natural -> GYValue
expectedValueOut poi@PartialOrderInfo {..} amnt =
poiUTxOValue
<> partialOrderPrice poi amnt `valueMinus` ( valueSingleton poiOfferedAsset (toInteger amnt)
<> if poiOfferedAmount == amnt
then valueSingleton (GYToken poiNFTCS poiNFT) 1
else mempty
)
4 changes: 2 additions & 2 deletions geniusyield-dex-api/src/GeniusYield/DEX/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ mkDEXMintingPolicy mintingPolicyRaw v = mintingPolicyFromPly $ mintingPolicyRaw
# toInteger minDeposit

mkDEXValidator
:: TypedScript 'ValidatorRole '[Address, AssetClass, Integer]
:: TypedScript 'ValidatorRole '[Address, AssetClass]
-> Address
-> GYAssetClass
-> GYValidator PlutusV2
mkDEXValidator validatorRaw addr ac = validatorFromPly $ validatorRaw
# addr
# assetClassToPlutus ac
# fromIntegral minDeposit

mkPORefs
:: GYAddress
-> GYAssetClass
Expand Down
Loading

0 comments on commit 4aca2ef

Please sign in to comment.