DAO/LP代幣質押流動性挖礦分紅系統開發(方案及專案)丨原始碼功能

xiaofufu發表於2023-03-15

  #notice Convert ETH to Tokens.


  #dev User specifies exact input(msg.value).


  #dev User cannot specify minimum output or deadline.


  #用ETH兌換代幣的預設函式,使用者只需要指定輸入的ETH的數量


  public 


  payable


  def __default__():


  self.ethToTokenInput(msg.value,1,block.timestamp,msg.sender,msg.sender)


   

  #notice Convert ETH to Tokens.


  #dev User specifies exact input(msg.value)and minimum output.所以接收的代幣最小值


  #param min_tokens Minimum Tokens bought.


  #param deadline Time after which this transaction can no longer be executed.


  #return Amount of Tokens bought.


關於區塊鏈專案技術開發唯:MrsFu123,代幣發行、dapp智慧合約開發、鏈遊開發、多鏈錢包開發


  交易所開發、量化合約開發、互助遊戲開發、Nft數字藏品開發、眾籌互助開發、元宇宙開發、swap開發、


  鏈上合約開發、ido開發、商城開發等,開發過各種各樣的系統模式,更有多種模式、制度、案例、後臺等,成熟技術團隊,歡迎實體參考。





  #ETH透過msg.value的方式傳送給代幣合約,代幣合約根據所接收到的ETH計算所需支付的代幣


  public


  payable


  def ethToTokenSwapInput(min_tokens:uint256,deadline:timestamp)->uint256:


  return self.ethToTokenInput(msg.value,min_tokens,deadline,msg.sender,msg.sender)


  


  #notice Convert ETH to Tokens and transfers Tokens to recipient.


  #dev User specifies exact input(msg.value)and minimum output


  #param min_tokens Minimum Tokens bought.


  #param deadline Time after which this transaction can no longer be executed.


  #param recipient The address that receives output Tokens.


  #return Amount of Tokens bought.


  public


  payable


  def ethToTokenTransferInput(min_tokens:uint256,deadline:timestamp,recipient:address)->uint256:


  assert recipient!=self and recipient!=ZERO_ADDRESS


  return self.ethToTokenInput(msg.value,min_tokens,deadline,msg.sender,recipient)


  


  #notice Convert ETH to Tokens.


  #dev User specifies maximum input(msg.value)and exact output.


  #param tokens_bought Amount of tokens bought.


  #param deadline Time after which this transaction can no longer be executed.


  #return Amount of ETH sold.


  public


  payable


  def ethToTokenSwapOutput(tokens_bought:uint256,deadline:timestamp)->uint256(wei):


  return self.ethToTokenOutput(tokens_bought,msg.value,deadline,msg.sender,msg.sender)


  


  #notice Convert ETH to Tokens and transfers Tokens to recipient.


  #dev User specifies maximum input(msg.value)and exact output.


  #param tokens_bought Amount of tokens bought.


  #param deadline Time after which this transaction can no longer be executed.


  #param recipient The address that receives output Tokens.


  #return Amount of ETH sold.


  public


  payable


  def ethToTokenTransferOutput(tokens_bought:uint256,deadline:timestamp,recipient:address)->uint256(wei):


  assert recipient!=self and recipient!=ZERO_ADDRESS


  return self.ethToTokenOutput(tokens_bought,msg.value,deadline,msg.sender,recipient)


  


  private


  def ethToTokenInput(eth_sold:uint256(wei),min_tokens:uint256,deadline:timestamp,buyer:address,recipient:address)->uint256:


  assert deadline>=block.timestamp and(eth_sold>0 and min_tokens>0)


  token_reserve:uint256=self.token.balanceOf(self)#獲取代幣儲備量


  #用getInputPrice獲得所購買得到的代幣數量


  #因為交易是先轉賬再執行合約,所以獲得ETH儲備量的時候需要先減去該買者已經傳送的ETH


  tokens_bought:uint256=self.getInputPrice(as_unitless_number(eth_sold),as_unitless_number(self.balance-eth_sold),token_reserve)


  assert tokens_bought>=min_tokens


  assert self.token.transfer(recipient,tokens_bought)#向接收者傳送代幣


  log.TokenPurchase(buyer,eth_sold,tokens_bought)


  return tokens_bought


  


  private


  def ethToTokenOutput(tokens_bought:uint256,max_eth:uint256(wei),deadline:timestamp,buyer:address,recipient:address)->uint256(wei):


  assert deadline>=block.timestamp and(tokens_bought>0 and max_eth>0)


  token_reserve:uint256=self.token.balanceOf(self)#獲取代幣儲備


  #用getOutputPrice獲得所需支付的ETH數量


  #因為交易是先轉賬再執行合約程式碼,所以呼叫該合約時ETH已經轉到兌換合約中,


  #而入口函式會直接將msg.value作為max_eth傳入,所以ETH儲備量為self.balance-max_eth


  eth_sold:uint256=self.getOutputPrice(tokens_bought,as_unitless_number(self.balance-max_eth),token_reserve)


  #Throws if eth_sold>max_eth


  #計算需要退還給使用者的ETH


  eth_refund:uint256(wei)=max_eth-as_wei_value(eth_sold,'wei')


  if eth_refund>0:


  send(buyer,eth_refund)#如果需要退還的ETH大於0,轉賬。


  assert self.token.transfer(recipient,tokens_bought)#向使用者傳送代幣


  log.TokenPurchase(buyer,as_wei_value(eth_sold,'wei'),tokens_bought)


  return as_wei_value(eth_sold,'wei')


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69956839/viewspace-2939877/,如需轉載,請註明出處,否則將追究法律責任。

相關文章