-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathalways_succeeds.ts
77 lines (59 loc) · 1.91 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
import { Blockfrost, Data, Lucid } 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 = new Lucid({
provider: new Blockfrost(
"https://cardano-preview.blockfrost.io/api/v0",
"<projectId>",
),
});
const api = await (globalThis as any).cardano.nami.enable();
// Assumes you are in a browser environment
lucid.selectWalletFromApi(api);
const alwaysSucceed = lucid.newScript({
type: "PlutusV2",
script: "49480100002221200101",
});
const alwaysSucceedAddress = alwaysSucceed.toAddress();
const Datum = () => Data.void();
const Redeemer = () => Data.void();
export async function lockUtxo(
lovelace: bigint,
): Promise<string> {
const tx = await lucid
.newTx()
.payToContract(alwaysSucceedAddress, { Inline: Datum() }, { lovelace })
.payToContract(alwaysSucceedAddress, {
AsHash: Datum(),
scriptRef: alwaysSucceed.script, // adding plutusV2 script to output
}, {})
.commit();
const signedTx = await tx.sign().commit();
const txHash = await signedTx.submit();
return txHash;
}
export async function redeemUtxo(): Promise<string> {
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())
.commit();
const signedTx = await tx.sign().commit();
const txHash = await signedTx.submit();
return txHash;
}