'create and sign transaction automatically with wallet instance created with a private key
I have a contract which I want to interact with automatically using a specific wallet at certain times from my web3 app. Only my wallet (A) can interact with the contract. I have an instance of said wallet created, and when I am logged in with that wallet, I can click a button on my web front end to have it pop up for me to sign.
What I need however, is for antoher user to click the button and the wallet calls the method on the contract and signs it without me having to sign it.
User A: wallet created with a privatekey which can call a method in the solidity contract. User B: has tokens from the same contract, uses them to login and do something at a webpage. can't call the method.
In short, the scenario is that user B logs into the website using their wallet, does something that ends up user A's wallet calling a method from the contract and eating the fees.
I can call the method just fine when I do
const provider = new ethers.providers.Web3Provider(window.ethereum);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const contract = new ethers.Contract(address, abi, wallet);
contract.method();
the wallet pops up and asks me to sign when logged in with that wallet. How would I go about automatically doing this, having it sign when needed without being prompted, assuming it has gas money in it?
Solution 1:[1]
Just create wallet and export your privatekey then use this code
let privateKey="your private key";
let wallet = new ethers.Wallet(privateKey);
let provider = "the provider";
let walletWithProvider = new ethers.Wallet(privateKey, provider);
Now you dont need to accept the contract transaction every time You can review documents here for more info https://docs.ethers.io/v5/api/signer/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mohamad Al Zohbie |
