首页 > 其他分享 >使用hardhat/ethers.js调用已经存在的合约

使用hardhat/ethers.js调用已经存在的合约

时间:2022-11-20 11:46:54浏览次数:76  
标签:console log await js provider hardhat ethers const

使用hre : https://hardhat.org/hardhat-runner/docs/advanced/hardhat-runtime-environment

Hardhat Runtime Environment 里边通过hardhat-ethers插件注入了一个ethers实例可以直接用。具体功能跟ethers.js是一致的。

Ethers.js相关文档:https://docs.ethers.io/v5/getting-started/#getting-started--contracts

call.js代码

我们在hardhat工程的script目录下新建call.js文件:

const main = async () => {

    //合约地址
    let contractAddress = "0x7e163122D7D0ce2f997d1AAFCb662C400F06e998"; 
    //ABI合约接口定义
    let contractABI = [
        {
          "inputs": [],
          "stateMutability": "nonpayable",
          "type": "constructor"
        },
       ......
        {
          "inputs": [
            {
              "internalType": "address",
              "name": "from",
              "type": "address"
            },
            {
              "internalType": "address",
              "name": "to",
              "type": "address"
            },
            {
              "internalType": "uint256",
              "name": "tokenId",
              "type": "uint256"
            }
          ],
          "name": "transferFrom",
          "outputs": [],
          "stateMutability": "nonpayable",
          "type": "function"
        }
      ];
    //Provider
    const quicknodeUrl = "https://bold-blue-fog.ethereum-goerli.discover.quiknode.pro/API_KEY/";
    const alchemyUrl = "https://eth-goerli.g.alchemy.com/v2/API_KEY";
    const provider = await new hre.ethers.getDefaultProvider(quicknodeUrl);  //连接网络
    //console.log(provider);

    //只读操作
    let balance = await provider.getBalance("0x4953BC5Fc7d5ABD0cD86A198A4A34EB2e3b8dC1d");
    balance = hre.ethers.utils.formatEther(balance);
    console.log("address[0x4953BC5Fc7d5ABD0cD86A198A4A34EB2e3b8dC1d] balance is: " + balance);
    let blockNumber = await provider.getBlockNumber();
    console.log("current block number: ", blockNumber);
  
    //Signer签名
    console.log("获取provider");
    const signer =  await new hre.ethers.Wallet("PRIVATE_KEY", provider);
    //let signerAddress = await signer.getAddress();
    //console.log("signer address", signerAddress);
    console.log("获取signer");
    //合约对象
    const nftContract = await new ethers.Contract(contractAddress, contractABI, provider);
    console.log("获取nftContract");
    const nftWithSinger = await nftContract.connect(signer); //合约的写方法,需要Signer
    console.log("nftContract.connect(signer)");
    //调用合约方法
    let txn = await nftWithSinger.makeAnEpicNFT();
    console.log("mint");

    await txn.wait(); //等待mint结束
    console.log("Minted NFT #1");
    console.log(txn);

  };
  
  const runMain = async () => {
    try {
      await main();
      process.exit(0);
    } catch (error) {
      console.log(error);
      process.exit(1);
    }
  };
  
  runMain();

输出:

$npx hardhat run scripts/call.js --network goerli
address[0x4953BC5Fc7d5ABD0cD86A198A4A34EB2e3b8dC1d] balance is: 0.804995619844421299
current block number:  7984396
linyangdeMacBook-Air:epic-nfts hayashihiroshi$ npx hardhat run scripts/call.js --network goerli
address[0x4953BC5Fc7d5ABD0cD86A198A4A34EB2e3b8dC1d] balance is: 0.804995619844421299
current block number:  7984414
获取provider
获取signer
获取nftContract
nftContract.connect(signer)
mint
Minted NFT #1
{
  type: 2,
  chainId: 5,
  nonce: 21,
  maxPriorityFeePerGas: BigNumber { value: "1500000000" },
  maxFeePerGas: BigNumber { value: "1500000052" },
  gasPrice: null,
  gasLimit: BigNumber { value: "94870" },
  to: '0x7e163122D7D0ce2f997d1AAFCb662C400F06e998',
  value: BigNumber { value: "0" },
  data: '0xde9d132f',
  accessList: [],
  hash: '0x9dc1040d736dfbe0ac5aa56f8d4b4d16aee7cd1ff183a1a0f0d4f60da3c888fd',
  v: 1,
  r: '0x4fad2fba284aec58c9d3f8d9238cc074ea4d0a6c2fe4b31ba15b166f29638fbd',
  s: '0x76d8635fcb8f7e8d351ee0e2ab02f51979524b9ca8b77edaf55e49c97f83b52f',
  from: '0x4953BC5Fc7d5ABD0cD86A198A4A34EB2e3b8dC1d',
  confirmations: 0,
  wait: [Function (anonymous)]
}

浏览器查看合约,可以看到多了一个交易:https://goerli.etherscan.io/address/0x7e163122d7d0ce2f997d1aafcb662c400f06e998

PS:ERC-721合约的部署真的贵,0.5个goerli ETH,但是调用mint就便宜很多、才0.00014

总结一下

1、Provider是区块链连接的抽象

Web3Provider用在浏览器上,MetaMask插件向window注入ethereum对象,这个对象作为Provider的构造参数。
在Hardhat工程里边,非浏览器环境,一般用DefaultProvider就够了。这里指定quicknode的endpoint作为入参数进行构造。

2、Signer是区块链账户的抽象

一般有Provider就可以做只读操作了,但是要做写操作需要Signer签名。
Signer是抽象类,需要具体的实现,这里用的是Wallet:

const signer = await new hre.ethers.Wallet(PRIVATE_KEY, provider);
3、获得合约对象

我们获得合约对象之后就可以用它来调用合约的方法了。

const nftContract = await new ethers.Contract(address, abi, provider);
const nftContractWithSigner = await nftContract.connect(signer);

上面一共得到了两个对象nftContractnftContractWithSigner,前者用来调用合约的查询方法,后者可以调用写方法。比如前文代码里的mint操作就需要用后者了。

标签:console,log,await,js,provider,hardhat,ethers,const
From: https://www.cnblogs.com/lyhero11/p/16908122.html

相关文章

  • JsonResult
    https://github.com/dotnet/aspnetcore/blob/39f0e0b8f40b4754418f81aef0de58a9204a1fe5/src/Mvc/Mvc.Core/src/JsonResult.cs#L13//Licensedtothe.NETFoundationu......
  • 【JS】查验电话号码(fCC)
    题目要求查验电话号码是否输入正确,总结来说,需要满足以下条件:号码中的数字应该在10-13个之间不包含()-以外的符号()要完整可以有空格如果最前面加了国家编号,限定编号......
  • js 事件循环中宏任务和微任务执行顺序
    asyncfunctionasync1(){console.log("async1start");//2awaitasync2();console.log("async1end");//6}asyncfunctionasync2(){console......
  • Node.js学习随笔
    Node.jsNode.js不是JS,但是和JavaScript的语法非常相似,是一种服务器端技术,他的竞争对手PHP/JAVA/C++/C#/PYTHON-历史上第一次一门语言可以通吃前后端-前端崛起原因之......
  • js一键通关
    1.typeof运算符和instanceof运算符以及isPrototypeOf()方法的区别typeof是一个运算符,用于检测数据的类型,比如基本数据类型null、undefined、string、number、boolean,以及......
  • js undefined null 区别
    在JavaScript中存在这样两种原始类型:Null与Undefined。这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined?Un......
  • js 右下角动态提示消息框
    js:varsheyMsg=function(box,options){this.box=this.g(box);this.setOptions(options);this.init();}sheyMsg.prototype={ae:function(e......
  • js cookie 操作
    functiongetCookie(name)...{varvalue=document.cookie;vararr1=value.split(";");for(i=0;i<arr1.length;i++)...{if(value.length=......
  • js实现树的存储和遍历
    树的概念:树这样的结构挺起来十分的吓人,实际上非常的简单,树是由一个个节点组成A//\\BCDE//\FGH我们使用数组来存储......
  • JS数据类型与对象
    <!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title></head><body><!--数据类型与对象--><script>......