My Image
CoursesQuizzesProblemsContestsSmartBooks
Contest!

No results found

LOGINREGISTER
My ProgressCoursesQuizzesProblemsContestsSmartbooks
Published on 18 May 2022
How to solve the anchor src toArraylike is not a function
This is a short smartbook addressing how to solve the anchor src.toArraylike is not a function
img
Ajith Parmar
0
Like
726

 

If you are developing the solana program and passing the integer as argument in the function called by program.rpc then you may be getting the error like 

TypeError: src.toArrayLike is not a function
    at BNLayout.encode (/home/ganesh/Documents/Blockchain/solana/myproject/node_modules/@project-serum/borsh/dist/lib/index.js:37:37)
    at Structure.encode (/home/ganesh/Documents/Blockchain/solana/myproject/node_modules/buffer-layout/lib/Layout.js:1263:26)
    at BorshInstructionCoder._encode (/home/ganesh/Documents/Blockchain/solana/myproject/node_modules/@project-serum/anchor/dist/cjs/coder/borsh/instruction.js:91:28)
    at BorshInstructionCoder.encode (/home/ganesh/Documents/Blockchain/solana/myproject/node_modules/@project-serum/anchor/dist/cjs/coder/borsh/instruction.js:76:21)
    at /home/ganesh/Documents/Blockchain/solana/myproject/node_modules/@project-serum/anchor/dist/cjs/program/namespace/index.js:41:100
    at ix (/home/ganesh/Documents/Blockchain/solana/myproject/node_modules/@project-serum/anchor/dist/cjs/program/namespace/instruction.js:50:23)
    at txFn (/home/ganesh/Documents/Blockchain/solana/myproject/node_modules/@project-serum/anchor/dist/cjs/program/namespace/transaction.js:16:20)
    at Object.rpc [as updateValue] (/home/ganesh/Documents/Blockchain/solana/myproject/node_modules/@project-serum/anchor/dist/cjs/program/namespace/rpc.js:9:24)
    at TestFunc (/home/ganesh/Documents/Blockchain/solana/myproject/tests/myproject.js:28:31)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

 

This problem can be solved by converting the number to the bignumber.

the solution is provided as below :

const anchor = require('@project-serum/anchor')

/*
--------------
-----------

code body

-------------
------------------
*/

const value = new anchor.BN(40);

  let tx2 = await program.rpc.updateValue(value, {
    accounts: {
      storageAccount: account.publicKey,
    },
    // signers: [account],
  })
  console.log('Your transaction signature', tx2)

where the updateValue accpets the context and integer in the solana anchor program as given below :

pub fn update_value(ctx: Context<UpdateValue>, value : u64) -> Result<()> {
        
        let storage_account = &mut ctx.accounts.storage_account;
        storage_account.value = value;
        Ok(())

    }
Enjoyed the SmartBook?
Like
logo
contact@dapp-world.com
Katraj, Pune, Maharashtra, India - 411048

Follow Us

linkedintwitteryoutubediscordinstagram

Products

  • SmartBooks
  • Courses
  • Quizzes
  • Assessments

Support

  • Contact Us
  • FAQ
  • Privacy Policy
  • T&C

Backed By

ah! ventures

Copyright 2023 - All Rights Reserved.

Recommended from DAppWorld
img
1 May 2021
How to connect Ganache with Metamask and deploy Smart contracts on remix without
Set up your development environment with (Metamask + Ganache + Remix) and skip truffle :)
3 min read
11441
5
img
8 Jul 2021
How to interact with smart contarct from backend node js
call and send functions from backend server side using nodejs
3 min read
7959
2
img
18 Aug 2021
Send transaction with web3 using python
Introduction to web3.py and sending transaction on testnet
3 min read
6184
5
img
5 Aug 2021
Deploy Smart Contract on Polygon POS using Hardhat
how to deploy smart contracts on polygon pos chain using hardhat both mainnet and testnet ?
3 min read
5512
3