forked from spacebudz/lucid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mint_simple_NFT.ts
82 lines (66 loc) · 1.62 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
import {
Blockfrost,
fromText,
Lucid,
MintingPolicy,
PolicyId,
TxHash,
Unit,
} from "../mod.ts";
/*
MintSimpleNFT Example
Mint or burn a simple NFT.
*/
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 { paymentCredential } = lucid.utils.getAddressDetails(
await lucid.wallet.address(),
);
const mintingPolicy: MintingPolicy = lucid.utils.nativeScriptFromJson(
{
type: "all",
scripts: [
{ type: "sig", keyHash: paymentCredential?.hash! },
{
type: "before",
slot: lucid.utils.unixTimeToSlot(Date.now() + 1000000),
},
],
},
);
const policyId: PolicyId = lucid.utils.mintingPolicyToId(
mintingPolicy,
);
export async function mintNFT(
name: string,
): Promise<TxHash> {
const unit: Unit = policyId + fromText(name);
const tx = await lucid
.newTx()
.mintAssets({ [unit]: 1n })
.validTo(Date.now() + 100000)
.attachMintingPolicy(mintingPolicy)
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
return txHash;
}
export async function burnNFT(
name: string,
): Promise<TxHash> {
const unit: Unit = policyId + fromText(name);
const tx = await lucid
.newTx()
.mintAssets({ [unit]: -1n })
.validTo(Date.now() + 100000)
.attachMintingPolicy(mintingPolicy)
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
return txHash;
}