DAPP马蹄链开发运营版丨DAPP马蹄链系统开发(智能合约开发详细)

173 阅读1分钟

 tokenToEthInput通过token.balanceOf获得代币兑换合约当前的代币ERC20代币存储量,然后用getInputPrice获得可兑换到的ETH的数量,接着用as_wei_value将单位转换成wei后用send函数将eth发送给接收者,最后再调用transferForm从buyer手中收取应当支付的代币。

  #dev Pricing function for converting between ETH and Tokens.

  #param input_amount Amount of ETH or Tokens being sold.

  案例及设计I35详细7O98开发O7I8

  #param input_reserve Amount of ETH or Tokens(input type)in exchange reserves.

  #param output_reserve Amount of ETH or Tokens(output type)in exchange reserves.

  #return Amount of ETH or Tokens bought.关于区块链项目技术开发唯:MrsFu123,代币发行、dapp智能合约开发、链游开发、多链钱包开发

  交易所开发、量化合约开发、互助游戏开发、Nft数字藏品开发、众筹互助开发、元宇宙开发、swap开发、

  链上合约开发、ido开发、商城开发等,开发过各种各样的系统模式,更有多种模式、制度、案例、后台等,成熟技术团队,欢迎实体参考。

  private

  constant

  def getInputPrice(input_amount:uint256,input_reserve:uint256,output_reserve:uint256)->uint256:

  assert input_reserve>0 and output_reserve>0#需要两币的储备都大于0

  input_amount_with_fee:uint256=input_amount*997#抽取千分之3手续费

  numerator:uint256=input_amount_with_fee*output_reserve

  denominator:uint256=(input_reserve*1000)+input_amount_with_fee

  return numerator/denominator

  private

  def tokenToEthOutput(eth_bought:uint256(wei),max_tokens:uint256,deadline:timestamp,buyer:address,recipient:address)->uint256:

  assert deadline>=block.timestamp and eth_bought>0

  token_reserve:uint256=self.token.balanceOf(self)#获取代币储备量

  #通过getOutputPrice计算所需要花费的代币数量

  tokens_sold:uint256=self.getOutputPrice(as_unitless_number(eth_bought),token_reserve,as_unitless_number(self.balance))

  #tokens sold is always>0

  assert max_tokens>=tokens_sold

  send(recipient,eth_bought)#向接收者发送所兑换得到的ETH

  assert self.token.transferFrom(buyer,self,tokens_sold)#从购买者收取代币

  log.EthPurchase(buyer,tokens_sold,eth_bought)

  return tokens_sold