Skip to content

Commit 1ea7ea5

Browse files
Multi trees (#15)
* pick pseudorandom * batch transfer * for mainnet * 0.19.0 * add airdrop.ts
1 parent b9c7b24 commit 1ea7ea5

20 files changed

+757
-115
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
RPC_ENDPOINT="https://devnet.helius-rpc.com?api-key=<YOUR_API_KEY>"
33
PAYER_KEYPAIR="bs58encoded string of secret key"
44
MINT_ADDRESS="D3tw8PvgsJqqFBUKcJxiLAe78wbvu3jzospbtbTt9u18"
5+
AUTHORITY_KEYPAIR="bs58encoded string of secret key"
6+
HELIUS_API_KEY="your helius api key"

package-lock.json

Lines changed: 78 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"token": "ts-node src/scripts/token.ts",
1010
"connection": "ts-node src/scripts/connection.ts",
1111
"lookup-table": "ts-node src/scripts/lookup-table.ts",
12+
"state-lut": "ts-node src/internal/state-lut.ts",
1213
"batch-compress": "ts-node src/scripts/batch-compress.ts",
1314
"compress-with-delegate": "ts-node src/scripts/compress-with-delegate.ts",
1415
"mint-spl": "ts-node src/scripts/mint-spl.ts",
@@ -17,6 +18,7 @@
1718
"pulse": "ts-node src/internal/transfer-pulse.ts",
1819
"create-account-pulse": "ts-node src/internal/create-account-pulse.ts",
1920
"create-account-with-lamports-pulse": "ts-node src/internal/create-account-with-lamports-pulse.ts",
21+
"airdrop": "ts-node src/scripts/airdrop/airdrop.ts",
2022
"test": "npm run lamports && npm run token && npm run connection && npm run batch-compress && npm run mint-spl && npm run mint-spl-22 && npm run mint-spl-22-manual"
2123
},
2224
"repository": {
@@ -31,15 +33,19 @@
3133
},
3234
"homepage": "https://github.com/Lightprotocol/example-nodejs-client#readme",
3335
"dependencies": {
34-
"@lightprotocol/compressed-token": "0.18.0",
35-
"@lightprotocol/stateless.js": "0.18.0",
36+
"@lightprotocol/compressed-token": "0.20.3",
37+
"@lightprotocol/stateless.js": "0.20.3",
3638
"@solana/spl-token": "0.3.9",
3739
"@solana/spl-token-metadata": "^0.1.6",
3840
"@solana/web3.js": "^1.98.0",
3941
"@types/node": "^20.12.7",
42+
"bn.js": "^5.2.1",
4043
"bs58": "^6.0.0",
4144
"dotenv": "^16.4.5",
4245
"ts-node": "^10.9.2",
4346
"typescript": "^5.4.5"
47+
},
48+
"devDependencies": {
49+
"@types/bn.js": "^5.1.6"
4450
}
4551
}

src/constants.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ export const RPC_ENDPOINT = process.env.RPC_ENDPOINT;
77
export const PAYER_KEYPAIR = Keypair.fromSecretKey(
88
bs58.decode(process.env.PAYER_KEYPAIR!)
99
);
10+
11+
export const AUTHORITY_KEYPAIR = process.env.LUT_AUTHORITY_KEYPAIR
12+
? Keypair.fromSecretKey(
13+
Uint8Array.from(JSON.parse(process.env.LUT_AUTHORITY_KEYPAIR))
14+
)
15+
: undefined;
16+
17+
export const LUT_MAINNET_AUTHORITY_KEYPAIR = process.env.LUT_AUTHORITY_KEYPAIR
18+
? Keypair.fromSecretKey(
19+
Uint8Array.from(JSON.parse(process.env.LUT_AUTHORITY_KEYPAIR))
20+
)
21+
: undefined;
22+
23+
export const LUT_DEVNET_AUTHORITY_KEYPAIR = process.env.LUT_DEVNET_AUTH_KEYPAIR
24+
? Keypair.fromSecretKey(bs58.decode(process.env.LUT_DEVNET_AUTH_KEYPAIR))
25+
: undefined;
26+
1027
export const MINT_ADDRESS = new PublicKey(process.env.MINT_ADDRESS!);
1128
if (!RPC_ENDPOINT) throw new Error("Please set RPC_ENDPOINT in .env");
1229
if (!PAYER_KEYPAIR)

src/internal/create-account-pulse.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,37 @@ import {
66
} from "@lightprotocol/stateless.js";
77
import { PAYER_KEYPAIR, RPC_ENDPOINT } from "../constants";
88
import { randomBytes } from "crypto";
9+
import { PublicKey } from "@solana/web3.js";
910

1011
const fromKeypair = PAYER_KEYPAIR;
1112

1213
const connection: Rpc = createRpc(RPC_ENDPOINT, RPC_ENDPOINT);
1314

15+
const trees = [
16+
"smt2rJAFdyJJupwMKAqTNAJwvjhmiZ4JYGZmbVRw1Ho",
17+
"smt3AFtReRGVcrP11D6bSLEaKdUmrGfaTNowMVccJeu",
18+
"smt4vjXvdjDFzvRMUxwTWnSy4c7cKkMaHuPrGsdDH7V",
19+
"smt5uPaQT9n6b1qAkgyonmzRxtuazA53Rddwntqistc",
20+
"smt6ukQDSPPYHSshQovmiRUjG9jGFq2hW9vgrDFk5Yz",
21+
"smt7onMFkvi3RbyhQCMajudYQkB1afAFt9CDXBQTLz6",
22+
"smt8TYxNy8SuhAdKJ8CeLtDkr2w6dgDmdz5ruiDw9Y9",
23+
"smt9ReAYRF5eFjTd5gBJMn5aKwNRcmp3ub2CQr2vW7j",
24+
"smtAvYA5UbTRyKAkAj5kHs1CmrA42t6WkVLi4c6mA1f",
25+
];
1426
(async () => {
1527
try {
1628
while (true) {
29+
const pseudoRandomTree = trees[Math.floor(Math.random() * trees.length)];
1730
// Create account with random address
1831
const randomSeed = new Uint8Array(randomBytes(32));
1932
const txId = await createAccount(
2033
connection,
2134
fromKeypair,
2235
[randomSeed],
23-
LightSystemProgram.programId
36+
LightSystemProgram.programId,
37+
undefined,
38+
undefined,
39+
new PublicKey(pseudoRandomTree)
2440
);
2541
console.log(
2642
`Compressed Account Creation Success. Transaction Signature:`,

src/internal/create-account-with-lamports-pulse.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
import {
2+
bn,
3+
compress,
24
createAccountWithLamports,
35
createRpc,
46
LightSystemProgram,
7+
pickRandomTreeAndQueue,
58
Rpc,
69
} from "@lightprotocol/stateless.js";
710
import { PAYER_KEYPAIR, RPC_ENDPOINT } from "../constants";
811
import { randomBytes } from "crypto";
912

1013
const fromKeypair = PAYER_KEYPAIR;
11-
const connection: Rpc = createRpc(RPC_ENDPOINT, RPC_ENDPOINT);
14+
const connection: Rpc = createRpc(RPC_ENDPOINT);
1215

1316
(async () => {
1417
try {
1518
while (true) {
19+
const activeStateTrees = await connection.getCachedActiveStateTreeInfo();
20+
21+
const { tree, queue } = pickRandomTreeAndQueue(activeStateTrees);
22+
console.log("Picked output state tree:", tree.toBase58());
23+
const compressedTxId = await compress(
24+
connection,
25+
fromKeypair,
26+
bn(10),
27+
fromKeypair.publicKey,
28+
tree
29+
);
30+
console.log("Compressed TxId", compressedTxId);
1631
// Creat account with random address
1732
const txId = await createAccountWithLamports(
1833
connection,
1934
fromKeypair,
2035
[randomBytes(32)],
2136
10,
22-
LightSystemProgram.programId
37+
LightSystemProgram.programId,
38+
undefined,
39+
undefined,
40+
tree
2341
);
2442
console.log(
2543
`Compressed Account Creation Success. Transaction Signature:`,

src/internal/extend-lut.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import {
2+
Rpc,
3+
buildAndSignTx,
4+
createRpc,
5+
dedupeSigner,
6+
getLightStateTreeInfo,
7+
sendAndConfirmTx,
8+
} from "@lightprotocol/stateless.js";
9+
import {
10+
LUT_MAINNET_AUTHORITY_KEYPAIR,
11+
LUT_DEVNET_AUTHORITY_KEYPAIR,
12+
PAYER_KEYPAIR,
13+
RPC_ENDPOINT,
14+
} from "../constants";
15+
import { AddressLookupTableProgram, Keypair, PublicKey } from "@solana/web3.js";
16+
17+
const payer = PAYER_KEYPAIR;
18+
const authority = LUT_DEVNET_AUTHORITY_KEYPAIR;
19+
const connection: Rpc = createRpc(RPC_ENDPOINT, RPC_ENDPOINT, RPC_ENDPOINT);
20+
21+
(async () => {
22+
// const address = new PublicKey("9NYFyEqPkyXUhkerbGHXUXkvb4qpzeEdHuGpgbgpH1NJ");
23+
const address = new PublicKey("qAJZMgnQJ8G6vA3WRcjD9Jan1wtKkaCFWLWskxJrR5V");
24+
25+
// const stateTreeLookupTableMainnet = new PublicKey(
26+
// "7i86eQs3GSqHjN47WdWLTCGMW6gde1q96G2EVnUyK2st"
27+
// );
28+
// const nullifiedStateTreeLookupTableMainnet = new PublicKey(
29+
// "H9QD4u1fG7KmkAzn2tDXhheushxFe1EcrjGGyEFXeMqT"
30+
// );
31+
32+
const stateTreeLookupTableDevnet = new PublicKey(
33+
"8n8rH2bFRVA6cSGNDpgqcKHCndbFCT1bXxAQG89ejVsh"
34+
);
35+
const nullifiedStateTreeLookupTableDevnet = new PublicKey(
36+
"5dhaJLBjnVBQFErr8oiCJmcVsx3Zj6xDekGB2zULPsnP"
37+
);
38+
39+
const info = await getLightStateTreeInfo({
40+
connection,
41+
stateTreeLookupTableAddress: stateTreeLookupTableDevnet,
42+
nullifyTableAddress: nullifiedStateTreeLookupTableDevnet,
43+
});
44+
45+
await extend(
46+
payer,
47+
authority,
48+
address,
49+
info.flatMap((i) => [i.tree])
50+
);
51+
})();
52+
53+
async function extend(
54+
payer: Keypair,
55+
authority: Keypair,
56+
lookupTableAddress: PublicKey,
57+
addresses: PublicKey[]
58+
) {
59+
const extendInstruction = AddressLookupTableProgram.extendLookupTable({
60+
payer: payer.publicKey,
61+
authority: authority.publicKey,
62+
lookupTable: lookupTableAddress,
63+
addresses,
64+
});
65+
const bhash = await connection.getLatestBlockhash();
66+
const tx = await buildAndSignTx(
67+
[extendInstruction],
68+
payer,
69+
bhash.blockhash,
70+
dedupeSigner(payer, [authority])
71+
);
72+
await sendAndConfirmTx(connection, tx);
73+
74+
console.log("extended lookup table:", lookupTableAddress.toBase58());
75+
}

0 commit comments

Comments
 (0)