@revibase/core 0.0.59 → 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.
package/README.md ADDED
@@ -0,0 +1,316 @@
1
+ # @revibase/core
2
+
3
+ Core types and helpers for Revibase multi-wallet: transfer intents and custom vault-paid transactions (sync or Jito bundles).
4
+
5
+ **Contents:** [Create user](#create-a-user-account) → [Create wallet](#create-a-wallet) → [Transfer intents](#transfer-intents) (SOL / SPL) → [Custom transactions](#custom-transactions-sync-vs-bundle) (sync or Jito).
6
+
7
+ ---
8
+
9
+ ## Create a user account
10
+
11
+ Create one or more user accounts. Each user is identified by a member key (e.g. an Ed25519 signer) and a role. The helper returns an instruction—send it in a transaction with your Solana client.
12
+
13
+ ```ts
14
+ import { createUserAccounts, UserRole } from "@revibase/core";
15
+ import type { TransactionSigner } from "gill";
16
+
17
+ declare const payer: TransactionSigner;
18
+ declare const memberSigner: TransactionSigner;
19
+
20
+ const createUserIx = await createUserAccounts({
21
+ payer,
22
+ createUserArgs: [{ member: memberSigner, role: UserRole.Member }],
23
+ });
24
+ // Build a tx with createUserIx; sign with payer + memberSigner, then send.
25
+ ```
26
+
27
+ ---
28
+
29
+ ## Create a wallet
30
+
31
+ Create a wallet (settings + vault) with an existing user as the initial member. The user must exist first (see [Create a user account](#create-a-user-account)). Use the global counter for the next wallet index, then optionally set this wallet as the user’s delegate.
32
+
33
+ ```ts
34
+ import {
35
+ createWallet,
36
+ editUserDelegate,
37
+ fetchGlobalCounter,
38
+ getGlobalCounterAddress,
39
+ getSolanaRpc,
40
+ } from "@revibase/core";
41
+ import type { TransactionSigner } from "gill";
42
+
43
+ declare const payer: TransactionSigner;
44
+ declare const memberSigner: TransactionSigner;
45
+
46
+ const globalCounter = await fetchGlobalCounter(
47
+ getSolanaRpc(),
48
+ await getGlobalCounterAddress(),
49
+ );
50
+
51
+ const createWalletIx = await createWallet({
52
+ index: globalCounter.data.index,
53
+ payer,
54
+ initialMember: memberSigner,
55
+ });
56
+
57
+ // Build a tx with createWalletIx; sign with payer + memberSigner, then send.
58
+
59
+ const setWalletAsDelegateIx = await editUserDelegate({
60
+ payer,
61
+ user: memberSigner,
62
+ newDelegate: {
63
+ index: globalCounter.data.index,
64
+ settingsAddressTreeIndex: 0,
65
+ },
66
+ });
67
+
68
+ // Build a tx with setWalletAsDelegateIx; sign with payer + memberSigner, then send.
69
+ ```
70
+
71
+ After confirmation, use [Resolve settings and compressed flag](#1-resolve-settings-and-compressed-flag) with this index to get `settings`, `compressed`, and `walletAddress` for transfers or custom transactions.
72
+
73
+ ---
74
+
75
+ ## Transfer intents
76
+
77
+ Move SOL or SPL tokens from a multi-wallet via on-chain intent instructions.
78
+
79
+ ### 1. Resolve settings and compressed flag
80
+
81
+ Using the member signer, get the delegated wallet’s settings and compression flag:
82
+
83
+ ```ts
84
+ import {
85
+ fetchUserAccountData,
86
+ fetchSettingsAccountData,
87
+ getSettingsFromIndex,
88
+ getWalletAddressFromSettings,
89
+ } from "@revibase/core";
90
+ import type { TransactionSigner } from "gill";
91
+
92
+ declare const memberSigner: TransactionSigner;
93
+
94
+ const user = await fetchUserAccountData(memberSigner.address);
95
+ const delegatedWallet = user.wallets.find((w) => w.isDelegate);
96
+ if (!delegatedWallet)
97
+ throw new Error("memberSigner is not delegated to any wallet");
98
+
99
+ const settingsIndex = delegatedWallet.index;
100
+ const settings = await getSettingsFromIndex(settingsIndex);
101
+ const settingsAccount = await fetchSettingsAccountData(settings);
102
+ const compressed = settingsAccount.isCompressed;
103
+ const walletAddress = await getWalletAddressFromSettings(settings);
104
+ ```
105
+
106
+ Use `settings`, `compressed`, and (optionally) `walletAddress` in the following steps.
107
+
108
+ ### 2. Native SOL transfer
109
+
110
+ ```ts
111
+ import {
112
+ nativeTransferIntent,
113
+ retrieveTransactionManager,
114
+ getSignedTransactionManager,
115
+ } from "@revibase/core";
116
+ import type { TransactionSigner } from "gill";
117
+
118
+ declare const payer: TransactionSigner;
119
+ declare const memberSigner: TransactionSigner;
120
+ declare const destination: string;
121
+
122
+ // For wallets with a transaction manager, add its signer. See Custom transactions for full flow.
123
+ const tmResult = retrieveTransactionManager(
124
+ memberSigner.address.toString(),
125
+ settingsAccount,
126
+ );
127
+ const transactionManagerSigner =
128
+ "transactionManagerAddress" in tmResult
129
+ ? await getSignedTransactionManager({
130
+ transactionManagerAddress: tmResult.transactionManagerAddress,
131
+ userAddressTreeIndex: tmResult.userAddressTreeIndex,
132
+ })
133
+ : null;
134
+
135
+ const instructions = await nativeTransferIntent({
136
+ settings,
137
+ destination,
138
+ amount: 100_000n, // lamports
139
+ signers: [
140
+ memberSigner,
141
+ ...(transactionManagerSigner ? [transactionManagerSigner] : []),
142
+ ],
143
+ payer,
144
+ compressed,
145
+ });
146
+ // Build tx from instructions with prepareTransactionMessage (or similar), then send.
147
+ ```
148
+
149
+ ### 3. SPL / Token-2022 transfer
150
+
151
+ ```ts
152
+ import {
153
+ tokenTransferIntent,
154
+ retrieveTransactionManager,
155
+ getSignedTransactionManager,
156
+ } from "@revibase/core";
157
+ import type { Address, TransactionSigner } from "gill";
158
+ import { TOKEN_2022_PROGRAM_ADDRESS } from "gill/programs";
159
+
160
+ declare const payer: TransactionSigner;
161
+ declare const memberSigner: TransactionSigner;
162
+ declare const destinationWallet: Address;
163
+ declare const mint: Address;
164
+
165
+ const tmResult = retrieveTransactionManager(
166
+ memberSigner.address.toString(),
167
+ settingsAccount,
168
+ );
169
+ const transactionManagerSigner =
170
+ "transactionManagerAddress" in tmResult
171
+ ? await getSignedTransactionManager({
172
+ transactionManagerAddress: tmResult.transactionManagerAddress,
173
+ userAddressTreeIndex: tmResult.userAddressTreeIndex,
174
+ })
175
+ : null;
176
+
177
+ const instructions = await tokenTransferIntent({
178
+ settings,
179
+ payer,
180
+ signers: [
181
+ memberSigner,
182
+ ...(transactionManagerSigner ? [transactionManagerSigner] : []),
183
+ ],
184
+ destination: destinationWallet,
185
+ amount: 1_000_000n,
186
+ mint,
187
+ tokenProgram: TOKEN_2022_PROGRAM_ADDRESS,
188
+ compressed,
189
+ });
190
+ // Build tx from instructions, then send. Same signer pattern as native transfer if using a transaction manager.
191
+ ```
192
+
193
+ ---
194
+
195
+ ## Custom transactions (sync vs bundle)
196
+
197
+ - **Small tx size** → **sync**: `prepareTransactionMessage` → `prepareTransactionSync` → `signAndSendTransaction`
198
+ - **Larger tx size** → **Jito bundle**: `prepareTransactionMessage` → `prepareTransactionBundle` → `signAndSendBundledTransactions`
199
+
200
+ Prerequisite: `settings`, `compressed`, `walletAddress`, and `settingsAccount` from [Resolve settings and compressed flag](#1-resolve-settings-and-compressed-flag).
201
+
202
+ ### Sync: prepareTransactionSync
203
+
204
+ ```ts
205
+ import {
206
+ prepareTransactionMessage,
207
+ prepareTransactionSync,
208
+ signAndSendTransaction,
209
+ retrieveTransactionManager,
210
+ getSignedTransactionManager,
211
+ } from "@revibase/core";
212
+ import {
213
+ createNoopSigner,
214
+ type Address,
215
+ type AddressesByLookupTableAddress,
216
+ type TransactionSigner,
217
+ } from "gill";
218
+ import { getTransferSolInstruction } from "gill/programs";
219
+
220
+ declare const destination: Address;
221
+ declare const payer: TransactionSigner;
222
+ declare const memberSigner: TransactionSigner;
223
+ declare const addressLookups: AddressesByLookupTableAddress | undefined;
224
+
225
+ const transferIx = getTransferSolInstruction({
226
+ source: createNoopSigner(walletAddress),
227
+ destination,
228
+ amount: 1_000_000n,
229
+ });
230
+
231
+ const transactionMessageBytes = prepareTransactionMessage({
232
+ payer: walletAddress,
233
+ instructions: [transferIx],
234
+ addressesByLookupTableAddress: addressLookups,
235
+ });
236
+
237
+ const { transactionManagerAddress, userAddressTreeIndex } =
238
+ retrieveTransactionManager(memberSigner.address.toString(), settingsAccount);
239
+ const transactionManagerSigner = await getSignedTransactionManager({
240
+ transactionMessageBytes,
241
+ transactionManagerAddress,
242
+ userAddressTreeIndex,
243
+ });
244
+
245
+ const details = await prepareTransactionSync({
246
+ compressed,
247
+ payer,
248
+ settings,
249
+ transactionMessageBytes,
250
+ signers: [
251
+ memberSigner,
252
+ ...(transactionManagerSigner ? [transactionManagerSigner] : []),
253
+ ],
254
+ addressesByLookupTableAddress: addressLookups,
255
+ });
256
+
257
+ const signature = await signAndSendTransaction(details);
258
+ ```
259
+
260
+ ### Jito bundle
261
+
262
+ ```ts
263
+ import {
264
+ prepareTransactionMessage,
265
+ prepareTransactionBundle,
266
+ signAndSendBundledTransactions,
267
+ pollJitoBundleConfirmation,
268
+ retrieveTransactionManager,
269
+ getSignedTransactionManager,
270
+ } from "@revibase/core";
271
+ import {
272
+ createNoopSigner,
273
+ type Address,
274
+ type AddressesByLookupTableAddress,
275
+ type TransactionSigner,
276
+ } from "gill";
277
+ import { getTransferSolInstruction } from "gill/programs";
278
+
279
+ declare const destination: Address;
280
+ declare const payer: TransactionSigner;
281
+ declare const memberSigner: TransactionSigner;
282
+ declare const addressLookups: AddressesByLookupTableAddress | undefined;
283
+
284
+ const transferIx = getTransferSolInstruction({
285
+ source: createNoopSigner(walletAddress),
286
+ destination,
287
+ amount: 1_000_000n,
288
+ });
289
+ const transactionMessageBytes = prepareTransactionMessage({
290
+ payer: walletAddress,
291
+ instructions: [transferIx],
292
+ addressesByLookupTableAddress: addressLookups,
293
+ });
294
+
295
+ const { transactionManagerAddress, userAddressTreeIndex } =
296
+ retrieveTransactionManager(memberSigner.address.toString(), settingsAccount);
297
+ const transactionManagerSigner = await getSignedTransactionManager({
298
+ transactionMessageBytes,
299
+ transactionManagerAddress,
300
+ userAddressTreeIndex,
301
+ });
302
+
303
+ const bundle = await prepareTransactionBundle({
304
+ payer,
305
+ settings,
306
+ transactionMessageBytes,
307
+ creator: transactionManagerSigner ?? memberSigner,
308
+ executor: transactionManagerSigner ? memberSigner : undefined,
309
+ compressed,
310
+ addressesByLookupTableAddress: addressLookups,
311
+ jitoBundlesTipAmount: 10_000, // optional, lamports
312
+ });
313
+
314
+ const bundleId = await signAndSendBundledTransactions(bundle);
315
+ const signature = await pollJitoBundleConfirmation(bundleId);
316
+ ```