reason: 'Contract with a Signer cannot override from', #3327
-
Ethers Version5.6.9 Search Termscallstatic Describe the ProblemI try to use const contractWETH = new ethers.Contract(addressWETH, abiWETH, signer)
const tx = await contractWETH.callStatic.transfer("vitalik.eth", ethers.utils.parseEther("100000"), {from: "vitalik.eth"}) I expect it to work, but it throws following
If I change Code Snippetconst contractWETH = new ethers.Contract(addressWETH, abiWETH, signer)
const tx = await contractWETH.callStatic.transfer("vitalik.eth", ethers.utils.parseEther("100000"), {from: "vitalik.eth"}) Contract ABIconst abiWETH = [
"function balanceOf(address) public view returns(uint)",
"function transfer(address, uint) public returns (bool)",
]; ErrorsError: Contract with a Signer cannot override from (operation="overrides.from", code=UNSUPPORTED_OPERATION, version=contracts/5.6.2)
at Logger.makeError (/Users/liang/Documents/Fight for PHD/work/MiniSolidity/repo/WTFEthers/node_modules/@ethersproject/logger/lib/index.js:233:21)
at Logger.throwError (/Users/liang/Documents/Fight for PHD/work/MiniSolidity/repo/WTFEthers/node_modules/@ethersproject/logger/lib/index.js:242:20)
at /Users/liang/Documents/Fight for PHD/work/MiniSolidity/repo/WTFEthers/node_modules/@ethersproject/contracts/lib/index.js:175:48
at step (/Users/liang/Documents/Fight for PHD/work/MiniSolidity/repo/WTFEthers/node_modules/@ethersproject/contracts/lib/index.js:48:23)
at Object.next (/Users/liang/Documents/Fight for PHD/work/MiniSolidity/repo/WTFEthers/node_modules/@ethersproject/contracts/lib/index.js:29:53)
at /Users/liang/Documents/Fight for PHD/work/MiniSolidity/repo/WTFEthers/node_modules/@ethersproject/contracts/lib/index.js:23:71
at new Promise (<anonymous>)
at __awaiter (/Users/liang/Documents/Fight for PHD/work/MiniSolidity/repo/WTFEthers/node_modules/@ethersproject/contracts/lib/index.js:19:12)
at /Users/liang/Documents/Fight for PHD/work/MiniSolidity/repo/WTFEthers/node_modules/@ethersproject/contracts/lib/index.js:172:63
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
reason: 'Contract with a Signer cannot override from',
code: 'UNSUPPORTED_OPERATION',
operation: 'overrides.from'
} Environmentnode.js (v12 or newer) Environment (Other)No response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
This is very intentional. A contract attached to a Signer performs all its operations (including reads) as that Signer. This prevents mistakes from accidentally passing in A contract connected to a provider has no strong attachment to a specific account, so it allows overriding it. Another method I would recommend is using When using connect with a string, a VoidSigner is automatically created and attached to the new instance. Does that make sense? :) |
Beta Was this translation helpful? Give feedback.
-
@ricmoo @AmazingAng I'm having the same problem. |
Beta Was this translation helpful? Give feedback.
-
This is my code
|
Beta Was this translation helpful? Give feedback.
This is very intentional.
A contract attached to a Signer performs all its operations (including reads) as that Signer. This prevents mistakes from accidentally passing in
from
for a request and getting a result that would not reflect that Signer.A contract connected to a provider has no strong attachment to a specific account, so it allows overriding it.
Another method I would recommend is using
contract.connect(fromAddress).transfer.staticCall(…)
. This highlights the value of locking in a Signer; the instance returned fromcontract.connect(fromAddress)
can now be passed to another function which accepts a Contract object, and all operations performed by that function would “creep” outs…