Skip to content

Commit

Permalink
Freeze return values
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher committed Dec 15, 2024
1 parent 5ad2dcd commit bd284a7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
68 changes: 68 additions & 0 deletions packages/compat/src/__tests__/instruction-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import '@solana/test-matchers/toBeFrozenObject';

import { address } from '@solana/addresses';
import { AccountRole, IInstruction } from '@solana/instructions';
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
Expand Down Expand Up @@ -37,6 +39,72 @@ describe('fromLegacyTransactionInstruction', () => {
});
});

it('freezes the accounts array', () => {
const programId = new Uint8Array([1, 2, 3, 4]);
const keys = [
{
isSigner: false,
isWritable: true,
pubkey: new PublicKey('7EqQdEULxWcraVx3mXKFjc84LhCkMGZCkRuDpvcMwJeK'),
},
];
const data = new Uint8Array([10, 20, 30]);

const instruction = new TransactionInstruction({
data: Buffer.from(data),
keys,
programId: new PublicKey(programId),
});

const converted = fromLegacyTransactionInstruction(instruction);

expect(converted.accounts).toBeFrozenObject();
});

it('freezes each account', () => {
const programId = new Uint8Array([1, 2, 3, 4]);
const keys = [
{
isSigner: false,
isWritable: true,
pubkey: new PublicKey('7EqQdEULxWcraVx3mXKFjc84LhCkMGZCkRuDpvcMwJeK'),
},
];
const data = new Uint8Array([10, 20, 30]);

const instruction = new TransactionInstruction({
data: Buffer.from(data),
keys,
programId: new PublicKey(programId),
});

const converted = fromLegacyTransactionInstruction(instruction);

expect(converted.accounts?.[0]).toBeFrozenObject();
});

it('freezes the instruction', () => {
const programId = new Uint8Array([1, 2, 3, 4]);
const keys = [
{
isSigner: false,
isWritable: true,
pubkey: new PublicKey('7EqQdEULxWcraVx3mXKFjc84LhCkMGZCkRuDpvcMwJeK'),
},
];
const data = new Uint8Array([10, 20, 30]);

const instruction = new TransactionInstruction({
data: Buffer.from(data),
keys,
programId: new PublicKey(programId),
});

const converted = fromLegacyTransactionInstruction(instruction);

expect(converted).toBeFrozenObject();
});

it('applies no acccounts given an instruction with no keys', () => {
const programId = new Uint8Array([5, 6, 7, 8]);
const data = new Uint8Array([40, 50, 60]);
Expand Down
16 changes: 9 additions & 7 deletions packages/compat/src/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import { fromLegacyPublicKey } from './address';

export function fromLegacyTransactionInstruction(legacyInstruction: TransactionInstruction): IInstruction {
const data = legacyInstruction.data?.byteLength > 0 ? Uint8Array.from(legacyInstruction.data) : undefined;
const accounts = legacyInstruction.keys.map(accountMeta => ({
address: fromLegacyPublicKey(accountMeta.pubkey),
role: determineRole(accountMeta.isSigner, accountMeta.isWritable),
}));
const accounts = legacyInstruction.keys.map(accountMeta =>
Object.freeze({
address: fromLegacyPublicKey(accountMeta.pubkey),
role: determineRole(accountMeta.isSigner, accountMeta.isWritable),
}),
);
const programAddress = fromLegacyPublicKey(legacyInstruction.programId);
return {
...(accounts.length ? { accounts } : null),
return Object.freeze({
...(accounts.length ? { accounts: Object.freeze(accounts) } : null),
...(data ? { data } : null),
programAddress,
};
});
}

function determineRole(isSigner: boolean, isWritable: boolean): AccountRole {
Expand Down

0 comments on commit bd284a7

Please sign in to comment.