部署 & 运行 (第 2 部分)

Interacting with deployed contracts

After a contract is deployed or after it is accessed with AtAddress, the deployed instance will appear in the Deploy/Unpinned Contracts section at the bottom of the Deploy & Run panel.

The deployed contract’s address is visible as are a few other icons - one of which is the pin icon.

Pinned contracts

When a contract is pinned, it will jump up to the Pinned Contracts section and Remix will save the contract’s address and the ABI (in the .deploys folder of the current Workspace). When Remix is refreshed, the pinned contracts will be loaded into the Pinned Contracts section.

Pinned contracts are chain & Workspace specific

Because a pinned contract’s address and ABI are stored in a File Explorer Workspace, the same Workspace must be active to see its pinned contracts. Similarly, only the pinned contracts of the currently selected chain will show.

Functions

To see a contract’s functions, click the caret on the left side of the panel.

The functions’ buttons can have different colors.

  • Blue buttons are for view or pure functions. Clicking a blue button does not create a new transaction - so there will be no gas fees.

  • Orange buttons are for non-payable functions. Non-payable functions change the state of the contract BUT do not accept value (typically ETH) being sent with the transaction. Clicking an orange button will create a transaction and will cost gas.

  • Red buttons are for payable functions. Clicking a red button will create a new transaction and this transaction can accept a value (typically ETH). The amount of value is set in in the Value field which is under the Gas Limit field.

See more information about Solidity modifiers - in the Solidity docs.

输入参数

A function has two views - the collapsed and the expanded view, which is visible after clicking the caret on the right side of the panel.

  • The input box shows the expected type of each parameter.

  • 数字和地址不需要用双引号括起来。

  • 字符串不需要用双引号括起来。

在折叠视图中输入参数

In the collapsed view:

  • 参数用逗号分隔。

在上面的例子中,”delegate” 方法有 3 个参数。

在展开视图中输入参数

Clicking the ‘down’ caret brings you to the expanded view - where parameters are input one at a time.

低级交互

低级交互可以通过 recieve()fallback() 方法,向合约发送资金或 calldata 或者资金和 calldata 。 通常,您只有在遵循升级或代理模式时,才需要实现 fallback 方法。

低级交互在每个已部署合约的方法的下方。

请注意以下事项:

  • 如果你只是单纯地想往一个合约转账以太币,那么这个合约需要有 receive() 方法。 如果你的合约已部署,并且你想给它转账以太币,你可以输入以太币的数量(见下图中的 A),单位可以是 Ether 或 Wei 或其他,然后在低级交互的 calldata 字段中什么都不需要输入(见下图中的 B),直接单击 Transact 按钮即可(参见下图中的 C)。

  • 如果您想往您的合约发送 calldata 的同时转账以太币,那么您需要使用 fallback() 方法,并且标识方法的状态可变性为 payable

  • 如果你没有向合约转账以太币,只是发送 calldata,那么你需要使用 fallback() 方法。

  • 如果您在使用低级交互时违反规则,您将受到警告。

更多有关使用 fallbackreceive 方法的细节,请参阅solidity 文档

Inputting a tuple or struct to a function

To pass a tuple, you need to put in an array [].

Similarly, to pass in a struct as a parameter of a function, it needs to be put in as an array [].

NOTE: the file’s pragma must be set to: pragma experimental ABIEncoderV2;

将嵌套结构体传递给方法的示例

假定一个嵌套结构体是这样定义:

struct Garden {
    uint slugCount;  
    uint wormCount;
    Flower[] theFlowers;
}
struct Flower {
    uint flowerNum;
    string color;
}

如果方法签名是 fertilizer(Garden memory gardenPlot),那么输入参数的正确语法就是:

[1,2,[[3,"Petunia"]]]

为了延伸一下这个例子,这里有一个示例合约:

pragma solidity >=0.4.22 <0.7.0;
pragma experimental ABIEncoderV2;

contract Sunshine {
    struct Garden {
      uint slugCount;  
      uint wormCount;
      Flower[] theFlowers;
    }
    struct Flower {
        uint flowerNum;
        string color;
    }
      
    function fertilizer(Garden memory gardenPlot) public {
        uint a = gardenPlot.slugCount;
        uint b = gardenPlot.wormCount;
        Flower[] memory cFlowers = gardenPlot.theFlowers;
        uint d = gardenPlot.theFlowers[0].flowerNum;
        string memory e = gardenPlot.theFlowers[0].color;
    }
}

在编译、部署合约并打开部署的实例后,我们可以将下面的输入参数添加到名为 fertilizer 的方法中:

[1,2,[[3,"Black-eyed Susan"],[4,"Pansy"]]]

fertilizer 方法只接受一个 Garden 类型的参数。 Garden 类型是一个结构体。 结构体用方括号括起来。 Garden 内部有一个叫 theFlowers 数组,这个数组的元素也是结构体。 theFlowers 数组本来就有一对括号,里面的结构体元素又有另一对括号。 所以,这里出现了两个方括号。