Skip to content

Instantly share code, notes, and snippets.

View bhavya2611's full-sized avatar

Bhavya Mehta bhavya2611

View GitHub Profile
import Web3 from "web3";
import { abi as IUniswapV2Pair } from "@uniswap/v2-core/build/IUniswapV2Pair.json";
import { BigNumber } from "bignumber.js";
import { ChainId, Token, WETH, Fetcher, Route } from "@uniswap/sdk";
// Create a new Web3 Instance
const web3 = new Web3(window.ethereum);
// Replace the addresses to point to your Farming Contract
// and LP Token Contract on the desired network
// Create a new Web3 Instance
const web3 = new Web3(window.ethereum);
// Replace the addresses to point to your Farming Contract
// and LP Token Contract on the desired network
const FARMING_CONTRACT_ADDRESS = "0xeF682Ae89e74BcBB3fC3B24B6b3e************"
const LP_TOKEN_ADDRESS = "0x9ba8dd47C8a85aD45aB7dab82727************"
const calculateAPY = async () => {
try {
//BLOCKS_PER_DAY varies acccording to network all values are approx and they keep changing
//BLOCKS_PER_DAY = 21600 for Kovan Testnet
//BLOCKS_PER_DAY = 28800 for BSC Testnet
//BLOCKS_PER_DAY = 6400 for Ethereum Mainnet
//I am using the value for Ethereum mainnet
const BLOCKS_PER_YEAR = 6400 * 365;
let rewardTokenPrice = 0;
const calculateLpTokenPrice = async () => {
let rewardTokenPrice = 0;
// For Price IN ETH
// Reward Token is Dodgecoin in our case
rewardTokenPrice = await getDogecoinPriceInETH();
// For Price in BNB
// If you want to do calculations in BNB uncomment line below and comment line number 5
// rewardTokenPrice = await getDodgecoinPriceInBNB()
const getLpTokenTotalSupply = async () => {
try {
const LpTokenContract = new web3.eth.Contract(
IUniswapV2Pair,
LP_TOKEN_ADDRESS
);
const totalSupply = await LpTokenContract.methods.totalSupply().call();
return totalSupply;
} catch (e) {
console.log(e);
const getLpTokenReserves = async () => {
try {
const LpTokenContract = new web3.eth.Contract(
IUniswapV2Pair,
LP_TOKEN_ADDRESS
);
const totalReserves = await LpTokenContract.methods.getReserves().call();
// For ETH/DOGE Pool totalReserves[0] = ETH Reserve and totalReserves[1] = DOGE Reserve
// For BNB/DOGE Pool totalReserves[0] = BNB Reserve and totalReserves[1] = DOGE Reserve
return [totalReserves[0], totalReserves[1]];
// Get DOGECOIN price in ETH
const getDogecoinPriceInETH = async () => {
try {
const DOGECOIN = new Token(
ChainId.MAINNET, //ChainId for Ethereum Mainnet
"0x4206931337dc273a630d328da6441786bfad668f", //DOGECOIN address on Ethereum Mainnet
8 //Number of Decimals
);
const pair = await Fetcher.fetchPairData(DOGECOIN, WETH[DOGECOIN.chainId]);
const route = new Route([pair], WETH[DOGECOIN.chainId]);
import Web3 from "web3";
import { abi as IUniswapV2Pair } from "@uniswap/v2-core/build/IUniswapV2Pair.json";
import { BigNumber } from "bignumber.js";
import { ChainId, Token, WETH, Fetcher, Route } from "@uniswap/sdk";