-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I set the sender for a Contract? #973
Comments
You will need a signer to connect the contract too. In ethers there is a very explicit separation from a Provider (which is a read-only connection to the blockchain) and a Signer (which may write; such as sending transactions). This is one of the biggest differences people coming from Web3 people have to learn and one that is initially the most confusing. Once you are used to it though, it helps keep your code abstract and re-usable. :) If you use a JSON-RPC, the JsonRpcProvider happens to also control the Signers. To connect a contract to a specific signer, you use the You above code would look like: await contract.connect(provider.getSigner(accounts[x]).methodName(arg, { value: 1000 });
// or if you wanted many operations to be executed with the same signer, you may want to keep it around as a variable:
const contractAccountX = contract.connect(provider.getSigner(accounts[x]); The One key to help remember this is that Now it is easy in the future to swap out whatever holds the private key, whether it is a Geth instance, JSON wallet, GSN managed account, hardware wallet or whatever else you can dream up. Just use a different Signer. The provider doesn’t need to change. :) Make sense? :) |
Thank you.
|
That may be an etherlime-specific extension? I’m not sure, I don’t use etherlime, but if it works for you, awesome. :) |
I think this is resolved? If not, please re-open. Thanks! :) |
Hi @ricmoo, when we want to call a method with different account we uses .connect() for it, so what is the need of .attach()?? |
Hi,
I used to use web3.js, just now learning for ethers. In web3, when I want to test interaction from account[x], I can have following code to call a function. How should I write similar thing with eths.js?
(I am using etherlime ganache + ethers)
await contractName.methodName(arg, {value: 1000, from: accounts[x]})
The text was updated successfully, but these errors were encountered: