1

I have a functions looks somewhat like this in the program

enum DummyState {
  one,
  two
}

fn dummy(ctx: Context<Dummy>, state: Option<DummyState>) {
...
}

How do I pass in the arguments to the function?

The snipped below does not work.

await program.methods
       .dummy({ one: {} })
       .accounts({
           user: user.publicKey,
           state: statePDA,
       })
       .signers([user])
       .rpc();

I get this error instead. Error: unable to infer src variant

1 Answer 1

3

I cannot replicate the issue. I was able to pass null when I did not have an argument to pass. Happy to take a look at your full code if you have a GH.

Here's what I tested (and anchor test ran fine):

use anchor_lang::prelude::*;

declare_id!("6itiTNjzVZWxSgrjaGwMMjakDvrmnkwPG5YsbiU2jSkX");

#[program]
mod enum_test {
    use super::*;
    pub fn dummy(_ctx: Context<Dummy>, _data: Option<DummyState>) -> Result<()> {
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Dummy<'info> {
    #[account(mut)]
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Copy, Clone, PartialEq, Eq)]
pub enum DummyState {
    One,
    Two,
}

And my test:

import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { EnumTest } from "../target/types/enum_test";
import { expect } from "chai";

describe("enum-test", () => {
  // Configure the client to use the local cluster.
  anchor.setProvider(anchor.AnchorProvider.env());
  const program = anchor.workspace.EnumTest as Program<EnumTest>;
  // Reusable function to test dummy calls
  async function testDummyCall(dummyArgs: any) {
    try {
      const tx = await program.methods.dummy(dummyArgs).rpc();
      expect(tx).to.be.ok;
    } catch (error) {
      expect(error).to.not.be.ok;
    }
  }

  it("Is dummy one!", async () => {
    await testDummyCall({ one: {} });
  });

  it("Is dummy two!", async () => {
    await testDummyCall({ two: {} });
  });

  it("Is not a dummy!", async () => {
    await testDummyCall(null);
  });

  it("Fails on dummy three!", async () => {
    try {
      await program.methods.dummy({ three: {} }).rpc();
    } catch (error) {
      expect(error).to.be.ok;
    }
  });
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.