Skip to content

Instantly share code, notes, and snippets.

@Planxnx
Last active August 2, 2021 23:18
Show Gist options
  • Select an option

  • Save Planxnx/786ddff5d2cdc60bc2ef57945f42e97d to your computer and use it in GitHub Desktop.

Select an option

Save Planxnx/786ddff5d2cdc60bc2ef57945f42e97d to your computer and use it in GitHub Desktop.
swap rate bot example
import { ethers } from "ethers";
// Node RPC for Ethereum, BSC, Matic
const jsonRPC = "https://bsc-dataseed1.ninicoin.io/";
// Pancakeswap V2 factory address
const factoryAddress = "0xBCfCcbde45cE874adCB698cC183deBcF17952812";
// Tokens address
const tokens = {
BNB: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
BUSD: "0xe9e7cea3dedca5984780bafc599bd69add087d56",
DOLLY: "0xff54da7caf3bc3d34664891fc8f3c9b6dea6c7a5",
USDT: "0x55d398326f99059ff775485246999027b3197955",
};
// BNB-BUSD LP token address at Twindex
const bnbBUSDLPAddr = "0x683c41b49d154ccc30887a66e52e3917f3839b09";
// Application Binary Interface
const abi = {
factory: [
"function getPair(address tokenA, address tokenB) external view returns (address pair)",
],
lpToken: [
"function token0() external view returns (address)",
"function token1() external view returns (address)",
"function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast)",
],
token: [
"function symbol() public view returns (string memory)",
"function decimals() public view returns (uint8)",
],
};
// create connection to Ethereum network
const provider = new ethers.providers.JsonRpcProvider(jsonRPC);
// Show BNB/BUSD swap rate at PancakeSwap by tokens addess (ใช้วิธีเอา address ไปเสริชหา LP Address เอาจาก factory contract)
(async () => {
// Create Pancakswap Contract client
const pancake = new ethers.Contract(factoryAddress, abi.factory, provider);
// Get LP Address
const pairAddr = await pancake.getPair(tokens.BNB, tokens.BUSD);
// Create LP Token Contract client
const lp = new ethers.Contract(pairAddr, abi.lpToken, provider);
// Get Tokens addresses
let [token0Addr, token1Addr] = await Promise.all([lp.token0(), lp.token1()]);
// Create Tokens Contract client
const token0 = new ethers.Contract(token0Addr, abi.token, provider);
const token1 = new ethers.Contract(token1Addr, abi.token, provider);
// Get Tokens symbol
let [token0Symbol, token1Symbol] = await Promise.all([
token0.symbol(),
token1.symbol(),
]);
// Set interval for 5 sec
setInterval(async () => {
try {
// Get LP Reserve (จำนวนเหรียญใน LP)
let reserves = await lp.getReserves();
// Calulate swap rate (เอาจำนวน token1 หารด้วย จำนวน token0)
let value = reserves[1] / reserves[0];
console.log(`Pancake: 1 ${token0Symbol} = ${value} ${token1Symbol}`);
} catch (err) {
console.error(err);
}
}, 5000);
})();
// Show BNB/BUSD swap rate at Twindex by LP Address (หากมี LP Address อยู่แล้วจะใช้วิธีนี้)
(async () => {
// Create LP token contract client
const lp = new ethers.Contract(bnbBUSDLPAddr, abi.lpToken, provider);
// Get tokens address
let [token0Addr, token1Addr] = await Promise.all([lp.token0(), lp.token1()]);
// Create tokens contract client
const token0 = new ethers.Contract(token0Addr, abi.token, provider);
const token1 = new ethers.Contract(token1Addr, abi.token, provider);
// Get tokens symbol
let [token0Symbol, token1Symbol] = await Promise.all([
token0.symbol(),
token1.symbol(),
]);
// Set Interval for 5 sec
setInterval(async () => {
try {
// Get lp reserve
let reserves = await lp.getReserves();
// calulate swap rate
let Value = reserves[1] / reserves[0];
console.log(`Twindex: 1 ${token0Symbol} = ${value} ${token1Symbol}`);
} catch (err) {
console.error(err);
}
}, 5000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment