kevinheavey / solana-bankrun

Superfast Solana Tests for Node.js
https://kevinheavey.github.io/solana-bankrun/
Apache License 2.0
86 stars 8 forks source link

Integration with spl-token #3

Closed metaproph3t closed 10 months ago

metaproph3t commented 10 months ago

It would be nice to be able to call the spl-token functions, such as the following:

underlyingTokenMint = await token.createMint(
      provider.connection.banksClient,
      payer,
      underlyingMintAuthority.publicKey,
      null,
      8
);

I think this would require implementing some more of the functions on web3.js' Connection, such as getMinimumBalanceForRentExemption

kevinheavey commented 10 months ago

Making createMint work as-is would require hacks for getMinimumBalanceForRentExemptMint and sendAndConfirmTransaction which would be quite ugly and not compatible with TypeScript.

I'd recommend just rebuilding the transaction like they build it:

    const transaction = new Transaction().add(
        SystemProgram.createAccount({
            fromPubkey: payer.publicKey,
            newAccountPubkey: keypair.publicKey,
            space: MINT_SIZE,
            lamports,
            programId,
        }),
        createInitializeMint2Instruction(keypair.publicKey, decimals, mintAuthority, freezeAuthority, programId)
    );

If you want to calculate the rent you can do it with (await banksClient.getRent()).minimumBalance(MINT_SIZE). Or you could just stick a large number in the lamports field if you're happy to skip the rent calculation step.

I would like if there were SPL Token helpers for solana-program-test though. Then I'd be happy to wrap them in solana-bankrun or an extension library

metaproph3t commented 10 months ago

Huh, getting a fairly non-informative error from doing (await banksClient.getRent()).minimumBalance(MINT_SIZE):

      at Context.<anonymous> (tests/conditionalVault.ts:59:35)
      at Generator.next (<anonymous>)
      at fulfilled (tests/conditionalVault.ts:28:58)
kevinheavey commented 10 months ago

Oh what about (await banksClient.getRent()).minimumBalance(BigInt(MINT_SIZE))?

metaproph3t commented 10 months ago

Tried that one, it tells me that BigInts must be explicitly converted

Turns out that BigInts and bigints are different. Ah JS, the most well-designed language

kevinheavey commented 10 months ago

Isn't MINT_SIZE just a regular number?

metaproph3t commented 10 months ago

Yep, it was something w/ BigInts but unrelated to that line. Just on one test now supported, I got test time down from 4s to 80ms - this is awesome!

There are some small footguns I've run into (e.g., you have to do context initialization in a before function rather than just inside the describe block for some reason), so I think I'll publish a gist of my test file as an example migration from anchor to bankrun when I'm done. Relatedly, how do I stop these debug and info messages from getting sent to stdout?

kevinheavey commented 10 months ago

These come from the underlying Rust libraries. Use the RUST_LOG env var to silence them e.g. RUST_LOG= jest --runInBand

metaproph3t commented 10 months ago

Got it, thx