Ethereum allows developers to create decentralized applications(dapps)that share computing power pools.This shared pool is limited,so Ethereum needs a mechanism to determine who can use it.Otherwise,a dapp may accidentally or maliciously consume all network resources,causing other applications to be unable to access the computing pool.
区块链智能合约是法律机构的一项颠覆性技术。它允许企业将第三方排除在协议的创建之外,并改善其内部文件流。在本文中,我们将研究什么是区块链智能合约、它们的好处,以及公司如何应用这项技术来成功开发。
智能合约的概念
智能合约基于区块链技术运行。它们可以用作为各种业务签订协议的现实合同。
智能合约的主要优势在于它们不需要像律师或法律代表这样的中间人来执行任何谈判。合约参与者可以自行创建智能合约,并在满足包含的条件后自动执行。通过这种方式,承包商可以在租房、兑换货币、登记车辆甚至举行总统选举时节省大量时间和金钱。
There are many situations where smart contracts can be applied,and many companies have already done so.For example,Slock.it helps its users automatically share,pay and rent through smart contracts.Fizzy AXA uses blockchain automation flight insurance compensation.Populous uses smart contracts and many other ways to alleviate the problem of purchasing and selling open invoices.
#Open Auction
#Auction params
#Beneficiary receives money from the highest bidder
beneficiary:public(address)
auctionStart:public(uint256)
auctionEnd:public(uint256)
#Current state of auction
highestBidder:public(address)
highestBid:public(uint256)
#Set to true at the end,disallows any change
ended:public(bool)
#Keep track of refunded bids so we can follow the withdraw pattern
pendingReturns:public(HashMap[address,uint256])
external
def __init__(_beneficiary:address,_bidding_time:uint256):
self.beneficiary=_beneficiary
self.auctionStart=block.timestamp
self.auctionEnd=self.auctionStart+_bidding_time
#Bid on the auction with the value sent
#together with this transaction.
#The value will only be refunded if the
#auction is not won.
external
payable
def bid():
#Check if bidding period is over.
assert block.timestamp<self.auctionEnd
#Check if bid is high enough
assert msg.value>self.highestBid
#Track the refund for the previous high bidder
self.pendingReturns[self.highestBidder]+=self.highestBid
#Track new high bid
self.highestBidder=msg.sender
self.highestBid=msg.value The withdraw pattern is
#used here to avoid a security issue.If refunds were directly
#sent as part of bid(),a malicious bidding contract could block
#those refunds and thus block new higher bids from coming in.
external
def withdraw():
pending_amount:uint256=self.pendingReturns[msg.sender]
self.pendingReturns[msg.sender]=0
send(msg.sender,pending_amount)
#End the auction and send the highest bid
#to the beneficiary.
external
def endAuction():
#It is a good guideline to structure functions that interact
#with other contracts(i.e.they call functions or send ether)
#into three phases:
#1.checking conditions
#2.performing actions(potentially changing conditions)
#3.interacting with other contracts
#If these phases are mixed up,the other contract could call
#back into the current contract and modify the state or cause
#effects(ether payout)to be performed multiple times.
#If functions called internally include interaction with external
#contracts,they also have to be considered interaction with
#external contracts.
#1.Conditions
#Check if auction endtime has been reached
assert block.timestamp>=self.auctionEnd
#Check if this function has already been called
assert not self.ended
#2.Effects
self.ended=True
#3.Interaction
send(self.beneficiary,self.highestBid)