Skip to content

Instantly share code, notes, and snippets.

@AlecZadikian9001
Last active May 13, 2021 14:49
Show Gist options
  • Save AlecZadikian9001/95b619318660fec8444e4983d8c58243 to your computer and use it in GitHub Desktop.
Save AlecZadikian9001/95b619318660fec8444e4983d8c58243 to your computer and use it in GitHub Desktop.
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: https://infura.io/signup
// - 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 `https://infura.io/signup`, 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(`https://mainnet.infura.io/${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');
transaction.sign(privateKeyBuf);
var serializedTx = transaction.serialize().toString('hex');
// finally send
web3.eth.sendSignedTransaction('0x' + serializedTx).then(console.log);
});
@AlecZadikian9001
Copy link
Author

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