Skip to content

Instantly share code, notes, and snippets.

@david-mart
Created June 12, 2021 02:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-mart/364198f4e5d74e49a23a1018468c3db1 to your computer and use it in GitHub Desktop.
Save david-mart/364198f4e5d74e49a23a1018468c3db1 to your computer and use it in GitHub Desktop.
PancakeSwap Token Price
import Web3 from 'web3';
import { Fetcher, ChainId, Token } from '@pancakeswap/sdk';
import { JsonRpcProvider } from '@ethersproject/providers';
import { BNB_ADDRESS, MAINNET_URL } from '../constants';
const provider = new JsonRpcProvider('https://bsc-dataseed1.binance.org/');
class Web3Service {
constructor() {
this.web3 = new Web3(MAINNET_URL);
this.eth = this.web3.eth;
this.wallet = this.eth.accounts.wallet;
}
getTokenPrice = async (address) => {
const BNB = new Token(ChainId.MAINNET, BNB_ADDRESS, 18);
const token = await Fetcher.fetchTokenData(
ChainId.MAINNET,
address,
provider,
);
const pair = await Fetcher.fetchPairData(BNB, token, provider);
const price = pair.token0Price.toSignificant(10);
return price;
};
}
export default Web3Service;
@furkancelik
Copy link

@david-mart Hi, how can I see the price instantly? (Poocoin like)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment