12

I've tried two different ways, and have looked through ethers.js documentation, and I just haven't been able to figure it out. Does anyone have any suggestions? This is in a browser.

trans(web3,ethers);

async function trans(web3,ethers,provider){
    if (typeof web3 !== 'undefined') {
        var web3Provider = new ethers.providers.Web3Provider(web3.currentProvider);
        var signer = web3Provider.getSigner();

        const parameters = [
            {
              gasLimit: "0x2710",
              gasPrice: ethers.utils.parseUnits("1.0", "gwei").toHexString(),

              from: this.state.provider._web3Provider.selectedAddress,
              to: this.state.provider._web3Provider.selectedAddress,
              value: 0,
              data: "0x1"
            }
          ];

          const payload = {
            method: "eth_sendTransaction",
            params: parameters,
            from: this.state.provider._web3Provider.selectedAddress
          };

          this.state.provider._web3Provider.sendAsync(payload, function(
            err,
            response
          ) {
            if (err) {
              console.log(err);
            } else {
              console.log(response.result);
            }
          });

and this way...

trans(web3,ethers);

async function trans(web3,ethers,provider){
    if (typeof web3 !== 'undefined') {
        var web3Provider = new ethers.providers.Web3Provider(web3.currentProvider);
        var signer = web3Provider.getSigner();
        let tx = await signer.sendTransaction({
                to: 0xC046B25B3B5F960E1D7004499FBd8dc4C1BDe2f4,
                value: 1000,
        });
        let signature = await signer.signMessage("Hello world");
        // web3Provider.getBalance("0x9Ff548c1B3eA3dd123AFE39C759dDA548009B6C8").then(function(balance) {
        //   var etherString = ethers.utils.formatEther(balance);
        //   console.log("Balance: " + etherString);
        // });
      }
}


2
  • Is there any error message? It seems you are using it from a react app, can you try it with a simple html?
    – Ismael
    Nov 2, 2019 at 17:25
  • you can replace signer by other accounts ... which can be found here: [user1, user2, user3, ...addrs] = await ethers.getSigners();
    – Russo
    Feb 16, 2021 at 11:01

2 Answers 2

12

I was able to do it with this code below:


import { ethers, utils } from 'ethers';


export async function payWithMetamask(sender, receiver, strEther) {
    console.log(`payWithMetamask(receiver=${receiver}, sender=${sender}, strEther=${strEther})`)

    let ethereum = window.ethereum;


    // Request account access if needed
    await ethereum.enable();


    let provider = new ethers.providers.Web3Provider(ethereum);

    // Acccounts now exposed
    const params = [{
        from: sender,
        to: receiver,
        value: ethers.utils.parseUnits(strEther, 'ether').toHexString()
    }];

    const transactionHash = await provider.send('eth_sendTransaction', params)
    console.log('transactionHash is ' + transactionHash);
}

1
  • ncaught (in promise) Error: value must be a string (argument="value", value=10, code=INVALID_ARGUMENT, version=units/5.5.0)
    – Anonymous
    Mar 2, 2022 at 11:43
10

The way I was able to do it as of March 2, 2022 was as follow:

const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
// get a signer wallet!
const signer = provider.getSigner();

// Creating a transaction param
const tx = {
    from: DefaultAccount,
    to: "0x611E72c39419168FfF07F068E76a077588225798",
    value: ethers.utils.parseEther("0.05"),
    nonce: await provider.getTransactionCount(DefaultAccount, "latest"),
    gasLimit: ethers.utils.hexlify(10000),
    gasPrice: ethers.utils.hexlify(parseInt(await provider.getGasPrice())),
};

signer.sendTransaction(tx).then((transaction) => {
    console.dir(transaction);
    alert("Send finished!");
});

Hope this helps!

1
  • 1
    This will work but it would be better to follow the new EIP-1559 (on supported networks) instead of providing legacy gasPrice param
    – ihor.eth
    Jul 16, 2022 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.