typescript - 使用@nomiclabs/hardhat-waffle 实现装置

标签 typescript ethereum solidity waffle hardhat

在官方的 waffle 文档中,您可能会找到下一个实现装置的方法:

import {expect} from 'chai';
import {loadFixture, deployContract} from 'ethereum-waffle';
import BasicTokenMock from './build/BasicTokenMock';

describe('Fixtures', () => {
  async function fixture([wallet, other], provider) {
    const token = await deployContract(wallet, BasicTokenMock, [
      wallet.address, 1000
    ]);
    return {token, wallet, other};
  }

  it('Assigns initial balance', async () => {
    const {token, wallet} = await loadFixture(fixture);
    expect(await token.balanceOf(wallet.address)).to.equal(1000);
  });

  it('Transfer adds amount to destination account', async () => {
    const {token, other} = await loadFixture(fixture);
    await token.transfer(other.address, 7);
    expect(await token.balanceOf(other.address)).to.equal(7);
  });
});
但是,这在安全帽上使用插件时不起作用。插件文档中没有给出官方说明。
下面回答。

最佳答案

尽管您可以通过“Alt + 单击”在每个变量上找到自己的解决方案,直到提出正确的类型结构,但最好使用以下代码段:
以下适用于 Typescript,如果您想在 javascript 上使用它,只需切换到使用“require()”导入以及摆脱类型:

    import {Wallet, Contract} from "ethers";
    import {MockProvider} from "ethereum-waffle";
    import {ethers, waffle} from "hardhat";
    const {loadFixture, deployContract} = waffle;


//Contract ABI
// For typescript only!
// In order to be able to import .json files make sure you tsconfig.json has set "compilerOptions" > "resolveJsonModule": true. My tsconfig.json at the bottom!
//For obvious reasons change this to the path of your compiled ABI

  import * as TodoListABI from "../artifacts/contracts/TodoList.sol/TodoList.json";

    //Fixtures
  async function fixture(_wallets: Wallet[], _mockProvider: MockProvider) {
    const signers = await ethers.getSigners();
    let token: Contract = await deployContract(signers[0], TodoListABI);
    return {token};
  }
然后,在你的摩卡茶测试中
it("My unit test", async function () {
    const {token} = await loadFixture(fixture);
    // Your code....
  });
我这个安全帽项目的 tsconfig.json
{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "dist",
    "resolveJsonModule": true
  },
  "include": ["./scripts", "./test"],
  "files": ["./hardhat.config.ts"]
}

关于typescript - 使用@nomiclabs/hardhat-waffle 实现装置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68182729/

相关文章:

ruby-on-rails - 将以太坊节点绑定(bind)到Rails应用程序中,得到 'JSON::ParserError: 776'

node.js - npm install solc 和 solc-cli 但在 ubuntu 中失败

javascript - 在solidity文件中查找结构

ethereum - Solidity 和 Web3js : EIP712 signing uint256 works signing uint256[] does not

javascript - bytes32 到 javascript 中的字符串

javascript - 在 Ionic 3 应用程序上放置 angular-cli.json 内容的位置

Angular2 EventEmitter 触发父组件

angular - 如何修复错误类型错误 : Class constructor HammerGestureConfig cannot be invoked without 'new'

当数字有 3 位小数并固定为 2 位小数时,Javascript number.toFixed() 不起作用

next.js - 在 NextJS 应用程序中导入 web3 时出现 Netlify 构建错误 - 'Error: Can' t 解析 'electron' ...'