Skip to content

Commit

Permalink
Add optional identifier to PublicKeyValueNode (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored Jan 9, 2024
1 parent a9768f9 commit ceb467a
Show file tree
Hide file tree
Showing 45 changed files with 247 additions and 96 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-pillows-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': patch
---

Add optional identifier to PublicKeyValueNode
14 changes: 12 additions & 2 deletions src/nodes/valueNodes/PublicKeyValueNode.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { MainCaseString, mainCase } from '../../shared/utils';

export type PublicKeyValueNode = {
readonly kind: 'publicKeyValueNode';

// Data.
readonly publicKey: string;
readonly identifier?: MainCaseString;
};

export function publicKeyValueNode(publicKey: string): PublicKeyValueNode {
return { kind: 'publicKeyValueNode', publicKey };
export function publicKeyValueNode(
publicKey: string,
identifier?: string
): PublicKeyValueNode {
return {
kind: 'publicKeyValueNode',
publicKey,
identifier: identifier ? mainCase(identifier) : undefined,
};
}
11 changes: 9 additions & 2 deletions src/renderers/js/renderInstructionDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,15 @@ export function renderInstructionDefaults(
}
return render(`${pdaFunction}(${pdaArgs.join(', ')})`);
case 'publicKeyValueNode':
imports.add('umi', 'publicKey');
return render(`publicKey('${defaultValue.publicKey}')`);
if (!defaultValue.identifier) {
imports.add('umi', 'publicKey');
return render(`publicKey('${defaultValue.publicKey}')`);
}
interfaces.add('programs');
return render(
`context.programs.getPublicKey('${defaultValue.identifier}', '${defaultValue.publicKey}')`,
false
);
case 'programLinkNode':
const importFrom = defaultValue.importFrom ?? 'generatedPrograms';
const functionName = `get${pascalCase(defaultValue.name)}ProgramId`;
Expand Down
5 changes: 4 additions & 1 deletion src/visitors/getDebugStringVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ function getNodeDetails(node: Node): string[] {
case 'booleanValueNode':
return [node.boolean ? 'true' : 'false'];
case 'publicKeyValueNode':
return [node.publicKey];
return [
...(node.identifier ? [`${node.identifier}`] : []),
node.publicKey,
];
case 'enumValueNode':
return [node.variant];
case 'resolverValueNode':
Expand Down
23 changes: 16 additions & 7 deletions src/visitors/setInstructionAccountDefaultValuesVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,49 +47,58 @@ export const DEFAULT_INSTRUCTION_ACCOUNT_DEFAULT_RULES: InstructionAccountDefaul
},
{
account: /^systemProgram|splSystemProgram$/,
defaultValue: publicKeyValueNode('11111111111111111111111111111111'),
defaultValue: publicKeyValueNode(
'11111111111111111111111111111111',
'splSystem'
),
ignoreIfOptional: true,
},
{
account: /^tokenProgram|splTokenProgram$/,
defaultValue: publicKeyValueNode(
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
'splToken'
),
ignoreIfOptional: true,
},
{
account: /^ataProgram|splAtaProgram$/,
defaultValue: publicKeyValueNode(
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL'
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
'splAssociatedToken'
),
ignoreIfOptional: true,
},
{
account: /^tokenMetadataProgram|mplTokenMetadataProgram$/,
defaultValue: publicKeyValueNode(
'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'
'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s',
'mplTokenMetadata'
),
ignoreIfOptional: true,
},
{
account:
/^(tokenAuth|mplTokenAuth|authorization|mplAuthorization|auth|mplAuth)RulesProgram$/,
defaultValue: publicKeyValueNode(
'auth9SigNpDKz4sJJ1DfCTuZrZNSAgh9sFD3rboVmgg'
'auth9SigNpDKz4sJJ1DfCTuZrZNSAgh9sFD3rboVmgg',
'mplTokenAuthRules'
),
ignoreIfOptional: true,
},
{
account: /^candyMachineProgram|mplCandyMachineProgram$/,
defaultValue: publicKeyValueNode(
'CndyV3LdqHUfDLmE5naZjVN8rBZz4tqhdefbAnjHG3JR'
'CndyV3LdqHUfDLmE5naZjVN8rBZz4tqhdefbAnjHG3JR',
'mplCandyMachine'
),
ignoreIfOptional: true,
},
{
account: /^candyGuardProgram|mplCandyGuardProgram$/,
defaultValue: publicKeyValueNode(
'Guard1JwRhJkVH6XZhzoYxeBVQe872VH6QggF4BWmS9g'
'Guard1JwRhJkVH6XZhzoYxeBVQe872VH6QggF4BWmS9g',
'mplCandyGuard'
),
ignoreIfOptional: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
PublicKey,
Signer,
TransactionBuilder,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Expand Down Expand Up @@ -126,9 +125,11 @@ export function approveCollectionAuthority(
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = publicKey(
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}

// Accounts in order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
PublicKey,
Signer,
TransactionBuilder,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Expand Down Expand Up @@ -162,14 +161,18 @@ export function approveUseAuthority(
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.tokenProgram.value) {
resolvedAccounts.tokenProgram.value = publicKey(
resolvedAccounts.tokenProgram.value = context.programs.getPublicKey(
'splToken',
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
);
resolvedAccounts.tokenProgram.isWritable = false;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = publicKey(
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}

// Accounts in order.
Expand Down
5 changes: 3 additions & 2 deletions test/packages/js/src/generated/instructions/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
PublicKey,
Signer,
TransactionBuilder,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Expand Down Expand Up @@ -135,9 +134,11 @@ export function burn(

// Default values.
if (!resolvedAccounts.splTokenProgram.value) {
resolvedAccounts.splTokenProgram.value = publicKey(
resolvedAccounts.splTokenProgram.value = context.programs.getPublicKey(
'splToken',
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
);
resolvedAccounts.splTokenProgram.isWritable = false;
}

// Accounts in order.
Expand Down
5 changes: 3 additions & 2 deletions test/packages/js/src/generated/instructions/burnEditionNft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
PublicKey,
Signer,
TransactionBuilder,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Expand Down Expand Up @@ -142,9 +141,11 @@ export function burnEditionNft(

// Default values.
if (!resolvedAccounts.splTokenProgram.value) {
resolvedAccounts.splTokenProgram.value = publicKey(
resolvedAccounts.splTokenProgram.value = context.programs.getPublicKey(
'splToken',
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
);
resolvedAccounts.splTokenProgram.isWritable = false;
}

// Accounts in order.
Expand Down
5 changes: 3 additions & 2 deletions test/packages/js/src/generated/instructions/burnNft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
PublicKey,
Signer,
TransactionBuilder,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Expand Down Expand Up @@ -110,9 +109,11 @@ export function burnNft(

// Default values.
if (!resolvedAccounts.splTokenProgram.value) {
resolvedAccounts.splTokenProgram.value = publicKey(
resolvedAccounts.splTokenProgram.value = context.programs.getPublicKey(
'splToken',
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
);
resolvedAccounts.splTokenProgram.isWritable = false;
}

// Accounts in order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ export function closeEscrowAccount(
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = publicKey(
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}
if (!resolvedAccounts.sysvarInstructions.value) {
resolvedAccounts.sysvarInstructions.value = publicKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ export function createEscrowAccount(
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = publicKey(
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}
if (!resolvedAccounts.sysvarInstructions.value) {
resolvedAccounts.sysvarInstructions.value = publicKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
PublicKey,
Signer,
TransactionBuilder,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Expand Down Expand Up @@ -124,9 +123,11 @@ export function createFrequencyRule(
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = publicKey(
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}

// Accounts in order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,18 @@ export function createMasterEdition(
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.tokenProgram.value) {
resolvedAccounts.tokenProgram.value = publicKey(
resolvedAccounts.tokenProgram.value = context.programs.getPublicKey(
'splToken',
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
);
resolvedAccounts.tokenProgram.isWritable = false;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = publicKey(
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}
if (!resolvedAccounts.rent.value) {
resolvedAccounts.rent.value = publicKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
PublicKey,
Signer,
TransactionBuilder,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Expand Down Expand Up @@ -154,14 +153,18 @@ export function createMasterEditionV3(
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.tokenProgram.value) {
resolvedAccounts.tokenProgram.value = publicKey(
resolvedAccounts.tokenProgram.value = context.programs.getPublicKey(
'splToken',
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
);
resolvedAccounts.tokenProgram.isWritable = false;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = publicKey(
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}

// Accounts in order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ export function createMetadataAccount(
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = publicKey(
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}
if (!resolvedAccounts.rent.value) {
resolvedAccounts.rent.value = publicKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
PublicKey,
Signer,
TransactionBuilder,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Expand Down Expand Up @@ -138,9 +137,11 @@ export function createMetadataAccountV2(
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = publicKey(
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}

// Accounts in order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
PublicKey,
Signer,
TransactionBuilder,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Expand Down Expand Up @@ -158,9 +157,11 @@ export function createMetadataAccountV3(
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = publicKey(
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}

// Accounts in order.
Expand Down
Loading

0 comments on commit ceb467a

Please sign in to comment.