Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,7 @@ test/hardhat-waffle-project/artifacts
test/hardhat-waffle-project/cache
test/hardhat-forked-project/artifacts
test/hardhat-forked-project/cache
test/hardhat-viem-project/artifacts
test/hardhat-viem-project/cache
dist/

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,20 @@
"README.md"
],
"devDependencies": {
"@nomicfoundation/hardhat-network-helpers": "^1.0.10",
"@nomicfoundation/hardhat-toolbox-viem": "^2.0.0",
"@nomicfoundation/hardhat-viem": "^1.0.2",
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@nomiclabs/hardhat-truffle5": "^2.0.0",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@types/chai": "^4.2.14",
"@types/chai-as-promised": "^7.1.8",
"@types/fs-extra": "^5.0.4",
"@types/mocha": "7",
"@types/node": "^8.10.38",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"dotenv": "^6.2.0",
"ethereum-waffle": "^3.2.1",
"ethers": "^5.0.0",
Expand All @@ -52,7 +57,8 @@
"tslint": "^5.16.0",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.0.1",
"typescript": "4.7.4",
"typescript": "~5.0.4",
"viem": "^1.18.0",
"web3": "^1.3.0"
},
"peerDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ npx mocha test/waffle.ts --timeout 100000 --exit
# Forked Network + HardhatEVM
npx mocha test/forked.ts --timeout 100000 --exit

# Viem + HardhatEVM
npx mocha test/viem.ts --timeout 100000 --exit

# Ethers + Hardhat Node
start_hardhatevm
npx mocha test/hardhatevm.node.ts --timeout 100000 --exit
Expand Down
21 changes: 21 additions & 0 deletions src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ export class EGRDataCollectionProvider extends ProviderWrapper {
}
}
return txHash;

// Viem
} else if (args.method === "eth_sendTransaction") {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in doubling the number of calls counted for both ethers and truffle plugins...ideally there would be a way to detect Viem and only select this route if it's being used.


const txHash = await this._wrappedProvider.request(args);

if (typeof txHash === "string") {
const tx = await this._wrappedProvider.request({
method: "eth_getTransactionByHash",
params: [txHash],
});
const receipt: any = await this._wrappedProvider.request({
method: "eth_getTransactionReceipt",
params: [txHash],
});

if (receipt?.status) {
await this.mochaConfig.attachments.recordTransaction(receipt, tx);
}
}
return txHash;
}
return this._wrappedProvider.request(args);
}
Expand Down
24 changes: 24 additions & 0 deletions test/hardhat-viem-project/contracts/Greeter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pragma solidity ^0.5.1;

contract Greeter {
string public greeting;

constructor(string memory _greeting) public {
greeting = _greeting;
}

function greet() public view returns (string memory) {
return greeting;
}

function setGreeting(string memory _greeting) public {
greeting = _greeting;
}

function asOther() public {}

function throwAnError(string memory message) public {
greeting = "goodbye";
require(false, message);
}
}
10 changes: 10 additions & 0 deletions test/hardhat-viem-project/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// We load the plugin here.
import { HardhatUserConfig } from "hardhat/types";
import "../../src/index";
import "@nomicfoundation/hardhat-viem";

const config: HardhatUserConfig = {
solidity: "0.5.8",
};

export default config;
78 changes: 78 additions & 0 deletions test/hardhat-viem-project/test/greeter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// @ts-nocheck
// tslint:disable-next-line no-implicit-dependencies
import { assert, expect, use } from "chai";
import { loadFixture } from "@nomicfoundation/hardhat-toolbox-viem/network-helpers";
import { viem } from "hardhat";
import chaiAsPromised from "chai-as-promised";

use(chaiAsPromised);

describe("Greeter contract", function() {
async function deployGreeterFixture() {
const publicClient = await viem.getPublicClient();
const [owner, other] = await viem.getWalletClients();

const deployGreeter = (args: [_greeting: string]) => viem.deployContract("Greeter", args);

const greeter = await deployGreeter(["Hi"]);

const greeterAsOther = await viem.getContractAt(
"Greeter",
greeter.address,
{ walletClient: other }
);

return {
publicClient,
owner,
other,
deployGreeter,
greeter,
greeterAsOther,
}
}

it("Should shoud be deployable with different greetings", async function() {
const {
greeter,
deployGreeter,
} = await loadFixture(deployGreeterFixture);
assert.equal(await greeter.read.greeting(), "Hi");
const greeter2 = await deployGreeter(["Hola"]);
assert.equal(await greeter2.read.greeting(), "Hola");
});

it("Should return the greeting when greet is called", async function() {
const {
greeter,
} = await loadFixture(deployGreeterFixture);
assert.equal(await greeter.read.greet(), "Hi");
});

it("Should set a greeting", async function(){
const {
greeter,
} = await loadFixture(deployGreeterFixture);
await greeter.write.setGreeting(['ciao']);
assert.equal(await greeter.read.greet(), "ciao");
})

it("Should call as other", async function(){
// NOTE: This test is to check whether gas reporter can catch calls from viem using other accounts.
// The result should be checked manually on the gas usage table.
// Expected to see a row for `asOther` method.
const {
greeterAsOther,
} = await loadFixture(deployGreeterFixture);
await greeterAsOther.write.asOther();
})

it("should revert with a message", async function(){
const {
greeterAsOther,
} = await loadFixture(deployGreeterFixture);
await expect(
greeterAsOther.write.throwAnError(['throwing...'])
).to.eventually.be.rejectedWith('throwing...');
})
});
13 changes: 13 additions & 0 deletions test/viem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TASK_TEST } from "hardhat/builtin-tasks/task-names";
// tslint:disable-next-line no-implicit-dependencies
import { assert } from "chai";
import { useEnvironment } from "./helpers";

describe("Viem plugin with signers", function() {
useEnvironment(__dirname + "/hardhat-viem-project");

it("no options", async function() {
await this.env.run(TASK_TEST, { testFiles: [] });
});
});

Loading