truffle
truffle copied to clipboard
Truffle doesn't support ethers.js providers
Issue
In my truffle.config, I should be able to use an ethers provider and signer when I initialize a network.
Steps to Reproduce
- Try returning an ethers provider directly:
const ethers = require("ethers");
module.exports = {
networks: {
kovan: {
provider: function () {
const l1Provider = new ethers.providers.JsonRpcProvider("https://kovan.infura.io/ws/v3/" + infuraKey);
const wallet = ethers.Wallet.fromMnemonic(kovanMnemonic);
const l1Signer = wallet.connect(l1Provider);
return l1Provider;
}
- Try returning an HDWalletProvider given an ether's provider:
const ethers = require("ethers");
module.exports = {
networks: {
kovan: {
provider: function () {
const l1Provider = new ethers.providers.JsonRpcProvider("https://kovan.infura.io/ws/v3/" + infuraKey);
return new HDWalletProvider(
{ mnemonic: kovanMnemonic, providerOrUrl: l1Provider }
);
Expected Behavior
The provider should be initialized.
Actual Results
UnhandledRejections detected
Promise {
<rejected>
...
reason: 'bad response',
code: 'SERVER_ERROR',
status: 400,
headers: ...,
body: '{"jsonrpc":"2.0","error":{"code":-32600,"message":"invalid json request"}}',
requestBody: '{"method":{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params":[],"skipCache":true},"id":43,"jsonrpc":"2.0"}',
requestMethod: 'POST',
url: 'https://kovan.infura.io/v3/...'
Environment
- Operating System:
- Ethereum client:
- Truffle version (
truffle version): v5.5.13 - Ganache v^7.1.0
- node version (
node --version): 14.18.0 - npm version (
npm --version): 8.5.0
Worth noting: Ethers's notion of a "provider" is a bit different from web3's. Web3 has a separation between the provider, which provides the low-level interface, and the web3 object, which provides the high-level interface on top of it. Ethers doesn't have this separation; its "provider" objects provide both the high-level and the low-level interface. And obviously that interface is a bit different. So, this should be quite doable, but watch out for the differences!
On top of the "provider" equivocation @haltman-at mentioned, here's the web3 => ethers migration guide.
I am assuming that this is being worked on internally?
@swisstackle Not yet! Are you interested in working on this? We haven't analyzed this task in its entirety to figure out exactly what this will entail yet.
@eggplantzzz Yes. I think it will be a decent challenge for me as I am not familiar with ethers.js yet but Id love to get involved. I can start working in this tomorrow.
Thank you for your interest @swisstackle! I plan to look into this a bit next week. Let me know if you've researched it at all, otherwise I'll report back here with my findings.
@kevinweaver You can report back with your findings as I first have to finish the other feature Im working on for truffle. Im looking forward to help with this though.
I spiked on this this issue for a some time, but have to leave it as it. In an attempt to capture my findings, I put together this research doc: https://hackmd.io/bncUOJIFSMmFp9qGVVdgbg and a branch: add-ethers-provider-support
There’s not much of value in that branch, I mainly used it to poke around while I was learning what does what. That doc holds more of my learnings.
To anyone that picks up this work, start by following the reproduction steps in that issue, pull down ethers locally and point your local truffle to it to help you triangulate where the request is timing out.
That will probably point you toward setting up the new /interface-adapter/lib/adapter/ethers/index.ts file that I discuss in the research doc.
🫡
@benjamincburns are you the keeper of the current status of this issue? any latest updates on it? thanks!
New keeper of the current status is @gnidan. @benjamincburns if you are available before this gets done @gnidan welcomes your assistance.
Since this issue is specifically about using Ethers.js providers in truffle-config.js, I think we can close this as won't fix. It's not clear that there's value in supporting this, since the work to instantiate an Ethers provider in config is not any easier or less overhead than just following Truffle's own provider configuration patterns.
Leaving #5947 in this issue's place, to cover the more useful case of using Ethers.js other abstractions in tests and scripts.