Skip to content

Commit

Permalink
remove anchor dep in ctoken
Browse files Browse the repository at this point in the history
  • Loading branch information
Swenschaeferjohann authored and Swenschaeferjohann committed Dec 27, 2024
1 parent df76dfe commit 4d687ab
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 211 deletions.
1 change: 0 additions & 1 deletion js/compressed-token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"@solana/web3.js": ">=1.95.5"
},
"dependencies": {
"@coral-xyz/anchor": "0.29.0",
"@coral-xyz/borsh": "^0.29.0",
"@solana/spl-token": "0.4.8",
"bn.js": "^5.2.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
getIndexOrAdd,
bn,
padOutputStateMerkleTrees,
TokenTransferOutputData,
} from '@lightprotocol/stateless.js';
import { PublicKey, AccountMeta } from '@solana/web3.js';
import { PackedTokenTransferOutputData } from '../types';
import {
PackedTokenTransferOutputData,
TokenTransferOutputData,
} from '../types';

export type PackCompressedTokenAccountsParams = {
/** Input state to be consumed */
Expand Down
61 changes: 47 additions & 14 deletions js/compressed-token/src/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import {
} from '@coral-xyz/borsh';
import { Buffer } from 'buffer';

import { CompressedTokenInstructionDataTransfer } from '@lightprotocol/stateless.js';
import { AccountMeta, PublicKey } from '@solana/web3.js';
import { CompressedTokenProgram } from './program';
import BN from 'bn.js';
import {
CompressedCpiContext,
CompressedTokenInstructionDataTransfer,
} from './types';

export const CREATE_TOKEN_POOL_DISCRIMINATOR = Buffer.from([
23, 169, 27, 122, 147, 169, 209, 152,
Expand All @@ -27,13 +30,16 @@ const CompressedProofLayout = struct([
array(u8(), 32, 'c'),
]);

const TokenTransferOutputDataLayout = struct([
const PackedTokenTransferOutputDataLayout = struct([
publicKey('owner'),
u64('amount'),
option(u64(), 'lamports'),
u8('merkleTreeIndex'),
option(vecU8(), 'tlv'),
]);

const QueueIndexLayout = struct([u8('queueId'), u16('index')]);

const InputTokenDataWithContextLayout = struct([
u64('amount'),
option(u8(), 'delegateIndex'),
Expand All @@ -42,14 +48,15 @@ const InputTokenDataWithContextLayout = struct([
u8('merkleTreePubkeyIndex'),
u8('nullifierQueuePubkeyIndex'),
u32('leafIndex'),
option(struct([u8('queueId'), u16('index')]), 'queueIndex'),
option(QueueIndexLayout, 'QueueIndex'),
],
'merkleContext',
),
u16('rootIndex'),
option(u64(), 'lamports'),
option(vecU8(), 'tlv'),
]);

export const DelegatedTransferLayout = struct([
publicKey('owner'),
option(u8(), 'delegateChangeAccountIndex'),
Expand All @@ -65,11 +72,8 @@ export const CompressedTokenInstructionDataTransferLayout = struct([
option(CompressedProofLayout, 'proof'),
publicKey('mint'),
option(DelegatedTransferLayout, 'delegatedTransfer'),
vec(
InputTokenDataWithContextLayout,
'inputCompressedAccountsWithMerkleContext',
),
vec(TokenTransferOutputDataLayout, 'outputCompressedAccounts'),
vec(InputTokenDataWithContextLayout, 'inputTokenDataWithContext'),
vec(PackedTokenTransferOutputDataLayout, 'outputCompressedAccounts'),
bool('isCompress'),
option(u64(), 'compressOrDecompressAmount'),
option(CpiContextLayout, 'cpiContext'),
Expand All @@ -82,9 +86,18 @@ export const mintToLayout = struct([
option(u64(), 'lamports'),
]);

export const compressSplTokenAccountInstructionDataLayout = struct([
publicKey('owner'),
option(u64(), 'remainingAmount'),
option(CpiContextLayout, 'cpiContext'),
]);

const MINT_TO_DISCRIMINATOR = Buffer.from([
241, 34, 48, 186, 37, 179, 123, 192,
]);
export const TRANSFER_DISCRIMINATOR = Buffer.from([
163, 52, 200, 231, 140, 3, 69, 186,
]);
export function encodeMintToInstructionData(
recipients: PublicKey[],
amounts: BN[],
Expand All @@ -102,17 +115,40 @@ export function encodeMintToInstructionData(
return Buffer.concat([MINT_TO_DISCRIMINATOR, buffer.slice(0, len)]);
}

export const COMPRESS_SPL_TOKEN_ACCOUNT_DISCRIMINATOR = Buffer.from([
112, 230, 105, 101, 145, 202, 157, 97,
]);

export function encodeCompressSplTokenAccountInstructionData(
owner: PublicKey,
remainingAmount: BN | null,
cpiContext: CompressedCpiContext | null,
): Buffer {
const buffer = Buffer.alloc(1000);
const len = compressSplTokenAccountInstructionDataLayout.encode(
{
owner,
remainingAmount,
cpiContext,
},
buffer,
);
return Buffer.concat([
COMPRESS_SPL_TOKEN_ACCOUNT_DISCRIMINATOR,
buffer.slice(0, len),
]);
}

export function encodeCompressedTokenInstructionDataTransfer(
data: CompressedTokenInstructionDataTransfer,
): Buffer {
const buffer = Buffer.alloc(1000);
console.log('DATA raw:', data);

const len = CompressedTokenInstructionDataTransferLayout.encode(
data,
buffer,
);
console.log('DATA len:', len);
// console.log('DATA buffer:', Array.from(buffer));

const lengthBuffer = Buffer.alloc(4);
lengthBuffer.writeUInt32LE(len, 0);

Expand All @@ -122,9 +158,6 @@ export function encodeCompressedTokenInstructionDataTransfer(
buffer.slice(0, len),
]);
}
export const TRANSFER_DISCRIMINATOR = Buffer.from([
163, 52, 200, 231, 140, 3, 69, 186,
]);

export type createTokenPoolAccountsLayoutParams = {
feePayer: PublicKey;
Expand Down
Loading

0 comments on commit 4d687ab

Please sign in to comment.