question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I set the sender for a Contract?

See original GitHub issue

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]})

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
ricmoocommented, Jul 26, 2020

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:

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 getSigner method accepts and address or an index directly (so you could probably also just use provider.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? 😃

0reactions
MisterDefender99commented, Sep 2, 2022

Hi @ricmoo, when we want to call a method with different account we uses .connect() for it, so what is the need of .attach()??

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found