forked from spacebudz/lucid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyped_data.ts
42 lines (34 loc) · 1.09 KB
/
typed_data.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
import { Blockfrost, Data, fromText, Lucid, TxHash } from "../mod.ts";
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);
// Type definition could be auto generated from on-chain script
const MyDatumSchema = Data.Object({
name: Data.Bytes(),
age: Data.Integer(),
colors: Data.Array(Data.Bytes()),
description: Data.Nullable(Data.Bytes()),
});
type MyDatum = Data.Static<typeof MyDatumSchema>;
const MyDatum = MyDatumSchema as unknown as MyDatum;
export async function send(): Promise<TxHash> {
const datum: MyDatum = {
name: fromText("Lucid"),
age: 0n,
colors: [fromText("Blue"), fromText("Purple")],
description: null,
};
const tx = await lucid
.newTx()
.payToAddressWithData("addr_test...", Data.to(datum, MyDatum), {
lovelace: 10000000n,
})
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
return txHash;
}