【科学家养成日记#9】自动查找LP对

ericet
·
·
IPFS

之前介绍过怎样实现Pancake的swap功能,其中有个困恼我的问题是,怎么自动查找到交易对?

之前的做法是,要找到比如BNB-BUSD的交易对合约,我先提供流动,提供流动后,钱包里面就出现BNB-BUSD LP的一个代币,从这个代币就能获得LP的合约

今天了解到,原来可以利用Factory的合约来自动查找LP

用的是BSC上的Pancake Factory合约做的例子。

Factory合约里面的getPair函数会自动返回2种币的LP合约:

async function getPair(tokenA, tokenB) {
    let tokenContract = new web3.eth.Contract(factoryAbi, addresses.PANCAKE_FACTORY);
    let pair = await tokenContract.methods.getPair(tokenA, tokenB).call();
    return pair;
}

完整代码如下:

const Web3 = require('web3');
const erc20Abi = require('./ABI/erc20.json');
const factoryAbi = require('./ABI/factory.json');
const rpcUrl = 'https://bsc-dataseed.binance.org/';
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
const addresses = {
    WBNB: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
    BUSD: '0xe9e7cea3dedca5984780bafc599bd69add087d56',
    DAI: '0x1af3f329e8be154074d8769d1ffa4ee058b1dbc3',
    PANCAKE_FACTORY: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73'
}

async function getPair(tokenA, tokenB) {
    let tokenContract = new web3.eth.Contract(factoryAbi, addresses.PANCAKE_FACTORY);
    let pair = await tokenContract.methods.getPair(tokenA, tokenB).call();
    return pair;
}
//通过小数点多少位,转换对应的数据
function getWeiName(tokenDecimals = 18) {
    tokenDecimals = Number(tokenDecimals);
    let weiName = 'ether';
    switch (tokenDecimals) {
        case 3:
            weiName = "Kwei";
            break;
        case 6:
            weiName = 'mwei';
            break;
        case 9:
            weiName = 'gwei';
            break;
        case 12:
            weiName = 'microether ';
            break;
        case 15:
            weiName = 'milliether';
            break;
        case 18:
            weiName = 'ether';
            break;
        default:
            weiName = 'ether';
            break;

    }
    return weiName;
}

/**
 * 获得ERC20代币余额
 * @param {*} tokenAddress 代币的合约
 * @param {*} address 钱包地址
 * @returns ERC20代币余额
 */
const getTokenBalance = (tokenAddress, address) => {
    return new Promise(async (resolve, reject) => {
        let tokenContract = new web3.eth.Contract(erc20Abi, tokenAddress);
        let result = await tokenContract.methods.balanceOf(address).call();
        let decimals = await tokenContract.methods.decimals().call();
        let weiName = getWeiName(decimals);
        let tokenBalance = web3.utils.fromWei(result, weiName);
        resolve(tokenBalance);
    })
}

async function main() {
    let BnbBusdLp = await getPair(addresses.WBNB, addresses.BUSD);
    let BusdPerBnb = await getTokenBalance(addresses.BUSD, BnbBusdLp) / await getTokenBalance(addresses.WBNB, BnbBusdLp);
    console.log("BNB price: $"+BusdPerBnb.toFixed(2))
    let BusdDaiLp = await getPair(addresses.BUSD, addresses.DAI);
    let DaiPerBusd = await getTokenBalance(addresses.DAI, BusdDaiLp) / await getTokenBalance(addresses.BUSD, BusdDaiLp);
    console.log("BUSD price: $"+DaiPerBusd.toFixed(2))
}

main();

效果:


CC BY-NC-ND 2.0 授权

喜欢我的作品吗?别忘了给予支持与赞赏,让我知道在创作的路上有你陪伴,一起延续这份热忱!

ericetLikecoin/Desmos/Evmos/Nomic/Cerberus 验证人。喜欢分享各类脚本。 网站: https://ericet.xyz
  • 来自作者
  • 相关推荐

运行Bundlr验证人节点(新手版)

运行Teritori测试网节点

Sei激励测试网运行节点