簡體   English   中英

Solidity 眾籌 function 在我的 web 頁面上失敗,但在直接發送時成功,但在成功交易時不發送 ERC20

[英]Solidity crowdsale function fails on my web page but is successful when sent directly but doesn't send ERC20 on successful transaction

我有一個眾籌合約,它在我的代幣合約中直接從鑄幣廠 function 接收代幣。function 鑄幣並發送到眾籌賬戶地址。 當我跑步時

let token = await Token.deployed()

await token.mint('0xc2646F5bcB2B59a3AB3E6ccD1806D8be241C4A94',50000000000000)

在松露控制台中。 我得到一個 tx hash 和一個傳輸事件。 之后,我向眾籌賬戶進行交易,我測試到眾籌地址的交易,它使用 21000 氣體

web3.eth.sendTransaction({ to: "0x7B012920910A2A29673A15b24335617bbd2CF451", from: accounts[0], value: 2})

它返回一個 tx hash 並且它可以工作。 當我通過 metamask 嘗試並將交易發送到眾籌時,我將 gaslimit 指定為 200,000,我讀到這是眾籌合約的推薦金額。 我發送了 1 個以太幣,它說等待幾秒鍾,然后說成功。 當我點擊與眾籌合約交互的網頁時,交易失敗

在此處輸入圖像描述

當我查看 metamask 中的交易詳細信息時,它說 6385876 是失敗交易的氣體限制。 我的 Crowdsale 合同如下所示。

pragma solidity ^0.5.0;

import "./SafeMath.sol";
import "./Token.sol";

contract Own {

    address public owner;

    constructor() public {
        owner = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }
}


// rate is 1 token = 0.01 ether

contract Crowdsale is Own, Token {
    using SafeMath for uint256;

    constructor() public payable{
        rate = 10000000000000000;
        wallet = 0xAa6f0507bF7baaB35E2f0Bb9a318E1D0F61F371b;
    }

    Token public token;
    address payable public wallet;
    uint256 public rate;

    event TokenPurchase(address recipient, uint256 numPaid, uint256 numTokensPurchased);

    function buyTokens() public payable {
        // Define a uint256 variable that is equal to the number of wei sent with the message.
        uint256 val = msg.value;
        require(msg.sender != address(0));
        require(val > 0);
        uint256 tokenAmount = _getTokenAmount(val);
        require(token.balanceOf(address(this)) >= tokenAmount);
        token.transferFrom(address(this), msg.sender, tokenAmount);
        emit TokenPurchase( msg.sender, val, tokenAmount);
        _forwardFunds();
    }
    
    function () external payable{
        buyTokens();
    }

    function _getTokenAmount(uint256 weiVal) internal returns (uint256) {
        return weiVal * rate;
    }

    function _forwardFunds() internal {
        transferFrom.(address(this), address(wallet), address(this).balance);
    }
}

前端是用react寫的。 我已經實例化了 web3 object,就像在另一個成功發送事務的頁面中一樣。

  const accounts = await MyWeb3.getInstance().getAccounts();
  console.log(accounts);
  const crowdsale = MyWeb3.getInstance().getContract(Crowdsale);
  const crowdsaleInstance = await MyWeb3.getInstance().deployContract(crowdsale);
  console.log(crowdsaleInstance);
  const res = crowdsaleInstance.eth.sendTransaction({ to: "0x7B012920910A2A29673A15b24335617bbd2CF451", from: accounts[0], value: ether})//.estimateGas({gas: 200000})

我通過元掩碼直接發送到眾籌地址而不是在 web 頁面上的交易有效,但它們不會將任何 ERC20 代幣發送到買家地址。 當我將我的自定義令牌添加到元掩碼時,它會識別它並制作一個錢包,但余額保持在 0。它應該轉發資金,但它也沒有這樣做。 它只是將以太幣發送到 Crowdsale 合約地址,資金就留在 ganache 賬戶的頂部。

這是我項目的最后一部分,我已經在這個問題上停留了 3 天。 我想不通。 我真的需要幫助來理解問題以及如何解決它。 謝謝

首先,使buyTokens() function 工作,然后嘗試回退function。 為此,請檢查值:

  1. msg.value通過require
require(val > 0);
  1. Crowdsale 合約的代幣余額,通過require
require(token.balanceOf(address(this)) >= tokenAmount);

注意:您可以在require中添加一條消息以了解它落在哪里。

  1. 最重要的是,以下代碼的含義:
function _forwardFunds() internal {
  transferFrom.(address(this), address(wallet), address(this).balance);
}

也許你需要:

function _forwardFunds() internal {
  wallet.transfer(address(this).balance);
}
function _getTokenAmount(uint256 weiVal) internal returns (uint256) {
        return weiVal * rate;
    }

難道不應該

function _getTokenAmount(uint256 weiVal) internal returns (uint256) {
        return weiVal / rate;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM