-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathe2e.ts
123 lines (112 loc) · 3.66 KB
/
e2e.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
import {
TokenProgramVersion,
TokenStandard,
} from "@metaplex-foundation/mpl-bubblegum";
import { ConcurrentMerkleTreeAccount } from "@solana/spl-account-compression";
import { Keypair } from "@solana/web3.js";
import base58 from "bs58";
import {
getCompressedNftId,
initCollection,
initTree,
mintCompressedNft,
transferAsset,
} from "./utils";
import { WrappedConnection } from "./wrappedConnection";
const e2e = async () => {
const apiKey = process.env["API_KEY"];
if (!apiKey) {
throw new Error("Api key must be provided via API_KEY env var");
}
const secretKey = process.env["SECRET_KEY"];
if (!secretKey) {
throw new Error(
"Wallet secret key must be provided via SECRET_KEY env var"
);
}
let decodedSecretKey;
try {
decodedSecretKey = base58.decode(secretKey);
} catch {
throw new Error(
"Invalid secret key provided. Must be a base 58 encoded string."
);
}
const ownerWallet = Keypair.fromSecretKey(decodedSecretKey);
console.log("Owner wallet: " + ownerWallet.publicKey);
const connectionString = `https://rpc-devnet.helius.xyz?api-key=${apiKey}`;
const connectionWrapper = new WrappedConnection(
ownerWallet,
connectionString
);
// Fixed wallet to manage the merkle tree used to store the collection.
const treeWallet = Keypair.generate();
console.log("Tree wallet: " + treeWallet.publicKey);
console.log("Creating merkle tree.");
await initTree(connectionWrapper, ownerWallet, treeWallet);
const {
collectionMint,
collectionMetadataAccount,
collectionMasterEditionAccount,
} = await initCollection(connectionWrapper, ownerWallet);
console.log("\n===Collection Details===");
console.log("Mint account: " + collectionMint.publicKey.toBase58());
console.log("Metadata account: " + collectionMetadataAccount.toBase58());
console.log(
"Master edition account: " + collectionMasterEditionAccount.toBase58()
);
console.log("\n");
// Mint a compressed NFT
const nftArgs = {
name: "Compression Test",
symbol: "COMP",
uri: "https://arweave.net/gfO_TkYttQls70pTmhrdMDz9pfMUXX8hZkaoIivQjGs",
creators: [],
editionNonce: 253,
tokenProgramVersion: TokenProgramVersion.Original,
tokenStandard: TokenStandard.NonFungible,
uses: null,
collection: null,
primarySaleHappened: false,
sellerFeeBasisPoints: 0,
isMutable: false,
};
const sig = await mintCompressedNft(
connectionWrapper,
nftArgs,
ownerWallet,
treeWallet,
collectionMint,
collectionMetadataAccount,
collectionMasterEditionAccount
);
console.log("Minted compressed nft with txn: " + sig);
// Get the NFT mint ID from the merkle tree.
const treeAccount = await ConcurrentMerkleTreeAccount.fromAccountAddress(
connectionWrapper,
treeWallet.publicKey
);
// Get the most rightmost leaf index, which will be the most recently minted compressed NFT.
// Alternatively you can keep a counter that is incremented on each mint.
const leafIndex = treeAccount.tree.rightMostPath.index - 1;
const assetId = await getCompressedNftId(treeWallet, leafIndex);
console.log("Minted asset: " + assetId);
// Fixed wallet to receive the NFT when we test transfer.
const newOwnerWallet = Keypair.fromSeed(
new TextEncoder().encode("next wallet".padEnd(32, "\0"))
);
console.log("New owner wallet: " + newOwnerWallet.publicKey.toBase58());
console.log("\n===Transfer===");
console.log("Transfer to new wallet.");
await transferAsset(
connectionWrapper,
ownerWallet,
newOwnerWallet,
assetId.toBase58()
);
console.log(
"Successfully transferred nft to wallet: " +
newOwnerWallet.publicKey.toBase58()
);
};
e2e();