-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathmint_simple_nft.ts
84 lines (68 loc) · 1.58 KB
/
mint_simple_nft.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
import {
Addresses,
Blockfrost,
Cardano,
Codec,
fromText,
Lucid,
} from "../mod.ts";
/*
MintSimpleNFT Example
Mint or burn a simple NFT.
*/
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 { payment } = Addresses.inspect(
await lucid.wallet.address(),
);
const mintingPolicy = lucid.newScript({
type: "Native",
script: Codec.encodeNativeScript(
{
type: "All",
scripts: [
{ type: "Sig", keyHash: payment?.hash! },
{
type: "Before",
slot: lucid.utils.unixTimeToSlots(Date.now() + 1000000),
},
],
},
),
});
const policyId = mintingPolicy.toHash();
export async function mintNFT(
name: string,
): Promise<string> {
const unit = policyId + fromText(name);
const tx = await lucid
.newTx()
.mint({ [unit]: 1n })
.validTo(Date.now() + 100000)
.attachScript(mintingPolicy.script)
.commit();
const signedTx = await tx.sign().commit();
const txHash = await signedTx.submit();
return txHash;
}
export async function burnNFT(
name: string,
): Promise<string> {
const unit = policyId + fromText(name);
const tx = await lucid
.newTx()
.mint({ [unit]: -1n })
.validTo(Date.now() + 100000)
.attachScript(mintingPolicy.script)
.commit();
const signedTx = await tx.sign().commit();
const txHash = await signedTx.submit();
return txHash;
}