什么是托管智能合约?
托管是在两方存在的情况下持有资产(资产可以是货币、债券、股票)的第三方。当满足某些条件时,托管将释放资金。
例如,“A”是卖家,想卖掉他的车,“B”是买家,想买“A”的车,所以他们会联系持有资产的托管“C”(仲裁者),直到“ B”收到车。当满足此条件时,Escrow 会将资金释放给“A”。这解决了信任问题并防止了任何差异。
让我们使用 Solidity 语言为 Escrow 编写智能合约。
在这里,智能合约将持有该资产,该资产将在条件满足时释放。
Solidity
pragma solidity 0.6.0;
// Defining a Contract
contract escrow{
// Declaring the state variables
address payable public buyer;
address payable public seller;
address payable public arbiter;
mapping(address => uint) TotalAmount;
// Defining a enumerator 'State'
enum State{
// Following are the data members
awate_payment, awate_delivery, complete
}
// Declaring the object of the enumerator
State public state;
// Defining function modifier 'instate'
modifier instate(State expected_state){
require(state == expected_state);
_;
}
// Defining function modifier 'onlyBuyer'
modifier onlyBuyer(){
require(msg.sender == buyer ||
msg.sender == arbiter);
_;
}
// Defining function modifier 'onlySeller'
modifier onlySeller(){
require(msg.sender == seller);
_;
}
// Defining a constructor
constructor(address payable _buyer,
address payable _sender) public{
// Assigning the values of the
// state variables
arbiter = msg.sender;
buyer = _buyer;
seller = _sender;
state = State.awate_payment;
}
// Defining function to confirm payment
function confirm_payment() onlyBuyer instate(
State.awate_payment) public payable{
state = State.awate_delivery;
}
// Defining function to confirm delivery
function confirm_Delivery() onlyBuyer instate(
State.awate_delivery) public{
seller.transfer(address(this).balance);
state = State.complete;
}
// Defining function to return payment
function ReturnPayment() onlySeller instate(
State.awate_delivery)public{
buyer.transfer(address(this).balance);
}
}