pancakeswap.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const HttpKit = require('./kit/HttpKit')
  2. class PancakeSwap {}
  3. PancakeSwap.pairs = async function () {
  4. const url = 'https://api.pancakeswap.info/api/v2/pairs'
  5. const { data: rst } = await HttpKit.get(url)
  6. return rst
  7. }
  8. PancakeSwap.tokenList = async function () {
  9. const url = 'https://api.pancakeswap.info/api/v2/tokens'
  10. const { data: rst } = await HttpKit.get(url)
  11. return rst
  12. }
  13. PancakeSwap.tokenInfo = async function (symbol = 'WBNB', tokens) {
  14. if (!tokens) {
  15. tokens = await PancakeSwap.tokenList()
  16. }
  17. const { data: priceList } = tokens
  18. for (const key in priceList) {
  19. const token = priceList[key]
  20. if (symbol === token.symbol) {
  21. return token
  22. }
  23. }
  24. }
  25. PancakeSwap.priceByBNB = async function (symbol, list) {
  26. const token = await PancakeSwap.tokenInfo(symbol, list)
  27. return token ? token.price_BNB : 0
  28. }
  29. PancakeSwap.price = async function (symbol, list) {
  30. const token = await PancakeSwap.tokenInfo(symbol, list)
  31. return token ? token.price : 0
  32. }
  33. module.exports = PancakeSwap