Skip to content

Instantly share code, notes, and snippets.

@hefgi
Last active August 29, 2019 15:52
Show Gist options
  • Save hefgi/bd8180b7a1fcc560b16c930d6cff3e3a to your computer and use it in GitHub Desktop.
Save hefgi/bd8180b7a1fcc560b16c930d6cff3e3a to your computer and use it in GitHub Desktop.
Ethers.js - Basic implementation
// Require ethers & axios for CommonJS
// const ethers = require('ethers')
// const axios = require('axios')
// Import ethers & axios for ES6 modules
import { ethers } from 'ethers'
import { axios } from 'axios'
// Constants for infura URL provider
const overrides = {
ethUrl: 'https://rinkeby.infura.io/v3/INFURA_TOKEN_HERE',
}
// 1 - get ETH Provider
const ethUrl = overrides.ethUrl || `http://localhost:8545` // Use Ganache localhost if overrides undefined
const ethProvider = new ethers.providers.JsonRpcProvider(ethUrl)
// Generate private key
// let wallet = ethers.Wallet.createRandom()
// let privateKey = randomWallet.privateKey
// 2 - Connect to wallet using existing private key
const privateKey = "PRIVATE_KEY_HERE"
let wallet = new ethers.Wallet(privateKey, ethProvider)
console.log('Wallet public key : ', wallet.address)
// 3 - Get balance
let balance
try {
balance = await wallet.getBalance()
} catch (e) {
console.log('Error get balance : ', e)
}
balance = ethers.utils.formatEther(balance)
console.log('balance ', balance)
// 4 - Get transaction count for nonce
// this is necessary in order to create a transaction
let transactionsCount
try {
transactionsCount = await wallet.getTransactionCount()
} catch (e) {
console.log('Error get transactions count : ', e)
}
console.log('transactionsCount ', transactionsCount)
// 5 - Get current gas price from ethgasstation
let response
try {
response = await axios.get('https://ethgasstation.info/json/ethgasAPI.json')
} catch (e) {
console.log('Error eth gas station : ', e)
}
console.log('eth gas price response', response.data)
let gasPrice = response.data.fastest/10 // Using fast here - in gwei
console.log('eth gas price ', gasPrice)
gasPrice = gasPrice * 1000000000 //in wei
gasPrice = ethers.utils.bigNumberify(gasPrice) || ethers.utils.bigNumberify("20000000000")
// 6 - Get Gas limit
// TODO: https://docs.ethers.io/ethers.js/html/api-wallet.html#blockchain-operations ---> look at estimateGas
// This is needed because smart contract wallet need more gas limit in order to process transactions
// 7 - Send transaction
const receiver = 'RECEIVER_ADDRESS'
const value = 'VALUE_IN_ETH' //in eth
const transaction = {
gasLimit: 31560, //eth only 21000 //erc-20 54000 //TODO: Should estimate gasLimit before sending transaction
gasPrice: gasPrice,
to: receiver,
value: ethers.utils.parseEther(value),
nonce: transactionsCount
}
console.log('transaction ', transaction)
try {
const signedTransaction = await wallet.sign(transaction)
const tx = await ethProvider.sendTransaction(signedTransaction)
console.log('finalTx ', finalTx)
} catch (e) {
console.log('Error send transaction : ', e)
}
console.log('Tx ', tx)
console.log('Tx hash ', tx.hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment