forked from spacebudz/lucid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
always_succeeds.ts
85 lines (67 loc) · 2 KB
/
always_succeeds.ts
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
import {
Address,
Blockfrost,
Data,
Lovelace,
Lucid,
SpendingValidator,
TxHash,
} from "../../mod.ts";
/*
AlwaysSucceeds Example
Lock a UTxO with some ADA
UTxO can be unlocked by anyone
Showcasing PlutusV2
Contract:
validate :: () -> () -> ScriptContext -> Bool
validate _ _ _ = True
*/
const lucid = await Lucid.new(
new Blockfrost("https://cardano-preview.blockfrost.io/api/v0", "<projectId>"),
"Preview",
);
const api = await window.cardano.nami.enable();
// Assumes you are in a browser environment
lucid.selectWallet(api);
const alwaysSucceedScript: SpendingValidator = {
type: "PlutusV2",
script: "49480100002221200101",
};
const alwaysSucceedAddress: Address = lucid.utils.validatorToAddress(
alwaysSucceedScript,
);
const Datum = () => Data.void();
const Redeemer = () => Data.void();
export async function lockUtxo(
lovelace: Lovelace,
): Promise<TxHash> {
const tx = await lucid
.newTx()
.payToContract(alwaysSucceedAddress, { inline: Datum() }, { lovelace })
.payToContract(alwaysSucceedAddress, {
asHash: Datum(),
scriptRef: alwaysSucceedScript, // adding plutusV2 script to output
}, {})
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
return txHash;
}
export async function redeemUtxo(): Promise<TxHash> {
const referenceScriptUtxo = (await lucid.utxosAt(alwaysSucceedAddress)).find(
(utxo) => Boolean(utxo.scriptRef),
);
if (!referenceScriptUtxo) throw new Error("Reference script not found");
const utxo = (await lucid.utxosAt(alwaysSucceedAddress)).find((utxo) =>
utxo.datum === Datum() && !utxo.scriptRef
);
if (!utxo) throw new Error("Spending script utxo not found");
const tx = await lucid
.newTx()
.readFrom([referenceScriptUtxo]) // spending utxo by reading plutusV2 from reference utxo
.collectFrom([utxo], Redeemer())
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
return txHash;
}