@loyal-labs/transactions 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 +292 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +521 -0
- package/dist/src/LoyalTransactionsClient.d.ts +126 -0
- package/dist/src/constants.d.ts +27 -0
- package/dist/src/idl.d.ts +336 -0
- package/dist/src/pda.d.ts +17 -0
- package/dist/src/types.d.ts +65 -0
- package/dist/src/wallet-adapter.d.ts +31 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# @loyal-labs/transactions
|
|
2
|
+
|
|
3
|
+
SDK for Telegram-based Solana deposits. Deposit SOL for any Telegram username, which can later be claimed by the verified account owner.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @loyal-labs/transactions
|
|
9
|
+
# or
|
|
10
|
+
npm install @loyal-labs/transactions
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Peer Dependencies
|
|
14
|
+
|
|
15
|
+
This SDK requires the following peer dependencies:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bun add @coral-xyz/anchor @solana/web3.js
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { Connection } from "@solana/web3.js";
|
|
25
|
+
import { LoyalTransactionsClient, solToLamports } from "@loyal-labs/transactions";
|
|
26
|
+
|
|
27
|
+
const connection = new Connection("https://api.devnet.solana.com");
|
|
28
|
+
const client = LoyalTransactionsClient.fromKeypair(connection, myKeypair);
|
|
29
|
+
|
|
30
|
+
const result = await client.deposit({
|
|
31
|
+
username: "alice",
|
|
32
|
+
amountLamports: solToLamports(0.1), // 0.1 SOL
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
console.log("Transaction:", result.signature);
|
|
36
|
+
console.log("Deposited:", result.deposit.amount, "lamports");
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
### Browser with Wallet Adapter
|
|
42
|
+
|
|
43
|
+
For React applications using `@solana/wallet-adapter-react`:
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { useConnection, useWallet } from "@solana/wallet-adapter-react";
|
|
47
|
+
import { LoyalTransactionsClient, solToLamports } from "@loyal-labs/transactions";
|
|
48
|
+
|
|
49
|
+
function DepositButton() {
|
|
50
|
+
const { connection } = useConnection();
|
|
51
|
+
const wallet = useWallet();
|
|
52
|
+
|
|
53
|
+
const handleDeposit = async () => {
|
|
54
|
+
if (!wallet.publicKey) return;
|
|
55
|
+
|
|
56
|
+
const client = LoyalTransactionsClient.fromWallet(connection, wallet);
|
|
57
|
+
|
|
58
|
+
const result = await client.deposit({
|
|
59
|
+
username: "bob",
|
|
60
|
+
amountLamports: solToLamports(0.5),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
console.log("Success!", result.signature);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return <button onClick={handleDeposit}>Deposit 0.5 SOL</button>;
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Server-Side with Keypair
|
|
71
|
+
|
|
72
|
+
For backend scripts, bots, or CLI tools:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { Connection, Keypair } from "@solana/web3.js";
|
|
76
|
+
import { LoyalTransactionsClient, solToLamports } from "@loyal-labs/transactions";
|
|
77
|
+
|
|
78
|
+
const connection = new Connection("https://api.devnet.solana.com");
|
|
79
|
+
const keypair = Keypair.fromSecretKey(mySecretKey);
|
|
80
|
+
|
|
81
|
+
const client = LoyalTransactionsClient.fromKeypair(connection, keypair);
|
|
82
|
+
|
|
83
|
+
const result = await client.deposit({
|
|
84
|
+
username: "charlie",
|
|
85
|
+
amountLamports: solToLamports(1.0),
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Existing Anchor Projects
|
|
90
|
+
|
|
91
|
+
If you're already using Anchor with a configured provider:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { AnchorProvider } from "@coral-xyz/anchor";
|
|
95
|
+
import { LoyalTransactionsClient } from "@loyal-labs/transactions";
|
|
96
|
+
|
|
97
|
+
const provider = AnchorProvider.env();
|
|
98
|
+
const client = LoyalTransactionsClient.fromProvider(provider);
|
|
99
|
+
|
|
100
|
+
const result = await client.deposit({
|
|
101
|
+
username: "dave",
|
|
102
|
+
amountLamports: 100_000_000,
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Universal Factory Method
|
|
107
|
+
|
|
108
|
+
The `from()` method auto-detects the signer type:
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
// Works with any supported signer
|
|
112
|
+
const client = LoyalTransactionsClient.from(connection, signer);
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## API Reference
|
|
116
|
+
|
|
117
|
+
### LoyalTransactionsClient
|
|
118
|
+
|
|
119
|
+
The main SDK class for interacting with the Telegram Transfer program.
|
|
120
|
+
|
|
121
|
+
#### Factory Methods
|
|
122
|
+
|
|
123
|
+
| Method | Description |
|
|
124
|
+
|--------|-------------|
|
|
125
|
+
| `fromProvider(provider)` | Create from an AnchorProvider |
|
|
126
|
+
| `fromWallet(connection, wallet)` | Create from Connection + wallet adapter |
|
|
127
|
+
| `fromKeypair(connection, keypair)` | Create from Connection + Keypair |
|
|
128
|
+
| `from(connection, signer)` | Auto-detect signer type |
|
|
129
|
+
|
|
130
|
+
#### Instance Methods
|
|
131
|
+
|
|
132
|
+
##### `deposit(params): Promise<DepositResult>`
|
|
133
|
+
|
|
134
|
+
Deposit SOL for a Telegram username.
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
interface DepositParams {
|
|
138
|
+
username: string; // Telegram username (5-32 chars, without @)
|
|
139
|
+
amountLamports: number | bigint; // Amount in lamports
|
|
140
|
+
commitment?: Commitment; // Optional, defaults to 'confirmed'
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
interface DepositResult {
|
|
144
|
+
signature: string; // Transaction signature
|
|
145
|
+
deposit: DepositData; // Updated deposit account data
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
##### `getDeposit(depositor, username): Promise<DepositData | null>`
|
|
150
|
+
|
|
151
|
+
Fetch deposit data for a specific depositor and username.
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
const deposit = await client.getDeposit(depositorPubkey, "alice");
|
|
155
|
+
if (deposit) {
|
|
156
|
+
console.log("Amount:", deposit.amount);
|
|
157
|
+
console.log("Address:", deposit.address.toBase58());
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
##### `findDepositPda(depositor, username): [PublicKey, number]`
|
|
162
|
+
|
|
163
|
+
Derive the deposit PDA address.
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
const [pda, bump] = client.findDepositPda(depositorPubkey, "alice");
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
##### `findVaultPda(): [PublicKey, number]`
|
|
170
|
+
|
|
171
|
+
Derive the vault PDA address.
|
|
172
|
+
|
|
173
|
+
#### Properties
|
|
174
|
+
|
|
175
|
+
| Property | Type | Description |
|
|
176
|
+
|----------|------|-------------|
|
|
177
|
+
| `publicKey` | `PublicKey` | Connected wallet's public key |
|
|
178
|
+
|
|
179
|
+
### Types
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
// Deposit account data
|
|
183
|
+
interface DepositData {
|
|
184
|
+
user: PublicKey; // Depositor's wallet
|
|
185
|
+
username: string; // Telegram username
|
|
186
|
+
amount: number; // Deposited lamports
|
|
187
|
+
lastNonce: number; // Replay protection nonce
|
|
188
|
+
address: PublicKey; // PDA address
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Supported wallet/signer types
|
|
192
|
+
type WalletSigner = WalletLike | Keypair | AnchorProvider;
|
|
193
|
+
|
|
194
|
+
// Wallet adapter interface
|
|
195
|
+
interface WalletLike {
|
|
196
|
+
publicKey: PublicKey;
|
|
197
|
+
signTransaction<T extends Transaction | VersionedTransaction>(tx: T): Promise<T>;
|
|
198
|
+
signAllTransactions<T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]>;
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Utility Functions
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
import {
|
|
206
|
+
solToLamports,
|
|
207
|
+
lamportsToSol,
|
|
208
|
+
findDepositPda,
|
|
209
|
+
findVaultPda,
|
|
210
|
+
PROGRAM_ID,
|
|
211
|
+
LAMPORTS_PER_SOL,
|
|
212
|
+
} from "@loyal-labs/transactions";
|
|
213
|
+
|
|
214
|
+
// Convert SOL to lamports
|
|
215
|
+
const lamports = solToLamports(1.5); // 1_500_000_000
|
|
216
|
+
|
|
217
|
+
// Convert lamports to SOL
|
|
218
|
+
const sol = lamportsToSol(1_500_000_000); // 1.5
|
|
219
|
+
|
|
220
|
+
// Derive PDAs directly (without client instance)
|
|
221
|
+
const [depositPda] = findDepositPda(userPubkey, "alice");
|
|
222
|
+
const [vaultPda] = findVaultPda();
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Constants
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import { PROGRAM_ID, DEPOSIT_SEED, VAULT_SEED, LAMPORTS_PER_SOL } from "@loyal-labs/transactions";
|
|
229
|
+
|
|
230
|
+
// Program ID: 4ewpzEPF5xrVAHeRkoe7XS1yKFGQBekD7PgFwEz9SaxY
|
|
231
|
+
console.log(PROGRAM_ID.toBase58());
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## Advanced Usage
|
|
235
|
+
|
|
236
|
+
### Accessing the Anchor Program
|
|
237
|
+
|
|
238
|
+
For advanced users who need direct access to the underlying Anchor program:
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
const program = client.getProgram();
|
|
242
|
+
|
|
243
|
+
// Use program directly for custom operations
|
|
244
|
+
const accounts = await program.account.deposit.all();
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Type Guards
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
import { isKeypair, isAnchorProvider, isWalletLike } from "@loyal-labs/transactions";
|
|
251
|
+
|
|
252
|
+
if (isKeypair(signer)) {
|
|
253
|
+
console.log("Using Keypair");
|
|
254
|
+
} else if (isAnchorProvider(signer)) {
|
|
255
|
+
console.log("Using AnchorProvider");
|
|
256
|
+
} else if (isWalletLike(signer)) {
|
|
257
|
+
console.log("Using wallet adapter");
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## Error Handling
|
|
262
|
+
|
|
263
|
+
The SDK throws descriptive errors for common issues:
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
try {
|
|
267
|
+
await client.deposit({
|
|
268
|
+
username: "ab", // Too short!
|
|
269
|
+
amountLamports: 100,
|
|
270
|
+
});
|
|
271
|
+
} catch (error) {
|
|
272
|
+
// "Username must be between 5 and 32 characters"
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
Common errors:
|
|
277
|
+
- `"Username must be between 5 and 32 characters"` - Invalid username length
|
|
278
|
+
- `"Amount must be greater than 0"` - Invalid deposit amount
|
|
279
|
+
- `"Failed to fetch deposit account after transaction"` - Network/confirmation issue
|
|
280
|
+
|
|
281
|
+
## Development
|
|
282
|
+
|
|
283
|
+
```bash
|
|
284
|
+
# Install dependencies
|
|
285
|
+
bun install
|
|
286
|
+
|
|
287
|
+
# Type check
|
|
288
|
+
bun run typecheck
|
|
289
|
+
|
|
290
|
+
# Run tests
|
|
291
|
+
bun test
|
|
292
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @loyal-labs/transactions - SDK for Telegram-based Solana deposits
|
|
3
|
+
*
|
|
4
|
+
* This SDK provides a simple interface for depositing SOL for Telegram usernames.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* // Browser with wallet adapter
|
|
8
|
+
* import { LoyalTransactionsClient } from '@loyal-labs/transactions';
|
|
9
|
+
* import { useConnection, useWallet } from '@solana/wallet-adapter-react';
|
|
10
|
+
*
|
|
11
|
+
* const { connection } = useConnection();
|
|
12
|
+
* const wallet = useWallet();
|
|
13
|
+
* const client = LoyalTransactionsClient.fromWallet(connection, wallet);
|
|
14
|
+
*
|
|
15
|
+
* const result = await client.deposit({
|
|
16
|
+
* username: 'alice',
|
|
17
|
+
* amountLamports: 100_000_000,
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Server-side with keypair
|
|
22
|
+
* import { LoyalTransactionsClient } from '@loyal-labs/transactions';
|
|
23
|
+
* import { Connection, Keypair } from '@solana/web3.js';
|
|
24
|
+
*
|
|
25
|
+
* const connection = new Connection('https://api.devnet.solana.com');
|
|
26
|
+
* const keypair = Keypair.fromSecretKey(secretKey);
|
|
27
|
+
* const client = LoyalTransactionsClient.fromKeypair(connection, keypair);
|
|
28
|
+
*
|
|
29
|
+
* const result = await client.deposit({
|
|
30
|
+
* username: 'bob',
|
|
31
|
+
* amountLamports: 50_000_000,
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // Existing Anchor project
|
|
36
|
+
* import { LoyalTransactionsClient } from '@loyal-labs/transactions';
|
|
37
|
+
* import { AnchorProvider } from '@coral-xyz/anchor';
|
|
38
|
+
*
|
|
39
|
+
* const provider = AnchorProvider.env();
|
|
40
|
+
* const client = LoyalTransactionsClient.fromProvider(provider);
|
|
41
|
+
*/
|
|
42
|
+
export { LoyalTransactionsClient } from "./src/LoyalTransactionsClient";
|
|
43
|
+
export type { WalletSigner, WalletLike, DepositParams, DepositResult, DepositData, } from "./src/types";
|
|
44
|
+
export { isKeypair, isAnchorProvider, isWalletLike } from "./src/types";
|
|
45
|
+
export { PROGRAM_ID, DEPOSIT_SEED, VAULT_SEED, LAMPORTS_PER_SOL, solToLamports, lamportsToSol, } from "./src/constants";
|
|
46
|
+
export { findDepositPda, findVaultPda } from "./src/pda";
|
|
47
|
+
export { IDL } from "./src/idl";
|
|
48
|
+
export type { TelegramTransfer } from "./src/idl";
|