Skip to content

Commit 1a7894f

Browse files
authored
Merge pull request #1 from Lightprotocol/refactor
chore: refactor
2 parents d2c9465 + b8044ab commit 1a7894f

51 files changed

Lines changed: 1387 additions & 2126 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_KEY=YOUR_KEY

.gitignore

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
node_modules/
2-
dist/
3-
.env
4-
pnpm-lock.yaml
5-
package-lock.json
1+
**/node_modules/
2+
**/dist/
3+
**/.env
4+
**/pnpm-lock.yaml
5+
**/package-lock.json
6+
**/*.json.bak
7+
**/test-ledger

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"tabWidth": 4
3+
}

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,41 +34,41 @@ End-to-end example: create a mint, token account, mint tokens, and transfer on d
3434

3535
## Cookbook
3636

37-
Step-by-step recipes for light-token on localnet.
37+
Step-by-step recipes for light-token on Devnet/Localnet.
3838

3939
### [Actions](cookbook/actions/)
4040

41-
- **[create-mint](cookbook/actions/create-mint.ts)** - Create a new light-token mint
42-
- **[create-ata](cookbook/actions/create-ata.ts)** - Create an associated light-token account
43-
- **[load-ata](cookbook/actions/load-ata.ts)** - Load a cold token account (compressed) to hot balance (light-token ata)
44-
- **[mint-to](cookbook/actions/mint-to.ts)** - Mint tokens to a light-account
45-
- **[transfer-interface](cookbook/actions/transfer-interface.ts)** - Transfer tokens between light-token, T22, and SPL token accounts.
46-
- **[wrap](cookbook/actions/wrap.ts)** - Wrap SPL to light-token
47-
- **[unwrap](cookbook/actions/unwrap.ts)** - Unwrap light-token to SPL for off-ramps and legacy integrations
41+
- **[create-mint](cookbook/actions/create-mint.ts)** - Create a new light-token mint
42+
- **[create-ata](cookbook/actions/create-ata.ts)** - Create an associated light-token account
43+
- **[load-ata](cookbook/actions/load-ata.ts)** - Load a cold token account (compressed) to hot balance (light-token ata)
44+
- **[mint-to](cookbook/actions/mint-to.ts)** - Mint tokens to a light-account
45+
- **[transfer-interface](cookbook/actions/transfer-interface.ts)** - Transfer tokens between light-token, T22, and SPL token accounts.
46+
- **[wrap](cookbook/actions/wrap.ts)** - Wrap SPL to light-token
47+
- **[unwrap](cookbook/actions/unwrap.ts)** - Unwrap light-token to SPL for off-ramps and legacy integrations
4848

4949
### [Instructions](cookbook/instructions/)
5050

5151
Low-level instruction builders:
5252

53-
- **[create-mint](cookbook/instructions/create-mint.ts)** - Build create mint instruction
54-
- **[create-ata](cookbook/instructions/create-ata.ts)** - Build create ATA instruction
55-
- **[load-ata](cookbook/instructions/load-ata.ts)** - Build load ATA instruction
56-
- **[mint-to](cookbook/instructions/mint-to.ts)** - Build mint-to instruction
57-
- **[transfer-interface](cookbook/instructions/transfer-interface.ts)** - Build transfer interface instruction for transfers between light-token, T22, and SPL token accounts.
58-
- **[wrap](cookbook/instructions/wrap.ts)** - Build wrap instruction
59-
- **[unwrap](cookbook/instructions/unwrap.ts)** - Build unwrap instruction for off-ramps and legacy integrations
53+
- **[create-mint](cookbook/instructions/create-mint.ts)** - Build create mint instruction
54+
- **[create-ata](cookbook/instructions/create-ata.ts)** - Build create ATA instruction
55+
- **[load-ata](cookbook/instructions/load-ata.ts)** - Build load ATA instruction
56+
- **[mint-to](cookbook/instructions/mint-to.ts)** - Build mint-to instruction
57+
- **[transfer-interface](cookbook/instructions/transfer-interface.ts)** - Build transfer interface instruction for transfers between light-token, T22, and SPL token accounts.
58+
- **[wrap](cookbook/instructions/wrap.ts)** - Build wrap instruction
59+
- **[unwrap](cookbook/instructions/unwrap.ts)** - Build unwrap instruction for off-ramps and legacy integrations
6060

6161
## Toolkits
6262

6363
### [Payments and Wallets](toolkits/payments-and-wallets/)
6464

6565
Examples for wallet integrations and payment flows:
6666

67-
- **[get-balance](toolkits/payments-and-wallets/get-balance.ts)** - Fetch token balances for light-token accounts
68-
- **[get-history](toolkits/payments-and-wallets/get-history.ts)** - Fetch transaction history for light-token accounts
69-
- **[send-and-receive](toolkits/payments-and-wallets/send-and-receive.ts)** - Send and receive light-tokens using the transfer interface
70-
- **[wrap-from-spl](toolkits/payments-and-wallets/wrap-from-spl.ts)** - Wrap SPL tokens to light-token
71-
- **[unwrap-to-spl](toolkits/payments-and-wallets/unwrap-to-spl.ts)** - Unwrap light-token to SPL for off-ramps and legacy integrations
67+
- **[get-balance](toolkits/payments-and-wallets/get-balance.ts)** - Fetch token balances for light-token accounts
68+
- **[get-history](toolkits/payments-and-wallets/get-history.ts)** - Fetch transaction history for light-token accounts
69+
- **[send-and-receive](toolkits/payments-and-wallets/send-and-receive.ts)** - Send and receive light-tokens using the transfer interface
70+
- **[wrap](toolkits/payments-and-wallets/wrap.ts)** - Wrap SPL tokens to light-token
71+
- **[unwrap](toolkits/payments-and-wallets/unwrap.ts)** - Unwrap light-token to SPL for off-ramps and legacy integrations
7272

7373
### [Streaming Tokens](toolkits/streaming-tokens/)
7474

cookbook/.env.example

Lines changed: 0 additions & 2 deletions
This file was deleted.

cookbook/actions/compress-batch.ts

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,32 @@
11
import "dotenv/config";
22
import { Keypair } from "@solana/web3.js";
33
import { createRpc, bn } from "@lightprotocol/stateless.js";
4-
import {
5-
createMint,
6-
mintTo,
7-
decompress,
8-
compress,
9-
} from "@lightprotocol/compressed-token";
4+
import { createMint, mintTo, decompress, compress } from "@lightprotocol/compressed-token";
105
import { createAssociatedTokenAccount } from "@solana/spl-token";
6+
import { homedir } from "os";
7+
import { readFileSync } from "fs";
118

12-
async function main() {
13-
// 1. Setup RPC and fund accounts
14-
const rpc = createRpc();
15-
const payer = Keypair.generate();
16-
const airdropSig = await rpc.requestAirdrop(payer.publicKey, 10e9);
17-
await rpc.confirmTransaction(airdropSig, "confirmed");
18-
console.log("Payer:", payer.publicKey.toBase58());
9+
const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
10+
const payer = Keypair.fromSecretKey(
11+
new Uint8Array(
12+
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
13+
)
14+
);
1915

20-
const owner = Keypair.generate();
21-
const airdropSig2 = await rpc.requestAirdrop(owner.publicKey, 1e9);
22-
await rpc.confirmTransaction(airdropSig2, "confirmed");
16+
(async function () {
17+
const rpc = createRpc(RPC_URL);
2318

24-
// 2. Create SPL mint with token pool
25-
const mintAuthority = Keypair.generate();
26-
const mintKeypair = Keypair.generate();
27-
const { mint } = await createMint(
28-
rpc,
29-
payer,
30-
mintAuthority.publicKey,
31-
9,
32-
mintKeypair
33-
);
34-
console.log("Mint:", mint.toBase58());
19+
// Setup: Get SPL tokens (needed to compress)
20+
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
21+
const splAta = await createAssociatedTokenAccount(rpc, payer, mint, payer.publicKey);
22+
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(10000));
23+
await decompress(rpc, payer, mint, bn(10000), payer, splAta);
3524

36-
// 3. Create SPL ATA and fund it
37-
const splAta = await createAssociatedTokenAccount(
38-
rpc,
39-
payer,
40-
mint,
41-
owner.publicKey
42-
);
43-
console.log("SPL ATA:", splAta.toBase58());
25+
// Batch compress to multiple recipients
26+
const recipients = Array.from({ length: 5 }, () => Keypair.generate().publicKey);
27+
const amounts = [bn(100), bn(200), bn(300), bn(400), bn(500)];
4428

45-
await mintTo(rpc, payer, mint, owner.publicKey, mintAuthority, bn(10000));
46-
await decompress(rpc, payer, mint, bn(10000), owner, splAta);
47-
console.log("Funded SPL ATA with 10000 tokens");
29+
const tx = await compress(rpc, payer, mint, amounts, payer, splAta, recipients);
4830

49-
// 4. Batch compress to multiple recipients (max 10 for V2 trees)
50-
const recipients = Array.from({ length: 5 }, () => Keypair.generate().publicKey);
51-
const amounts = [bn(100), bn(200), bn(300), bn(400), bn(500)];
52-
53-
const signature = await compress(
54-
rpc,
55-
payer,
56-
mint,
57-
amounts, // array of amounts
58-
owner,
59-
splAta,
60-
recipients // array of recipients (same length as amounts)
61-
);
62-
63-
console.log("Batch compressed to 5 recipients");
64-
console.log("Amounts:", amounts.map((a) => a.toString()).join(", "));
65-
console.log("Transaction:", signature);
66-
}
67-
68-
main().catch((err) => {
69-
console.error("Error:", err);
70-
if (err.logs) console.error("Logs:", err.logs);
71-
process.exit(1);
72-
});
31+
console.log("Tx:", tx);
32+
})();

cookbook/actions/compress.ts

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,30 @@
11
import "dotenv/config";
22
import { Keypair } from "@solana/web3.js";
33
import { createRpc, bn } from "@lightprotocol/stateless.js";
4-
import {
5-
createMint,
6-
mintTo,
7-
decompress,
8-
compress,
9-
} from "@lightprotocol/compressed-token";
4+
import { createMint, mintTo, decompress, compress } from "@lightprotocol/compressed-token";
105
import { createAssociatedTokenAccount } from "@solana/spl-token";
11-
12-
async function main() {
13-
// 1. Setup RPC and fund accounts
14-
const rpc = createRpc();
15-
const payer = Keypair.generate();
16-
const airdropSig = await rpc.requestAirdrop(payer.publicKey, 10e9);
17-
await rpc.confirmTransaction(airdropSig, "confirmed");
18-
console.log("Payer:", payer.publicKey.toBase58());
19-
20-
const owner = Keypair.generate();
21-
const airdropSig2 = await rpc.requestAirdrop(owner.publicKey, 1e9);
22-
await rpc.confirmTransaction(airdropSig2, "confirmed");
23-
24-
// 2. Create SPL mint with token pool
25-
const mintAuthority = Keypair.generate();
26-
const mintKeypair = Keypair.generate();
27-
const { mint } = await createMint(
28-
rpc,
29-
payer,
30-
mintAuthority.publicKey,
31-
9,
32-
mintKeypair
33-
);
34-
console.log("Mint:", mint.toBase58());
35-
36-
// 3. Create SPL ATA and fund it with SPL tokens
37-
const splAta = await createAssociatedTokenAccount(
38-
rpc,
39-
payer,
40-
mint,
41-
owner.publicKey
42-
);
43-
console.log("SPL ATA:", splAta.toBase58());
44-
45-
// Mint compressed then decompress to get SPL tokens
46-
await mintTo(rpc, payer, mint, owner.publicKey, mintAuthority, bn(1000));
47-
await decompress(rpc, payer, mint, bn(1000), owner, splAta);
48-
console.log("Funded SPL ATA with 1000 tokens");
49-
50-
// 4. Compress SPL tokens to cold storage
51-
const recipient = Keypair.generate();
52-
53-
const signature = await compress(
54-
rpc,
55-
payer,
56-
mint,
57-
bn(500), // amount to compress
58-
owner, // owner of SPL account (signer)
59-
splAta, // source SPL token account
60-
recipient.publicKey // recipient of compressed tokens
61-
);
62-
63-
console.log("Compressed 500 tokens to cold storage");
64-
console.log("Recipient:", recipient.publicKey.toBase58());
65-
console.log("Transaction:", signature);
66-
}
67-
68-
main().catch((err) => {
69-
console.error("Error:", err);
70-
if (err.logs) console.error("Logs:", err.logs);
71-
process.exit(1);
72-
});
6+
import { homedir } from "os";
7+
import { readFileSync } from "fs";
8+
9+
const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
10+
const payer = Keypair.fromSecretKey(
11+
new Uint8Array(
12+
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
13+
)
14+
);
15+
16+
(async function () {
17+
const rpc = createRpc(RPC_URL);
18+
19+
// Setup: Get SPL tokens (needed to compress)
20+
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
21+
const splAta = await createAssociatedTokenAccount(rpc, payer, mint, payer.publicKey);
22+
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
23+
await decompress(rpc, payer, mint, bn(1000), payer, splAta);
24+
25+
// Compress SPL tokens to cold storage
26+
const recipient = Keypair.generate();
27+
const tx = await compress(rpc, payer, mint, bn(500), payer, splAta, recipient.publicKey);
28+
29+
console.log("Tx:", tx);
30+
})();

cookbook/actions/create-ata.ts

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,27 @@
11
import "dotenv/config";
22
import { Keypair } from "@solana/web3.js";
3+
import { createRpc } from "@lightprotocol/stateless.js";
34
import {
4-
createRpc,
5-
featureFlags,
6-
VERSION,
7-
} from "@lightprotocol/stateless.js";
8-
import {
9-
createMintInterface,
10-
createAtaInterface,
11-
getAssociatedTokenAddressInterface,
5+
createMintInterface,
6+
createAtaInterface,
127
} from "@lightprotocol/compressed-token";
8+
import { homedir } from "os";
9+
import { readFileSync } from "fs";
1310

14-
featureFlags.version = VERSION.V2;
15-
16-
async function main() {
17-
// 1. Setup RPC and fund payer
18-
const rpc = createRpc();
19-
const payer = Keypair.generate();
20-
const airdropSig = await rpc.requestAirdrop(payer.publicKey, 10e9);
21-
await rpc.confirmTransaction(airdropSig, "confirmed");
22-
console.log("Payer:", payer.publicKey.toBase58());
11+
const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
12+
const payer = Keypair.fromSecretKey(
13+
new Uint8Array(
14+
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
15+
)
16+
);
2317

24-
// 2. Create a light-mint
25-
const mintSigner = Keypair.generate();
26-
const { mint } = await createMintInterface(
27-
rpc,
28-
payer,
29-
payer, // mintAuthority
30-
null, // freezeAuthority
31-
9, // decimals
32-
mintSigner
33-
);
34-
console.log("Mint:", mint.toBase58());
18+
(async function () {
19+
const rpc = createRpc(RPC_URL);
3520

36-
// 3. Create associated token account for owner
37-
const owner = Keypair.generate();
38-
const txSignature = await createAtaInterface(rpc, payer, mint, owner.publicKey);
39-
console.log("ATA created for:", owner.publicKey.toBase58());
40-
console.log("Transaction:", txSignature);
21+
const { mint } = await createMintInterface(rpc, payer, payer, null, 9);
4122

42-
// 4. Derive the ATA address
43-
const ata = getAssociatedTokenAddressInterface(mint, owner.publicKey);
44-
console.log("ATA address:", ata.toBase58());
45-
}
23+
const owner = Keypair.generate();
24+
const ata = await createAtaInterface(rpc, payer, mint, owner.publicKey);
4625

47-
main().catch((err) => {
48-
console.error("Error:", err);
49-
if (err.logs) console.error("Logs:", err.logs);
50-
process.exit(1);
51-
});
26+
console.log("ATA:", ata.toBase58());
27+
})();

0 commit comments

Comments
 (0)