Solidity

不推薦使用“.value(…)”。改用“{value: …}”

  • October 4, 2020

這在 Remix 中執行良好,但是當嘗試在 VSCode 中工作時(用於編譯目的;DApp 課程,嘗試通過 compile.js 建構 .json )它會引發錯誤

  1. >

不推薦使用“.value(…)”。請改用“{value: …}”。

  1. >

security/no-call-value:考慮使用“transfer”代替“call.value()”。

從這一行:-

(bool success,) = recipient.call.value(request.value)(""); //下面的倒數第三行

pragma solidity >=0.5.16 <0.7.0;

contract CampaignFactory{
  Campaign[] public deployedCampaigns;

   function createCampaign(uint minimum) public{
       Campaign newCampaign = new Campaign(minimum,msg.sender);
       deployedCampaigns.push(newCampaign);

   }

   function getDeployedCampaigns() public view returns ( Campaign[] memory) {
       return deployedCampaigns;
   }
}


contract Campaign{

   struct Request {
       string description;
       uint value;
       address recipient;
       bool complete;
       uint approvalCount;
       mapping( address => bool ) approvals;


   }


Request[]  public requests;                   
address public manager;                               
uint public minimumContribution;                                 
mapping( address => bool) public approvers;                     
uint public contributersCount;                           

   modifier restricted(){
       require(msg.sender == manager);
       _;
   }

 // constructor************************************************  
   constructor(uint minimum,address creator) public {
       manager = creator;
       minimumContribution = minimum;

   }


   function contribute() public payable{
       require(msg.value > minimumContribution);

       approvers[msg.sender] = true;
       contributersCount++;

   }



   function createRequest(string memory description, uint value, address recipient ) public restricted() {
       Request memory newRequest = Request({
           description: description,
           value: value,
           recipient: recipient,
           complete: false,
           approvalCount: 0

       });

       requests.push(newRequest);

   }

   function approveRequest( uint index) public{
       Request storage request_index = requests[index];

       require(approvers[msg.sender]);

       require(!request_index.approvals[msg.sender]);

       request_index.approvals[msg.sender] = true;

       request_index.approvalCount++;

   }

   function finalizeRequest(uint  index) public payable restricted {
       Request storage  request = requests[index];
       require(request.approvalCount > (contributersCount / 2));
       require(!request.complete);

       address payable recipient = address(uint160(request.recipient));
       (bool success,) = recipient.call.value(request.value)("");
       (success, "Transfer failed.");
       request.complete = true;

   }  

}

那是一條過時的錯誤消息。我假設您使用ethlint的是 linter,這意味著您可以通過.soliumrc.json在儲存庫中創建具有以下內容的內容來消除此警告:

{
 "extends": "solium:recommended",
 "plugins": ["security"],
 "rules": {
   "no-call-value": "off"
 }
}

請參閱Github 問題以及有關如何使用 call.value的進一步說明。

關於".value(...)" is deprecated. Use "{value: ...}"警告:

您可以使用以下模式刪除警告:

(bool success, ) = recipient.call{value:amt}("");
require(success, "Transfer failed.");

solidity.readthedocs.io ,solidity dotsyntax 0.7.0 =>

The following syntax is deprecated: 
f.gas(...)(), f.value(...)() and (new C).value(...)().  

Replace these calls by
f{gas: ..., value: ...}() and (new C){value: ...}(). 

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