0

I built a demo project using solang solidity, and successfully deployed the contract to solana devnet This is Program ID: 6qv4P3v732FZen4Yq6tAgnpTraV12jkc5sAHpXoL7DtX

When I use @solana/web3.js to call the contract view function, the operation fails and an error occurs:

Program 6qv4P3v732FZen4Yq6tAgnpTraV12jkc5sAHpXoL7DtX failed: invalid program argument

solidity

// SPDX-License-Identifier: Apache-2.0

contract Test {
    
    uint private value = 666;

    function getValue() external view returns(uint) {
        return value;
    }
}

javascript

const web3 = require("@solana/web3.js")
const bufferLayout = require("@solana/buffer-layout")
const fs = require('fs')
const bs58 = require('bs58')

async function main() {
    
    const privateKey = await new Promise((resolve, reject) => {
        fs.readFile("./private.key", "utf-8", (err, data) => {
            err ? reject(err) : resolve(data)
        })
    })
    
    const secretKey = Buffer.from(bs58.decode(privateKey), "utf-8")
    const fromWallet = web3.Keypair.fromSecretKey(secretKey)

    console.log( "wallet.address=" , fromWallet.publicKey.toBase58() )

    const connection = new web3.Connection(web3.clusterApiUrl('devnet'))

    const { value: { blockhash } } = await connection.getLatestBlockhashAndContext()
    console.log( "blockhash=", blockhash )

    const transaction = new web3.Transaction()

    let methodStruct = {
        index: 1,
        layout: bufferLayout.struct([
            bufferLayout.u8('instruction')
        ])
    }

    let data = Buffer.alloc(methodStruct.layout.span);
    methodStruct.layout.encode({instruction: methodStruct.index}, data)
    console.log( data  )

    const programId = new web3.PublicKey('6qv4P3v732FZen4Yq6tAgnpTraV12jkc5sAHpXoL7DtX');

    transaction.add(
        new web3.TransactionInstruction({
            keys: [],
            programId: programId,
            data,
        })
    )

    console.log( await web3.sendAndConfirmTransaction(connection, transaction, [fromWallet]) )
}

main()

Test.json

{
  "version": "0.1.0",
  "name": "Test",
  "instructions": [
    {
      "name": "new",
      "accounts": [
        {
          "name": "dataAccount",
          "isMut": true,
          "isSigner": false,
          "isOptional": false
        }
      ],
      "args": []
    },
    {
      "name": "getValue",
      "accounts": [
        {
          "name": "dataAccount",
          "isMut": false,
          "isSigner": false,
          "isOptional": false
        }
      ],
      "args": [],
      "returns": "u256"
    }
  ]
}

It's so weird! I tried changing the index, but the call still failed.

1
  • 1
    Thank you all for your attention
    – Harry
    Dec 2, 2023 at 6:15

0

Your Answer

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

Browse other questions tagged or ask your own question.