1

This is what the logic of my code looks like

pub fn create_coin(
        ctx: Context<CreateCoin>,
        coin_bump: u8,
        _req_bump: u8,
        vault_bump: u8,
        amount: u64,
    ) -> Result<()> {
        let authority_key = ctx.accounts.initiator.key();
        // Set data for PDAs
        { 
            let coin = &mut ctx.accounts.coin.load_init()?;
            let clock: Clock = Clock::get().unwrap();
            
            coin.initiator = authority_key;
            coin.acceptor = ctx.accounts.acceptor.key();
            coin.is_flipping = false;
            coin.created_at = clock.unix_timestamp;
            coin.bump = coin_bump;

            let vault = &mut ctx.accounts.vault;
            vault.amount = amount;
            vault.bump = vault_bump;
        }

        // Transfer authority for the oracle requester to the Coin PDA
        let cpi_accounts = solrand::cpi::accounts::TransferAuthority {
            requester: ctx.accounts.requester.to_account_info(),
            authority: ctx.accounts.initiator.to_account_info(),
            new_authority: ctx.accounts.coin.to_account_info(),
            system_program: ctx.accounts.system_program.to_account_info()
        };

        let cpi_context = CpiContext::new(
            ctx.accounts.solrand_program.clone(),
            cpi_accounts
        );

        solrand::cpi::transfer_authority(cpi_context)?;

        // Transfer sol from Initiator to Vault PDA
        let ix = anchor_lang::solana_program::system_instruction::transfer(
            &ctx.accounts.initiator.key(),
            &ctx.accounts.vault.key(),
            amount,
        );

        anchor_lang::solana_program::program::invoke(
            &ix,
            &[
                ctx.accounts.initiator.to_account_info(),
                ctx.accounts.vault.to_account_info(),
                ctx.accounts.system_program.to_account_info(),
            ],
        )?;

        Ok(())
    }

and here is what my test script looks like

it('Create a coin!', async () => {
  [coinAccount, coinBump] = await anchor.web3.PublicKey.findProgramAddress(
      [Buffer.from("coin-seed"), userKeyPair.publicKey.toBuffer()],
      program.programId
      );

  [vaultAccount, vaultBump] = await anchor.web3.PublicKey.findProgramAddress(
      [Buffer.from("vault-seed"), userKeyPair.publicKey.toBuffer()],
      program.programId
      );

  console.log('Coin account: ', coinAccount.toString());
  console.log('Req account: ', reqAccount.toString());
  console.log('Vault account: ', vaultAccount.toString());

  await program.rpc.createCoin(
      coinBump,
      reqBump,
      vaultBump,
      amount,
      {
          accounts: {
              coin: coinAccount,
              vault: vaultAccount,
              requester: reqAccount,
              initiator: userKeyPair.publicKey,
              acceptor: user2KeyPair.publicKey,
              oracle: oraclePubkey,
              //oracleVault: reqVaultAccount,
              solrandProgram: solrandId,
              rent: anchor.web3.SYSVAR_RENT_PUBKEY,
              systemProgram: anchor.web3.SystemProgram.programId,
          },
          signers: [userKeyPair],
      }
  );

  let userBalance = await getBalance(provider, userKeyPair.publicKey);
  assert(userBalance < airdropAmount);

  console.log('User Balance: ', userBalance);

  await provider.connection.confirmTransaction(
      await provider.connection.requestAirdrop(reqVaultAccount, 1000000000),
      "confirmed"
  );

when i run d test,i got this error

Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x1
      at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:4480:13)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:4439:20)
      at sendAndConfirmRawTransaction (node_modules/@project-serum/anchor/src/provider.ts:286:21)
      at Provider.send (node_modules/@project-serum/anchor/src/provider.ts:120:14)
      at Object.rpc [as createCoin] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:24:16)

  2) coin-flip
       Approve a flip:
     Error: AnchorError caused by account: vault. Error Code: AccountNotInitialized. Error Number: 3012. Error Message: The program expected this account to be already initialized.
      at Function.parse (node_modules/@project-serum/anchor/src/error.ts:176:14)
      at translateError (node_modules/@project-serum/anchor/src/error.ts:285:35)
      at Object.rpc [as approveFlip] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:26:29)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

Any help on how i can solve this will be appreciated

1 Answer 1

2

So every time you see Custom Program Error Code it means it's an error defined by either Solana or Anchor.

For Solana Programs it can be usually (not all) be found here: Solana Program Errors

So for custom program error 0x1,

// Insufficient funds for the operation requested.
// This is the Custom Program Error 0x1
#[error("Insufficient funds")]

it means you have insufficient funds for the operation you requested.

Either you use solana airdrop or solana transfer to give your dev account some devnet SOL.

For anchor errors they start at error code 6000 and you can check them out here: Anchor Lang Error Codes

3
  • clarification: "custom program error" is always defined by the program that declared the instruction. it's true that anchor carves out some values for its own internal use, but Solana's runtime doesn't define any. Runtime only declares TransactionError (the top-level error type returned during tx execution), one of its variants being InstructionError (tx errors specific to a processing a specific instruction), which has it's own Custom variant, which the program defines the corresponding u32 error codes
    – trent.sol
    Jul 20, 2022 at 17:49
  • is there a way to print those custom errors, instead of just saying "custom error"? how do I identify which error is triggering the failure?
    – coco
    Jul 29, 2022 at 21:26
  • Custom Errors have respective "names" or "identifications" in the code. I identify them via that. Like this here: github.com/solana-labs/solana-program-library/blob/master/token/… . Just find the corresponding number Jul 30, 2022 at 6:09

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.