Edw*_*ova 5 waffle typescript ethereum solidity hardhat
在官方华夫饼文档中,您可能会找到实现装置的下一种方法:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
但是,在安全帽上使用该插件时这将不起作用。插件文档没有给出官方说明。
回答如下。
尽管您可以通过在每个变量上“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};
}
Run Code Online (Sandbox Code Playgroud)
然后,在你的摩卡茶测试中
it("My unit test", async function () {
const {token} = await loadFixture(fixture);
// Your code....
});
Run Code Online (Sandbox Code Playgroud)
我的这个安全帽项目的 tsconfig.json
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "dist",
"resolveJsonModule": true
},
"include": ["./scripts", "./test"],
"files": ["./hardhat.config.ts"]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3301 次 |
| 最近记录: |