Solidity

無論我設置多高,都超過了 Remix Gas 限制

  • March 21, 2022

我正在 Remix 中部署眾售合約,但無論我將 gas 限制設置多高(一次將其設置為 40000000),甚至我更改值欄位多少,我都無法讓該合約不超過 gas 限制。我部署的另一個契約要大得多,而且部署得很好,所以我認為我的程式碼有問題。這裡是

pragma solidity ^0.4.19;

/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
 function mul(uint256 a, uint256 b) internal constant returns (uint256) {
   uint256 c = a * b;
   assert(a == 0 || c / a == b);
   return c;
 }

 function add(uint256 a, uint256 b) internal constant returns (uint256) {
   uint256 c = a + b;
   assert(c >= a);
   return c;
 }
}

/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {

 address public owner;

 /**
  * @dev The Ownable constructor sets the original `owner` of the contract to the sender
  * account.
  */
 function Ownable() {
   owner = msg.sender;
 }

 /**
  * @dev Throws if called by any account other than the owner.
  */
 modifier onlyOwner() {
   require(msg.sender == owner);
   _;
 }

 /**
  * @dev Allows the current owner to transfer control of the contract to a newOwner.
  * @param newOwner The address to transfer ownership to.
  */
 function transferOwnership(address newOwner) onlyOwner {
   require(newOwner != address(0));
   owner = newOwner;
 }
}

/**
* @title Token
* @dev API interface for interacting with the BQPOT Token contract 
*/
interface Token {
 function transfer(address _to, uint256 _value) returns (bool);
 function balanceOf(address _owner) constant returns (uint256 balance);
}

contract Crowdsale is Ownable {

   using SafeMath for uint256;

   Token token;

   uint256 public constant RATE = 1000; //Tokens per Ether
   uint256 public constant CAP = 73; //Cap in Ether
   uint256 public constant START = 1517383764; //Must change for timestamp
   uint256 public DAYS = 1; //Preorder lasts for 14 DAYS

   bool public initialized = false;
   uint256 public raisedAmount = 0;

   // Who bought tokens and how many?
   event BoughtTokens(address indexed to, uint256 value);

   //Is the sale active?
   modifier whenSaleIsActive() {
       assert(isActive());
       _;
   }
   //Pass in token contract address
   function Crowdsale(address _tokenAddr){
       require(_tokenAddr !=0);
       token = Token(_tokenAddr);
   }
   //Makes sure there is correct amount of tokens sent to address
   function initialize(uint256 numTokens) onlyOwner {
       require(initialized == false);
       require(tokensAvailable() ==numTokens); //Make sure tokens are available
       initialized = true; //If they check out set initialize to true
   }

   function isActive() constant returns (bool) {
       return (
           initialized == true && //Is contract initialized?
           now >= START && //To be active it must be after the start date
           now <= START.add(DAYS * 1 days) && //Before end date
           goalReached() == false //Also Goal is not yet reached 
           );
       }
   function goalReached() constant returns (bool) {
       return (raisedAmount >= CAP * 1 ether);
   }

   function () payable {
       buyTokens();
   }

   function buyTokens() payable whenSaleIsActive{

       uint256 weiAmount = msg.value;
       uint256 tokens = weiAmount.mul(RATE);

       BoughtTokens(msg.sender, tokens);
       //Add to amount raised 
       raisedAmount = raisedAmount.add(msg.value);

       //Send buyer tokens
       token.transfer(msg.sender, tokens);

       //Send ether to owner
       owner.transfer(msg.value);
   }

   //Return number of tokens allocated to contract
   function tokensAvailable() constant returns (uint256) {
       return token.balanceOf(this);
   }

   //Stop contract and refund the owner
   function destroy() onlyOwner {
       //Send rest of tokens to the owner
       uint256 balance = token.balanceOf(this);
       assert(balance > 0);
       token.transfer(owner, balance);

       //Incase ether is left in contract
       selfdestruct(owner);

   }
}

我不確定,但可能是這樣的:

部署實例時,請遵循以下說明:

token = Token(_tokenAddr);

但是 Token 定義如下:

interface Token {
   function transfer(address _to, uint256 _value) returns (bool);
   function balanceOf(address _owner) constant returns (uint256 balance);
}

我會在這裡看到兩點:

  1. 這是一個介面,而不是契約,因此不應直接用作實例化(它應該用於建立另一個契約)。
  2. 即使它不是一個介面,其中也沒有建構子,並且帶有建構子的事件,在部署時傳遞參數時,您應該在建構子的簽名呼叫中指定一個參數。

我使用 Javascript VM 在 Remix IDE 中部署了您的CrowdSale契約,沒有任何問題。

creation of Crowdsale pending...
[vm]from:0xca3...a733cto:Crowdsale.(constructor)value:0 wei data:0x608...77b3alogs:0hash:0x650...40b38 

status  0x1 Transaction mined and execution succeed
transaction hash    0x65017f107f3c5309590ff765861ad0dd0f6bb2b6bee5c60bf99c5bdbe0340b38
contract address    0xbbf289d846208c16edc8474705c748aff07732db
from    0xca35b7d915458ef540ade6068dfe2f44e8fa733c
to  Crowdsale.(constructor)
gas     3000000 gas 
transaction cost    941985 gas 
execution cost  677381 gas 
hash    0x65017f107f3c5309590ff765861ad0dd0f6bb2b6bee5c60bf99c5bdbe0340b38
input   0x608...77b3a
decoded input   {
   "address _tokenAddr": "0x692a70D2e424a56D2C6C27aA97D1a86395877b3A"
}
decoded output   - 
logs    []
value   0 wei 

我認為如果沒有您如何部署的更多詳細資訊,例如環境、其他契約部署,就無法進一步解決此問題。

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