1

I'm trying to get an example lottery project to run in my local environment.

Anchor test projects and all that work for me. Anchor build - deploy - test all runs well.

However on this https://github.com/ExtropyIO/SolanaBootcamp/tree/main/examples_anchor/programs/example1-lottery

I'm having test errors when its trying to create wallets and obviously it fails the rest because of that.

I tried different versions of Solana/Anchor. Currently running Anchor 0.29.0 and Solana 1.16.25

I noticed however when I tried it with anchor generated folder that the test files imports from coral-xyz/anchor

import * as anchor from "@coral-xyz/anchor";

and the project above imports from

import * as anchor from "@project-serum/anchor";

Apart from that, I don't see the reason why it wouldn't run the tests properly if it works with a normal hello world.

Configuration and steps I took:

  • ubuntu 20.04 on WSL
  • installed Rust/Solana/Anchor/avm lastest
  • anchor build -> deploy -> then with deployed program IDs I changed declare IDs in anchor.toml and in all 3 programs/../src/lib.rs
  • at first it threw me an error about ts-mocha not being installed which is weird because 'anchor init' will not say that
  • installed ts-mocha with code I found online
  • error I got first was different but pushed with

.rpc({skipPreflight:true})

I'm coming from .js this is all a bit quite new for me. Is this a version error? If so what are up to date versions people are running without issues?

I checked some yaml scripts to get workspace setup but always end up here.

5
  • so @project-serum/anchor should link to @coral-xyz/anchor I don't think that's the issue. Can you give more details on the error? the entire stack trace or program log. or signature of the failed transaction... And maybe also the code snippet you're running. right now i only know that your first transaction is failing.
    – Solandy
    Jan 2 at 18:04
  • Running 'anchor run test1' on the above cloned github repo 1) Tests for example1-lottery Creates a lottery account: Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Program failed to complete at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:4153:13) at processTicksAndRejections (node:internal/process/task_queues:95:5)
    – vonyi
    Jan 2 at 18:13
  • Solana logs Transaction executed in slot 13956: Signature: NiffuTsQCe3ZhhtWdhCsXqVnnDkT9cJsCeXiHEp1x1zzFuuDGJqvFmP7WPgXC2K26JznR1Dhd3bNDZKUty6p2Xh Status: Ok Log Messages: Program 11111111111111111111111111111111 invoke [1] Program 11111111111111111111111111111111 success Transaction executed in slot 13957: Signature: 3rLJv3yUNFHBBTMXK3BKLuh4CQCyDFypUxDyiDXzaf1PpbFV5bdYAg9vCsDWpEMWCFHkGafn5hpdNLdkRvvWwVVf Status: Ok Log Messages: Program 11111111111111111111111111111111 invoke [1] Program 11111111111111111111111111111111 success
    – vonyi
    Jan 2 at 18:15
  • Transaction executed in slot 13958: Signature: 3tdZkhdr8HCeHtBBHe74xTikPYpQ4EkNbfQ7aonFuh51ej4t5tRMKbz11tDTck6LLjvFryimx6QN8mjPjDtuKYHS Status: Ok Log Messages: Program 11111111111111111111111111111111 invoke [1] Program 11111111111111111111111111111111 success Transaction executed in slot 13959: Signature: 2sn4uLqF1aGuv9dswpMUyHAEAYBhrgh3Uzi4ydpwuxDbcUJg8d2RB18neGUHAhG2jjU1rEKytigQG2fDwpf75FMA Status: Ok Log Messages: Program 11111111111111111111111111111111 invoke [1] Program 11111111111111111111111111111111 success
    – vonyi
    Jan 2 at 18:15
  • oh yeah, Anchor is not really giving us much of the error details, just the message :( and since you're working on local those signatures are not useful either (i assume they are form other non-failing transactions though)
    – Solandy
    Jan 2 at 18:25

1 Answer 1

3

As @Solandy says, the error message coming out of the test isn't really giving you much to work from - the useful data in the error isn't present in the message that is printed out.

I'd suggest the first change you make, is to catch and log the error to get some more useful data. E.g., something like updating line 61 of tests/test_example1_lottery.ts to be .rpc().catch(e => console.error(e));.

When I do this on a fresh checkout of the repo and run anchor run test1 I see

    'Program 6p7uosLZVm41BS4PRy44Rc5gUWxASALfDhws3nDrrkcH failed: Invoked an instruction with data that is too large (12884934228 > 10240)'

This to me says that there's some library/version compatibility issues.

There are a few issues trying to bring this repo up to date to work with anchor 0.29.0.

  • TS/JS: package.json updated to use "@coral-xyz/anchor": "^0.29.0", along with each test file to correct the imports. The example2 test also needs js-sha256 which seemed to be missing from package.json yarn add js-sha256

  • Rust: Update each example's Cargo.toml to use anchor 0.29.0, anchor-lang = "0.29.0".

    • example3's usage of remaining_accounts no longer seems compatible, something like this seems to do the trick
-    pub fn tally(ctx: Context<Tally>) -> Result<()> {
+    pub fn tally<'info>(ctx: Context<'_, '_, 'info, 'info, Tally>) -> Result<()> {

         let question: &mut Account<Question> = &mut ctx.accounts.question;

         // All of the acounts not explicitely mentioned in the Tally context
-        let answers: Vec<AccountInfo> = ctx.remaining_accounts.to_vec();
+        let answers: &'info [AccountInfo] = ctx.remaining_accounts;

After all that, you should have clean anchor build, anchor deploy. anchor run test unhelpfully prints out 0 passing.

This is down to the example programs using numbers in their names, i.e., example1, example2, example3. The program names and hence IDL files generated in /target/idl are of the form e.g., example1.json, but in the test code workspace.Example1 looks for /target/idl/example_1.json thanks to this: https://github.com/coral-xyz/anchor/blob/v0.29.0/ts/packages/anchor/src/workspace.ts#L26-L41

I'm not sure why the program name/idl filename generation when building your program is different to program name/idl filename generation when using the workspaces.

Anyway, all that said, it might not be the best repo to be using as an example. Maybe if you follow some of my suggestions above, and rename the example program from example1 to example_lottery or something you'll have better luck?

1
  • 1
    Incredible. Thank you very much this made it work. Great lesson on Rust and Anchor hahah Was a bit above my current experience level but it makes sense and thank you very much for a detailed explanation.
    – vonyi
    Jan 4 at 19:55

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.