Skip to content

Commit

Permalink
Fix type definition of Anchor discriminators (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored May 16, 2024
1 parent 5794385 commit d43493e
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-days-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kinobi-so/nodes-from-anchor": patch
---

Fix type definition of Anchor discriminators
3 changes: 2 additions & 1 deletion packages/nodes-from-anchor/src/v00/AccountNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
camelCase,
DiscriminatorNode,
fieldDiscriminatorNode,
fixedSizeTypeNode,
pdaLinkNode,
structFieldTypeNode,
StructTypeNode,
Expand Down Expand Up @@ -34,7 +35,7 @@ export function accountNodeFromAnchorV00(
defaultValue: getAnchorAccountDiscriminatorV00(idlName),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), 8),
});
data = structTypeNode([discriminator, ...data.fields]);
discriminators = [fieldDiscriminatorNode('discriminator')];
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-from-anchor/src/v00/InstructionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
camelCase,
DiscriminatorNode,
fieldDiscriminatorNode,
fixedSizeTypeNode,
instructionArgumentNode,
InstructionNode,
instructionNode,
Expand Down Expand Up @@ -36,7 +37,7 @@ export function instructionNodeFromAnchorV00(idl: IdlV00Instruction, origin?: 'a
defaultValue: getAnchorInstructionDiscriminatorV00(idlName),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), 8),
});
dataArguments = [discriminatorField, ...dataArguments];
discriminators = [fieldDiscriminatorNode('discriminator')];
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-from-anchor/src/v01/AccountNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
bytesTypeNode,
camelCase,
fieldDiscriminatorNode,
fixedSizeTypeNode,
structFieldTypeNode,
structTypeNode,
} from '@kinobi-so/nodes';
Expand Down Expand Up @@ -33,7 +34,7 @@ export function accountNodeFromAnchorV01(idl: IdlV01Account, types: IdlV01TypeDe
defaultValue: getAnchorDiscriminatorV01(idl.discriminator),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), idl.discriminator.length),
});

return accountNode({
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-from-anchor/src/v01/InstructionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
bytesTypeNode,
camelCase,
fieldDiscriminatorNode,
fixedSizeTypeNode,
instructionArgumentNode,
InstructionNode,
instructionNode,
Expand All @@ -20,7 +21,7 @@ export function instructionNodeFromAnchorV01(idl: IdlV01Instruction): Instructio
defaultValue: getAnchorDiscriminatorV01(idl.discriminator),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), idl.discriminator.length),
});
dataArguments = [discriminatorField, ...dataArguments];
const discriminators = [fieldDiscriminatorNode('discriminator')];
Expand Down
6 changes: 5 additions & 1 deletion packages/nodes-from-anchor/src/v01/PdaNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
bytesTypeNode,
camelCase,
constantPdaSeedNode,
fixedSizeTypeNode,
PdaNode,
pdaNode,
PdaSeedNode,
Expand All @@ -17,7 +18,10 @@ export function pdaNodeFromAnchorV01(idl: IdlV01InstructionAccount): PdaNode {
const seeds = idl.pda?.seeds.map((seed): PdaSeedNode => {
switch (seed.kind) {
case 'const':
return constantPdaSeedNode(bytesTypeNode(), getAnchorDiscriminatorV01(seed.value));
return constantPdaSeedNode(
fixedSizeTypeNode(bytesTypeNode(), seed.value.length),
getAnchorDiscriminatorV01(seed.value),
);
case 'account':
return variablePdaSeedNode(seed.path, publicKeyTypeNode());
case 'arg':
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-from-anchor/test/v00/AccountNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
bytesTypeNode,
bytesValueNode,
fieldDiscriminatorNode,
fixedSizeTypeNode,
numberTypeNode,
structFieldTypeNode,
structTypeNode,
Expand Down Expand Up @@ -49,7 +50,7 @@ test('it creates account nodes with anchor discriminators', () => {
defaultValue: bytesValueNode('base16', 'f61c0657fb2d322a'),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), 8),
}),
]),
discriminators: [fieldDiscriminatorNode('discriminator')],
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-from-anchor/test/v00/InstructionNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
bytesTypeNode,
bytesValueNode,
fieldDiscriminatorNode,
fixedSizeTypeNode,
instructionAccountNode,
instructionArgumentNode,
instructionNode,
Expand Down Expand Up @@ -44,7 +45,7 @@ test('it creates instruction nodes with anchor discriminators', () => {
defaultValue: bytesValueNode('base16', 'c3f1b80e7f9b4435'),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), 8),
}),
],
discriminators: [fieldDiscriminatorNode('discriminator')],
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-from-anchor/test/v01/AccountNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
accountNode,
bytesTypeNode,
fieldDiscriminatorNode,
fixedSizeTypeNode,
numberTypeNode,
structFieldTypeNode,
structTypeNode,
Expand Down Expand Up @@ -40,7 +41,7 @@ test('it creates account nodes with anchor discriminators', () => {
defaultValue: getAnchorDiscriminatorV01([246, 28, 6, 87, 251, 45, 50, 42]),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), 8),
}),
structFieldTypeNode({
name: 'name',
Expand Down
5 changes: 3 additions & 2 deletions packages/nodes-from-anchor/test/v01/InstructionNode.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
bytesTypeNode,
fieldDiscriminatorNode,
fixedSizeTypeNode,
instructionAccountNode,
instructionArgumentNode,
instructionNode,
Expand All @@ -26,7 +27,7 @@ test('it creates instruction nodes', () => {
defaultValue: getAnchorDiscriminatorV01([246, 28, 6, 87, 251, 45, 50, 42]),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), 8),
}),
instructionArgumentNode({ name: 'amount', type: numberTypeNode('u8') }),
],
Expand All @@ -51,7 +52,7 @@ test('it creates instruction nodes with anchor discriminators', () => {
defaultValue: getAnchorDiscriminatorV01([246, 28, 6, 87, 251, 45, 50, 42]),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), 8),
}),
],
discriminators: [fieldDiscriminatorNode('discriminator')],
Expand Down
11 changes: 9 additions & 2 deletions packages/nodes-from-anchor/test/v01/PdaNode.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { bytesTypeNode, constantPdaSeedNode, pdaNode, publicKeyTypeNode, variablePdaSeedNode } from '@kinobi-so/nodes';
import {
bytesTypeNode,
constantPdaSeedNode,
fixedSizeTypeNode,
pdaNode,
publicKeyTypeNode,
variablePdaSeedNode,
} from '@kinobi-so/nodes';
import { expect, test } from 'vitest';

import { getAnchorDiscriminatorV01, pdaNodeFromAnchorV01 } from '../../src';
Expand All @@ -18,7 +25,7 @@ test('it creates PDA nodes', () => {
pdaNode({
name: 'myPda',
seeds: [
constantPdaSeedNode(bytesTypeNode(), getAnchorDiscriminatorV01([42, 31, 29])),
constantPdaSeedNode(fixedSizeTypeNode(bytesTypeNode(), 3), getAnchorDiscriminatorV01([42, 31, 29])),
variablePdaSeedNode('authority', publicKeyTypeNode()),
],
}),
Expand Down
10 changes: 7 additions & 3 deletions packages/nodes-from-anchor/test/v01/ProgramNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
definedTypeNode,
errorNode,
fieldDiscriminatorNode,
fixedSizeTypeNode,
instructionAccountNode,
instructionArgumentNode,
instructionNode,
Expand Down Expand Up @@ -58,7 +59,7 @@ test('it creates program nodes', () => {
defaultValue: getAnchorDiscriminatorV01([246, 28, 6, 87, 251, 45, 50, 42]),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), 8),
}),
]),
discriminators: [fieldDiscriminatorNode('discriminator')],
Expand Down Expand Up @@ -93,7 +94,7 @@ test('it creates program nodes', () => {
defaultValue: getAnchorDiscriminatorV01([246, 28, 6, 87, 251, 45, 50, 42]),
defaultValueStrategy: 'omitted',
name: 'discriminator',
type: bytesTypeNode(),
type: fixedSizeTypeNode(bytesTypeNode(), 8),
}),
],
discriminators: [fieldDiscriminatorNode('discriminator')],
Expand All @@ -106,7 +107,10 @@ test('it creates program nodes', () => {
pdaNode({
name: 'authority',
seeds: [
constantPdaSeedNode(bytesTypeNode(), getAnchorDiscriminatorV01([42, 31, 29])),
constantPdaSeedNode(
fixedSizeTypeNode(bytesTypeNode(), 3),
getAnchorDiscriminatorV01([42, 31, 29]),
),
variablePdaSeedNode('owner', publicKeyTypeNode()),
],
}),
Expand Down

0 comments on commit d43493e

Please sign in to comment.