2

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);
        // });
      }
}


  • 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 '19 at 17:25
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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