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
2 changes: 1 addition & 1 deletion examples/anchor/counter/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ where
.unwrap();

let account_meta = CompressedAccountMeta {
tree_info: packed_tree_accounts.packed_tree_infos[0],
address: compressed_account.address.unwrap(),
tree_info: packed_tree_accounts.packed_tree_infos[0],
output_state_tree_index: packed_tree_accounts.output_tree_index,
};

Expand Down
4 changes: 2 additions & 2 deletions js/compressed-token/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BN from 'bn.js';
import { Buffer } from 'buffer';
import {
ValidityProof,
PackedMerkleContext,
PackedMerkleContextLegacy,
CompressedCpiContext,
} from '@lightprotocol/stateless.js';
import { TokenPoolInfo } from './utils/get-token-pool-infos';
Expand Down Expand Up @@ -53,7 +53,7 @@ export type PackedTokenTransferOutputData = {
export type InputTokenDataWithContext = {
amount: BN;
delegateIndex: number | null;
merkleContext: PackedMerkleContext;
merkleContext: PackedMerkleContextLegacy;
rootIndex: number;
lamports: BN | null;
tlv: Buffer | null;
Expand Down
11 changes: 2 additions & 9 deletions js/compressed-token/tests/e2e/delegate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,8 @@ describe('delegate', () => {
mint,
})
).items;
const txId = await approve(
rpc,
payer,
mint,
totalAmount,
payer,
bob.publicKey,
);
console.log('txid approve ', txId);
await approve(rpc, payer, mint, totalAmount, payer, bob.publicKey);

await assertDelegate(
rpc,
mint,
Expand Down
4 changes: 2 additions & 2 deletions js/compressed-token/tests/e2e/layout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import BN from 'bn.js';
import {
bn,
InputTokenDataWithContext,
PackedMerkleContext,
PackedMerkleContextLegacy,
ValidityProof,
COMPRESSED_TOKEN_PROGRAM_ID,
defaultStaticAccountsStruct,
Expand Down Expand Up @@ -165,7 +165,7 @@ describe('layout', () => {
queuePubkeyIndex: 1,
leafIndex: 10,
proveByIndex: false,
} as PackedMerkleContext,
} as PackedMerkleContextLegacy,
rootIndex: 11,
lamports: null,
tlv: null,
Expand Down
8 changes: 1 addition & 7 deletions js/compressed-token/tests/e2e/mint-to.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ describe('mintTo', () => {
}, 80_000);

it('should mint to bob', async () => {
console.log('statetreeinfo', stateTreeInfo);
console.log('tokenpoolinfo', tokenPoolInfo);
console.log('all state tree infos', await rpc.getStateTreeInfos());

const amount = bn(1000);
const txId = await mintTo(
rpc,
Expand All @@ -117,7 +113,6 @@ describe('mintTo', () => {
stateTreeInfo,
tokenPoolInfo,
);
console.log('txId', txId);

await assertMintTo(rpc, mint, amount, bob.publicKey);

Expand Down Expand Up @@ -172,7 +167,7 @@ describe('mintTo', () => {
stateTreeInfo,
tokenPoolInfo,
);
console.log('txId 10 recipients', tx);

// Uneven amounts
await expect(
mintTo(
Expand Down Expand Up @@ -215,6 +210,5 @@ describe('mintTo', () => {
[lookupTableAccount],
);
const txId = await sendAndConfirmTx(rpc, tx);
console.log('txId 22 recipients', txId);
});
});
129 changes: 124 additions & 5 deletions js/stateless.js/src/programs/system/pack.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { AccountMeta, PublicKey } from '@solana/web3.js';
import {
CompressedAccount,
AccountProofInput,
CompressedAccountLegacy,
NewAddressProofInput,
OutputCompressedAccountWithPackedContext,
PackedCompressedAccountWithMerkleContext,
TreeInfo,
TreeType,
} from '../../state';
import { CompressedAccountWithMerkleContext } from '../../state/compressed-account';
import { toArray } from '../../utils/conversion';
import {
CompressedAccountWithMerkleContextLegacy,
PackedAddressTreeInfo,
PackedStateTreeInfo,
} from '../../state/compressed-account';
import { featureFlags } from '../../constants';

/**
Expand Down Expand Up @@ -67,6 +72,120 @@ export function toAccountMetas(remainingAccounts: PublicKey[]): AccountMeta[] {
);
}

export interface PackedStateTreeInfos {
packedTreeInfos: PackedStateTreeInfo[];
outputTreeIndex: number;
}

export interface PackedTreeInfos {
stateTrees?: PackedStateTreeInfos;
addressTrees: PackedAddressTreeInfo[];
}

const INVALID_TREE_INDEX = -1;
/**
* Packs TreeInfos. Replaces PublicKey with index pointer to remaining accounts.
*
* Only use for MUT, CLOSE, NEW_ADDRESSES. For INIT, pass
* {@link newAddressParamsPacked} and `outputStateTreeIndex` to your program
* instead.
*
*
* @param remainingAccounts Optional existing array of accounts
* to append to.
* @param accountProofInputs Account proof inputs.
* @param newAddressProofInputs New address proof inputs.
*
* @returns Remaining accounts, packed state and address tree infos, state tree
* output index and address tree infos.
*/
export function packTreeInfos(
remainingAccounts: PublicKey[],
accountProofInputs: AccountProofInput[],
newAddressProofInputs: NewAddressProofInput[],
): PackedTreeInfos {
const _remainingAccounts = remainingAccounts.slice();

const stateTreeInfos: PackedStateTreeInfo[] = [];
const addressTreeInfos: PackedAddressTreeInfo[] = [];
let outputTreeIndex: number = INVALID_TREE_INDEX;

// Early exit.
if (accountProofInputs.length === 0 && newAddressProofInputs.length === 0) {
return {
stateTrees: undefined,
addressTrees: addressTreeInfos,
};
}

// input
accountProofInputs.forEach((account, index) => {
const merkleTreePubkeyIndex = getIndexOrAdd(
_remainingAccounts,
account.treeInfo.tree,
);

const queuePubkeyIndex = getIndexOrAdd(
_remainingAccounts,
account.treeInfo.queue,
);

stateTreeInfos.push({
rootIndex: account.rootIndex,
merkleTreePubkeyIndex,
queuePubkeyIndex,
leafIndex: account.leafIndex,
proveByIndex: account.proveByIndex,
});
});

// output
if (stateTreeInfos.length > 0) {
// Use next tree if available, otherwise fall back to current tree.
// `nextTreeInfo` always takes precedence.
const activeTreeInfo =
accountProofInputs[0].treeInfo.nextTreeInfo ||
accountProofInputs[0].treeInfo;
let activeTreeOrQueue = activeTreeInfo.tree;

if (activeTreeInfo.treeType === TreeType.StateV2) {
if (featureFlags.isV2()) {
activeTreeOrQueue = activeTreeInfo.queue;
} else throw new Error('V2 trees are not supported yet');
}
outputTreeIndex = getIndexOrAdd(_remainingAccounts, activeTreeOrQueue);
}

// new addresses
newAddressProofInputs.forEach((account, index) => {
const addressMerkleTreePubkeyIndex = getIndexOrAdd(
_remainingAccounts,
account.treeInfo.tree,
);
const addressQueuePubkeyIndex = getIndexOrAdd(
_remainingAccounts,
account.treeInfo.queue,
);

addressTreeInfos.push({
rootIndex: account.rootIndex,
addressMerkleTreePubkeyIndex,
addressQueuePubkeyIndex,
});
});

return {
stateTrees:
stateTreeInfos.length > 0
? {
packedTreeInfos: stateTreeInfos,
outputTreeIndex,
}
: undefined,
addressTrees: addressTreeInfos,
};
}

/**
* Packs Compressed Accounts.
*
Expand All @@ -86,9 +205,9 @@ export function toAccountMetas(remainingAccounts: PublicKey[]): AccountMeta[] {
* to append to.
**/
export function packCompressedAccounts(
inputCompressedAccounts: CompressedAccountWithMerkleContext[],
inputCompressedAccounts: CompressedAccountWithMerkleContextLegacy[],
inputStateRootIndices: number[],
outputCompressedAccounts: CompressedAccount[],
outputCompressedAccounts: CompressedAccountLegacy[],
outputStateTreeInfo?: TreeInfo,
remainingAccounts: PublicKey[] = [],
): {
Expand Down
37 changes: 21 additions & 16 deletions js/stateless.js/src/programs/system/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
} from '@solana/web3.js';
import { Buffer } from 'buffer';
import {
CompressedAccount,
CompressedAccountWithMerkleContext,
ValidityProof,
InstructionDataInvoke,
TreeInfo,
bn,
createCompressedAccount,
createCompressedAccountLegacy,
CompressedAccountLegacy,
} from '../../state';
import {
packCompressedAccounts,
Expand Down Expand Up @@ -199,33 +199,33 @@ export class LightSystemProgram {
inputCompressedAccounts: CompressedAccountWithMerkleContext[],
toAddress: PublicKey,
lamports: number | BN,
): CompressedAccount[] {
): CompressedAccountLegacy[] {
lamports = bn(lamports);
const inputLamports = sumUpLamports(inputCompressedAccounts);
const changeLamports = inputLamports.sub(lamports);

validateSufficientBalance(changeLamports);

if (changeLamports.eq(bn(0))) {
return [createCompressedAccount(toAddress, lamports)];
return [createCompressedAccountLegacy(toAddress, lamports)];
}

validateSameOwner(inputCompressedAccounts);

const outputCompressedAccounts: CompressedAccount[] = [
createCompressedAccount(
const outputCompressedAccounts: CompressedAccountLegacy[] = [
createCompressedAccountLegacy(
inputCompressedAccounts[0].owner,
changeLamports,
),
createCompressedAccount(toAddress, lamports),
createCompressedAccountLegacy(toAddress, lamports),
];
return outputCompressedAccounts;
}

static createDecompressOutputState(
inputCompressedAccounts: CompressedAccountWithMerkleContext[],
lamports: number | BN,
): CompressedAccount[] {
): CompressedAccountLegacy[] {
lamports = bn(lamports);
const inputLamports = sumUpLamports(inputCompressedAccounts);
const changeLamports = inputLamports.sub(lamports);
Expand All @@ -239,8 +239,8 @@ export class LightSystemProgram {

validateSameOwner(inputCompressedAccounts);

const outputCompressedAccounts: CompressedAccount[] = [
createCompressedAccount(
const outputCompressedAccounts: CompressedAccountLegacy[] = [
createCompressedAccountLegacy(
inputCompressedAccounts[0].owner,
changeLamports,
),
Expand All @@ -256,7 +256,7 @@ export class LightSystemProgram {
owner: PublicKey,
lamports?: BN | number,
inputCompressedAccounts?: CompressedAccountWithMerkleContext[],
): CompressedAccount[] {
): CompressedAccountLegacy[] {
lamports = bn(lamports ?? 0);
const inputLamports = sumUpLamports(inputCompressedAccounts ?? []);
const changeLamports = inputLamports.sub(lamports);
Expand All @@ -265,17 +265,22 @@ export class LightSystemProgram {

if (changeLamports.eq(bn(0)) || !inputCompressedAccounts) {
return [
createCompressedAccount(owner, lamports, undefined, address),
createCompressedAccountLegacy(
owner,
lamports,
undefined,
address,
),
];
}

validateSameOwner(inputCompressedAccounts);
const outputCompressedAccounts: CompressedAccount[] = [
createCompressedAccount(
const outputCompressedAccounts: CompressedAccountLegacy[] = [
createCompressedAccountLegacy(
inputCompressedAccounts[0].owner,
changeLamports,
),
createCompressedAccount(owner, lamports, undefined, address),
createCompressedAccountLegacy(owner, lamports, undefined, address),
];
return outputCompressedAccounts;
}
Expand Down Expand Up @@ -422,7 +427,7 @@ export class LightSystemProgram {
/// Create output state
lamports = bn(lamports);

const outputCompressedAccount = createCompressedAccount(
const outputCompressedAccount = createCompressedAccountLegacy(
toAddress,
lamports,
);
Expand Down
Loading
Loading