Skip to content

Instantly share code, notes, and snippets.

@Sowmayjain
Last active April 29, 2020 11:04
Show Gist options
  • Save Sowmayjain/64690959985a1b47715c79f49ac79a34 to your computer and use it in GitHub Desktop.
Save Sowmayjain/64690959985a1b47715c79f49ac79a34 to your computer and use it in GitHub Desktop.
require('dotenv').config()
const Tx = require('ethereumjs-tx').Transaction;
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider(process.env.INFURA_ETH_NODE_URL))
const WALLET_PRIVATE_KEY = Buffer.from(process.env.WALLET_PRIVATE_KEY, 'hex');
const WALLET_ADDRESS = process.env.WALLET_ADDRESS
var BUILD_ADDRESS = "0x2971AdFa57b20E5a416aE5a708A8655A9c74f723";
var BUILD_ABI = [{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"uint256","name":"accountVersion","type":"uint256"},{"internalType":"address","name":"_origin","type":"address"}],"name":"build","outputs":[{"internalType":"address","name":"_account","type":"address"}],"stateMutability":"nonpayable","type":"function"}];
// build DSA
async function buildSmartAccount(authority, version, origin) {
// checks the tx count and increments by 1
const txCount = await web3.eth.getTransactionCount(authority);
console.log(`Tx count: ${txCount}`);
// build contract
const contract = new web3.eth.Contract(BUILD_ABI, BUILD_ADDRESS);
// transaction data
const txData = contract.methods.build(authority, version, origin).encodeABI();
// transaction to be sent
const rawTx = new Tx({
from: authority,
to: BUILD_ADDRESS,
data: txData,
gasLimit: web3.utils.toHex(700000),
gasPrice: web3.utils.toHex(web3.utils.toWei('11', 'gwei')),
nonce: txCount
});
// sign private key
rawTx.sign(WALLET_PRIVATE_KEY);
// serialize tx
const serializedTx = await rawTx.serialize();
const txHash = await web3.utils.sha3(serializedTx)
console.log(`https://etherscan.io/tx/${txHash}`)
var txReceipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).catch(error => console.log(error));
console.log(txReceipt);
}
buildSmartAccount(WALLET_ADDRESS, 1, "0x0000000000000000000000000000000000000000");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment