-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeployOnChain.hs
232 lines (180 loc) · 9.25 KB
/
DeployOnChain.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
{-# LANGUAGE NumericUnderscores #-}
module Milestones.DeployOnChain where
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Short as SBS
import qualified Data.Aeson as DataAeson
import Codec.Serialise (serialise)
import qualified Ledger as Ledger
import qualified Plutus.V2.Ledger.Api as LedgerApiV2
import qualified PlutusTx.Prelude as PlutusPrelude
import Cardano.Api
import Cardano.Api.Shelley (PlutusScript (..))
import qualified PlutusTx
import qualified Plutus.V1.Ledger.Scripts as ScriptsLedger
import qualified Milestones.OnChain as OnChain
-- ByteString (string) of the pkh of the contractor's wallet
contractor :: B.ByteString
contractor = "ENTER_CONTRACTOR_PKH_HERE"
-- ByteString (string) of the pkh of the homeowner's wallet
homeowner :: B.ByteString
homeowner = "ENTER_HOMEOWNER_PKH_HERE"
-- policyID for reference InspectorNFTs, created by inspector using Inspector.Deploy.hs
-- should be at Inspector/Deploy/policyID after running any inspector script that calls mintInspectorNFT.sh
inspectionPolicyId :: B.ByteString
inspectionPolicyId = "ENTER_POLICY_ID_HERE"
-- policyID for this project, created by contractor using Milestones.DeployNFT.hs
-- should be at Milestones/Deploy/policyID after running mintMilestonesNFT.sh script
projectPolicyId :: B.ByteString
projectPolicyId = "ENTER_POLICY_ID_HERE"
-- Values below can be changed, but their corresponding values in scripts and datums will need to be updated as well
-- To use provided walkthrough, leave these values below as-is
-- must be in lovelaces
totalCost :: Integer
totalCost = 2_000_000_000 -- ENTER YOUR TOTAL COST HERE. 2k ADA default here
-- payments below MUST add up to totalCost or validator will fail
-- must be in lovelaces
deposit :: Integer
deposit = 500_000_000 -- ENTER AMOUNT HERE
-- must be in lovelaces
secondPayment :: Integer
secondPayment = 500_000_000 -- ENTER AMOUNT HERE
-- must be in lovelaces
thirdPayment :: Integer
thirdPayment = 500_000_000 -- ENTER AMOUNT HERE
-- must be in lovelaces
finalPayment :: Integer
finalPayment = 500_000_000 -- ENTER AMOUNT HERE
-- these match the InspectorNFT Names in Inspector/Deploy.hs - DO NOT CHANGE
permitName :: B.ByteString
permitName = "PermitIssued"
roughName :: B.ByteString
roughName = "RoughPassed"
drywallName :: B.ByteString
drywallName = "DrywallPassed"
finalName :: B.ByteString
finalName = "FinalPassed"
closedName :: B.ByteString
closedName = "ClosedIncomplete"
parameters :: OnChain.MilestoneParam
parameters = OnChain.MilestoneParam
{ OnChain.homeowner = convertToPubKeyHash homeowner
, OnChain.contractor = convertToPubKeyHash contractor
, OnChain.totalCost = totalCost
, OnChain.deposit = deposit
, OnChain.secondPayment = secondPayment
, OnChain.thirdPayment = thirdPayment
, OnChain.finalPayment = finalPayment
, OnChain.projectPolicyId = decodeHex projectPolicyId
, OnChain.inspectionPolicyId = decodeHex inspectionPolicyId
, OnChain.permitName = convertToTokenName permitName
, OnChain.roughName = convertToTokenName roughName
, OnChain.drywallName = convertToTokenName drywallName
, OnChain.finalName = convertToTokenName finalName
, OnChain.closedName = convertToTokenName closedName
}
convertToTokenName :: B.ByteString -> LedgerApiV2.TokenName
convertToTokenName b = LedgerApiV2.TokenName (PlutusPrelude.toBuiltin b)
decodeHex :: B.ByteString -> PlutusPrelude.BuiltinByteString
decodeHex hexBs =
case getTx of
Right decHex -> PlutusPrelude.toBuiltin(decHex)
Left _ -> PlutusPrelude.emptyByteString
where
getTx :: Either String B.ByteString
getTx = B16.decode hexBs
convertToPubKeyHash :: B.ByteString -> Ledger.PaymentPubKeyHash
convertToPubKeyHash b = Ledger.PaymentPubKeyHash (Ledger.PubKeyHash $ decodeHex b)
-- Datum must be initialized to StartProject state
lastContractorAction :: OnChain.RedeemContractor
lastContractorAction = OnChain.NotStarted
-- this is enforced by OnChain to start at 2ADA for MinUTXO that comes with the AuthNFT
lastBalance :: Integer
lastBalance = 2_000_000
-- these are enforced by OnChain to start at 0
totalDeposited :: Integer
totalDeposited = 0
totalWithdrawnContractor :: Integer
totalWithdrawnContractor = 0
totalWithdrawnHomeowner :: Integer
totalWithdrawnHomeowner = 0
milestoneDatum :: OnChain.MilestoneDatum
milestoneDatum = OnChain.MilestoneDatum
{ OnChain.lastContractorAction = lastContractorAction
, OnChain.totalDeposited = totalDeposited
, OnChain.lastBalance = lastBalance
, OnChain.totalWithdrawnContractor = totalWithdrawnContractor
, OnChain.totalWithdrawnHomeowner = totalWithdrawnHomeowner
}
main :: IO()
main = do
writeDatumUnit
writeMilestoneDatum
writeRedeemerHomeownerAddFunds
writeRedeemerHomeownerWithdrawFunds
writeRedeemerContractorStartProject
writeRedeemerContractorPermitPayment
writeRedeemerContractorRoughPayment
writeRedeemerContractorDrywallPayment
writeRedeemerContractorFinalPayment
_ <- writeParameterized
return ()
dataToScriptData :: LedgerApiV2.Data -> ScriptData
dataToScriptData (LedgerApiV2.Constr n xs) = ScriptDataConstructor n $ dataToScriptData <$> xs
dataToScriptData (LedgerApiV2.Map xs) = ScriptDataMap [(dataToScriptData x, dataToScriptData y) | (x, y) <- xs]
dataToScriptData (LedgerApiV2.List xs) = ScriptDataList $ dataToScriptData <$> xs
dataToScriptData (LedgerApiV2.I n) = ScriptDataNumber n
dataToScriptData (LedgerApiV2.B bs) = ScriptDataBytes bs
writeJSON :: PlutusTx.ToData a => FilePath -> a -> IO ()
writeJSON file = LBS.writeFile file . DataAeson.encode . scriptDataToJson ScriptDataJsonDetailedSchema . dataToScriptData . PlutusTx.toData
writeValidator :: FilePath -> LedgerApiV2.Validator -> IO (Either (FileError ()) ())
writeValidator file = writeFileTextEnvelope @(PlutusScript PlutusScriptV2) file Nothing . PlutusScriptSerialised . SBS.toShort . LBS.toStrict . serialise . LedgerApiV2.unValidatorScript
writeDatumUnit :: IO ()
writeDatumUnit = writeJSON "src/Milestones/Deploy/unit.json" ()
writeMilestoneDatum :: IO ()
writeMilestoneDatum =
let d = PlutusTx.toBuiltinData milestoneDatum
in writeJSON "src/Milestones/Deploy/0-parameterized-initial-datum.json" d
writeRedeemerHomeownerAddFunds :: IO ()
writeRedeemerHomeownerAddFunds =
let ho = ScriptsLedger.Redeemer $ PlutusTx.toBuiltinData $ OnChain.Homeowner (OnChain.HomeownerAddFunds)
in
writeJSON "src/Milestones/Deploy/redeemer-homeowner-add-funds.json" ho
writeRedeemerHomeownerWithdrawFunds :: IO ()
writeRedeemerHomeownerWithdrawFunds =
let ho = ScriptsLedger.Redeemer $ PlutusTx.toBuiltinData $ OnChain.Homeowner (OnChain.HomeownerWithdrawFunds)
in
writeJSON "src/Milestones/Deploy/redeemer-homeowner-withdraw-funds.json" ho
writeRedeemerContractorNotStarted :: IO ()
writeRedeemerContractorNotStarted =
let ctr = ScriptsLedger.Redeemer $ PlutusTx.toBuiltinData $ OnChain.Contractor (OnChain.NotStarted)
in
writeJSON "src/Milestones/Deploy/redeemer-contractor-not-started.json" ctr
writeRedeemerContractorStartProject :: IO ()
writeRedeemerContractorStartProject =
let ctr = ScriptsLedger.Redeemer $ PlutusTx.toBuiltinData $ OnChain.Contractor (OnChain.StartProject)
in
writeJSON "src/Milestones/Deploy/redeemer-contractor-start-project.json" ctr
writeRedeemerContractorPermitPayment :: IO ()
writeRedeemerContractorPermitPayment =
let ctr = ScriptsLedger.Redeemer $ PlutusTx.toBuiltinData $ OnChain.Contractor (OnChain.WithdrawPermitPayment)
in
writeJSON "src/Milestones/Deploy/redeemer-contractor-permit-payment.json" ctr
writeRedeemerContractorRoughPayment :: IO ()
writeRedeemerContractorRoughPayment =
let ctr = ScriptsLedger.Redeemer $ PlutusTx.toBuiltinData $ OnChain.Contractor (OnChain.WithdrawRoughPayment)
in
writeJSON "src/Milestones/Deploy/redeemer-contractor-rough-payment.json" ctr
writeRedeemerContractorDrywallPayment :: IO ()
writeRedeemerContractorDrywallPayment =
let ctr = ScriptsLedger.Redeemer $ PlutusTx.toBuiltinData $ OnChain.Contractor (OnChain.WithdrawDrywallPayment)
in
writeJSON "src/Milestones/Deploy/redeemer-contractor-drywall-payment.json" ctr
writeRedeemerContractorFinalPayment :: IO ()
writeRedeemerContractorFinalPayment =
let ctr = ScriptsLedger.Redeemer $ PlutusTx.toBuiltinData $ OnChain.Contractor (OnChain.WithdrawFinalPayment)
in
writeJSON "src/Milestones/Deploy/redeemer-contractor-final-payment.json" ctr
writeParameterized :: IO (Either (FileError ()) ())
writeParameterized = writeValidator "src/Milestones/Deploy/Milestones.plutus" $ OnChain.validator parameters