Metamask

使用 ethers.js 和 metamask 發送乙太坊交易

  • March 2, 2022

我嘗試了兩種不同的方法,並查看了 ethers.js 文件,但我一直無法弄清楚。有沒有人有什麼建議?這是在瀏覽器中。

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

而這樣…

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

我可以用下面的程式碼做到這一點:


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

截至 2022 年 3 月 2 日,我能夠做到的方式如下:

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

希望這可以幫助!

引用自:https://ethereum.stackexchange.com/questions/77028