Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Interacting with an Ethereum contract on mainnet with your own wallet but a third-party node
// Finally figured out how to interact with contracts without running a Geth node or any kind of daemon on my machine.
// Running a Geth node was way harder than I thought. It worked sometimes, randomly broke other times... I lost interest.
// Prerequisites:
// - web3-js 1.0.0, NOT the older versions that have different APIs.
// - An Ethereum wallet for which you have the private key.
// - A public web3-rpc-compatible node. Infura is an easy one:
// - You don't trust the node with your wallet private key
// but do trust that it won't lie to you (maybe you shouldn't).
// WARNING: I'm not an expert. I know this works, and it's secure AFAIK (besides above warnings), but I could be wrong.
// also the code style is gonna suck cause I don't really know JS, nor do I care cause I assume you're going to adapt it yourself anyway
// TODO edit these:
var YOUR_INFURA_TOKEN = "..."; // first set up a free account at ``, or use some other web3-rpc-compatible node
var YOUR_WALLET_ADDRESS = "..."; // your contract address, not sensitive
var privateKey = "..."; // the private key of your wallet; load this securely somehow
var YOUR_CONTRACT_ABI = "..."; // output from contract compiler
var YOUR_CONTRACT_ADDRESS = "..."; // the address of your deployed contract
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
var web3 = new Web3(new Web3.providers.HttpProvider(`${YOUR_INFURA_TOKEN}`)); // TODO edit me if you don't use Infura
var from_addr = YOUR_WALLET_ADDRESS;
var contract_abi = YOUR_CONTRACT_ABI;
var contract_addr = YOUR_CONTRACT_ADDRESS;
// get the contract instance
var contract = new web3.eth.Contract(contract_abi, contract_addr)
// call `myMethod` method on contract instance
var method_call_abi = contract.methods.myMethod(arg1, arg2, ...).encodeABI(); // TODO edit me
web3.eth.getTransactionCount(from_addr).then(txCount => { // get tx count of my wallet...
// build transaction dict
const txData = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(250000), // TODO edit me (use gas estimation maybe?)
gasPrice: web3.utils.toHex(10e8), // 1 Gwei // TODO edit me (use gas price estimation maybe?)
to: contract_addr,
from: from_addr,
data: method_call_abi,
// serialize and sign transaction
var transaction = new Tx(txData);
var privateKeyBuf = new Buffer(privateKey, 'hex');
var serializedTx = transaction.serialize().toString('hex');
// finally send
web3.eth.sendSignedTransaction('0x' + serializedTx).then(console.log);

This comment has been minimized.

Copy link

@JamesKnippel JamesKnippel commented Jun 5, 2018

Cool approach! Was looking for a non ether-js solution. Will be trying this out!


This comment has been minimized.

Copy link
Owner Author

@AlecZadikian9001 AlecZadikian9001 commented Dec 6, 2019

Glad you like it. I have relied upon this, so it's somewhat battle-tested, and at the time I couldn't find any other way as clean as this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment