Skip to content

Commit

Permalink
Fundraising info fields types (#54)
Browse files Browse the repository at this point in the history
* changed FundraisingInfo fields types

* updated dists
  • Loading branch information
KateBushueva authored Aug 4, 2023
1 parent 0385b2d commit ceb63c1
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion dist/131.index.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion src/Ext/Contract/Value.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import Contract.Monad (Contract, liftContractM)
import Contract.Prim.ByteArray (byteArrayFromAscii)
import Contract.Scripts (MintingPolicy)
import Contract.Value as Value
import Ctl.Internal.Types.ByteArray (byteArrayToHex, ByteArray(..))
import Ctl.Internal.Plutus.Types.CurrencySymbol as CurrencySymbol
import Ctl.Internal.Types.ByteArray (ByteArray(..), byteArrayToHex, hexToByteArray)
import Data.Array (filter) as Array
import Data.TextDecoder (decodeUtf8)

Expand Down Expand Up @@ -36,6 +37,13 @@ getCurrencyByTokenName val tokenName =
currencySymbolToString :: Value.CurrencySymbol -> String
currencySymbolToString = byteArrayToHex <<< Value.getCurrencySymbol

currencyFromString :: String -> Maybe Value.CurrencySymbol
currencyFromString csString = hexToByteArray csString >>= CurrencySymbol.mkCurrencySymbol

mkCurrencySymbolFromString :: String -> Contract Value.CurrencySymbol
mkCurrencySymbolFromString cs =
liftContractM "Impossible to make currency symbol from String" $ currencyFromString cs

tokenNameToString :: Value.TokenName -> Maybe String
tokenNameToString tn =
let
Expand Down
8 changes: 4 additions & 4 deletions src/Fundraising/Create.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Ext.Contract.Time (addTimes)
import Ext.Contract.Value (currencySymbolToString, mkCurrencySymbol)
import Ext.Seriaization.Key (pkhToBech32M)
import Fundraising.Datum (PFundraisingDatum(..), titleLength)
import Fundraising.FundraisingScript (getFundraisingTokenName, getFundraisingValidatorHash)
import Fundraising.FundraisingScript (fundraisingTokenNameString, getFundraisingTokenName, getFundraisingValidatorHash)
import Fundraising.Models (Fundraising(..))
import Fundraising.UserData (CreateFundraisingParams(..))
import Info.UserData (FundraisingInfo(..))
Expand Down Expand Up @@ -182,14 +182,14 @@ contract protocolData (CreateFundraisingParams { title, amount, duration }) = do
logInfo' $ "Current fundraising address: " <> show bech32Address

creatorPkh <- pkhToBech32M creds.ownPkh

pure $ FundraisingInfo
{ creator: creatorPkh
, title: title
, goal: targetAmount
, raisedAmt: fromInt 0
, deadline: deadline
, threadTokenCurrency: nftCs
, threadTokenName: nftTn
, path: currencySymbolToString nftCs
, threadTokenCurrency: currencySymbolToString nftCs
, threadTokenName: fundraisingTokenNameString
, isCompleted: false
}
6 changes: 3 additions & 3 deletions src/Fundraising/Donate.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Ctl.Internal.Types.Datum (Datum(..))
import Ctl.Internal.Types.Interval (from)
import Data.BigInt (fromInt)
import Effect.Exception (throw)
import Ext.Contract.Value (mkCurrencySymbolFromString, runMkTokenName)
import Fundraising.Datum (PFundraisingDatum(..))
import Fundraising.FundraisingScriptInfo (FundraisingScriptInfo(..), getFundraisingScriptInfo, makeFundraising)
import Fundraising.Models (Fundraising(..))
Expand All @@ -35,9 +36,8 @@ runDonate onComplete onError pData networkParams fundraisingData amount =
contract :: ProtocolData -> FundraisingData -> Int -> Contract Unit
contract pData (FundraisingData fundraisingData) adaAmount = do
logInfo' "Running donate"
let
threadTokenCurrency = fundraisingData.frThreadTokenCurrency
threadTokenName = fundraisingData.frThreadTokenName
threadTokenCurrency <- mkCurrencySymbolFromString fundraisingData.frThreadTokenCurrency
threadTokenName <- runMkTokenName fundraisingData.frThreadTokenName
fundraising@(Fundraising fr) <- makeFundraising pData
(FundraisingScriptInfo frInfo) <- getFundraisingScriptInfo fundraising threadTokenCurrency threadTokenName
let isVerTokenInUtxo = checkTokenInUTxO (fr.verTokenCurrency /\ fr.verTokenName) frInfo.frUtxo
Expand Down
5 changes: 3 additions & 2 deletions src/Fundraising/ReceiveFunds.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Ctl.Internal.Types.Interval (from)
import Ctl.Internal.Types.TokenName (adaToken)
import Data.BigInt (fromInt)
import Effect.Exception (throw)
import Ext.Contract.Value (mkCurrencySymbolFromString, runMkTokenName)
import Fundraising.Calculations (calcFee)
import Fundraising.Datum (PFundraisingDatum(..))
import Fundraising.FundraisingScriptInfo (FundraisingScriptInfo(..), getFundraisingScriptInfo, makeFundraising)
Expand All @@ -46,8 +47,8 @@ contract :: ProtocolData -> FundraisingData -> Contract Unit
contract pData (FundraisingData fundraisingData) = do
logInfo' "Running receive funds"
protocol <- dataToProtocol pData
let threadTokenCurrency = fundraisingData.frThreadTokenCurrency
let threadTokenName = fundraisingData.frThreadTokenName
threadTokenCurrency <- mkCurrencySymbolFromString fundraisingData.frThreadTokenCurrency
threadTokenName <- runMkTokenName fundraisingData.frThreadTokenName
fundraising@(Fundraising fr) <- makeFundraising pData
(FundraisingScriptInfo frInfo) <- getFundraisingScriptInfo fundraising threadTokenCurrency threadTokenName
let isVerTokenInUtxo = checkTokenInUTxO (fr.verTokenCurrency /\ fr.verTokenName) frInfo.frUtxo
Expand Down
5 changes: 2 additions & 3 deletions src/Fundraising/UserData.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Fundraising.UserData where

import Contract.Prelude

import Contract.Value (CurrencySymbol, TokenName)
import Info.UserData (FundraisingInfo(..))
import Shared.Duration (Duration)

Expand All @@ -16,8 +15,8 @@ derive newtype instance Show CreateFundraisingParams
derive newtype instance Eq CreateFundraisingParams

newtype FundraisingData = FundraisingData
{ frThreadTokenCurrency :: CurrencySymbol
, frThreadTokenName :: TokenName
{ frThreadTokenCurrency :: String
, frThreadTokenName :: String
}

derive newtype instance Show FundraisingData
Expand Down
13 changes: 5 additions & 8 deletions src/Info/UserData.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Ext.Contract.Value (getCurrencyByTokenName, currencySymbolToString)
import Ext.Data.Either (eitherContract)
import Ext.Seriaization.Key (pkhToBech32M)
import Fundraising.Datum (PFundraisingDatum(..))
import Fundraising.FundraisingScript (getFundraisingTokenName)
import Fundraising.FundraisingScript (fundraisingTokenNameString, getFundraisingTokenName)
import Protocol.UserData (ProtocolConfigParams)
import Shared.MinAda (minAdaValue)
import Shared.Utxo (UtxoTuple, extractDatumFromUTxO, extractValueFromUTxO)
Expand All @@ -26,9 +26,8 @@ newtype FundraisingInfo = FundraisingInfo
, goal :: BigInt -- Goal in lovelaces
, raisedAmt :: BigInt -- Raised amount in lovelaces
, deadline :: POSIXTime
, threadTokenCurrency :: Value.CurrencySymbol
, threadTokenName :: Value.TokenName
, path :: String
, threadTokenCurrency :: String
, threadTokenName :: String
, isCompleted :: Boolean
}

Expand All @@ -44,7 +43,6 @@ mapToFundraisingInfo utxo = do
title <- eitherContract "Title decoding failed: " $ decodeUtf8 unwrappedTitle
frTokenName <- getFundraisingTokenName
cs <- liftContractM "Impossible to get currency by token name" $ getCurrencyByTokenName frVal frTokenName
let pathStr = currencySymbolToString cs
now <- currentTime
creator <- pkhToBech32M currentDatum.creatorPkh
pure $ FundraisingInfo
Expand All @@ -53,9 +51,8 @@ mapToFundraisingInfo utxo = do
, goal: currentDatum.frAmount
, raisedAmt: currentFunds
, deadline: currentDatum.frDeadline
, threadTokenCurrency: cs
, threadTokenName: frTokenName
, path: pathStr
, threadTokenCurrency: currencySymbolToString cs
, threadTokenName: fundraisingTokenNameString
, isCompleted: now > currentDatum.frDeadline || currentFunds >= currentDatum.frAmount
}

Expand Down
2 changes: 1 addition & 1 deletion src/Shared/ScriptRef.purs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ mkFundraisingRefScript protocolData = do
createPolicyRefUtxo :: String -> MintingPolicy Contract Unit
createPolicyRefUtxo _ (NativeMintingPolicy _) = liftEffect $ throw "Unexpected minting policy type"
createPolicyRefUtxo mpName (PlutusMintingPolicy policy) = do
logInfo' $ "Creating UTxO with " <> mpName <> " minting policy name"
logInfo' $ "Creating UTxO with " <> mpName <> " minting policy reference"
(OwnCredentials creds) <- getOwnCreds
let
scriptRef = PlutusScriptRef policy
Expand Down
5 changes: 2 additions & 3 deletions test/Plutip/Fixtures.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Contract.Value as Value
import Data.BigInt as BigInt
import Data.Tuple (Tuple)
import Data.Tuple.Nested ((/\))
import Ext.Contract.Value (runMkTokenName)
import Ext.Contract.Value (currencySymbolToString)
import Fundraising.UserData (FundraisingData(..))
import Protocol.UserData (ProtocolConfigParams(..))
import Test.Plutip.Common (privateStakeKey)
Expand All @@ -29,8 +29,7 @@ distribution =

incorrectFundraisingData :: Contract FundraisingData
incorrectFundraisingData = do
tn <- runMkTokenName "FundraisingThreadToken"
pure $ FundraisingData { frThreadTokenCurrency: Value.adaSymbol, frThreadTokenName: tn }
pure $ FundraisingData { frThreadTokenCurrency: currencySymbolToString Value.adaSymbol, frThreadTokenName: "FundraisingThreadToken" }

minDurationStartProtocolParams :: ProtocolConfigParams
minDurationStartProtocolParams = ProtocolConfigParams
Expand Down
2 changes: 1 addition & 1 deletion ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const root = ReactDOM.createRoot(document.getElementById('root')!);
const App = () => {

const protocolData = {
protocolCurrency: "484c32d9f92cf5b5e9b649e547316501cd3d0e9bc0964341cf25e0d4",
protocolCurrency: "b991178ad09c0a2293d9a0afb4d52af6be95fae15f15ff00940145f4",
protocolTokenName: "DonatPoolProtocol"
}

Expand Down

0 comments on commit ceb63c1

Please sign in to comment.