forked from helius-labs/compression-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
110 lines (100 loc) · 2.52 KB
/
Copy pathhelpers.ts
File metadata and controls
110 lines (100 loc) · 2.52 KB
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
import { PROGRAM_ID, TreeConfig } from "@metaplex-foundation/mpl-bubblegum";
import {
Connection,
PublicKey,
Signer,
Transaction,
TransactionInstruction,
} from "@solana/web3.js";
import { BN, Provider } from "@project-serum/anchor";
import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from "@metaplex-foundation/mpl-token-metadata";
export async function getBubblegumAuthorityPDA(merkleRollPubKey: PublicKey) {
const [bubblegumAuthorityPDAKey] = await PublicKey.findProgramAddress(
[merkleRollPubKey.toBuffer()],
PROGRAM_ID
);
return bubblegumAuthorityPDAKey;
}
export async function getNonceCount(
connection: Connection,
tree: PublicKey
): Promise<BN> {
const treeAuthority = await getBubblegumAuthorityPDA(tree);
return new BN(
(await TreeConfig.fromAccountAddress(connection, treeAuthority)).numMinted
);
}
export function bufferToArray(buffer: Buffer): number[] {
const nums = [];
for (let i = 0; i < buffer.length; i++) {
nums.push(buffer[i]);
}
return nums;
}
export async function execute(
provider: Provider,
instructions: TransactionInstruction[],
signers: Signer[],
skipPreflight = false,
verbose = false
): Promise<string> {
let tx = new Transaction();
instructions.map((ix) => {
tx = tx.add(ix);
});
let txid: string | null = null;
try {
txid = await provider.sendAndConfirm!(tx, signers, {
skipPreflight,
});
} catch (e: any) {
console.log("Tx error!", e.logs);
throw e;
}
if (verbose && txid) {
console.log(
(await provider.connection.getConfirmedTransaction(txid, "confirmed"))!
.meta!.logMessages
);
}
return txid;
}
export async function getVoucherPDA(
tree: PublicKey,
leafIndex: number
): Promise<PublicKey> {
const [voucher] = await PublicKey.findProgramAddress(
[
Buffer.from("voucher", "utf8"),
tree.toBuffer(),
Uint8Array.from(new BN(leafIndex).toArray("le", 8)),
],
PROGRAM_ID
);
return voucher;
}
export async function getMetadata(mint: PublicKey) {
return (
await PublicKey.findProgramAddress(
[
Buffer.from("metadata"),
TOKEN_METADATA_PROGRAM_ID.toBuffer(),
mint.toBuffer(),
],
TOKEN_METADATA_PROGRAM_ID
)
)[0];
}
export async function getMasterEdition(mint: PublicKey) {
return (
await PublicKey.findProgramAddress(
[
Buffer.from("metadata"),
TOKEN_METADATA_PROGRAM_ID.toBuffer(),
mint.toBuffer(),
Buffer.from("edition"),
],
TOKEN_METADATA_PROGRAM_ID
)
)[0];
}