Created
May 13, 2021 15:12
-
-
Save earthchie/b8dc9f8cbbf842ee1975938845d96bc2 to your computer and use it in GitHub Desktop.
Simple API to get a exchange rate from PancakeSwap V2 Router
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
const express = require('express'); | |
const { ethers } = require('ethers'); | |
const app = express(); | |
const port = 3000; | |
const provider = new ethers.providers.JsonRpcProvider('https://bsc-dataseed.binance.org/'); | |
const contract = { | |
factory: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73', // PancakeSwap V2 factory | |
router: '0x10ED43C718714eb63d5aA57B78B54704E256024E', // PancakeSwap V2 router | |
}; | |
const tokens = { | |
BUSD: '0xe9e7cea3dedca5984780bafc599bd69add087d56', | |
SCZ: '0x39f1014a88c8ec087cedf1bfc7064d24f507b894', | |
DOP: '0x844fa82f1e54824655470970f7004dd90546bb28', | |
}; | |
const router = new ethers.Contract( | |
contract.router, | |
['function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)'], | |
provider | |
); | |
app.get('/getPrice', (req, res) => { | |
let token = req.query.token; | |
if(token.indexOf('0x') === -1){ | |
token = tokens[token.toUpperCase()]; | |
} | |
if(token) { | |
getPrice(token, tokens.BUSD).then(price => { | |
res.json({ | |
success: true, | |
BUSD: price | |
}); | |
}); | |
}else{ | |
res.status(400).json({ | |
success: false, | |
description: 'missing `token` parameter' | |
}); | |
} | |
}); | |
app.listen(port, () => { | |
console.log(`getPrice listening at http://localhost:${port}`) | |
}); | |
async function getPrice(inputCurrency, outputCurrency){ | |
const amounts = await router.getAmountsOut(ethers.utils.parseUnits('1', 18), [inputCurrency, outputCurrency]); | |
return amounts[1].toString()/1e18; | |
} |
I already noticed this one gives slightly different results as shown in the liquidity pool... But liquidity pool also includes the accumulated transaction fees debited to liquidity provider.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow thanks!
By the way, please also check this gist.
https://gist.github.com/earthchie/bc528f3bcbc1ef00b43c40ab9238cbd2
It is an another implementation to get price and more accurate than this gist.