Skip to content

Commit ac589d8

Browse files
committed
Fix PDA prefix in Rust renderer
1 parent 799dade commit ac589d8

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

.changeset/breezy-lobsters-begin.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@kinobi-so/renderers-rust": patch
3+
---
4+
5+
Fix generated PDA prefix

packages/renderers-rust/src/getRenderMapVisitor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
7474
return { ...seed, resolvedType, typeManifest: seedManifest, valueManifest };
7575
});
7676
const hasVariableSeeds = pdaSeeds.filter(isNodeFilter('variablePdaSeedNode')).length > 0;
77-
const constantSeeds = pdaSeeds
77+
const constantSeeds = seeds
7878
.filter(isNodeFilter('constantPdaSeedNode'))
7979
.filter(seed => !isNode(seed.value, 'programIdValueNode'));
8080

packages/renderers-rust/test/_setup.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'vitest';
22

3-
export function codeContains(actual: string, expected: RegExp | RegExp[] | string[] | string) {
3+
export function codeContains(actual: string, expected: (RegExp | string)[] | RegExp | string) {
44
const expectedArray = Array.isArray(expected) ? expected : [expected];
55
expectedArray.forEach(e => {
66
if (typeof e === 'string') {

packages/renderers-rust/test/accountsPage.test.ts

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import {
22
accountNode,
33
bytesTypeNode,
4+
constantPdaSeedNode,
5+
constantPdaSeedNodeFromString,
46
fixedSizeTypeNode,
7+
numberTypeNode,
8+
numberValueNode,
59
pdaLinkNode,
610
pdaNode,
711
programNode,
12+
publicKeyTypeNode,
813
variablePdaSeedNode,
914
} from '@kinobi-so/nodes';
1015
import { visit } from '@kinobi-so/visitors-core';
@@ -41,7 +46,7 @@ test('it renders a byte array seed used on an account', () => {
4146
codeContains(renderMap.get('accounts/test_account.rs'), [`byte_array_seed: [u8; 32],`, `&byte_array_seed,`]);
4247
});
4348

44-
test('it renders an empty array seed used on an account', () => {
49+
test('it renders an empty array of seeds for seedless PDAs', () => {
4550
// Given the following program with 1 account and 1 pda with empty seeds.
4651
const node = programNode({
4752
accounts: [
@@ -66,3 +71,33 @@ test('it renders an empty array seed used on an account', () => {
6671
// as a parameters to be rendered.
6772
codeContains(renderMap.get('accounts/test_account.rs'), [/pub fn find_pda\(/, /&\[\s*\]/]);
6873
});
74+
75+
test('it renders constant PDA seeds as prefix consts', () => {
76+
// Given the following PDA node attached to an account.
77+
const node = programNode({
78+
accounts: [accountNode({ discriminators: [], name: 'testAccount', pda: pdaLinkNode('testPda') })],
79+
name: 'myProgram',
80+
pdas: [
81+
pdaNode({
82+
name: 'testPda',
83+
seeds: [
84+
constantPdaSeedNodeFromString('utf8', 'myPrefix'),
85+
variablePdaSeedNode('myAccount', publicKeyTypeNode()),
86+
constantPdaSeedNode(numberTypeNode('u64'), numberValueNode(42)),
87+
],
88+
}),
89+
],
90+
publicKey: '1111',
91+
});
92+
93+
// When we render it.
94+
const renderMap = visit(node, getRenderMapVisitor());
95+
96+
// Then we expect the following const helpers for constant seeds.
97+
codeContains(renderMap.get('accounts/test_account.rs'), [
98+
'/// 0. `TestAccount::PREFIX.0`',
99+
'/// 1. my_account (`Pubkey`)',
100+
'/// 2. `TestAccount::PREFIX.1`',
101+
/pub const PREFIX: \(\s*&'static \[u8\],\s*&'static \[u8\],\s*\) = \(\s*"myPrefix"\.as_bytes\(\),\s*42\.as_bytes\(\),\s*\)/,
102+
]);
103+
});

0 commit comments

Comments
 (0)