Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PDA prefix in Rust renderer #25

Merged
merged 1 commit into from
May 8, 2024
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
5 changes: 5 additions & 0 deletions .changeset/breezy-lobsters-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kinobi-so/renderers-rust": patch
---

Fix generated PDA prefix
2 changes: 1 addition & 1 deletion packages/renderers-rust/src/getRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
return { ...seed, resolvedType, typeManifest: seedManifest, valueManifest };
});
const hasVariableSeeds = pdaSeeds.filter(isNodeFilter('variablePdaSeedNode')).length > 0;
const constantSeeds = pdaSeeds
const constantSeeds = seeds
.filter(isNodeFilter('constantPdaSeedNode'))
.filter(seed => !isNode(seed.value, 'programIdValueNode'));

Expand Down
2 changes: 1 addition & 1 deletion packages/renderers-rust/test/_setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest';

export function codeContains(actual: string, expected: RegExp | RegExp[] | string[] | string) {
export function codeContains(actual: string, expected: (RegExp | string)[] | RegExp | string) {
const expectedArray = Array.isArray(expected) ? expected : [expected];
expectedArray.forEach(e => {
if (typeof e === 'string') {
Expand Down
37 changes: 36 additions & 1 deletion packages/renderers-rust/test/accountsPage.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {
accountNode,
bytesTypeNode,
constantPdaSeedNode,
constantPdaSeedNodeFromString,
fixedSizeTypeNode,
numberTypeNode,
numberValueNode,
pdaLinkNode,
pdaNode,
programNode,
publicKeyTypeNode,
variablePdaSeedNode,
} from '@kinobi-so/nodes';
import { visit } from '@kinobi-so/visitors-core';
Expand Down Expand Up @@ -41,7 +46,7 @@ test('it renders a byte array seed used on an account', () => {
codeContains(renderMap.get('accounts/test_account.rs'), [`byte_array_seed: [u8; 32],`, `&byte_array_seed,`]);
});

test('it renders an empty array seed used on an account', () => {
test('it renders an empty array of seeds for seedless PDAs', () => {
// Given the following program with 1 account and 1 pda with empty seeds.
const node = programNode({
accounts: [
Expand All @@ -66,3 +71,33 @@ test('it renders an empty array seed used on an account', () => {
// as a parameters to be rendered.
codeContains(renderMap.get('accounts/test_account.rs'), [/pub fn find_pda\(/, /&\[\s*\]/]);
});

test('it renders constant PDA seeds as prefix consts', () => {
// Given the following PDA node attached to an account.
const node = programNode({
accounts: [accountNode({ discriminators: [], name: 'testAccount', pda: pdaLinkNode('testPda') })],
name: 'myProgram',
pdas: [
pdaNode({
name: 'testPda',
seeds: [
constantPdaSeedNodeFromString('utf8', 'myPrefix'),
variablePdaSeedNode('myAccount', publicKeyTypeNode()),
constantPdaSeedNode(numberTypeNode('u64'), numberValueNode(42)),
],
}),
],
publicKey: '1111',
});

// When we render it.
const renderMap = visit(node, getRenderMapVisitor());

// Then we expect the following const helpers for constant seeds.
codeContains(renderMap.get('accounts/test_account.rs'), [
'/// 0. `TestAccount::PREFIX.0`',
'/// 1. my_account (`Pubkey`)',
'/// 2. `TestAccount::PREFIX.1`',
/pub const PREFIX: \(\s*&'static \[u8\],\s*&'static \[u8\],\s*\) = \(\s*"myPrefix"\.as_bytes\(\),\s*42\.as_bytes\(\),\s*\)/,
]);
});
Loading