如何在 Metamask 上只确认一次然后机器人会在之后下交换订单?

问题描述

我在 HTML/JavaScript 上有这段代码; 它命令 Metamask 交换 Pancakeswap,它运行,但它需要用户的 2 次批准:第一次令牌批准和第二次交换操作。我觉得第二次可能没有必要,特别是如果我们正在制作一个机器人。

如何避免第二个?

我只想批准订单一次,然后当机器人找到交换的好机会时自动进行这笔交易......

async function swapFunction() {
        const provider = new ethers.providers.Web3Provider(window.ethereum)

        const signer = provider.getSigner()

        var signerAddress = await signer.getAddress()

        const ACCOUNT = provider.getSigner(0);

        var ROUTER_ADDR = '0x05ff2b0db69458a0750badebc4f9e13add608c7f'; //pancakeswap router

        var ROUTER_CONTRACT = await new ethers.Contract(
            ROUTER_ADDR,[
            'function getAmountsOut(uint amountIn,address[] memory path) public view returns (uint[] memory amounts)','function swapExactTokensForTokens(uint amountIn,uint amountOutMin,address[] calldata path,address to,uint deadline) external returns (uint[] memory amounts)',],ACCOUNT
            );

        console.log(ROUTER_CONTRACT,"<< ROUTER_CONTRACT")

        var BASE = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c";
        var TOKEN = "0xe9e7cea3dedca5984780bafc599bd69add087d56";

        var BASE_CONTRACT = new ethers.Contract(
            BASE,[
            'function approve(address spender,uint value) external returns (bool)','function decimals() public view returns (uint8)','function balanceOf(address account) external view returns (uint256)','function allowance(address _owner,address spender) external view returns (uint256)'
            ],ACCOUNT
            );

        console.log(BASE_CONTRACT,"<< BASE_CONTRACT")
        var amountIn_weiBN = ethers.utils.parseEther("0.001");
        var gasLimit = 300000;
        var gasPrice_weiBN = ethers.utils.parseUnits(`${5}`,'gwei');;
        var approved = false

        // ******************  Here Metamask ask to Click Confirmation 1st time. *******************
        var approved = await BASE_CONTRACT.approve( ROUTER_ADDR,amountIn_weiBN,{
                                                'gasLimit': gasLimit,'gasPrice': gasPrice_weiBN 
                                                });

        console.log(approved,"<< approved")
        
        var routeramountsOutBN = await ROUTER_CONTRACT.getAmountsOut( amountIn_weiBN,[BASE,TOKEN]);
        
        console.log(routeramountsOutBN,"<<< routeramountsOutBN")
       
       // .... Here will come some bot logics .... 
       // How to avoid this second time Confirmation ??
       // ******************  Here Metamask ask to Click Confirmation 2nd time. *******************
        var tx = await ROUTER_CONTRACT.swapExactTokensForTokens(
                      routeramountsOutBN[0],routeramountsOutBN[1],TOKEN],signerAddress,Date.Now() + 1000 * 60 * 10,//10 minutes
                      {
                        'gasLimit': gasLimit,'gasPrice': gasPrice_weiBN
            })
        
        console.log(tx,"<<< tx")
        
      }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...