-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Contextual Value Nodes and use instead of InstructionDefaults (#129)
- Loading branch information
1 parent
c517404
commit b3ec72c
Showing
122 changed files
with
1,039 additions
and
1,318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@metaplex-foundation/kinobi': minor | ||
--- | ||
|
||
Add Contextual Value Nodes and use instead of InstructionDefaults |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { MainCaseString, mainCase } from '../../shared'; | ||
|
||
export type AccountBumpValueNode = { | ||
readonly kind: 'accountBumpValueNode'; | ||
readonly name: MainCaseString; | ||
}; | ||
|
||
export function accountBumpValueNode(name: string): AccountBumpValueNode { | ||
return { kind: 'accountBumpValueNode', name: mainCase(name) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { MainCaseString, mainCase } from '../../shared'; | ||
|
||
export type AccountValueNode = { | ||
readonly kind: 'accountValueNode'; | ||
readonly name: MainCaseString; | ||
}; | ||
|
||
export function accountValueNode(name: string): AccountValueNode { | ||
return { kind: 'accountValueNode', name: mainCase(name) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { MainCaseString, mainCase } from '../../shared'; | ||
|
||
export type ArgumentValueNode = { | ||
readonly kind: 'argumentValueNode'; | ||
readonly name: MainCaseString; | ||
}; | ||
|
||
export function argumentValueNode(name: string): ArgumentValueNode { | ||
return { kind: 'argumentValueNode', name: mainCase(name) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ValueNode } from '../valueNodes'; | ||
import { AccountValueNode } from './AccountValueNode'; | ||
import { ArgumentValueNode } from './ArgumentValueNode'; | ||
import { | ||
INSTRUCTION_INPUT_VALUE_NODE, | ||
InstructionInputValueNode, | ||
} from './ContextualValueNode'; | ||
import { ResolverValueNode } from './ResolverValueNode'; | ||
|
||
export type ConditionalValueBranch = InstructionInputValueNode; | ||
|
||
export const CONDITIONAL_VALUE_BRANCH_NODES = INSTRUCTION_INPUT_VALUE_NODE; | ||
|
||
export type ConditionalValueNode = { | ||
readonly kind: 'conditionalValueNode'; | ||
readonly condition: ResolverValueNode | AccountValueNode | ArgumentValueNode; | ||
readonly value?: ValueNode; | ||
readonly ifTrue?: ConditionalValueBranch; | ||
readonly ifFalse?: ConditionalValueBranch; | ||
}; | ||
|
||
export function conditionalValueNode(input: { | ||
condition: ConditionalValueNode['condition']; | ||
value?: ConditionalValueNode['value']; | ||
ifTrue?: ConditionalValueNode['ifTrue']; | ||
ifFalse?: ConditionalValueNode['ifFalse']; | ||
}): ConditionalValueNode { | ||
return { | ||
kind: 'conditionalValueNode', | ||
condition: input.condition, | ||
value: input.value, | ||
ifTrue: input.ifTrue, | ||
ifFalse: input.ifFalse, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { Mutable } from '../../shared'; | ||
import type { ProgramLinkNode } from '../linkNodes/ProgramLinkNode'; | ||
import { VALUE_NODES, ValueNode } from '../valueNodes/ValueNode'; | ||
import type { AccountBumpValueNode } from './AccountBumpValueNode'; | ||
import type { AccountValueNode } from './AccountValueNode'; | ||
import type { ArgumentValueNode } from './ArgumentValueNode'; | ||
import type { ConditionalValueNode } from './ConditionalValueNode'; | ||
import type { IdentityValueNode } from './IdentityValueNode'; | ||
import type { PayerValueNode } from './PayerValueNode'; | ||
import type { PdaValueNode } from './PdaValueNode'; | ||
import type { ProgramIdValueNode } from './ProgramIdValueNode'; | ||
import type { ResolverValueNode } from './ResolverValueNode'; | ||
|
||
// Node Group Registration. | ||
|
||
export const REGISTERED_CONTEXTUAL_VALUE_NODES = { | ||
accountBumpValueNode: {} as AccountBumpValueNode, | ||
accountValueNode: {} as AccountValueNode, | ||
argumentValueNode: {} as ArgumentValueNode, | ||
conditionalValueNode: {} as ConditionalValueNode, | ||
identityValueNode: {} as IdentityValueNode, | ||
payerValueNode: {} as PayerValueNode, | ||
pdaValueNode: {} as PdaValueNode, | ||
programIdValueNode: {} as ProgramIdValueNode, | ||
resolverValueNode: {} as ResolverValueNode, | ||
}; | ||
|
||
export const REGISTERED_CONTEXTUAL_VALUE_NODE_KEYS = Object.keys( | ||
REGISTERED_CONTEXTUAL_VALUE_NODES | ||
) as (keyof typeof REGISTERED_CONTEXTUAL_VALUE_NODES)[]; | ||
|
||
export type RegisteredContextualValueNodes = | ||
typeof REGISTERED_CONTEXTUAL_VALUE_NODES; | ||
|
||
// Node Group Helpers. | ||
|
||
export type ContextualValueNode = | ||
RegisteredContextualValueNodes[keyof RegisteredContextualValueNodes]; | ||
|
||
export const CONTEXTUAL_VALUE_NODES = REGISTERED_CONTEXTUAL_VALUE_NODE_KEYS; | ||
|
||
export type InstructionInputValueNode = | ||
| ValueNode | ||
| ContextualValueNode | ||
| ProgramLinkNode; | ||
|
||
const INSTRUCTION_INPUT_VALUE_NODE_INTERNAL = [ | ||
...VALUE_NODES, | ||
...CONTEXTUAL_VALUE_NODES, | ||
'programLinkNode', | ||
] as const; | ||
|
||
export const INSTRUCTION_INPUT_VALUE_NODE = | ||
INSTRUCTION_INPUT_VALUE_NODE_INTERNAL as Mutable< | ||
typeof INSTRUCTION_INPUT_VALUE_NODE_INTERNAL | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type IdentityValueNode = { | ||
readonly kind: 'identityValueNode'; | ||
}; | ||
|
||
export function identityValueNode(): IdentityValueNode { | ||
return { kind: 'identityValueNode' }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type PayerValueNode = { | ||
readonly kind: 'payerValueNode'; | ||
}; | ||
|
||
export function payerValueNode(): PayerValueNode { | ||
return { kind: 'payerValueNode' }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { MainCaseString, mainCase } from '../../shared'; | ||
import { isNode } from '../Node'; | ||
import { PdaNode } from '../PdaNode'; | ||
import { PdaLinkNode, pdaLinkNode } from '../linkNodes'; | ||
import { ValueNode } from '../valueNodes'; | ||
import { AccountValueNode, accountValueNode } from './AccountValueNode'; | ||
import { ArgumentValueNode, argumentValueNode } from './ArgumentValueNode'; | ||
|
||
export type PdaValueNode = { | ||
readonly kind: 'pdaValueNode'; | ||
readonly pda: PdaLinkNode; | ||
readonly seeds: Record< | ||
MainCaseString, | ||
ValueNode | AccountValueNode | ArgumentValueNode | ||
>; | ||
}; | ||
|
||
export function pdaValueNode( | ||
pda: PdaLinkNode | string, | ||
seeds: Record<string, ValueNode | AccountValueNode | ArgumentValueNode> = {} | ||
): PdaValueNode { | ||
return { | ||
kind: 'pdaValueNode', | ||
pda: typeof pda === 'string' ? pdaLinkNode(pda) : pda, | ||
seeds: Object.entries(seeds).reduce((acc, [name, seedValue]) => { | ||
acc[mainCase(name)] = seedValue; | ||
return acc; | ||
}, {} as PdaValueNode['seeds']), | ||
}; | ||
} | ||
|
||
export function getDefaultSeedValuesFromPda( | ||
node: PdaNode | ||
): PdaValueNode['seeds'] { | ||
return node.seeds.reduce((acc, seed) => { | ||
if (!isNode(seed, 'variablePdaSeedNode')) return acc; | ||
if (isNode(seed.type, 'publicKeyTypeNode')) { | ||
acc[seed.name] = accountValueNode(seed.name); | ||
} else { | ||
acc[seed.name] = argumentValueNode(seed.name); | ||
} | ||
return acc; | ||
}, {} as PdaValueNode['seeds']); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type ProgramIdValueNode = { | ||
readonly kind: 'programIdValueNode'; | ||
}; | ||
|
||
export function programIdValueNode(): ProgramIdValueNode { | ||
return { kind: 'programIdValueNode' }; | ||
} |
Oops, something went wrong.