Skip to content

Instantly share code, notes, and snippets.

@earthchie
Created May 13, 2021 15:12
Show Gist options
  • Save earthchie/b8dc9f8cbbf842ee1975938845d96bc2 to your computer and use it in GitHub Desktop.
Save earthchie/b8dc9f8cbbf842ee1975938845d96bc2 to your computer and use it in GitHub Desktop.
Simple API to get a exchange rate from PancakeSwap V2 Router
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;
}
@mtl1979
Copy link

mtl1979 commented Jun 3, 2022

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