I want to call a contract method like this:
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract MyNFT is ERC721URIStorage {
// ...
modifier onlyRoot(address message_sender){
emit SenderAddress(message_sender);
string memory message = string.concat("caller", Strings.toHexString(uint160(message_sender), 20) , " is not the contract root.");
require(rootAddress == message_sender, message);
_;
}
function mint(address to, string calldata _uri) public onlyRoot(msg.sender) {
// ...
}
when I call the mint
method from server ( nodejs using web3 ) , it always gives me error caller 0x00000000000000000000000000000 is not the contract root.
the nodejs code is shown as below: ( the private key belongs the root address
)
const Web3 = require('web3')
const fs = require('fs')
async function main(){
const myArgs = process.argv.slice(2);
let rpc_server = myArgs[0]
let contract_address = myArgs[1]
let abi_file = myArgs[2]
let abi = JSON.parse(fs.readFileSync(abi_file))
let to = myArgs[3]
let uri = myArgs[4]
let private_key = 'c218dd579b86393d4351c2d301b99aa8ee14678247a94e64????????????????'
const web3 = new Web3(new Web3.providers.HttpProvider(rpc_server))
const signer = web3.eth.accounts.privateKeyToAccount(private_key)
web3.eth.accounts.wallet.add(signer)
const contract = new web3.eth.Contract(abi, contract_address)
let tx = contract.methods.mint(to, uri)
tx.send({ from: signer.address, gas: await tx.estimateGas() })
.once("transactionHash" , (txHash) => {
console.info("mining transaction...", txHash)
})
console.info("mined in block: ", result.blockNumber)
}
- network: fuji
so my question is : how to get correct msg.sender
?
thanks a lot!