Skip to content
Open
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
1 change: 0 additions & 1 deletion basics/account-data/anchor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"@solana/web3.js": "^1.98.4"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^5.2.3",
"@types/mocha": "^10.0.10",
"chai": "^6.2.2",
Expand Down
10 changes: 0 additions & 10 deletions basics/account-data/anchor/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions basics/account-data/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
},
"dependencies": {
"@solana/kit": "^7.0.0",
"@solana-program/system": "^0.13.0",
"borsh": "^2.0.0"
"@solana-program/system": "^0.13.0"
},
"devDependencies": {
"@types/chai": "^5.2.3",
Expand Down
8 changes: 0 additions & 8 deletions basics/account-data/native/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 9 additions & 53 deletions basics/account-data/native/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Buffer } from 'node:buffer';
import {
AccountRole,
type Address,
appendTransactionMessageInstruction,
createTransactionMessage,
Expand All @@ -11,33 +9,18 @@ import {
setTransactionMessageFeePayerSigner,
signTransactionMessageWithSigners,
} from '@solana/kit';
import { SYSTEM_PROGRAM_ADDRESS } from '@solana-program/system';
import * as borsh from 'borsh';
import { assert } from 'chai';
import { FailedTransactionMetadata, LiteSVM } from 'litesvm';

const AddressInfoSchema = {
struct: {
name: 'string',
house_number: 'u8',
street: 'string',
city: 'string',
},
};

type AddressInfo = {
name: string;
house_number: number;
street: string;
city: string;
};

function borshSerialize(schema: borsh.Schema, data: object): Buffer {
return Buffer.from(borsh.serialize(schema, data));
}
import { type AddressInfo, addressInfoDecoder, createCreateAddressInfoInstruction } from '../ts';

describe('Account Data!', () => {
const svm = new LiteSVM();
const addressInfo: AddressInfo = {
name: 'Joe C',
houseNumber: 136,
street: 'Mile High Dr.',
city: 'Solana Beach',
};
let programId: Address;
let payer: KeyPairSigner;
let addressInfoAccount: KeyPairSigner;
Expand All @@ -51,30 +34,7 @@ describe('Account Data!', () => {
});

it('Create the address info account', async () => {
console.log(`Program Address : ${programId}`);
console.log(`Payer Address : ${payer.address}`);
console.log(`Address Info Acct : ${addressInfoAccount.address}`);

const ix = {
programAddress: programId,
accounts: [
{
address: addressInfoAccount.address,
role: AccountRole.WRITABLE_SIGNER,
signer: addressInfoAccount,
},
{ address: payer.address, role: AccountRole.WRITABLE_SIGNER, signer: payer },
{ address: SYSTEM_PROGRAM_ADDRESS, role: AccountRole.READONLY },
],
data: new Uint8Array(
borshSerialize(AddressInfoSchema, {
name: 'Joe C',
house_number: 136,
street: 'Mile High Dr.',
city: 'Solana Beach',
}),
),
};
const ix = createCreateAddressInfoInstruction(addressInfoAccount, payer, programId, addressInfo);

const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
Expand All @@ -92,10 +52,6 @@ describe('Account Data!', () => {
const accountInfo = svm.getAccount(addressInfoAccount.address);
assert(accountInfo.exists, 'address info account not found');

const readAddressInfo = borsh.deserialize(AddressInfoSchema, Buffer.from(accountInfo.data)) as AddressInfo;
console.log(`Name : ${readAddressInfo.name}`);
console.log(`House Num: ${readAddressInfo.house_number}`);
console.log(`Street : ${readAddressInfo.street}`);
console.log(`City : ${readAddressInfo.city}`);
assert.deepEqual(addressInfoDecoder.decode(accountInfo.data), addressInfo);
});
});
2 changes: 2 additions & 0 deletions basics/account-data/native/ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './instructions';
export * from './state';
41 changes: 41 additions & 0 deletions basics/account-data/native/ts/instructions/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
AccountRole,
addEncoderSizePrefix,
type Address,
getStructEncoder,
getU8Encoder,
getU32Encoder,
getUtf8Encoder,
type TransactionSigner,
} from '@solana/kit';
import { SYSTEM_PROGRAM_ADDRESS } from '@solana-program/system';
import type { AddressInfo } from '../state';

// Instruction data layout: the program deserializes the whole payload as an `AddressInfo`.
export const createAddressInfoEncoder = getStructEncoder([
['name', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
['houseNumber', getU8Encoder()],
['street', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
['city', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
]);

export function createCreateAddressInfoInstruction(
addressInfoAccount: TransactionSigner,
payer: TransactionSigner,
programId: Address,
addressInfo: AddressInfo,
) {
return {
programAddress: programId,
accounts: [
{
address: addressInfoAccount.address,
role: AccountRole.WRITABLE_SIGNER,
signer: addressInfoAccount,
},
{ address: payer.address, role: AccountRole.WRITABLE_SIGNER, signer: payer },
{ address: SYSTEM_PROGRAM_ADDRESS, role: AccountRole.READONLY },
],
data: createAddressInfoEncoder.encode(addressInfo),
};
}
1 change: 1 addition & 0 deletions basics/account-data/native/ts/instructions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './create';
16 changes: 16 additions & 0 deletions basics/account-data/native/ts/state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { addDecoderSizePrefix, getStructDecoder, getU8Decoder, getU32Decoder, getUtf8Decoder } from '@solana/kit';

export type AddressInfo = {
name: string;
houseNumber: number;
street: string;
city: string;
};

// Account data layout, matching the program's `AddressInfo` struct.
export const addressInfoDecoder = getStructDecoder([
['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
['houseNumber', getU8Decoder()],
['street', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
['city', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
]);
1 change: 0 additions & 1 deletion basics/checking-accounts/anchor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"@solana/web3.js": "^1.98.4"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^5.2.3",
"@types/mocha": "^10.0.10",
"anchor-litesvm": "^0.2.1",
Expand Down
10 changes: 0 additions & 10 deletions basics/checking-accounts/anchor/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion basics/close-account/anchor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"litesvm": "^0.8.0"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^5.2.3",
"@types/mocha": "^10.0.10",
"chai": "^6.2.2",
Expand Down
10 changes: 0 additions & 10 deletions basics/close-account/anchor/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion basics/cross-program-invocation/anchor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"litesvm": "^0.8.0"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^5.2.3",
"@types/mocha": "^10.0.10",
"chai": "^6.2.2",
Expand Down
10 changes: 0 additions & 10 deletions basics/cross-program-invocation/anchor/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion basics/cross-program-invocation/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"dependencies": {
"@solana/kit": "^7.0.0",
"@solana-program/system": "^0.13.0",
"borsh": "^2.0.0",
"litesvm": "^1.3.0"
},
"devDependencies": {
Expand Down
8 changes: 0 additions & 8 deletions basics/cross-program-invocation/native/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading