DEV Community

ChristianErdtmann
ChristianErdtmann

Posted on

Web3 be the msg.sender of a functioncall to a Smart Contract - Returned error: unknown account

Hello, i trying to call the function getRewards() from this contract:
https://polygonscan.com/address/0x4AB071C42C28c4858C4BAc171F06b13586b20F30#code

This function use msg.sender to give me the rewards to my accounts.
What i need to do that iam the message sender.

This is my code at the moment:

Web3 = require('web3')
const fs = require('fs');

const web3 = new Web3("https://polygon-rpc.com")
const contractAddress = "0x4AB071C42C28c4858C4BAc171F06b13586b20F30"
const contractJson = fs.readFileSync('./abi.json')
const abi = JSON.parse(contractJson)
const mSandMaticContract = new web3.eth.Contract(abi, contractAddress)

asyncCall()
async function asyncCall() {
    await mSandMaticContract.methods.getReward().send({ from: '0x560CC2be59c5deF53431783C3583B8f9F63b7793' })
}
Enter fullscreen mode Exit fullscreen mode

Maybe this is allready correct but i get the error:

Error: Returned error: unknown account

Any idea how to solve it?

I think i need to add to from a account instead of my public key so i tried:

web3.eth.accounts.privateKeyToAccount('PRIVATE_KEY');
Enter fullscreen mode Exit fullscreen mode

but if i try to access the account with

var accounts = await web3.eth.getAccounts()
console.log(accounts[0])
Enter fullscreen mode Exit fullscreen mode

or

console.log(await web3.eth.accounts[0])
Enter fullscreen mode Exit fullscreen mode

i just get beck "undefined".

Top comments (0)