@vibeiao/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,220 @@
1
+ // src/solana.ts
2
+ import { AnchorProvider, Program } from "@coral-xyz/anchor";
3
+ import {
4
+ LAMPORTS_PER_SOL,
5
+ PublicKey,
6
+ SystemProgram
7
+ } from "@solana/web3.js";
8
+ import BN from "bn.js";
9
+ import { VIBEIAO_IDL } from "@vibeiao/shared";
10
+ var toPublicKey = (value) => value instanceof PublicKey ? value : new PublicKey(value);
11
+ var toAnchorWallet = (wallet) => {
12
+ if ("secretKey" in wallet) {
13
+ const keypair = wallet;
14
+ return {
15
+ publicKey: keypair.publicKey,
16
+ signTransaction: async (tx) => {
17
+ if (tx && typeof tx === "object") {
18
+ const maybeLegacy = tx;
19
+ const maybeVersioned = tx;
20
+ if (typeof maybeLegacy.partialSign === "function") {
21
+ maybeLegacy.partialSign(keypair);
22
+ } else if (typeof maybeVersioned.sign === "function") {
23
+ maybeVersioned.sign([keypair]);
24
+ }
25
+ }
26
+ return tx;
27
+ },
28
+ signAllTransactions: async (txs) => {
29
+ txs.forEach((tx) => {
30
+ if (tx && typeof tx === "object") {
31
+ const maybeLegacy = tx;
32
+ const maybeVersioned = tx;
33
+ if (typeof maybeLegacy.partialSign === "function") {
34
+ maybeLegacy.partialSign(keypair);
35
+ } else if (typeof maybeVersioned.sign === "function") {
36
+ maybeVersioned.sign([keypair]);
37
+ }
38
+ }
39
+ });
40
+ return txs;
41
+ }
42
+ };
43
+ }
44
+ const walletLike = wallet;
45
+ if (typeof walletLike.signAllTransactions !== "function") {
46
+ return {
47
+ publicKey: walletLike.publicKey,
48
+ signTransaction: walletLike.signTransaction,
49
+ signAllTransactions: async (txs) => {
50
+ const signed = [];
51
+ for (const tx of txs) {
52
+ signed.push(await walletLike.signTransaction(tx));
53
+ }
54
+ return signed;
55
+ }
56
+ };
57
+ }
58
+ return walletLike;
59
+ };
60
+ var toLamports = (sol) => new BN(Math.round(sol * LAMPORTS_PER_SOL));
61
+ var toSol = (lamports) => Number(lamports) / LAMPORTS_PER_SOL;
62
+ var deriveListingPda = (creator, seed, programId) => PublicKey.findProgramAddressSync([
63
+ Buffer.from("listing"),
64
+ creator.toBuffer(),
65
+ Buffer.from(seed)
66
+ ], programId);
67
+ var deriveTicketReceiptPda = (listing, buyer, programId) => PublicKey.findProgramAddressSync([
68
+ Buffer.from("ticket"),
69
+ listing.toBuffer(),
70
+ buyer.toBuffer()
71
+ ], programId);
72
+ var deriveConfigPda = (programId) => PublicKey.findProgramAddressSync([Buffer.from("config")], programId);
73
+ var fetchSolBalance = async (connection, wallet) => {
74
+ const pubkey = toPublicKey(wallet);
75
+ const lamports = await connection.getBalance(pubkey, "confirmed");
76
+ return toSol(lamports);
77
+ };
78
+ var fetchTokenBalance = async (connection, wallet, mint) => {
79
+ const owner = toPublicKey(wallet);
80
+ const mintKey = toPublicKey(mint);
81
+ const response = await connection.getTokenAccountsByOwner(owner, { mint: mintKey });
82
+ const account = response.value[0];
83
+ if (!account) return 0;
84
+ const balance = await connection.getTokenAccountBalance(account.pubkey, "confirmed");
85
+ return Number(balance.value.uiAmount || 0);
86
+ };
87
+ var fetchTokenBalances = async (connection, wallet, mints) => {
88
+ const owner = toPublicKey(wallet);
89
+ const results = {};
90
+ await Promise.all(
91
+ mints.map(async (mint) => {
92
+ const mintKey = toPublicKey(mint);
93
+ const response = await connection.getTokenAccountsByOwner(owner, { mint: mintKey });
94
+ const account = response.value[0];
95
+ if (!account) {
96
+ results[mintKey.toBase58()] = 0;
97
+ return;
98
+ }
99
+ const balance = await connection.getTokenAccountBalance(account.pubkey, "confirmed");
100
+ results[mintKey.toBase58()] = Number(balance.value.uiAmount || 0);
101
+ })
102
+ );
103
+ return results;
104
+ };
105
+ var VibePayments = class {
106
+ connection;
107
+ wallet;
108
+ programId;
109
+ commitment;
110
+ constructor(options) {
111
+ this.connection = options.connection;
112
+ this.wallet = toAnchorWallet(options.wallet);
113
+ this.programId = toPublicKey(options.programId);
114
+ this.commitment = options.commitment || "confirmed";
115
+ }
116
+ getProgramId() {
117
+ return this.programId.toBase58();
118
+ }
119
+ getProgram() {
120
+ const provider = new AnchorProvider(this.connection, this.wallet, {
121
+ preflightCommitment: this.commitment
122
+ });
123
+ return new Program(VIBEIAO_IDL, this.programId, provider);
124
+ }
125
+ async fetchProtocolConfig() {
126
+ const program = this.getProgram();
127
+ const [config] = deriveConfigPda(this.programId);
128
+ const data = await program.account.protocolConfig.fetch(config);
129
+ return {
130
+ platformTreasury: data.platformTreasury.toBase58(),
131
+ buybackTreasury: data.buybackTreasury.toBase58()
132
+ };
133
+ }
134
+ getTicketReceiptAddress(listingAccount, buyer) {
135
+ const listingPubkey = new PublicKey(listingAccount);
136
+ const buyerKey = buyer ?? this.wallet.publicKey;
137
+ const [receipt] = deriveTicketReceiptPda(listingPubkey, buyerKey, this.programId);
138
+ return receipt.toBase58();
139
+ }
140
+ async purchaseTicket(input) {
141
+ const program = this.getProgram();
142
+ const listingPubkey = new PublicKey(input.listingAccount);
143
+ const [ticketReceipt] = deriveTicketReceiptPda(listingPubkey, this.wallet.publicKey, this.programId);
144
+ const [config] = deriveConfigPda(this.programId);
145
+ let platformTreasury = input.platformTreasury;
146
+ let buybackTreasury = input.buybackTreasury;
147
+ if (!platformTreasury || !buybackTreasury) {
148
+ const protocol = await this.fetchProtocolConfig();
149
+ platformTreasury = platformTreasury || protocol.platformTreasury;
150
+ buybackTreasury = buybackTreasury || protocol.buybackTreasury;
151
+ }
152
+ if (!platformTreasury || !buybackTreasury) {
153
+ throw new Error("protocol_config_missing");
154
+ }
155
+ return program.methods.purchaseTicket().accounts({
156
+ buyer: this.wallet.publicKey,
157
+ listing: listingPubkey,
158
+ config,
159
+ creator: new PublicKey(input.creatorWallet),
160
+ revenueWallet: new PublicKey(input.revenueWallet),
161
+ platformTreasury: new PublicKey(platformTreasury),
162
+ buybackTreasury: new PublicKey(buybackTreasury),
163
+ ticketReceipt,
164
+ systemProgram: SystemProgram.programId
165
+ }).rpc();
166
+ }
167
+ async purchaseTicketForListing(input) {
168
+ const listing = input.listing;
169
+ const listingAccount = listing.chain_listing_account;
170
+ const creatorWallet = listing.creator_wallet_address;
171
+ const revenueWallet = listing.revenue_wallet_address || listing.creator_wallet_address;
172
+ if (!listingAccount) {
173
+ throw new Error("listing_missing_chain_account");
174
+ }
175
+ if (!creatorWallet) {
176
+ throw new Error("listing_missing_creator_wallet");
177
+ }
178
+ if (!revenueWallet) {
179
+ throw new Error("listing_missing_revenue_wallet");
180
+ }
181
+ return this.purchaseTicket({
182
+ listingAccount,
183
+ creatorWallet,
184
+ revenueWallet,
185
+ platformTreasury: input.platformTreasury,
186
+ buybackTreasury: input.buybackTreasury
187
+ });
188
+ }
189
+ async redeemTicket(listingAccount) {
190
+ const program = this.getProgram();
191
+ const listingPubkey = new PublicKey(listingAccount);
192
+ const [ticketReceipt] = deriveTicketReceiptPda(listingPubkey, this.wallet.publicKey, this.programId);
193
+ return program.methods.redeemTicket().accounts({
194
+ buyer: this.wallet.publicKey,
195
+ listing: listingPubkey,
196
+ ticketReceipt
197
+ }).rpc();
198
+ }
199
+ async updateTicketPrice(listingAccount, priceSol) {
200
+ const program = this.getProgram();
201
+ const listingPubkey = new PublicKey(listingAccount);
202
+ const lamports = toLamports(priceSol);
203
+ return program.methods.updateTicketPrice(lamports).accounts({
204
+ creator: this.wallet.publicKey,
205
+ listing: listingPubkey
206
+ }).rpc();
207
+ }
208
+ };
209
+
210
+ export {
211
+ toLamports,
212
+ toSol,
213
+ deriveListingPda,
214
+ deriveTicketReceiptPda,
215
+ deriveConfigPda,
216
+ fetchSolBalance,
217
+ fetchTokenBalance,
218
+ fetchTokenBalances,
219
+ VibePayments
220
+ };