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

Pass tuple enum to instruction data #2088

Closed
rocalex opened this issue Jul 28, 2022 · 3 comments
Closed

Pass tuple enum to instruction data #2088

rocalex opened this issue Jul 28, 2022 · 3 comments
Labels
idl related to the IDL, either program or client side ts

Comments

@rocalex
Copy link

rocalex commented Jul 28, 2022

I pass custom struct including tuple enum to instruction:

#[derive(Debug, AnchorSerialize, AnchorDeserialize)]
pub enum FeedValue {
    Boolean(bool),
    U8(u8),
    U16(u16),
    U32(u32),
    U64(u64),
    U128(u128),
}

#[derive(Debug, AnchorSerialize, AnchorDeserialize)]
pub struct Feed {
    /// The value that is stored within a single feed. In most cases, intermediate
    /// nodes or non-leaf feeds will be None (although some may decide to have a
    /// summary value for their children), and leaf feeds will have concrete values.
    pub value: Option<FeedValue>,
}

#[program]
pub mod my_program {
    use super::*;

    pub fn create(ctx: Context<Create>, feed: Feed) -> Result<()> {
        Ok(())
    }
}

and I've got this error:

Error: Tuple enum variants not yet implemented.
    at /Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/idl.ts:134:19
    at Array.map (<anonymous>)
    at /Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/idl.ts:132:45
    at Array.map (<anonymous>)
    at Function.typeDefLayout (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/idl.ts:127:44)
    at Function.fieldLayout (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/idl.ts:96:27)
    at Function.fieldLayout (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/idl.ts:77:22)
    at /Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/idl.ts:122:28
    at Array.map (<anonymous>)
    at Function.typeDefLayout (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/idl.ts:121:48)
    at Function.fieldLayout (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/idl.ts:96:27)
    at /Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/instruction.ts:114:22
    at Array.map (<anonymous>)
    at /Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/instruction.ts:113:38
    at Array.map (<anonymous>)
    at Function.parseIxLayout (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/instruction.ts:112:26)
    at new BorshInstructionCoder (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/instruction.ts:47:43)
    at new BorshCoder (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/coder/borsh/index.ts:47:24)
    at new Program (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/program/index.ts:283:28)
    at /Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/workspace.ts:59:36
    at Array.forEach (<anonymous>)
    at Object.get (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/@project-serum/anchor/src/workspace.ts:52:10)
    at Suite.<anonymous> (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/tests/orao-onchain.ts:9:38)
    at Object.create (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/mocha/lib/interfaces/common.js:148:19)
    at context.describe.context.context (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/mocha/lib/interfaces/bdd.js:42:27)
    at Object.<anonymous> (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/tests/orao-onchain.ts:5:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module.m._compile (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.require.extensions.<computed> [as .ts] (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.exports.requireOrImport (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/mocha/lib/nodejs/esm-utils.js:60:20)
    at Object.exports.loadFilesAsync (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/mocha/lib/nodejs/esm-utils.js:103:20)
    at singleRun (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at Object.exports.handler (/Volumes/DATA/SourceCode/orao-network/orao/orao-anchor/node_modules/mocha/lib/cli/run.js:374:5)
error Command failed with exit code 1.

How to fix it?

@armaniferrante
Copy link
Member

See the error Error: Tuple enum variants not yet implemented.. Please use a struct enum variant for now.

@kklas
Copy link
Contributor

kklas commented Jul 29, 2022

You might want to check out anchor-client-gen - it has tuple enum support.

@acheroncrypto acheroncrypto added ts idl related to the IDL, either program or client side labels Sep 16, 2023
@acheroncrypto
Copy link
Collaborator

Using tuple enum as an argument was implemented in #2185 and inside account data in #2601.

pub fn enm(ctx: Context<Enum>, enum_arg: MyEnum) -> Result<()> {
ctx.accounts.account.enum_field = enum_arg;
Ok(())
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug, Eq, PartialEq)]
pub enum MyEnum {
Unit,
Named { x: u64, y: u64 },
Unnamed(u8, u8, u16, u16),
UnnamedStruct(MyStruct),
}

// Unnamed
const tupleArg = [1, 2, 3, 4] as const;
const unnamed = await testAccountEnum({ unnamed: tupleArg });
assert.strictEqual(unnamed.enumField.unnamed[0], tupleArg[0]);
assert.strictEqual(unnamed.enumField.unnamed[1], tupleArg[1]);
assert.strictEqual(unnamed.enumField.unnamed[2], tupleArg[2]);
assert.strictEqual(unnamed.enumField.unnamed[3], tupleArg[3]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idl related to the IDL, either program or client side ts
Projects
None yet
Development

No branches or pull requests

4 participants