hashgraph / hashgraph/hedera-hardhat-example-project

Error interacting with contract: TypeError: Cannot read properties of undefined (reading 'publicKey')

Open
#5 0 comments 0 reactions 0 assignees View on GitHub
Bug
Dominant language
JavaScript
Stars
10
Forks
11
PR merge metrics
No merged PRs in 30d

Description

### Description

hey,

i've been trying to deploy my smart contract on the hedera testnet account (ECDSA), followed all the steps mentioned in the readme.md file, all the environment variables have been set as mentioned but i still keep encountering this error.

when i run `npx hardhat test` the tests pass, but when i try to interact with the contract again gives me this error:

## Issue
`node uploadToHedera.js`
`OK till here
Error interacting with contract: TypeError: Cannot read properties of undefined (reading 'publicKey')
at NodeClient.setOperator (C:\Users\bawgi\OneDrive\Documents\DEHRSys\dehrsys\node_modules\@hashgraph\sdk\lib\client\Client.cjs:286:48)
at interactWithContract (C:\Users\bawgi\OneDrive\Documents\DEHRSys\dehrsys\uploadToHedera.js:28:16)
at Object. (C:\Users\bawgi\OneDrive\Documents\DEHRSys\dehrsys\uploadToHedera.js:44:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47`

i believe the issue is occurring in the `\node_modules\@hashgraph\sdk\lib\client\Client.cjs:286:48` file.

the code that is triggering the error:
```js
const { Client, ContractCallQuery } = require("@hashgraph/sdk");

var contractAddress = "0x5c755dDFFBD0B6405DE612Bc96609d21C129917c";

async function interactWithContract(moralisHash) {
try {
const client = Client.forTestnet();
console.log("OK till here")
client.setOperator(process.env.ACCOUNT_ID, process.env.TESTNET_OPERATOR_PRIVATE_KEY);

const contract = await new ContractCallQuery()
.setContractId(contractAddress)
.setGas(30000)
.setFunction("changeHash")
.addString(moralisHash)
.execute(client);

console.log("Transaction ID:", contract.transactionId);
} catch (error) {
console.error("Error interacting with contract:", error);
}
}

const moralisHash = "https://ipfs.moralis.io:2053/ipfs/QmQvASqTFsJxNqym55KTm98FVWxwCD9cKt37ba6kYT962z/trainmedications.json";
interactWithContract(moralisHash);
```

## Environment
- @hashgraph/hedera-local: v2.18.0
- @nomicfoundation/hardhat-toolbox: v2.0.1
- dotenv: v16.3.1
- fs: v0.0.1-security
- moralis: v2.23.2

### Steps to reproduce

`node index.js` to obtain the hash by Moralis Web3 to store in the uploadToHedera.js file.
`npx hardhat compile`
`npx hardhat deploy-contract`
`npx hardhat test`
`node uploadToHedera.js`

### Additional context

_No response_

### Hedera network

testnet

### Version

Hardhat: v2.12.6

### Operating system

Windows

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.