Skip to content

Instantly share code, notes, and snippets.

@togosh
Created November 3, 2021 07:46
Show Gist options
  • Save togosh/2ae7a3b1d01d61140a6879bd069cc437 to your computer and use it in GitHub Desktop.
Save togosh/2ae7a3b1d01d61140a6879bd069cc437 to your computer and use it in GitHub Desktop.
Ethereum Price and Gas Fees
var priceURL = "https://api.etherscan.io/api?module=stats&action=ethprice&apikey=YourApiKeyToken"
var gasURL = "https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey=YourApiKeyToken";
test();
async function test(){
var price = await getEthereumPrice();
await sleep(5000);
var {low, average, high} = await getGas();
console.log("Ethereum Price: $" + price);
console.log("ERC20 Transfer: $" + average * 65000 / 1000000000 * price);
console.log("Uniswap Swap: $" + average * 200000 / 1000000000 * price);
console.log("Add Liquidity: $" + average * 175000 / 1000000000 * price);
}
async function getEthereumPrice(){
return await fetch(priceURL, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(res => {
console.log(res);
return res.result.ethusd;
});
}
async function getGas(){
return await fetch(gasURL, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(res => {
console.log(res);
return {
low: res.result.SafeGasPrice,
average: res.result.ProposeGasPrice,
high: res.result.FastGasPrice
};
});
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment