-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #297 from mlabs-haskell/nazrhom/fee-escrow
Nazrhom/fee escrow
- Loading branch information
Showing
13 changed files
with
262 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{-# OPTIONS_GHC -fno-specialise #-} | ||
|
||
module HydraAuction.OnChain.FeeEscrow (mkFeeEscrowValidator) where | ||
|
||
-- Prelude imports | ||
import PlutusTx.Prelude | ||
|
||
-- import Prelude (quot) | ||
|
||
-- Plutus imports | ||
import Plutus.V1.Ledger.Address (pubKeyHashAddress, scriptHashAddress) | ||
import Plutus.V1.Ledger.Value (valueOf) | ||
import Plutus.V2.Ledger.Api (TxInfo (..), TxOut (..), Value, adaSymbol, adaToken, scriptContextTxInfo, txInInfoResolved) | ||
import Plutus.V2.Ledger.Contexts (ScriptContext, ownHash) | ||
|
||
-- Hydra auction imports | ||
import HydraAuction.Types ( | ||
AuctionTerms (..), | ||
FeeEscrowDatum, | ||
FeeEscrowRedeemer (..), | ||
) | ||
import HydraAuctionUtils.Plutus ( | ||
byAddress, | ||
nothingForged, | ||
) | ||
import HydraAuctionUtils.Types.Natural (naturalToInt) | ||
|
||
{-# INLINEABLE mkFeeEscrowValidator #-} | ||
mkFeeEscrowValidator :: AuctionTerms -> FeeEscrowDatum -> FeeEscrowRedeemer -> ScriptContext -> Bool | ||
mkFeeEscrowValidator terms () DistributeFees context = | ||
-- There is one input spent from the fee escrow validator with enough ADA to pay the fees | ||
adaValueOf singleFeeInputValue >= expectedFeePerDelegeate * length (delegates terms) | ||
-- Every delegate is payed at least the expected proportion of fee | ||
&& all receivesProportionOfFee (delegates terms) | ||
-- No tokens are minted or burned. | ||
&& nothingForged info | ||
where | ||
info :: TxInfo | ||
info = scriptContextTxInfo context | ||
|
||
outputs :: [TxOut] | ||
outputs = txInfoOutputs info | ||
|
||
adaValueOf v = valueOf v adaSymbol adaToken | ||
|
||
expectedFeePerDelegeate = naturalToInt $ auctionFeePerDelegate terms | ||
|
||
receivesProportionOfFee delegatePKH = case byAddress (pubKeyHashAddress delegatePKH) outputs of | ||
[] -> traceError "Delegate does not receive proportion of fee" | ||
outsToDelegate -> | ||
traceIfFalse "Delegate does not receive proportion of fee" $ | ||
any (\txOut -> adaValueOf (txOutValue txOut) >= naturalToInt (auctionFeePerDelegate terms)) outsToDelegate | ||
|
||
singleFeeInputValue :: Value | ||
singleFeeInputValue = case byAddress (scriptHashAddress $ ownHash context) $ txInInfoResolved <$> txInfoInputs info of | ||
[] -> traceError "Missing input for fee escrow" | ||
[feeOut] -> txOutValue feeOut | ||
_ : _ -> traceError "More than single input from fee escrow validator" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
module HydraAuction.Tx.FeeEscrow ( | ||
distributeFee, | ||
) where | ||
|
||
-- Prelude imports | ||
import Prelude | ||
|
||
-- Haskell imports | ||
import Control.Monad (void) | ||
|
||
-- Plutus imports | ||
import Plutus.V1.Ledger.Address (pubKeyHashAddress) | ||
import Plutus.V2.Ledger.Api (PubKeyHash) | ||
|
||
-- Cardano node imports | ||
import Cardano.Api.UTxO qualified as UTxO | ||
|
||
-- Hydra imports | ||
import Hydra.Cardano.Api ( | ||
Lovelace (..), | ||
Value, | ||
lovelaceToValue, | ||
txOutValue, | ||
valueToLovelace, | ||
pattern ReferenceScriptNone, | ||
pattern TxMintValueNone, | ||
pattern TxOut, | ||
pattern TxOutDatumNone, | ||
) | ||
|
||
-- Hydra auction imports | ||
import HydraAuction.OnChain (AuctionScript (..)) | ||
import HydraAuction.Tx.Common ( | ||
scriptPlutusScript, | ||
scriptUtxos, | ||
) | ||
import HydraAuction.Types ( | ||
AuctionTerms (..), | ||
FeeEscrowRedeemer (..), | ||
) | ||
import HydraAuctionUtils.L1.Runner (L1Runner) | ||
import HydraAuctionUtils.Monads ( | ||
fromPlutusAddressInMonad, | ||
logMsg, | ||
) | ||
import HydraAuctionUtils.Monads.Actors ( | ||
actorTipUtxo, | ||
addressAndKeys, | ||
) | ||
import HydraAuctionUtils.Tx.AutoCreateTx ( | ||
AutoCreateParams (..), | ||
autoSubmitAndAwaitTx, | ||
) | ||
import HydraAuctionUtils.Tx.Build ( | ||
mkInlinedDatumScriptWitness, | ||
) | ||
import HydraAuctionUtils.Tx.Utxo ( | ||
filterAdaOnlyUtxo, | ||
) | ||
|
||
distributeFee :: AuctionTerms -> L1Runner () | ||
distributeFee terms = do | ||
logMsg "Distributing fees to delegates" | ||
|
||
(actorAddress, _, actorSk) <- addressAndKeys | ||
|
||
actorMoneyUtxo <- filterAdaOnlyUtxo <$> actorTipUtxo | ||
|
||
feeEscrowUtxo <- scriptUtxos FeeEscrow terms | ||
|
||
feeEscrowValue <- case UTxO.pairs feeEscrowUtxo of | ||
[(_, txOut)] -> pure $ txOutValue txOut | ||
_ -> fail "wrong number of utxos in fee escrow script" | ||
|
||
lovelaceAmt <- case valueToLovelace feeEscrowValue of | ||
Just l -> pure l | ||
Nothing -> fail "fee escrow asset does not contain ada-only" | ||
|
||
delegateOuts <- mapM mkFeeOut (zipDistributingValue lovelaceAmt (delegates terms)) | ||
|
||
void $ | ||
autoSubmitAndAwaitTx $ | ||
AutoCreateParams | ||
{ signedUtxos = [(actorSk, actorMoneyUtxo)] | ||
, additionalSigners = [] | ||
, referenceUtxo = mempty | ||
, witnessedUtxos = [(feeWitness, feeEscrowUtxo)] | ||
, collateral = Nothing | ||
, outs = delegateOuts | ||
, toMint = TxMintValueNone | ||
, changeAddress = actorAddress | ||
, validityBound = (Nothing, Nothing) | ||
} | ||
where | ||
feeScript = scriptPlutusScript FeeEscrow terms | ||
feeWitness = mkInlinedDatumScriptWitness feeScript DistributeFees | ||
|
||
mkFeeOut (delegatePKH, valuePerDelegate) = do | ||
delegateAddress <- fromPlutusAddressInMonad $ pubKeyHashAddress delegatePKH | ||
pure $ | ||
TxOut | ||
delegateAddress | ||
valuePerDelegate | ||
TxOutDatumNone | ||
ReferenceScriptNone | ||
|
||
zipDistributingValue :: Lovelace -> [PubKeyHash] -> [(PubKeyHash, Value)] | ||
zipDistributingValue (Lovelace lovelaceAmt) delegatePKHs = zip delegatePKHs (lovelaceToValue . Lovelace <$> distributedLovelace) | ||
where | ||
delegateNumber = length delegatePKHs | ||
(quotient, remainder) = divMod lovelaceAmt (toInteger delegateNumber) | ||
|
||
spread _ [] = [] | ||
spread 0 xs = xs | ||
spread k (x : xs) = (x + 1) : spread (k - 1) xs | ||
|
||
distributedLovelace = spread remainder (replicate delegateNumber quotient) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.