-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnilNFT4.ts
134 lines (115 loc) · 3.72 KB
/
AnilNFT4.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {
Blockfrost,
fromText,
Lucid,
MintingPolicy,
PolicyId,
TxHash,
Address,
Unit,
Metadata,
Assets,
} from "lucid-cardano";
async function main() {
// Define the metadata details
const metadata: Metadata = {
222: {
name: "NFT",
description: "This is my NFT",
image: "ipfs://QmRzicpReutwCkM6aotuKjErFCUD213DpwPq6ByuzMJaua",
},
333: {
name: "",
description: "",
ticker: undefined,
url: undefined,
logo: undefined,
decimals: undefined
},
444: undefined
};
// Initialize Lucid with Blockfrost API
const lucid = await Lucid.new(
new Blockfrost(
"https://cardano-preprod.blockfrost.io/api/v0",
"preprodUFORB61aetgYVPS7c1qGhven5shd9cjE"
),
"Preprod"
);
// Select the wallet from seed phrase
lucid.selectWalletFromSeed(
"material chat insane area spatial when can clutch badge notable sing napkin goddess double check mandate thought bicycle spatial notable vendor judge donor liar"
);
// Get wallet address
const addr: Address = await lucid.wallet.address();
console.log(addr, "this is address");
// Get payment credentials from the address
const { paymentCredential } = lucid.utils.getAddressDetails(addr);
// Define the minting policy
const mintingPolicy: MintingPolicy = lucid.utils.nativeScriptFromJson({
type: "all",
scripts: [
{ type: "sig", keyHash: paymentCredential?.hash! },
{
type: "before",
slot: lucid.utils.unixTimeToSlot(Date.now() + 1000000),
},
],
});
// Define the policy ID
const policyId: PolicyId = lucid.utils.mintingPolicyToId(mintingPolicy);
console.log(policyId, "my policy id");
// Function to mint an NFT
async function mintNFT(name: string): Promise<TxHash> {
const unit: Unit = policyId + fromText(name);
try {
const tx = await lucid
.newTx()
.mintAssets({ [unit]: 1n }) // Mint 1 unit of the NFT
.attachMetadata(222, metadata[222]) // Attach metadata
.validTo(Date.now() + 100000) // Add validity
.attachMintingPolicy(mintingPolicy)
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
return txHash;
} catch (error) {
console.error("Error minting NFT:", error);
throw error;
}
}
// Function to burn an NFT
async function burnNFT(name: string): Promise<TxHash> {
const unit: Unit = policyId + fromText(name);
try {
const tx = await lucid
.newTx()
.mintAssets({ [unit]: -1n }) // Burn 1 unit of the NFT
.validTo(Date.now() + 100000)
.attachMintingPolicy(mintingPolicy)
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
return txHash;
} catch (error) {
console.error("Error burning NFT:", error);
throw error;
}
}
// Mint an NFT and log the result
try {
const txHash = await mintNFT("MY_nft");
console.log("NFT minted with TxHash:", txHash);
} catch (error) {
console.error("Error in minting:", error);
}
// Uncomment to burn the NFT
// try {
// const txHash = await burnNFT("MY_nft");
// console.log("NFT burned with TxHash:", txHash);
// } catch (error) {
// console.error("Error in burning:", error);
// }
}
// Run the main function
main().catch((error) => console.error("Error in main function:", error));