Skip to content

Latest commit

 

History

History
70 lines (57 loc) · 3.18 KB

ConditionalValueNode.md

File metadata and controls

70 lines (57 loc) · 3.18 KB

ConditionalValueNode

A value node that differs based on a condition.

Attributes

Data

Attribute Type Description
kind "conditionalValueNode" The node discriminator.

Children

Attribute Type Description
condition AccountValueNode | ArgumentValueNode | ResolverValueNode The condition that must be met. If it is an argument or value node, the value attribute may be used to assert on a specific value.
value ValueNode (Optional) If provided, the condition must be equal to this value. Otherwise, the condition must simply exist.
ifTrue InstructionInputValueNode (Optional) The value to use if the condition is true.
ifFalse InstructionInputValueNode (Optional) The value to use if the condition is false.

Functions

conditionalValueNode(input)

Helper function that creates a ConditionalValueNode object from an input object.

const node = conditionalValueNode({
    condition: argumentValueNode('amount'),
    value: numberValueNode(0),
    ifTrue: accountValueNode('mint'),
    ifFalse: programIdValueNode(),
});

Examples

An instruction account that defaults to another account if a condition is met

instructionNode({
    name: 'transfer',
    accounts: [
        instructionAccountNode({
            name: 'source',
            isSigner: false,
            isWritable: true,
        }),
        instructionAccountNode({
            name: 'destination',
            isSigner: false,
            isWritable: true,
            isOptional: true,
            defaultValue: conditionalValueNode({
                condition: argumentValueNode('amount'),
                value: numberValueNode(0),
                ifTrue: accountValueNode('source'),
            }),
        }),
        // ...
    ],
    arguments: [
        instructionArgumentNode({
            name: 'amount',
            type: numberTypeNode('u64'),
        }),
    ],
});