String

可以在合約之間傳遞字元串嗎

  • March 4, 2022

我想從另一個契約中呼叫一個函式。我可以傳遞一個字元串嗎?

IUpgrade(upgradeAddress).wrap(label, wrappedOwner, _fuses, resolver);

標籤是一個字元串。我在某處讀到你不能在契約之間傳遞字元串,但也許那隻是為了舊版本的solidity。

我剛剛在 remix.ethereum 上對其進行了測試,並在下面製作了一個展示程式碼,是的,你可以在合約之間發送字元串,因為合約函式仍然是函式:D

  • 但是合約函式參數必須有一個字元串參數才能接受一個字元串
   function foo(string memory content) public pure returns(string memory) {
           return content;
   }

這是我在 Remix 中用來測試它的程式碼。

pragma solidity ^0.8.4;


interface IName {

   function upgrade (string calldata name) external;

}

contract Storage {

   address public upgradeAddress;


   function setUpgradeAddress(address _upgradeAddress)
       public
   {
           upgradeAddress = _upgradeAddress;
   }


   function upgrade(string calldata name) public {
       IName(upgradeAddress).upgrade(name);
   }


}

contract UpgradeStorage is IName{

   string public name;
   
   function upgrade(string calldata _name) public override{
       name = _name;
   }

}

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