Solidity

檢索 Solidity 合約的所有公共狀態變數值的更好方法

  • January 10, 2022

我的solidity“0.7.0”合約

pragma solidity ^0.7.0;

contract DoubleEscrowTrade {
   uint256 private locked = 2; //2: unlocked, 1: locked. uint256 is gas efficient than bool
   uint256 public value=0;  //poster value
   uint256 public bidItemValue=0;  //bidder item value
   bool public openForBid = false;  //allow items with any value to bid.
   string public posterItemName;
   string public whoInitDispute;
   uint256 public posterItemID;
   string public bidderItemName; //no string array supported
   uint256 public bidderItemID;
   mapping (address=>address) public bidItemAddress; // bidder address and bidder item deploy address
   mapping (address=>uint256) public bidItemID;
   mapping (address=>uint256) public bidValue;
   mapping (address=>string) public bidItemName;
   uint256 public howManyBidItem=0;
   address payable public poster;
   address payable public bidder;
   bool public delisted = false;
   bool public posterDispute = false;
   bool public bidderDispute = false;
   bool public posterShipped = false;
   bool public bidderShipped = false;
   uint256 public posterReceiveProdTime = 0;
   uint256 public bidderReceiveProdTime = 0;
   uint256 public posterReceiveReturnTime = 0;
   uint256 public bidderReceiveReturnTime = 0;
   uint256 public posterAcceptBidTime = 0;
   uint256 public posterEscrowBalance = 0;
   uint256 public posterEscrowRefund = 0;
   uint256 public bidderEscrowBalance = 0;
   uint256 public bidderEscrowRefund = 0;
   uint256 public bidderShipProdTime = 0;
   uint256 public posterShipProdTime = 0;
   uint256 public posterDepositTime = 0;
   uint256 public bidderDepositTime = 0;
   uint256 public posterDisputeTime = 0;
   uint256 public bidderDisputeTime = 0;
   uint256 public posterDelistTime = 0;
   uint256 private graceDay = 14;
   uint256 private dayInSecond = 24*3600;
   enum State { ForTrade, PosterAcceptBid, PosterEscrowDeposited, BidderEscrowDeposited, PosterDelisted, BidderCancelled, BidderShipped, PosterShipped, 
   InDispute, Complete, PosterReceiveProduct, PosterReceiveReturn, BidderReceiveProduct, BidderReceiveReturn }
   State public state;

公共變數包括地址、映射、uint256、字元串和布爾值。該應用程序可以myVar使用myContract.myVar(). 然而,會有太多的請求和如此多的變數,這也很乏味。根據我的閱讀,我可能可以在數組中獲取所有帶有 return (var1, var2, … varN) 的 uint256 變數。但我不確定是否可以在solidity 0.7.0 的一次返回中返回映射、布爾、字元串和地址。或者有更好的方法來處理這個問題。

通過return(<...>)你可以返回沒有那麼多的值。這些值將在堆棧中返回,編譯器不會讓您使堆棧太深。它與變數類型無關。合約程式碼中有很多變數,我幾乎可以肯定你不能只返回一個return(<...>)

通過觀察 中返回值的個數限制return(<...>),可以通過它返回 public bool、string、address 和映射。

我有一個類似的問題,並且有一些方法可以解決這個問題。

1.您可以-正如您所指出的-將其全部退回。

2.您可以嘗試將其全部打包成一個巨大的數組。

3.您可以返回多個數組。一個包含地址,另一個包含 uint256 變數等。

在您的情況下(如果您不需要整個地圖,而只需要在此處映射的單個值)。我建議使用一個字元串數組來返回所有值。這樣,所有數據都將以可讀的格式保存。

但是,要返回一個字元串數組,您需要添加 [pragma experimental ABIEncoderV2; ] 到您的程式碼。

像這樣:

pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2; // Added experimental encoder

contract DoubleEscrowTrade {
   uint256 private locked = 2; //2: unlocked, 1: locked. uint256 is gas efficient than bool
   uint256 public value=0;  //poster value
   uint256 public bidItemValue=0;  //bidder item value
   bool public openForBid = false;  //allow items with any value to bid.
   string public posterItemName;
   string public whoInitDispute;
   uint256 public posterItemID;
   string public bidderItemName; //no string array supported
   uint256 public bidderItemID;
   mapping (address=>address) public bidItemAddress; // bidder address and bidder item deploy address
   mapping (address=>uint256) public bidItemID;
   mapping (address=>uint256) public bidValue;
   mapping (address=>string) public bidItemName;
   uint256 public howManyBidItem=0;
   address payable public poster;
   address payable public bidder;
   bool public delisted = false;
   bool public posterDispute = false;
   bool public bidderDispute = false;
   bool public posterShipped = false;
   bool public bidderShipped = false;
   uint256 public posterReceiveProdTime = 0;
   uint256 public bidderReceiveProdTime = 0;
   uint256 public posterReceiveReturnTime = 0;
   uint256 public bidderReceiveReturnTime = 0;
   uint256 public posterAcceptBidTime = 0;
   uint256 public posterEscrowBalance = 0;
   uint256 public posterEscrowRefund = 0;
   uint256 public bidderEscrowBalance = 0;
   uint256 public bidderEscrowRefund = 0;
   uint256 public bidderShipProdTime = 0;
   uint256 public posterShipProdTime = 0;
   uint256 public posterDepositTime = 0;
   uint256 public bidderDepositTime = 0;
   uint256 public posterDisputeTime = 0;
   uint256 public bidderDisputeTime = 0;
   uint256 public posterDelistTime = 0;
   uint256 private graceDay = 14;
   uint256 private dayInSecond = 24*3600;
   enum State { ForTrade, PosterAcceptBid, PosterEscrowDeposited, BidderEscrowDeposited, PosterDelisted, BidderCancelled, BidderShipped, PosterShipped, 
   InDispute, Complete, PosterReceiveProduct, PosterReceiveReturn, BidderReceiveProduct, BidderReceiveReturn }
   State public state;

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