Created
June 12, 2021 02:00
-
-
Save david-mart/364198f4e5d74e49a23a1018468c3db1 to your computer and use it in GitHub Desktop.
PancakeSwap Token Price
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@david-mart Hi, how can I see the price instantly? (Poocoin like)