Skip to content

Commit

Permalink
Manager addresses fix (#50)
Browse files Browse the repository at this point in the history
* updated scripts

* replaced managerPkh with mangerAddress in datums
  • Loading branch information
KateBushueva authored Jul 31, 2023
1 parent 090b9fd commit 7d73f60
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 30 deletions.
2 changes: 1 addition & 1 deletion scripts/fundraisingValidator.plutus

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scripts/protocolValidator.plutus

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Fundraising/Create.purs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import MintingPolicy.NftMinting as NFT
import MintingPolicy.NftRedeemer (PNftRedeemer(..))
import MintingPolicy.VerTokenMinting as VerToken
import MintingPolicy.VerTokenRedeemers (PVerTokenRedeemer(..))
import Protocol.Datum (_protocolFee, _minDuration, _maxDuration, _minAmount, _maxAmount, _managerPkh)
import Protocol.Datum (_protocolFee, _minDuration, _maxDuration, _minAmount, _maxAmount, _managerAddress)
import Protocol.Models (PFundriseConfig(..))
import Protocol.ProtocolScriptInfo (ProtocolScriptInfo(..), getProtocolScriptInfo)
import Protocol.Redeemer (PProtocolRedeemer(..))
Expand Down Expand Up @@ -94,7 +94,7 @@ contract protocolData (CreateFundraisingParams { title, amount, duration }) = do
, frAmount: targetAmount
, frDeadline: deadline
, frFee: view _protocolFee protocolInfo.pDatum
, managerPkh: view _managerPkh protocolInfo.pDatum
, managerAddress: view _managerAddress protocolInfo.pDatum
}

let
Expand Down
8 changes: 4 additions & 4 deletions src/Fundraising/Datum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Fundraising.Datum where

import Ctl.Internal.FromData

import Contract.Address (PaymentPubKeyHash)
import Contract.Address (Address, PaymentPubKeyHash)
import Contract.PlutusData (class HasPlutusSchema, type (:+), type (:=), type (@@), I, PNil, Z, genericToData)
import Contract.Prelude (class Generic, class Show)
import Contract.Time (POSIXTime)
Expand All @@ -23,7 +23,7 @@ newtype PFundraisingDatum = PFundraisingDatum
, frAmount :: BigInt -- amount to raise in Lovelace
, frDeadline :: POSIXTime
, frFee :: BigInt -- percentage
, managerPkh :: PaymentPubKeyHash
, managerAddress :: Address
}

derive instance Generic PFundraisingDatum _
Expand All @@ -48,8 +48,8 @@ instance
:= I POSIXTime
:+ "frFee"
:= I BigInt
:+ "managerPkh"
:= I PaymentPubKeyHash
:+ "managerAddress"
:= I Address
:+ PNil
)
@@ Z
Expand Down
7 changes: 3 additions & 4 deletions src/Fundraising/ReceiveFunds.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import Fundraising.Calculations (calcFee)
import Fundraising.Datum (PFundraisingDatum(..))
import Fundraising.FundraisingScriptInfo (FundraisingScriptInfo(..), getFundraisingScriptInfo, makeFundraising)
import Fundraising.Models (Fundraising(..))
import Shared.OwnCredentials (OwnCredentials(..), getOwnCreds)
import Fundraising.Redeemer (PFundraisingRedeemer(..))
import Fundraising.UserData (FundraisingData(..))
import MintingPolicy.NftMinting as NFT
Expand All @@ -32,6 +31,7 @@ import MintingPolicy.VerTokenMinting as VerToken
import Protocol.UserData (ProtocolData, dataToProtocol)
import Shared.MinAda (minAda)
import Shared.NetworkData (NetworkParams)
import Shared.OwnCredentials (OwnCredentials(..), getOwnCreds, getPkhSkhFromAddress)
import Shared.RunContract (runContractWithResult)
import Shared.Utxo (checkTokenInUTxO)

Expand All @@ -41,7 +41,6 @@ runReceiveFunds onComplete onError pData networkParams fundraisingData =

contract :: ProtocolData -> FundraisingData -> Contract Unit
contract pData (FundraisingData fundraisingData) = do
-- TODO: use mustPayToPubKeyAddress for managerPkh (need stake key hash)
logInfo' "Running receive funds"
protocol <- dataToProtocol pData
let threadTokenCurrency = fundraisingData.frThreadTokenCurrency
Expand All @@ -53,7 +52,7 @@ contract pData (FundraisingData fundraisingData) = do
let
currentFunds = frInfo.frValue
(PFundraisingDatum currentDatum) = frInfo.frDatum
managerPkh = currentDatum.managerPkh
managerPkh /\ managerSkh <- getPkhSkhFromAddress currentDatum.managerAddress
now <- currentTime
let donatedAmount = Value.valueOf currentFunds adaSymbol adaToken - minAda - minAda
when (now <= currentDatum.frDeadline && donatedAmount < currentDatum.frAmount)
Expand Down Expand Up @@ -87,7 +86,7 @@ contract pData (FundraisingData fundraisingData) = do
(Redeemer $ toData $ PBurnNft fr.verTokenName)
verTokenToBurnValue
<> Constraints.mustPayToPubKeyAddress creds.ownPkh creds.ownSkh amountToReceiver
<> Constraints.mustPayToPubKey managerPkh (Value.lovelaceValueOf feeByFundraising)
<> Constraints.mustPayToPubKeyAddress managerPkh managerSkh (Value.lovelaceValueOf feeByFundraising)
<> Constraints.mustValidateIn (from now)
<> Constraints.mustReferenceOutput (fst frInfo.frScriptRef)

Expand Down
4 changes: 2 additions & 2 deletions src/Info/AppInfo.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import Contract.Log (logInfo')
import Contract.Monad (Contract, liftContractM)
import Contract.Utxos (utxosAt)
import Ctl.Internal.Plutus.Types.Transaction (UtxoMap)
import Shared.OwnCredentials (getOwnUserInfo)
import Info.UserData (AppInfo(..))
import Protocol.Models (Protocol)
import Protocol.ProtocolScript (getProtocolValidatorHash)
import Protocol.UserData (ProtocolData, dataToProtocol, getConfigFromProtocolDatum)
import Shared.NetworkData (NetworkParams)
import Shared.OwnCredentials (getOwnUserInfo, getPkhSkhFromAddress)
import Shared.RunContract (runContractWithResult)
import Shared.Utxo (UtxoTuple, extractDatumFromUTxO, getUtxoByNFT)

Expand All @@ -32,7 +32,7 @@ appInfoContract protocolData = do
protocolUtxo <- getProtocolUtxo protocol utxos
protocolDatum <- liftContractM "Impossible to get Protocol Datum" $ extractDatumFromUTxO protocolUtxo
logInfo' $ "Current datum: " <> show protocolDatum
let managerPkh = unwrap >>> _.managerPkh $ protocolDatum
managerPkh /\ _ <- getPkhSkhFromAddress (unwrap protocolDatum).managerAddress
userInfo <- getOwnUserInfo managerPkh

let
Expand Down
8 changes: 4 additions & 4 deletions src/Protocol/CloseProtocol.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Protocol.CloseProtocol where

import Contract.Prelude

import Config.Protocol (mapToProtocolData, readProtocolConfig)
import Contract.Address (getWalletAddressesWithNetworkTag)
import Contract.BalanceTxConstraints (BalanceTxConstraintsBuilder, mustSendChangeToAddress)
import Contract.Log (logInfo')
Expand All @@ -13,17 +14,16 @@ import Contract.TxConstraints as Constraints
import Contract.Value as Value
import Data.Array as Array
import Data.BigInt (fromInt)
import Effect.Aff (launchAff_)
import Effect.Exception (throw)
import Shared.OwnCredentials (OwnCredentials(..), getOwnCreds)
import MintingPolicy.NftMinting as NFT
import MintingPolicy.NftRedeemer (PNftRedeemer(..))
import Protocol.Models (Protocol(..))
import Protocol.ProtocolScriptInfo (ProtocolScriptInfo(..), getProtocolScriptInfo)
import Protocol.Redeemer (PProtocolRedeemer(PCloseProtocol))
import Protocol.UserData (ProtocolData, dataToProtocol)
import Shared.KeyWalletConfig (testnetKeyWalletConfig)
import Config.Protocol (mapToProtocolData, readProtocolConfig)
import Effect.Aff (launchAff_)
import Shared.OwnCredentials (OwnCredentials(..), getOwnCreds, getPkhSkhFromAddress)

runCloseProtocol :: Effect Unit
runCloseProtocol = do
Expand All @@ -36,7 +36,7 @@ contract protocolData = do
logInfo' "Running closeProtocol"
protocol@(Protocol { protocolCurrency, protocolTokenName }) <- dataToProtocol protocolData
(ProtocolScriptInfo protocolInfo) <- getProtocolScriptInfo protocol
let managerPkh = (unwrap protocolInfo.pDatum).managerPkh
managerPkh /\ _ <- getPkhSkhFromAddress (unwrap protocolInfo.pDatum).managerAddress
(OwnCredentials creds) <- getOwnCreds
when (managerPkh /= creds.ownPkh) $ liftEffect $ throw "current user doesn't have permissions to close protocol"
let nftOref = (unwrap protocolInfo.pDatum).tokenOriginRef
Expand Down
12 changes: 6 additions & 6 deletions src/Protocol/Datum.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Protocol.Datum where

import Ctl.Internal.FromData
import Contract.Address (PaymentPubKeyHash)
import Contract.Address (Address)
import Contract.PlutusData (class HasPlutusSchema, type (:+), type (:=), type (@@), I, PNil, Z, genericToData)
import Contract.Prelude (class Generic, class Show)
import Ctl.Internal.ToData (class ToData)
Expand All @@ -20,7 +20,7 @@ newtype PProtocolDatum = PProtocolDatum
, minDuration :: BigInt
, maxDuration :: BigInt
, protocolFee :: BigInt -- the percentage
, managerPkh :: PaymentPubKeyHash
, managerAddress :: Address
, tokenOriginRef :: TransactionInput
}

Expand All @@ -39,8 +39,8 @@ _maxDuration = _Newtype <<< prop (Proxy :: Proxy "maxDuration")
_protocolFee :: Lens' PProtocolDatum BigInt
_protocolFee = _Newtype <<< prop (Proxy :: Proxy "protocolFee")

_managerPkh :: Lens' PProtocolDatum PaymentPubKeyHash
_managerPkh = _Newtype <<< prop (Proxy :: Proxy "managerPkh")
_managerAddress :: Lens' PProtocolDatum Address
_managerAddress = _Newtype <<< prop (Proxy :: Proxy "managerAddress")

_tokenOriginRef :: Lens' PProtocolDatum TransactionInput
_tokenOriginRef = _Newtype <<< prop (Proxy :: Proxy "tokenOriginRef")
Expand All @@ -65,8 +65,8 @@ instance
:= I BigInt
:+ "protocolFee"
:= I BigInt
:+ "managerPkh"
:= I PaymentPubKeyHash
:+ "managerAddress"
:= I Address
:+ "tokenOriginRef"
:= I TransactionInput
:+ PNil
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/StartProtocol.purs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ startProtocol params@(ProtocolConfigParams { minAmountParam, maxAmountParam, min
, minDuration: minDurationParam
, maxDuration: maxDurationParam
, protocolFee: protocolFeeParam
, managerPkh: ownPkh
, managerAddress: ownAddress
, tokenOriginRef: oref
}
nftValue = Value.singleton cs tn one
Expand Down
7 changes: 4 additions & 3 deletions src/Protocol/UpdateProtocol.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import Data.Array (head) as Array
import Data.Lens (view)
import Effect.Aff (launchAff_)
import Effect.Exception (throw)
import Protocol.Datum (PProtocolDatum(..), _managerPkh, _tokenOriginRef)
import Protocol.Datum (PProtocolDatum(..), _managerAddress, _tokenOriginRef)
import Protocol.Models (PProtocolConfig(..))
import Protocol.ProtocolScriptInfo (ProtocolScriptInfo(..), getProtocolScriptInfo)
import Protocol.Redeemer (PProtocolRedeemer(..))
import Protocol.UserData (ProtocolConfigParams, ProtocolData, dataToProtocol, getConfigFromProtocolDatum, mapToProtocolConfig)
import Shared.Config (mapToProtocolConfigParams, readDonatPoolConfig)
import Shared.KeyWalletConfig (testnetKeyWalletConfig)
import Shared.OwnCredentials (getPkhSkhFromAddress)
import Shared.Utxo (getNonCollateralUtxo)

runUpdateProtocol :: Effect Unit
Expand All @@ -46,7 +47,7 @@ contract protocolData protocolConfigParams = do
ownAddress <- liftedM "Failed to get own address" $ Array.head <$> getWalletAddresses
walletUtxo <- utxosAt ownAddress >>= getNonCollateralUtxo

let manager = view _managerPkh protocolInfo.pDatum
manager /\ _ <- getPkhSkhFromAddress $ view _managerAddress protocolInfo.pDatum
when (manager /= ownPkh) $ liftEffect $ throw "Current user doesn't have permissions to update protocol"

let protocolConfig = mapToProtocolConfig protocolConfigParams
Expand Down Expand Up @@ -96,6 +97,6 @@ makeDatum currentDatum (PProtocolConfig { minAmount, maxAmount, minDuration, max
, minDuration: minDuration
, maxDuration: maxDuration
, protocolFee: protocolFee
, managerPkh: view _managerPkh currentDatum
, managerAddress: view _managerAddress currentDatum
, tokenOriginRef: view _tokenOriginRef currentDatum
}
32 changes: 30 additions & 2 deletions src/Shared/OwnCredentials.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ module Shared.OwnCredentials where

import Contract.Prelude

import Contract.Address (addressWithNetworkTagToBech32, AddressWithNetworkTag, PaymentPubKeyHash, StakePubKeyHash, getWalletAddressesWithNetworkTag, ownPaymentPubKeysHashes, ownStakePubKeysHashes)
import Contract.Address
( Address
, AddressWithNetworkTag
, PaymentPubKeyHash(..)
, PubKeyHash
, StakePubKeyHash(..)
, addressWithNetworkTagToBech32
, getWalletAddressesWithNetworkTag
, ownPaymentPubKeysHashes
, ownStakePubKeysHashes
, toPubKeyHash
, toStakingCredential
)
import Contract.Credential (Credential(..), StakingCredential(..))
import Contract.Log (logInfo')
import Contract.Monad (Contract, liftContractM, liftedM)
import Contract.Transaction (TransactionInput, TransactionOutputWithRefScript)
import Contract.Utxos (utxosAt)
import Data.Array as Array
import Data.Map as Map
import Effect.Exception (throw)
import Info.UserData (UserInfo(..))
import Shared.Utxo (UtxoTuple, getNonCollateralUtxo)

Expand Down Expand Up @@ -51,4 +65,18 @@ getOwnPkhAndAddress = do
ownPkh <- liftContractM "Impossible to get own PaymentPubkeyHash" $ Array.head ownHashes
logInfo' $ "Own Payment pkh is: " <> show ownPkh
ownAddressWithNetworkTag <- liftedM "Failed to get own address" $ Array.head <$> getWalletAddressesWithNetworkTag
pure $ (ownPkh /\ ownAddressWithNetworkTag)
pure $ (ownPkh /\ ownAddressWithNetworkTag)

getPkhSkhFromAddress :: Address -> Contract (PaymentPubKeyHash /\ StakePubKeyHash)
getPkhSkhFromAddress address = do
pkh <- liftContractM "Impossible to extract payment pkh from script address" $ toPubKeyHash address
stakingCreds <- liftContractM "Staking creds missed from provided address" $ toStakingCredential address
skh <- case stakingCreds of
StakingHash creds -> liftContractM "Impossible to extract staking pkh" $ pkhFromCreds creds
_ -> liftEffect $ throw "Unexpected staking credentials"
pure (PaymentPubKeyHash pkh /\ StakePubKeyHash skh)
where
pkhFromCreds :: Credential -> Maybe PubKeyHash
pkhFromCreds creds = case creds of
PubKeyCredential pkh -> Just pkh
_ -> Nothing

0 comments on commit 7d73f60

Please sign in to comment.