massalabs / massa-web3

Web3 libraries for Massa
MIT License
29 stars 74 forks source link

Error when calling smart contract from js #597

Closed sebastien-forestier closed 3 months ago

sebastien-forestier commented 3 months ago

It's probably a noob problem but I can't call a smart contract with this code (replace the secret key by any of yours):

const { WalletClient, ClientFactory, Args, fromMAS, toMAS, DefaultProviderUrls, CHAIN_ID } = require('@massalabs/massa-web3');

const SC_ADDRESS = 'AS12qzyNBDnwqq2vYwvUMHzrtMkVp6nQGJJ3TETVKF5HCd4yymzJP';

async function test() {

    const baseAccount = await WalletClient.getAccountFromSecretKey('XXX');
    const web3Client = await ClientFactory.createDefaultClient(DefaultProviderUrls.BUILDNET, CHAIN_ID.BuildNet, true, baseAccount);

    const res = await web3Client.smartContracts().callSmartContract({
        maxGas: BigInt(200000), 
        coins: BigInt(1), 
        targetAddress: SC_ADDRESS, 
        functionName: "test", 
        parameter: new Args(),
        fee: fromMAS(0.01)
      })
}

test();

I get this error:

node:buffer:319
  throw new ERR_INVALID_ARG_TYPE(
        ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
    at Function.from (node:buffer:319:9)
    at Web3Account.compactBytesForOperation (/home/seb/node_modules/@massalabs/massa-web3/dist/cmd/web3/BaseClient.js:261:78)
    at Web3Account.executeOperation (/home/seb/node_modules/@massalabs/massa-web3/dist/cmd/web3/accounts/Web3Account.js:51:35)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async SmartContractsClient.callSmartContract (/home/seb/node_modules/@massalabs/massa-web3/dist/cmd/web3/SmartContractsClient.js:113:16)

I have node 22 and massa-web3 4.0.1, and I call the code in a js file with node file.js. The sc address and functionName are examples, I get the same error whatever these values.

Do you have any idea ? Thank you!

sebastien-forestier commented 3 months ago

Ok there was a change, "functionName" to "targetFunction" but the doc was not updated https://web3.docs.massa.net/interfaces/ICallData.html