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

Add support for empty array seeds #157

Merged
merged 4 commits into from
Jan 30, 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/five-lemons-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': patch
---

Add support for empty array seeds
5 changes: 1 addition & 4 deletions src/renderers/js-experimental/fragments/pdaFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PdaNode, ProgramNode, isNode, isNodeFilter } from '../../../nodes';
import { visit } from '../../../visitors';
import { ImportMap } from '../ImportMap';
import type { GlobalFragmentScope } from '../getRenderMapVisitor';
import { Fragment, fragment, fragmentFromTemplate } from './common';
import { Fragment, fragmentFromTemplate } from './common';

export function getPdaFunctionFragment(
scope: Pick<
Expand All @@ -20,9 +20,6 @@ export function getPdaFunctionFragment(
valueNodeVisitor,
nameApi,
} = scope;
if (pdaNode.seeds.length === 0) {
return fragment('');
}

// Seeds.
const imports = new ImportMap();
Expand Down
1 change: 1 addition & 0 deletions src/renderers/rust/getRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export function getRenderMapVisitor(options: GetRustRenderMapOptions = {}) {
seeds,
constantSeeds,
hasVariableSeeds,
pda,
})
);
},
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/rust/templates/accountsPage.njk
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl {{ account.name | pascalCase }} {
{% endif %}
{% endif %}

{% if seeds.length > 0 %}
{% if pda %}
pub fn create_pda(
{% if hasVariableSeeds %}
{% for seed in seeds %}
Expand Down
6 changes: 3 additions & 3 deletions src/visitors/updateAccountsVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ export function updateAccountsVisitor(map: Record<string, AccountUpdates>) {

const { seeds, pda, ...assignableUpdates } = updates;
let newPda = node.pda;
if (pda && !pda.importFrom && seeds) {
if (pda && !pda.importFrom && seeds !== undefined) {
newPda = pda;
pdasToUpsert.push({
program: stack.getProgram()!.name,
pda: pdaNode(pda.name, seeds),
});
} else if (pda) {
newPda = pda;
} else if (seeds && node.pda) {
} else if (seeds !== undefined && node.pda) {
pdasToUpsert.push({
program: stack.getProgram()!.name,
pda: pdaNode(node.pda.name, seeds),
});
} else if (seeds) {
} else if (seeds !== undefined) {
newPda = pdaLinkNode(newName ?? node.name);
pdasToUpsert.push({
program: stack.getProgram()!.name,
Expand Down
39 changes: 39 additions & 0 deletions test/renderers/js-experimental/pdasPage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'ava';
import {
accountNode,
pdaLinkNode,
pdaNode,
programNode,
visit,
} from '../../../src';
import { getRenderMapVisitor } from '../../../src/renderers/js-experimental/getRenderMapVisitor';
import { renderMapContains } from './_setup';

test('it renders an empty array seed used on a pda', (t) => {
// Given the following program with 1 account and 1 pda with empty seeds.
const node = programNode({
name: 'splToken',
publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
accounts: [
accountNode({
name: 'testAccount',
discriminators: [],
pda: pdaLinkNode('testPda'),
}),
],
pdas: [
// Empty array seeds.
pdaNode('testPda', []),
],
});

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

// Then we expect the following function and and empty seeds
// array used on program derived address function.
renderMapContains(t, renderMap, 'pdas/testPda.ts', [
/export async function findTestPdaPda/,
/getProgramDerivedAddress\({ programAddress, seeds: \[\] }\)/,
]);
});
31 changes: 30 additions & 1 deletion test/renderers/rust/accountsPage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
pdaNode,
programNode,
variablePdaSeedNode,
visit,
visit
} from '../../../src';
import { getRenderMapVisitor } from '../../../src/renderers/rust/getRenderMapVisitor';
import { codeContains } from './_setup';
Expand Down Expand Up @@ -42,3 +42,32 @@ test('it renders a byte array seed used on an account', (t) => {
`&byte_array_seed,`,
]);
});

test('it renders an empty array seed used on an account', (t) => {
// Given the following program with 1 account and 1 pda with empty seeds.
const node = programNode({
name: 'splToken',
publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
accounts: [
accountNode({
name: 'testAccount',
discriminators: [],
pda: pdaLinkNode('testPda'),
}),
],
pdas: [
// Empty array seeds.
pdaNode('testPda', []),
],
});

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

// Then we expect the following identifier and reference to the byte array
// as a parameters to be rendered.
codeContains(t, renderMap.get('accounts/test_account.rs'), [
/pub fn find_pda\(/,
/&\[\s*\]/,
]);
});
Loading