【问题标题】:EthersJS send amount to a payable functionEthers JS 将金额发送到应付函数
【发布时间】:2022-10-13 09:54:55
【问题描述】:

我目前正在开发一个彩票系统,并且有一个函数允许用户通过函数调用发送一些以太币。

function enter() public payable {
        require(msg.value > .01 ether);
        players.push(payable(msg.sender));
}

我现在正在尝试设置一个名为 entry 的状态,并通过单击按钮调用此函数并将输入状态解析为事务的值。我不知道如何将以太币与交易一起发送。我的交易总是失败,或者当他们通过时没有出现在合同中。

async function enterPotNow() {
    if (!entry) return;
    if (typeof window.ethereum !== 'undefined') {
      const accounts = await window.ethereum.request({
        method: 'eth_requestAccounts',
      });
      const provider = new ethers.providers.Web3Provider(window.ethereum);
      const signer = provider.getSigner();
      const contract = new ethers.Contract(lotteryAddress, Lottery.abi, signer);
      const options = { value: ethers.utils.parseEther(entry) };
      const transaction = await contract.enterPot(options);
      await transaction.wait();
    }
}

我在其他一些帖子上看到了带有选项变量的东西,这有所帮助,但交易仍然失败。

这是 JSX

import logo from './logo.svg';
import './App.css';
import { useState, useEffect } from 'react';
import { ethers } from 'ethers';
import Lottery from './artifacts/contracts/Lottery.sol/Lottery.json';

const lotteryAddress = '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512'; //Localhost contract with hardhat

function App() {
  const [potAmount, setPotAmount] = useState();
  const [playerCount, setPlayerCount] = useState();
  const [winnerDrawn, setWinnerDrawn] = useState(false);
  const [winner, setWinner] = useState();
  const [entry, setEntry] = useState(0);

  // async function requestAccount() {
  //   await window.ethereum.request({ method: 'eth_requestAccounts' });
  // }

  // async function getPotAmount() {
  //   if (typeof window.ethereum !== 'undefined') {
  //     const provider = new ethers.providers.Web3Provider(window.ethereum);
  //     const contract = new ethers.Contract(
  //       lotteryAddress,
  //       Lottery.abi,
  //       provider
  //     );
  //     try {
  //       const data = await contract.potCount();
  //       setPotAmount(data.toString());

  //       console.log(potAmount);
  //     } catch (err) {
  //       console.error(err);
  //     }
  //   }
  // }

  async function enterPotNow() {
    if (!entry) return;
    if (typeof window.ethereum !== 'undefined') {
      const accounts = await window.ethereum.request({
        method: 'eth_requestAccounts',
      });
      const provider = new ethers.providers.Web3Provider(window.ethereum);
      const signer = provider.getSigner();
      const contract = new ethers.Contract(lotteryAddress, Lottery.abi, signer);
      const options = { value: ethers.utils.parseEther(entry) };
      const transaction = await contract.enterPot(options);
      await transaction.wait();
    }
  }

  return (
    <div className='App'>
      <div className='flex items-center justify-center min-h-screen bg-slate-100 '>
        <div className='shadow-lg rounded w-45 h-62 p-10 bg-white'>
          <button
            onClick={getPotAmount}
            className='bg-transparent hover:bg-green-500 text-green-700 font-semibold hover:text-white py-2 px-4 border border-green-500 hover:border-transparent rounded'
          >
            Get Pot
          </button>
          <p className='font-semibold text-center '>
            Money in Pot (WEI): {potAmount}
          </p>
          <input
            type='text'
            className='m-2 rounded p-1 border-solid border-2 border-slate-200  '
            placeholder='Enter amount in Wei'
            onChange={(event) => setEntry(event.target.value)}
          />
          <button
            onClick={enterPotNow}
            className='bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded'
          >
            Enter Lottery
          </button>
          <div className='flex justify-center'>
            <button className='mt-5 bg-transparent hover:bg-red-500 text-red-600 font-semibold hover:text-white py-2 px-4 border border-red-300 hover:border-transparent rounded'>
              Draw Winner
            </button>
          </div>
          <div>
            {winnerDrawn ? (
              <div className='font-bold mt-3 text-base text-gray-500 ml-2'>
                Winner: {winner}
              </div>
            ) : (
              <p className='font-bold mt-2.5 text-base text-gray-500 ml-2'>
                Draw a winner to see a Winner...
              </p>
            )}
          </div>
        </div>
      </div>
    </div>
  );
}

export default App;

我知道这都是非常基础的,但我才刚刚开始学习这些东西,而且我已经上瘾了。我希望有人可以帮助我解决我的问题:)

【问题讨论】:

  • 你遇到了什么错误?
  • 我并没有真正得到错误,在我将钱与函数调用一起发送后,钱并没有出现在合同中。不是更新了吗?

标签: reactjs ethereum ethers.js hardhat


【解决方案1】:

您需要使用您的一个帐户中的私钥作为签名者来签署交易

const signer = new ethers.Wallet("<put your private key of an account here>", provider)

【讨论】:

    猜你喜欢
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 2020-12-15
    • 1970-01-01
    • 2016-10-22
    相关资源
    最近更新 更多