|
| 1 | +# GeniusYield DEX Contracts API |
| 2 | + |
| 3 | +This repository hosts off-chain code to interact with DEX smart contracts. |
| 4 | + |
| 5 | +Main file of interest is [`PartialOrder.hs`](./src/GeniusYield/Api/Dex/PartialOrder.hs). |
| 6 | + |
| 7 | +## Order creation |
| 8 | + |
| 9 | +https://github.com/geniusyield/dex-contracts-api/blob/cdc81e96ee45411786fa160bab51eff1bc281316/src/GeniusYield/Api/Dex/PartialOrder.hs#L429-L542 |
| 10 | + |
| 11 | +```haskell |
| 12 | +placePartialOrder |
| 13 | + ∷ GYDexApiMonad m a |
| 14 | + ⇒ PORefs |
| 15 | + → GYAddress |
| 16 | + -- ^ Order owner |
| 17 | + → (Natural, GYAssetClass) |
| 18 | + -- ^ Amount and asset to offer. |
| 19 | + → GYAssetClass |
| 20 | + -- ^ The asset being asked for as payment. |
| 21 | + → GYRational |
| 22 | + -- ^ The price for one unit of the offered asset. |
| 23 | + → Maybe GYTime |
| 24 | + -- ^ The earliest time when the order can be filled (optional). |
| 25 | + → Maybe GYTime |
| 26 | + -- ^ The latest time when the order can be filled (optional). |
| 27 | + → Maybe GYStakeCredential |
| 28 | + -- ^ Stake credential of user. We do not support pointer reference. |
| 29 | + → m (GYTxSkeleton 'PlutusV2) |
| 30 | +placePartialOrder por@PORefs {..} addr (offerAmt, offerAC) priceAC price start end stakeCred = do |
| 31 | + (cfgRef, pocd) ← fetchPartialOrderConfig porRefNft |
| 32 | + placePartialOrder' por addr (offerAmt, offerAC) priceAC price start end 0 0 stakeCred cfgRef pocd |
| 33 | + |
| 34 | +placePartialOrder' |
| 35 | + ∷ (GYDexApiMonad m a, HasCallStack) |
| 36 | + ⇒ PORefs |
| 37 | + → GYAddress |
| 38 | + -- ^ Order owner |
| 39 | + → (Natural, GYAssetClass) |
| 40 | + -- ^ Amount and asset to offer. |
| 41 | + → GYAssetClass |
| 42 | + -- ^ The asset being asked for as payment. |
| 43 | + → GYRational |
| 44 | + -- ^ The price for one unit of the offered asset. |
| 45 | + → Maybe GYTime |
| 46 | + -- ^ The earliest time when the order can be filled (optional). |
| 47 | + → Maybe GYTime |
| 48 | + -- ^ The latest time when the order can be filled (optional). |
| 49 | + → Natural |
| 50 | + -- ^ Additional lovelace fee. |
| 51 | + → Natural |
| 52 | + -- ^ Additional fee in offered tokens. |
| 53 | + → Maybe GYStakeCredential |
| 54 | + -- ^ Stake credential of user. We do not support pointer reference. |
| 55 | + → GYTxOutRef |
| 56 | + → PartialOrderConfigInfoF GYAddress |
| 57 | + → m (GYTxSkeleton 'PlutusV2) |
| 58 | +placePartialOrder' por@PORefs {..} addr (offerAmt, offerAC) priceAC price start end addLov addOff stakeCred cfgRef pocd = do |
| 59 | + when (offerAmt == 0) $ throwAppError $ PodNonPositiveAmount $ toInteger offerAmt |
| 60 | + when (price <= 0) $ throwAppError $ PodNonPositivePrice price |
| 61 | + when (offerAC == priceAC) $ throwAppError $ PodNonDifferentAssets offerAC |
| 62 | + |
| 63 | + case (start, end) of |
| 64 | + (Just start', Just end') → when (end' < start') $ throwAppError $ PodEndEarlierThanStart start' end' |
| 65 | + _ → pure () |
| 66 | + |
| 67 | + pkh ← addressToPubKeyHash' addr |
| 68 | + outAddr ← partialOrderAddr por |
| 69 | + nid ← networkId |
| 70 | + let outAddr' = addressFromCredential nid (addressToPaymentCredential outAddr & fromJust) stakeCred |
| 71 | + policy ← partialOrderNftPolicy por |
| 72 | + nftRef ← someUTxOWithoutRefScript |
| 73 | + |
| 74 | + let nftName = gyExpectedTokenName nftRef |
| 75 | + nftRedeemer = mkNftRedeemer $ Just nftRef |
| 76 | + nft = GYToken (mintingPolicyId policy) nftName |
| 77 | + nftInput = |
| 78 | + GYTxIn |
| 79 | + { gyTxInTxOutRef = nftRef, |
| 80 | + gyTxInWitness = GYTxInWitnessKey |
| 81 | + } |
| 82 | + nftV = valueSingleton nft 1 |
| 83 | + offerAmt' = toInteger offerAmt |
| 84 | + makerFeeFlat = fromIntegral addLov + pociMakerFeeFlat pocd |
| 85 | + makerFeeOff = (+) (fromIntegral addOff) $ ceiling $ toRational offerAmt * rationalToGHC (pociMakerFeeRatio pocd) |
| 86 | + makerFee = |
| 87 | + valueFromLovelace makerFeeFlat |
| 88 | + <> valueSingleton offerAC makerFeeOff |
| 89 | + offerV = |
| 90 | + valueSingleton offerAC offerAmt' |
| 91 | + <> nftV |
| 92 | + <> valueFromLovelace (toInteger $ pociMinDeposit pocd) |
| 93 | + <> makerFee |
| 94 | + containedFee = |
| 95 | + PartialOrderContainedFee |
| 96 | + { pocfLovelaces = makerFeeFlat, |
| 97 | + pocfOfferedTokens = makerFeeOff, |
| 98 | + pocfAskedTokens = 0 |
| 99 | + } |
| 100 | + od = |
| 101 | + PartialOrderDatum |
| 102 | + { podOwnerKey = pubKeyHashToPlutus pkh, |
| 103 | + podOwnerAddr = addressToPlutus addr, |
| 104 | + podOfferedAsset = assetClassToPlutus offerAC, |
| 105 | + podOfferedOriginalAmount = offerAmt', |
| 106 | + podOfferedAmount = offerAmt', |
| 107 | + podAskedAsset = assetClassToPlutus priceAC, |
| 108 | + podPrice = rationalToPlutus price, |
| 109 | + podNFT = tokenNameToPlutus nftName, |
| 110 | + podStart = timeToPlutus <$> start, |
| 111 | + podEnd = timeToPlutus <$> end, |
| 112 | + podPartialFills = 0, |
| 113 | + podMakerLovelaceFlatFee = makerFeeFlat, |
| 114 | + podTakerLovelaceFlatFee = pociTakerFee pocd, |
| 115 | + podContainedFee = containedFee, |
| 116 | + podContainedPayment = 0 |
| 117 | + } |
| 118 | + |
| 119 | + o = mkGYTxOut outAddr' offerV (datumFromPlutusData od) |
| 120 | + |
| 121 | + return |
| 122 | + $ mustHaveInput nftInput |
| 123 | + <> mustHaveOutput o |
| 124 | + <> mustMint (GYMintReference porMintRef $ mintingPolicyToScript policy) nftRedeemer nftName 1 |
| 125 | + <> mustHaveRefInput cfgRef |
| 126 | +``` |
0 commit comments