How do I set the sender for a Contract?
See original GitHub issueHi,
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]})
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to set msg.sender when reading data from a ...
The msg.sender is always the one who sends the transaction, so it will be whoever calls the function. It can also be a...
Read more >Call a contract from another contract with the same msg ...
You are correct, the call to contractA gets the address of ContractB as the msg.sender . You will need to do a delegate...
Read more >Is there a way to check if msg.sender owns some collection?
It's possible that a collection has a address public owner , especially if it inherits from Openzeppelin's Ownable library.
Read more >Msg.Sender Account | Contract Account in Solidity
Aim: to understand the difference between msg.sender account and contract account ; value section and click the “deposit” button. ; msg.sender to contract...
Read more >How do I send back ethers to the sender of the tokens in a ...
Or you can also create a payable function that allows your contract to get paid and send them to the people. A simple...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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
connect
method, which returns a new instance of a Contract object connected to the same on-chain contract.You above code would look like:
The
getSigner
method accepts and address or an index directly (so you could probably also just useprovider.getSigner(x)
.One key to help remember this is that
from
isn’t actually part of the transaction, it’s more of a hint to a JSON-RPC provider, so it doesn’t go into the transaction overrides, but needs to be set further up the chain (it’s more of a UX property).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? 😃
Hi @ricmoo, when we want to call a method with different account we uses .connect() for it, so what is the need of .attach()??