There's a number of questions and answers about calling contract methods via web3, but they are either outdated (i.e. for web3 0.x while 1.x has many breaking changes) and/or don't include signing (1, 2, 3). The first steps, as far as I understand, should be these:

import Web3 from "web3"
import { AbiItem } from "web3-utils"

;(async() => {
    const provider = new Web3.providers.HttpProvider(...)
    const web3 = new Web3(provider)

    // see https://ethereum.stackexchange.com/a/98149/40080 and https://github.com/ChainSafe/web3.js/issues/3310#issuecomment-997396686
    const abi = ... as AbiItem[] // imported from a file
    const methodName = ...
    const callParameters = [
    const contractAddress = ...
    const universeContract = new web3.eth.Contract(abi, contractAddress)

    const call = universeContract.methods[methodName](callParameters)

but then I also have to sign this with private key and send, and this is what I haven't achieved yet. According to this answer, I should be using eth.accounts.sign(data, privateKey) (since I have to do this without a node), but I have no idea what data I should extract from call and how (and how to further send it). Can somebody show how to do this or at least give some pointers?

1 Answer 1


Assuming you have a proper Web3 instance setup, the below is how I generally make contract calls from the server using a class (or where you have a fixed key that requires no user input). I use an instance of the Hyperledger Besu version of Ethereum, so if you use a different provider it might vary slightly. Also, if you are asking the user to sign the transaction, it's a different process, but from your question it sounded like you are using a fixed key without input.

this.contract = new this.web3.eth.Contract(abi, contractAddress);

let tx = {
   from: fromAddress,
   to: this.contract.options.address,
   gasPrice: gasPrice,
   gas: gasAmount,
   data: await this.contract.methods.methodName(param1, param2, ...).encodeABI()

let signedTx = await this.web3.eth.accounts.signTransaction(tx, privateKey);
let result = await this.web3.eth.sendSignedTransaction(signedTx.rawTransaction);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.