Skip to content

Commit a30d536

Browse files
committed
Add the partially working spending script
1 parent 9ec1f6c commit a30d536

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

dev-local/Main.hs

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Main (main) where
88
-- Imports
99
-------------------------------------------------------------------------------
1010

11+
import Control.Concurrent (threadDelay)
1112
import Data.Function ((&))
1213
import Data.Word (Word8)
1314
import Streamly.Data.Array (Array)
@@ -42,6 +43,9 @@ firstNonEmptyLine tag =
4243
Stream.fold (maybe (error [str|Empty: #{tag}|]) id <$> Fold.one)
4344
. nonEmptyLines
4445

46+
printVar :: String -> String -> IO ()
47+
printVar tag val = putStrLn [str|[#{tag}]: #{val}|]
48+
4549
-------------------------------------------------------------------------------
4650
-- Globals
4751
-------------------------------------------------------------------------------
@@ -91,13 +95,16 @@ env_TX_SIGNED = env_WORK_DIR </> "tx.signed"
9195
-------------------------------------------------------------------------------
9296

9397
type Command = String
94-
type CmdOption = (String, String)
98+
type CmdOption = (String, Maybe String)
9599

96100
opt :: (Show b) => String -> b -> CmdOption
97-
opt a b = (a, quoted b)
101+
opt a b = (a, Just (quoted b))
98102
where
99103
quoted = show
100104

105+
flg :: String -> CmdOption
106+
flg a = (a, Nothing)
107+
101108
optNetwork :: CmdOption
102109
optNetwork = opt "testnet-magic" env_CARDANO_TESTNET_MAGIC
103110

@@ -108,7 +115,7 @@ runCmd :: Command -> [CmdOption] -> Stream IO (Array Word8)
108115
runCmd cmd args =
109116
Stream.before (putStrLn [str|> #{cmdStr}|]) (Cmd.toChunks cmdStr)
110117
where
111-
cmdList = cmd : concatMap (\(k, v) -> ["--" ++ k, v]) args
118+
cmdList = cmd : concatMap (\(k, v) -> ["--" ++ k, maybe "" id v]) args
112119
cmdStr = unwords cmdList
113120

114121
getPolicyId :: FilePath -> IO String
@@ -167,16 +174,16 @@ getTransactionId txSigned =
167174
& Cmd.pipeChunks [str|jq -r ".txhash"|]
168175
& firstNonEmptyLine "getTransactionId"
169176

170-
queryUtxo :: String -> IO String
171-
queryUtxo walletAddr =
177+
getFirstUtxoAt :: String -> IO String
178+
getFirstUtxoAt walletAddr =
172179
runCmd
173180
"cardano-cli conway query utxo"
174181
[ optNetwork
175182
, optSocketPath
176183
, opt "address" walletAddr
177184
]
178185
& Cmd.pipeChunks [str|jq -r "keys[0]"|]
179-
& firstNonEmptyLine "queryUtxo"
186+
& firstNonEmptyLine "getFirstUtxoAt"
180187

181188
-------------------------------------------------------------------------------
182189
-- Output parsing
@@ -186,28 +193,33 @@ queryUtxo walletAddr =
186193
-- Main
187194
-------------------------------------------------------------------------------
188195

189-
setup :: IO ()
190-
setup = do
196+
ensureBlankWorkDir :: IO ()
197+
ensureBlankWorkDir = do
198+
Cmd.toStdout [str|rm -rf #{env_WORK_DIR}|]
191199
Cmd.toStdout [str|mkdir -p #{env_WORK_DIR}|]
192200

193201
main :: IO ()
194202
main = do
195203
printStep "Setup"
196-
setup
197204

198205
policyId <- getPolicyId env_POLICY_FILE
199206
faucetAddr <- env_FAUCET_WALLET_ADDR
200207
tokenNameHex <- env_TOKEN_NAME_HEX
201-
faucetUtxo <- queryUtxo faucetAddr
208+
faucetUtxo <- getFirstUtxoAt faucetAddr
202209
validatorAddress <- getAddress env_VALIDATOR_FILE
203210
let assetClass = [str|#{policyId}.#{tokenNameHex}|]
204211

212+
printVar "faucetAddr" faucetAddr
213+
printVar "validatorAddress" validatorAddress
214+
215+
ensureBlankWorkDir
205216
printStep "Mint"
206217
buildTransaction
207218
[ opt "tx-in" faucetUtxo
208219
, opt "tx-in-collateral" faucetUtxo
209220
, opt "tx-out" [str|#{validatorAddress} + 2000000 + 100 #{assetClass}|]
210221
, opt "tx-out-inline-datum-value" (10 :: Int)
222+
, opt "tx-out-reference-script-file" env_VALIDATOR_FILE
211223
, opt "mint" [str|100 #{assetClass}|]
212224
, opt "mint-script-file" env_POLICY_FILE
213225
, opt "mint-redeemer-value" [str|{"constructor": 0, "fields": []}|]
@@ -222,4 +234,36 @@ main = do
222234
submitTransaction
223235
[ opt "tx-file" env_TX_SIGNED
224236
]
225-
getTransactionId env_TX_SIGNED >>= print
237+
238+
mintTx <- getTransactionId env_TX_SIGNED
239+
printVar "mintTx" mintTx
240+
printStep "Waiting"
241+
threadDelay 5000000
242+
243+
faucetUtxo1 <- getFirstUtxoAt faucetAddr
244+
printVar "faucetUtxo1" faucetUtxo1
245+
246+
ensureBlankWorkDir
247+
printStep "Spend"
248+
let lockedUtxo = [str|#{mintTx}#0|]
249+
buildTransaction
250+
[ opt "tx-in" faucetUtxo1
251+
, opt "tx-in" lockedUtxo
252+
, opt "tx-in-collateral" faucetUtxo1
253+
, flg "spending-plutus-script-v2"
254+
, opt "spending-tx-in-reference" lockedUtxo
255+
, opt "spending-reference-tx-in-datum-value" (10 :: Int)
256+
, opt "spending-reference-tx-in-redeemer-value" (10 :: Int)
257+
, opt "tx-out" [str|#{validatorAddress} + 2000000 + 100 #{assetClass}|]
258+
, opt "tx-out-inline-datum-value" (20 :: Int)
259+
, opt "change-address" faucetAddr
260+
, opt "out-file" env_TX_UNSIGNED
261+
]
262+
signTransaction
263+
[ opt "signing-key-file" env_FAUCET_WALLET_SKEY_FILE
264+
, opt "tx-body-file" env_TX_UNSIGNED
265+
, opt "out-file" env_TX_SIGNED
266+
]
267+
submitTransaction
268+
[ opt "tx-file" env_TX_SIGNED
269+
]

0 commit comments

Comments
 (0)