当前位置:   article > 正文

智能合约怎么创建合约_可出售智能合约的协议

}  function destroy()  public  onlyowner  {  selfdestruct(payable(

智能合约怎么创建合约

by Pablo Ruiz

帕勃罗·鲁伊斯(Pablo Ruiz)

可出售智能合约的协议 (A Protocol for Sellable Smart Contracts)

Ethereum doesn’t have the concept of smart contract ownership built into it.Even though the creation and deployment of a smart contract is done by an account — be it an External Owned Account (EOA) or another contract — being the creator of the smart contract doesn’t give the account any special privileges over the contract they deployed.

以太坊没有内置智能合约所有权的概念。即使智能合约的创建和部署是由一个帐户(无论是外部拥有帐户(EOA)还是另一个合约)完成的,而该帐户又是智能合约的创建者合同并未为其部署的合同赋予帐户任何特殊特权。

Most use cases for smart contracts require someone to own the contracts. This “owner” is given privileges — and responsibilities — over the smart contract.

智能合约的大多数用例要求有人拥有合约。 该“所有者”拥有智能合约的特权和责任。

In a crowdsale contract they might be tasked with managing the whole process and pausing the crowdsale if something goes wrong.

在众包合同中,可能会负责管理整个过程并在出现问题时暂停众包。

In a Lottery/Ruffle Dapp they might be tasked with executing the number draw.

在彩票/皱纹Dapp中,他们可能会执行抽奖的任务。

In any contract that holds funds, they might be set as the beneficiary upon construct destruction.

在任何持有资金的合同中,可能会在销毁建筑物时将其设置为受益人。

A common pattern used by many smart contracts is to set the owner to the account deploying the contract like so:

许多智能合约使用的常见模式是将所有者设置为部署合约的帐户,如下所示:

pragma solidity 0.4.19;
contract MyContract {  address owner;
function MyContract(){    owner = msg.sender;  }}

Then, adding a modifier:

然后,添加修饰符:

modifier onlyOwner {  require(msg.sender == owner);  _;    }

And finally, using that modifier to enforce that critical operations can only be performed by the owner of the contract:

最后,使用该修饰符强制执行关键操作只能由合同所有者执行:

// Suicide the contract and transfer funds to the owner// Only available to the owner, for obvious reasons.
function destroyContract() public onlyOwner {  selfdestruct(owner);}

变更合同所有权的问题 (The Problem with Changing Contract Ownership)

There are some situations that would require ownership of a contract to be given to someone else. To name a few:

在某些情况下,需要将合同所有权授予其他人。 仅举几例:

  • The person that deployed the contract did it on behalf of someone else

    部署合同的人代表他人完成了合同

    A developer or consultant doing a contracting job for a company

    为公司承包工程的开发人员或顾问

  • A company wants to liquidate / sell its assets, which include smart contracts

    公司想要清算/出售其资产,包括智能合约

    Which might or not have ether balance

    哪些可能没有醚平衡

  • The owner of the smart contract wants to give it away, donate it, or just flip it for profit

    智能合约的所有者希望将其赠予,捐赠或只是为了获利而翻转

Some contracts, but unfortunately not many, include a function to give ownership of the contract to some other account. And some of them also include another function for that person to accept the ownership that has been bestowed upon him.

某些合同(但不幸的是数量不多)具有将合同所有权分配给其他帐户的功能。 其中一些功能还包括该人接受已授予他的所有权的另一个功能。

function changeOwner(address _newOwner)public onlyOwner {  ownerCandidate = _newOwner;}
function acceptOwnership()public {  require(msg.sender == ownerCandidate);    owner = ownerCandidate;}

Now, the situations mentioned above share a few common issues that these not-so-widely-used changeOwner() and acceptOwnership() functions don’t address:

现在,上述情况存在一些共同的问题,这些尚未广泛使用的changeOwner()acceptOwnership()函数无法解决:

  • How can the buyer of the contract be certain that once they pay for the ownership of the contract, the seller will actually execute the corresponding changeOwner() function?

    合同的购买者如何确定他们一旦支付了合同所有权,卖方实际上将执行相应的changeOwner()函数?

  • This can happen the other way around. How can the seller of the contract be certain that they will get paid if they cede ownership first?

    反之亦然。 合同的卖方如何确定如果他们首先放弃所有权,他们将获得报酬?
  • How can the buyer of the contract be certain that the current owner of the contract will not modify it (well, it’s data) before giving away its ownership?

    合同的购买者如何在放弃其所有权之前确定该合同的当前所有者不会修改它(是数据)?

可出售合同协议 (The Sellable Contract Protocol)

The solution I propose is implementing a series of functions that would allow the owner of a smart contract to sell it in exchange of ether to someone of his choosing or just put it up for sale for anyone to buy at the asking price on a first-come first-save basis. This could be extended to allow different sale methods using different auction styles.

我建议的解决方案是实现一系列功能,该功能允许智能合约的所有者将其出售以换取以太币给其选择的某人,或者将其出售给任何人以第一要价的价格购买,首先保存。 这可以扩展为允许使用不同拍卖方式的不同销售方式。

The details of the protocol can be read — and discussed — on the corresponding EIP.

协议的详细信息可以在相应的EIP上阅读和讨论。

In the following paragraphs I’ll go through an implementation example, which is available on my Github Repository.

在以下各段中,我将介绍一个实现示例,该示例可在我的Github存储库中找到。

处理所有权 (Handling Ownership)

Handling ownership of the contract is pretty basic. As typically done, we set the owner of the deployed contract to msg.sender upon initialization:

处理合同所有权是非常基本的。 通常,我们在初始化时将已部署合同的所有者设置为msg.sender

function Sellable() public {        owner = msg.sender;        Transfer(now,address(0),owner,0);    }

Then, we add the onlyOwner modifier, which will be used on every function that we want to make only executable by the person currently owning the contract:

然后,我们添加onlyOwner修饰符,该修饰符将用于每个我们希望仅由当前拥有合同的人执行的函数:

modifier onlyOwner {        require(msg.sender == owner);        _;    }

What we’ll want our contract to do is to allow the owner to be changed under certain conditions.

我们希望我们的合同要做的是允许在某些条件下更改owner

出售合同 (Putting the Contract for Sale)

The owner of the contract can put it up for sale by calling the following function:

合同的所有者可以通过调用以下函数将其出售:

function initiateSale(uint _price, address _to) onlyOwner public {        require(_to != address(this) && _to != owner);        require(!selling);                selling = true;                // Set the target buyer, if specified.        sellingTo = _to;                askingPrice = _price;    }

initiateSale() takes two parameters:

initiateSale()具有两个参数:

  • uint _price: which is the price the owner wants to sell the contract for.

    uint _price :所有者要出售合同的价格。

  • address _to: which is optional, and corresponds to who the owner wants to sell the contract to.

    地址_to:是可选的,与所有者要将合同出售给的人相对应。

When putting the contract up for sale, the owner has two options: They can choose the buyer, in that case the sale has been prearrange. Or they can simply “announce” the contract is for sale and the first person to claim it (and paying its price) gets it.

将合同出售时,所有者有两个选择:他们可以选择买方,在这种情况下,销售已经预先安排。 或者,他们可以简单地“宣布”合同待售,而第一个声称该合同(并支付其价格)的人得到了该合同。

Additionally, the asking price can be set to 0. This means that the owner of the contract is allowed to gift, donate or give the contract away.

此外,可以将要价设置为0。这意味着允许合同所有者赠送,捐赠或放弃合同。

There’s one more important thing to notice: There’s a ifNotLocked modifier that can be added to the contract’s functions to prevent them from being executed if the contract is in a sale process. If used properly, this prevents the contract’s data from being modified just before it is purchased.

需要注意的另一件事是:可以在合同功能中添加ifNotLocked修饰符,以防止在合同处于销售过程中执行它们。 如果使用得当,可以防止在购买合同之前修改合同的数据。

Finally, there’s the cancelPurchase() function which allows the owner to cancel the sale before someone completes it.

最后,还有cancelPurchase()函数,该函数允许所有者在完成销售之前取消销售。

function cancelSale() onlyOwner public {        require(selling);                // Reset sale variables        resetSale();    }
购买合同 (Buying the Contract)

Once the contract is up for sale, all it takes to complete the sale is for the buyer (if it was specified) or anyone (if no particular buyer was specified) to call the following function:

合同待售后,完成销售所需的全部工作都由买方(如果已指定)或任何人(如果未指定特定买方)调用以下功能:

function completeSale() public payable {        require(selling);        require(msg.sender != owner);        require(msg.sender == sellingTo || sellingTo == address(0));        require(msg.value == askingPrice);                // Swap ownership        address prevOwner = owner;        address newOwner = msg.sender;        uint salePrice = askingPrice;                owner = newOwner;                // Transaction cleanup        resetSale();                prevOwner.transfer(salePrice);                Transfer(now,prevOwner,newOwner,salePrice);    }

The completeSale() function is a payable function which requires the ether to be sent. The amount to be sent must be the exactly the same the owner set as the asking price.

completeSale()函数是一个payable函数,需要发送以太币。 要发送的金额必须与所有者设置的要价完全相同。

When completeSale() is executed, the ether will be transferred to the owner and then the ownership will be transferred to the buyer. This finishes the transaction and cleans up the contract for the new owner, who can now use it normally, or even put it up for sale again.

执行completeSale() ,以太币将被转让给所有者,然后所有权将被转让给买方。 这样就完成了交易并为新所有者清理了合同,新所有者现在可以正常使用它,甚至可以再次出售。

用例范例 (An Example Use Case)

Here’s a very simple example of how this base contract could be used:

这是一个如何使用此基础合同的非常简单的示例:

contract Kitty is Sellable {        string public name;    uint public kittyValue = 0;        function Kitty(string _name) public {        name = _name;    }        function findNewOwner() public onlyOwner {        kittyValue = kittyValue + 1 ether;           super.initiateSale(kittyValue,address(0));    }        function renameKitty(string newName) ifNotLocked public onlyOwner {        name = newName;    }        function buyKitty() public payable {        require(msg.value == kittyValue);        super.completeSale();    }}

We have a contract which represents a CryptoKitty ?. The owner can findNewOwner() to put it up for sale. Each time the kitty is bought his value increases by 1 ether. The owner of the kitty can change its name, as long as it is not being sold at the moment by implementing the ifNotLocked modifier in renameKitty .

我们有一个代表CryptoKitty的合同。 所有者可以indNewOwner()进行出售。 每次购买小猫时,他的价值增加1以太。 因为它没有被目前通过实现我公司销售的小猫的主人可以更改其名称,只要fNotLocked在r中修改enameKitty

而已! (That’s it!)

If you have further suggestions to improve this Sellable protocol, please add your comments, bugs or suggestions in the EIP I created.

如果您有进一步改进此Sellable协议的建议,请在我创建的EIP中添加您的评论,错误或建议

翻译自: https://www.freecodecamp.org/news/a-protocol-for-sellable-smart-contracts-829bc2ce02b3/

智能合约怎么创建合约

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/369158
推荐阅读
相关标签
  

闽ICP备14008679号