自动化调用dapp的方法一种是直接调用合约的方法进行交易,另一种是用无头浏览器通过操作dapp页面进行自动化交易。用到了puppeteer和@chainsafe/dappeteer两个库;
puppeteer:无头浏览器
@chainsafe/dappeteer: 是封装好的在无头浏览器操作MataMask插件的库文章来源:https://www.toymoban.com/news/detail-581080.html
以下是示例代码文章来源地址https://www.toymoban.com/news/detail-581080.html
const puppeteer = require('puppeteer');
const dappeteer = require('@chainsafe/dappeteer');
async function main() {
// 引入MetaMask
const browser = await dappeteer.launch(puppeteer, { metamaskVersion: 'v10.8.1' , headless: false});
const metamask = await dappeteer.setupMetamask(browser);
// 通过私钥导入账户
await metamask.importPK('账户私钥')
// 给MetaMask添加网络
await metamask.addNetwork({
networkName: "BSC-Test",
rpc: "https://data-seed-prebsc-1-s1.binance.org:8545/",
chainId: 97,
symbol: "BNB",
explorer: "https://testnet.bscscan.com/"
});
const page = await browser.newPage()
// 打开dapp的页面
let info = await page.goto('http://127.0.0.1:5502/dist/index.html')
// 授权地址操作权限
await metamask.approve()
await page.bringToFront()
let btn = await page.$('#addWhitelistBox .btn');
await page.$eval('#addWhitelist_account', el => el.value = '0x90a780054f372eEC70dEf3f1C96c1bbcd3a21336');
await btn.click()
await metamask.confirmTransaction()
await browser.close()
}
main();
到了这里,关于无头浏览器puppeteer自动调用Dapp交易的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!