Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/src/commands/compress-spl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CompressSplCommand extends Command {
const toPublicKey = new PublicKey(to);
const mintPublicKey = new PublicKey(mint);
const payer = defaultSolanaWalletKeypair();
const tokenProgramId = await CompressedTokenProgram.get_mint_program_id(
const tokenProgramId = await CompressedTokenProgram.getMintProgramId(
mintPublicKey,
rpc(),
);
Expand All @@ -75,7 +75,7 @@ class CompressSplCommand extends Command {
toPublicKey,
undefined,
undefined,
tokenProgramId,
undefined,
);

loader.stop(false);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/decompress-spl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DecompressSplCommand extends Command {
const toPublicKey = new PublicKey(to);
const mintPublicKey = new PublicKey(mint);
const payer = defaultSolanaWalletKeypair();
const tokenProgramId = await CompressedTokenProgram.get_mint_program_id(
const tokenProgramId = await CompressedTokenProgram.getMintProgramId(
mintPublicKey,
rpc(),
);
Expand All @@ -77,7 +77,7 @@ class DecompressSplCommand extends Command {
recipientAta.address,
undefined,
undefined,
tokenProgramId,
undefined,
);

loader.stop(false);
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
kebabCase,
snakeCase,
} from "case-anything";
import { execSync } from "child_process";

export default class InitCommand extends Command {
static description = "Initialize a compressed account project.";

Expand Down
11 changes: 7 additions & 4 deletions examples/browser/nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import {
bn,
buildTx,
confirmTx,
defaultTestStateTreeAccounts,
selectMinCompressedSolAccountsForTransfer,
createRpc,
selectStateTreeInfo,
} from '@lightprotocol/stateless.js';

// Default styles that can be overridden by your app
Expand All @@ -36,7 +36,10 @@ const SendButton: FC = () => {
const { publicKey, sendTransaction } = useWallet();

const onClick = useCallback(async () => {
const connection = await createRpc();
const connection = createRpc();
const stateTreeInfo = selectStateTreeInfo(
await connection.getCachedActiveStateTreeInfos(),
);

if (!publicKey) throw new WalletNotConnectedError();

Expand All @@ -51,7 +54,7 @@ const SendButton: FC = () => {
payer: publicKey,
toAddress: publicKey,
lamports: 1e8,
outputStateTree: defaultTestStateTreeAccounts().merkleTree,
outputStateTreeInfo: stateTreeInfo,
});
const compressInstructions = [
ComputeBudgetProgram.setComputeUnitLimit({ units: 1_000_000 }),
Expand Down Expand Up @@ -109,7 +112,7 @@ const SendButton: FC = () => {
toAddress: recipient,
lamports: 1e7,
inputCompressedAccounts: selectedAccounts,
outputStateTrees: [defaultTestStateTreeAccounts().merkleTree],
outputStateTreeInfo: stateTreeInfo,
recentValidityProof: compressedProof,
recentInputStateRootIndices: rootIndices,
});
Expand Down
4 changes: 2 additions & 2 deletions js/compressed-token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
"test:e2e:select-accounts": "vitest run tests/e2e/select-accounts.test.ts --reporter=verbose",
"test:e2e:create-token-pool": "pnpm test-validator && vitest run tests/e2e/create-token-pool.test.ts",
"test:e2e:mint-to": "pnpm test-validator && vitest run tests/e2e/mint-to.test.ts --reporter=verbose",
"test:e2e:approve-and-mint-to": "pnpm test-validator && vitest run tests/e2e/approve-and-mint-to.test.ts --reporter=verbose",
"test:e2e:approve-and-mint-to": "pnpm test-validator && vitest run tests/e2e/approve-and-mint-to.test.ts --reporter=verbose --bail=1",
"test:e2e:merge-token-accounts": "pnpm test-validator && vitest run tests/e2e/merge-token-accounts.test.ts --reporter=verbose",
"test:e2e:transfer": "pnpm test-validator && vitest run tests/e2e/transfer.test.ts --reporter=verbose",
"test:e2e:transfer": "pnpm test-validator && vitest run tests/e2e/transfer.test.ts --reporter=verbose --bail=1",
"test:e2e:compress": "pnpm test-validator && vitest run tests/e2e/compress.test.ts --reporter=verbose",
"test:e2e:compress-spl-token-account": "pnpm test-validator && vitest run tests/e2e/compress-spl-token-account.test.ts --reporter=verbose",
"test:e2e:decompress": "pnpm test-validator && vitest run tests/e2e/decompress.test.ts --reporter=verbose",
Expand Down
66 changes: 33 additions & 33 deletions js/compressed-token/src/actions/approve-and-mint-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,51 @@ import {
buildAndSignTx,
Rpc,
dedupeSigner,
pickRandomTreeAndQueue,
StateTreeInfo,
selectStateTreeInfo,
} from '@lightprotocol/stateless.js';
import { CompressedTokenProgram } from '../program';
import { getOrCreateAssociatedTokenAccount } from '@solana/spl-token';

import {
getTokenPoolInfos,
selectTokenPoolInfo,
selectTokenPoolInfosForDecompression,
TokenPoolInfo,
} from '../utils/get-token-pool-infos';

/**
* Mint compressed tokens to a solana address from an external mint authority
*
* @param rpc Rpc to use
* @param payer Payer of the transaction fees
* @param mint Mint for the account
* @param destination Address of the account to mint to
* @param authority Minting authority
* @param amount Amount to mint
* @param merkleTree State tree account that the compressed tokens should be
* part of. Defaults to random public state tree account.
* @param confirmOptions Options for confirming the transaction
* @param rpc Rpc to use
* @param payer Payer of the transaction fees
* @param mint Mint for the account
* @param toPubkey Address of the account to mint to
* @param authority Minting authority
* @param amount Amount to mint
* @param outputStateTreeInfo State tree info
* @param tokenPoolInfo Token pool info
* @param confirmOptions Options for confirming the transaction
*
* @return Signature of the confirmed transaction
*/
export async function approveAndMintTo(
rpc: Rpc,
payer: Signer,
mint: PublicKey,
destination: PublicKey,
toPubkey: PublicKey,
authority: Signer,
amount: number | BN,
merkleTree?: PublicKey,
outputStateTreeInfo?: StateTreeInfo,
tokenPoolInfo?: TokenPoolInfo,
confirmOptions?: ConfirmOptions,
tokenProgramId?: PublicKey,
): Promise<TransactionSignature> {
tokenProgramId = tokenProgramId
? tokenProgramId
: await CompressedTokenProgram.get_mint_program_id(mint, rpc);
outputStateTreeInfo =
outputStateTreeInfo ??
selectStateTreeInfo(await rpc.getCachedActiveStateTreeInfos());
tokenPoolInfo =
tokenPoolInfo ??
selectTokenPoolInfo(await getTokenPoolInfos(rpc, mint));

const authorityTokenAccount = await getOrCreateAssociatedTokenAccount(
rpc,
Expand All @@ -54,40 +65,29 @@ export async function approveAndMintTo(
undefined,
undefined,
confirmOptions,
tokenProgramId,
tokenPoolInfo.tokenProgram,
);

if (!merkleTree) {
const stateTreeInfo = await rpc.getCachedActiveStateTreeInfo();
const { tree } = pickRandomTreeAndQueue(stateTreeInfo);
merkleTree = tree;
}

const ixs = await CompressedTokenProgram.approveAndMintTo({
feePayer: payer.publicKey,
mint,
authority: authority.publicKey,
authorityTokenAccount: authorityTokenAccount.address,
amount,
toPubkey: destination,
merkleTree,
tokenProgramId,
toPubkey,
outputStateTreeInfo,
tokenPoolInfo,
});

const { blockhash } = await rpc.getLatestBlockhash();
const additionalSigners = dedupeSigner(payer, [authority]);

const tx = buildAndSignTx(
[
ComputeBudgetProgram.setComputeUnitLimit({ units: 1_000_000 }),
...ixs,
],
[ComputeBudgetProgram.setComputeUnitLimit({ units: 600_000 }), ...ixs],
payer,
blockhash,
additionalSigners,
);

const txId = await sendAndConfirmTx(rpc, tx, confirmOptions);

return txId;
return await sendAndConfirmTx(rpc, tx, confirmOptions);
}
46 changes: 28 additions & 18 deletions js/compressed-token/src/actions/compress-spl-token-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import {
buildAndSignTx,
Rpc,
dedupeSigner,
StateTreeInfo,
selectStateTreeInfo,
} from '@lightprotocol/stateless.js';

import BN from 'bn.js';

import {
getTokenPoolInfos,
selectTokenPoolInfo,
TokenPoolInfo,
} from '../utils/get-token-pool-infos';
import { CompressedTokenProgram } from '../program';

/**
Expand All @@ -23,10 +30,14 @@ import { CompressedTokenProgram } from '../program';
* @param payer Payer of the transaction fees
* @param mint Mint of the token to compress
* @param owner Owner of the token account
* @param tokenAccount Token account to compress
* @param outputStateTree State tree to insert the compressed token account into
* @param remainingAmount Optional: amount to leave in token account. Default: 0
* @param confirmOptions Options for confirming the transaction
* @param tokenAccount Token account to compress
* @param remainingAmount Optional: amount to leave in token account.
* Default: 0
* @param outputStateTreeInfo State tree to insert the compressed token
* account into
* @param tokenPoolInfo Token pool info
* @param confirmOptions Options for confirming the transaction

*
* @return Signature of the confirmed transaction
*/
Expand All @@ -36,27 +47,31 @@ export async function compressSplTokenAccount(
mint: PublicKey,
owner: Signer,
tokenAccount: PublicKey,
outputStateTree: PublicKey,
remainingAmount?: BN,
outputStateTreeInfo?: StateTreeInfo,
tokenPoolInfo?: TokenPoolInfo,
confirmOptions?: ConfirmOptions,
tokenProgramId?: PublicKey,
): Promise<TransactionSignature> {
tokenProgramId = tokenProgramId
? tokenProgramId
: await CompressedTokenProgram.get_mint_program_id(mint, rpc);
outputStateTreeInfo =
outputStateTreeInfo ??
selectStateTreeInfo(await rpc.getCachedActiveStateTreeInfos());
tokenPoolInfo =
tokenPoolInfo ??
selectTokenPoolInfo(await getTokenPoolInfos(rpc, mint));

const compressIx = await CompressedTokenProgram.compressSplTokenAccount({
feePayer: payer.publicKey,
authority: owner.publicKey,
tokenAccount,
mint,
remainingAmount,
outputStateTree,
tokenProgramId,
outputStateTreeInfo,
tokenPoolInfo,
});

const blockhashCtx = await rpc.getLatestBlockhash();
const additionalSigners = dedupeSigner(payer, [owner]);

const signedTx = buildAndSignTx(
[
ComputeBudgetProgram.setComputeUnitLimit({
Expand All @@ -68,11 +83,6 @@ export async function compressSplTokenAccount(
blockhashCtx.blockhash,
additionalSigners,
);
const txId = await sendAndConfirmTx(
rpc,
signedTx,
confirmOptions,
blockhashCtx,
);
return txId;

return await sendAndConfirmTx(rpc, signedTx, confirmOptions, blockhashCtx);
}
45 changes: 22 additions & 23 deletions js/compressed-token/src/actions/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ import {
Rpc,
dedupeSigner,
pickRandomTreeAndQueue,
StateTreeInfo,
selectStateTreeInfo,
} from '@lightprotocol/stateless.js';

import BN from 'bn.js';

import { CompressedTokenProgram } from '../program';
import {
getTokenPoolInfos,
selectTokenPoolInfo,
TokenPoolInfo,
} from '../utils/get-token-pool-infos';

/**
* Compress SPL tokens
Expand All @@ -27,12 +34,12 @@ import { CompressedTokenProgram } from '../program';
* @param owner Owner of the compressed tokens.
* @param sourceTokenAccount Source (associated) token account
* @param toAddress Destination address of the recipient
* @param merkleTree State tree account that the compressed tokens
* @param outputStateTreeInfo State tree account that the compressed tokens
* should be inserted into. Defaults to a default
* state tree account.
* @param tokenPoolInfo Token pool info
* @param confirmOptions Options for confirming the transaction
*
*
* @return Signature of the confirmed transaction
*/
export async function compress(
Expand All @@ -43,19 +50,16 @@ export async function compress(
owner: Signer,
sourceTokenAccount: PublicKey,
toAddress: PublicKey | Array<PublicKey>,
merkleTree?: PublicKey,
outputStateTreeInfo?: StateTreeInfo,
tokenPoolInfo?: TokenPoolInfo,
confirmOptions?: ConfirmOptions,
tokenProgramId?: PublicKey,
): Promise<TransactionSignature> {
tokenProgramId = tokenProgramId
? tokenProgramId
: await CompressedTokenProgram.get_mint_program_id(mint, rpc);

if (!merkleTree) {
const stateTreeInfo = await rpc.getCachedActiveStateTreeInfo();
const { tree } = pickRandomTreeAndQueue(stateTreeInfo);
merkleTree = tree;
}
outputStateTreeInfo =
outputStateTreeInfo ??
selectStateTreeInfo(await rpc.getCachedActiveStateTreeInfos());
tokenPoolInfo =
tokenPoolInfo ??
selectTokenPoolInfo(await getTokenPoolInfos(rpc, mint));

const compressIx = await CompressedTokenProgram.compress({
payer: payer.publicKey,
Expand All @@ -64,28 +68,23 @@ export async function compress(
toAddress,
amount,
mint,
outputStateTree: merkleTree,
tokenProgramId,
outputStateTreeInfo,
tokenPoolInfo,
});

const blockhashCtx = await rpc.getLatestBlockhash();
const additionalSigners = dedupeSigner(payer, [owner]);
const signedTx = buildAndSignTx(
[
ComputeBudgetProgram.setComputeUnitLimit({
units: 1_000_000,
units: 600_000,
}),
compressIx,
],
payer,
blockhashCtx.blockhash,
additionalSigners,
);
const txId = await sendAndConfirmTx(
rpc,
signedTx,
confirmOptions,
blockhashCtx,
);
return txId;

return await sendAndConfirmTx(rpc, signedTx, confirmOptions, blockhashCtx);
}
Loading