Skip to content

Commit ac2cb79

Browse files
refactor
1 parent 44ec10a commit ac2cb79

26 files changed

Lines changed: 340 additions & 627 deletions

cookbook/actions/compress-batch.ts

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
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";
116
import { homedir } from "os";
127
import { readFileSync } from "fs";
@@ -18,42 +13,20 @@ const payer = Keypair.fromSecretKey(
1813
)
1914
);
2015

21-
async function main() {
16+
(async function () {
2217
const rpc = createRpc(RPC_URL);
2318

19+
// Setup: Get SPL tokens (needed to compress)
2420
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
25-
console.log("Mint:", mint.toBase58());
26-
27-
const splAta = await createAssociatedTokenAccount(
28-
rpc,
29-
payer,
30-
mint,
31-
payer.publicKey
32-
);
33-
34-
// Fund SPL ATA: mint compressed, then decompress
21+
const splAta = await createAssociatedTokenAccount(rpc, payer, mint, payer.publicKey);
3522
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(10000));
3623
await decompress(rpc, payer, mint, bn(10000), payer, splAta);
3724

3825
// Batch compress to multiple recipients
39-
const recipients = Array.from(
40-
{ length: 5 },
41-
() => Keypair.generate().publicKey
42-
);
26+
const recipients = Array.from({ length: 5 }, () => Keypair.generate().publicKey);
4327
const amounts = [bn(100), bn(200), bn(300), bn(400), bn(500)];
4428

45-
const signature = await compress(
46-
rpc,
47-
payer,
48-
mint,
49-
amounts,
50-
payer,
51-
splAta,
52-
recipients
53-
);
54-
55-
console.log("Batch compressed to 5 recipients");
56-
console.log("Tx:", signature);
57-
}
29+
const tx = await compress(rpc, payer, mint, amounts, payer, splAta, recipients);
5830

59-
main().catch(console.error);
31+
console.log("Tx:", tx);
32+
})();

cookbook/actions/compress.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
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";
116
import { homedir } from "os";
127
import { readFileSync } from "fs";
@@ -18,36 +13,18 @@ const payer = Keypair.fromSecretKey(
1813
)
1914
);
2015

21-
async function main() {
16+
(async function () {
2217
const rpc = createRpc(RPC_URL);
2318

19+
// Setup: Get SPL tokens (needed to compress)
2420
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
25-
console.log("Mint:", mint.toBase58());
26-
27-
const splAta = await createAssociatedTokenAccount(
28-
rpc,
29-
payer,
30-
mint,
31-
payer.publicKey
32-
);
33-
34-
// Fund SPL ATA: mint compressed, then decompress
21+
const splAta = await createAssociatedTokenAccount(rpc, payer, mint, payer.publicKey);
3522
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
3623
await decompress(rpc, payer, mint, bn(1000), payer, splAta);
3724

25+
// Compress SPL tokens to cold storage
3826
const recipient = Keypair.generate();
39-
const signature = await compress(
40-
rpc,
41-
payer,
42-
mint,
43-
bn(500),
44-
payer,
45-
splAta,
46-
recipient.publicKey
47-
);
48-
49-
console.log("Compressed 500 tokens to:", recipient.publicKey.toBase58());
50-
console.log("Tx:", signature);
51-
}
27+
const tx = await compress(rpc, payer, mint, bn(500), payer, splAta, recipient.publicKey);
5228

53-
main().catch(console.error);
29+
console.log("Tx:", tx);
30+
})();

cookbook/actions/create-ata.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ const payer = Keypair.fromSecretKey(
1515
)
1616
);
1717

18-
async function main() {
18+
(async function () {
1919
const rpc = createRpc(RPC_URL);
2020

2121
const { mint } = await createMintInterface(rpc, payer, payer, null, 9);
22-
console.log("Mint:", mint.toBase58());
2322

2423
const owner = Keypair.generate();
2524
const ata = await createAtaInterface(rpc, payer, mint, owner.publicKey);
26-
console.log("ATA:", ata.toBase58());
27-
}
2825

29-
main().catch(console.error);
26+
console.log("ATA:", ata.toBase58());
27+
})();

cookbook/actions/create-mint.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import "dotenv/config";
22
import { Keypair } from "@solana/web3.js";
33
import { createRpc } from "@lightprotocol/stateless.js";
4-
import {
5-
createMintInterface,
6-
createTokenMetadata,
7-
} from "@lightprotocol/compressed-token";
4+
import { createMintInterface, createTokenMetadata } from "@lightprotocol/compressed-token";
85
import { homedir } from "os";
96
import { readFileSync } from "fs";
107

@@ -15,27 +12,21 @@ const payer = Keypair.fromSecretKey(
1512
)
1613
);
1714

18-
async function main() {
15+
(async function () {
1916
const rpc = createRpc(RPC_URL);
2017

2118
const { mint, transactionSignature } = await createMintInterface(
2219
rpc,
2320
payer,
24-
payer, // mintAuthority
25-
null, // freezeAuthority
21+
payer,
22+
null,
2623
9,
27-
undefined, // mintSigner
28-
undefined, // confirmOptions
29-
undefined, // programId
30-
createTokenMetadata(
31-
"Example Token",
32-
"EXT",
33-
"https://example.com/metadata.json"
34-
)
24+
undefined,
25+
undefined,
26+
undefined,
27+
createTokenMetadata("Example Token", "EXT", "https://example.com/metadata.json")
3528
);
3629

3730
console.log("Mint:", mint.toBase58());
3831
console.log("Tx:", transactionSignature);
39-
}
40-
41-
main().catch(console.error);
32+
})();

cookbook/actions/decompress.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,21 @@ const payer = Keypair.fromSecretKey(
1717
)
1818
);
1919

20-
async function main() {
20+
(async function () {
2121
const rpc = createRpc(RPC_URL);
2222

23+
// Setup: Get compressed tokens (cold storage)
2324
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
24-
console.log("Mint:", mint.toBase58());
25-
2625
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
2726

27+
// Decompress to SPL ATA
2828
const splAta = await createAssociatedTokenAccount(
2929
rpc,
3030
payer,
3131
mint,
3232
payer.publicKey
3333
);
34+
const tx = await decompress(rpc, payer, mint, bn(500), payer, splAta);
3435

35-
const signature = await decompress(
36-
rpc,
37-
payer,
38-
mint,
39-
bn(500),
40-
payer,
41-
splAta
42-
);
43-
44-
console.log("Decompressed 500 tokens to SPL ATA");
45-
console.log("Tx:", signature);
46-
}
47-
48-
main().catch(console.error);
36+
console.log("Tx:", tx);
37+
})();

cookbook/actions/delegate-approve.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@ const payer = Keypair.fromSecretKey(
1212
)
1313
);
1414

15-
async function main() {
15+
(async function () {
1616
const rpc = createRpc(RPC_URL);
1717

18+
// Setup: Get compressed tokens
1819
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
19-
console.log("Mint:", mint.toBase58());
20-
2120
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
2221

22+
// Approve delegation
2323
const delegate = Keypair.generate();
24-
const signature = await approve(rpc, payer, mint, bn(500), payer, delegate.publicKey);
25-
26-
console.log("Approved delegation of 500 tokens to:", delegate.publicKey.toBase58());
27-
console.log("Tx:", signature);
28-
29-
const delegatedAccounts = await rpc.getCompressedTokenAccountsByDelegate(delegate.publicKey, { mint });
30-
console.log("Delegated accounts:", delegatedAccounts.items.length);
31-
}
32-
33-
main().catch(console.error);
24+
const tx = await approve(
25+
rpc,
26+
payer,
27+
mint,
28+
bn(500),
29+
payer,
30+
delegate.publicKey
31+
);
32+
33+
console.log("Tx:", tx);
34+
})();
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import "dotenv/config";
22
import { Keypair } from "@solana/web3.js";
33
import { createRpc, bn } from "@lightprotocol/stateless.js";
4-
import { createMint, mintTo, approve, revoke } from "@lightprotocol/compressed-token";
4+
import {
5+
createMint,
6+
mintTo,
7+
approve,
8+
revoke,
9+
} from "@lightprotocol/compressed-token";
510
import { homedir } from "os";
611
import { readFileSync } from "fs";
712

@@ -12,28 +17,22 @@ const payer = Keypair.fromSecretKey(
1217
)
1318
);
1419

15-
async function main() {
20+
(async function () {
1621
const rpc = createRpc(RPC_URL);
1722

23+
// Setup: Get compressed tokens
1824
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
19-
console.log("Mint:", mint.toBase58());
20-
2125
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
2226

27+
// Approve then revoke delegation
2328
const delegate = Keypair.generate();
2429
await approve(rpc, payer, mint, bn(500), payer, delegate.publicKey);
25-
console.log("Approved delegation to:", delegate.publicKey.toBase58());
26-
27-
const delegatedAccounts = await rpc.getCompressedTokenAccountsByDelegate(delegate.publicKey, { mint });
28-
console.log("Delegated accounts:", delegatedAccounts.items.length);
29-
30-
const signature = await revoke(rpc, payer, delegatedAccounts.items, payer);
31-
32-
console.log("Revoked delegation");
33-
console.log("Tx:", signature);
3430

35-
const afterRevoke = await rpc.getCompressedTokenAccountsByDelegate(delegate.publicKey, { mint });
36-
console.log("After revoke:", afterRevoke.items.length, "accounts");
37-
}
31+
const delegatedAccounts = await rpc.getCompressedTokenAccountsByDelegate(
32+
delegate.publicKey,
33+
{ mint }
34+
);
35+
const tx = await revoke(rpc, payer, delegatedAccounts.items, payer);
3836

39-
main().catch(console.error);
37+
console.log("Tx:", tx);
38+
})();

cookbook/actions/load-ata.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,16 @@ const payer = Keypair.fromSecretKey(
1717
)
1818
);
1919

20-
async function main() {
20+
(async function () {
2121
const rpc = createRpc(RPC_URL);
2222

23+
// Setup: Get compressed tokens (cold storage)
2324
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
24-
console.log("Mint:", mint.toBase58());
25-
2625
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
2726

27+
// Load compressed tokens to hot balance
2828
const ctokenAta = getAssociatedTokenAddressInterface(mint, payer.publicKey);
29+
const tx = await loadAta(rpc, ctokenAta, payer, mint, payer);
2930

30-
// Load compressed tokens (cold) to hot balance, creates ATA if needed
31-
const signature = await loadAta(rpc, ctokenAta, payer, mint, payer);
32-
33-
if (signature) {
34-
console.log("Loaded tokens to hot balance");
35-
console.log("Tx:", signature);
36-
} else {
37-
console.log("Nothing to load");
38-
}
39-
}
40-
41-
main().catch(console.error);
31+
console.log("Tx:", tx);
32+
})();
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import "dotenv/config";
22
import { Keypair } from "@solana/web3.js";
33
import { createRpc, bn } from "@lightprotocol/stateless.js";
4-
import { createMint, mintTo, mergeTokenAccounts } from "@lightprotocol/compressed-token";
4+
import {
5+
createMint,
6+
mintTo,
7+
mergeTokenAccounts,
8+
} from "@lightprotocol/compressed-token";
59
import { homedir } from "os";
610
import { readFileSync } from "fs";
711

@@ -12,26 +16,17 @@ const payer = Keypair.fromSecretKey(
1216
)
1317
);
1418

15-
async function main() {
19+
(async function () {
1620
const rpc = createRpc(RPC_URL);
1721

22+
// Setup: Create multiple compressed token accounts
1823
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
19-
console.log("Mint:", mint.toBase58());
20-
21-
// Mint multiple times to create multiple compressed accounts
2224
for (let i = 0; i < 5; i++) {
2325
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(100));
2426
}
2527

26-
const accountsBefore = await rpc.getCompressedTokenAccountsByOwner(payer.publicKey, { mint });
27-
console.log("Accounts before merge:", accountsBefore.items.length);
28-
29-
const signature = await mergeTokenAccounts(rpc, payer, mint, payer);
30-
31-
console.log("Tx:", signature);
32-
33-
const accountsAfter = await rpc.getCompressedTokenAccountsByOwner(payer.publicKey, { mint });
34-
console.log("Accounts after merge:", accountsAfter.items.length);
35-
}
28+
// Merge multiple accounts into one
29+
const tx = await mergeTokenAccounts(rpc, payer, mint, payer);
3630

37-
main().catch(console.error);
31+
console.log("Tx:", tx);
32+
})();

0 commit comments

Comments
 (0)