开发者社区> 开发V_MrsFu123> 正文

区块链阿凡达泰山众筹商城系统开发(正式版)丨区块链阿凡达泰山众筹商城开发源码系统

简介: “新零售”的核心要义在于推动线上与线下的一体化进程,其关键在于使线上的互联网力量和线下的实体店终端形成真正意义上的合力,从而完成电商平台和实体零售店面在商业维度上的优化升级。同时,促成价格消费时代向价值消费时代的全面转型。
+关注继续查看

  “新零售”的核心要义在于推动线上与线下的一体化进程,其关键在于使线上的互联网力量和线下的实体店终端形成真正意义上的合力,从而完成电商平台和实体零售店面在商业维度上的优化升级。同时,促成价格消费时代向价值消费时代的全面转型。

  pragma solidity=0.5.16;

  import'./interfaces/IUniswapV2Pair.sol';

  import'./UniswapV2ERC20.sol';

  import'./libraries/Math.sol';

  import'./libraries/UQ112x112.sol';

  import'./interfaces/IERC20.sol';

  import'./interfaces/IUniswapV2Factory.sol';

  import'./interfaces/IUniswapV2Callee.sol';

  contract UniswapV2Pair is IUniswapV2Pair,UniswapV2ERC20{

  using SafeMath for uint;

  using UQ112x112 for uint224;

  //最低流动性

  uint public constant MINIMUM_LIQUIDITY=10**3;

  //获取transfer方法的bytecode前四个字节

  bytes4 private constant SELECTOR=bytes4(keccak256(bytes('transfer(address,uint256)')));

  address public factory;

  address public token0;

  address public token1;

  uint112 private reserve0;//uses single storage slot,accessible via getReserves==使用单个存储槽,可通过getReserves访问

  uint112 private reserve1;//uses single storage slot,accessible via getReserves

  uint32 private blockTimestampLast;//uses single storage slot,accessible via getReserves

  uint public price0CumulativeLast;//最后价格累计的0价格?

  uint public price1CumulativeLast;
  //紧接最近一次流动性事件之后

  uint public kLast;//reserve0*reserve1,as of immediately after the most recent liquidity event

  uint private unlocked=1;

  //防止递归迭代出现问题,所以要上锁

  //一个锁,使用该modifier的函数在unlocked==1时才可以进入,

  //第一个调用者进入后,会将unlocked置为0,此使第二个调用者无法再进入

  //执行完_部分的代码后,才会再将unlocked置1,重新将锁打开

  modifier lock(){

  require(unlocked==1,'UniswapV2:LOCKED');

  unlocked=0;

  _;

  unlocked=1;

  }

  //获取储备:返回:_reserve0,_reserve1,_blockTimestampLast

  //用于获取两个token在池子中的数量和最后更新的时间

  function getReserves()public view returns(uint112 _reserve0,uint112 _reserve1,uint32 _blockTimestampLast){

  _reserve0=reserve0;

  _reserve1=reserve1;

  //时间戳

  _blockTimestampLast=blockTimestampLast;

  }

  //转账,安全校验

  function _safeTransfer(address token,address to,uint value)private{

  //调用transfer方法,把地址token中的value个coin转账给to

  (bool success,bytes memory data)=token.call(abi.encodeWithSelector(SELECTOR,to,value));

  //检查返回值,必须成功否则报错

  require(success&&(data.length==0||abi.decode(data,(bool))),'UniswapV2:TRANSFER_FAILED');

  }

  event Mint(address indexed sender,uint amount0,uint amount1);

  event Burn(address indexed sender,uint amount0,uint amount1,address indexed to);

  event Swap(address indexed sender,uint amount0In,uint amount1In,uint amount0Out,uint amount1Out,address indexed to);

  event Sync(uint112 reserve0,uint112 reserve1);

  //部署此合约时将msg.sender设置为factory,后续初始化时会用到这个值

  constructor()public{

  factory=msg.sender;

  }

  //called once by the factory at time of deployment

  //在UniswapV2Factory.sol的createPair中调用过

  function initialize(address _token0,address _token1)external{

  require(msg.sender==factory,'UniswapV2:FORBIDDEN');//sufficient check

  token0=_token0;

  token1=_token1;

  }

  //update reserves and,on the first call per block,price accumulators

  //更新储备,并在每个区块的第一次调用时更新价格累加器

  /**

  更新变量:

  blockTimestampLast

  reserve0

  reserve1

  price0CumulativeLast

  price1CumulativeLast

  */

  //这个函数是用来更新价格oracle的,计算累计价格

  function _update(uint balance0,uint balance1,uint112 _reserve0,uint112 _reserve1)private{

  //溢出校验

  require(balance0<=uint112(-1)&&balance1<=uint112(-1),'UniswapV2:OVERFLOW');

  uint32 blockTimestamp=uint32(block.timestamp%2**32);

  uint32 timeElapsed=blockTimestamp-blockTimestampLast;//overflow is desired

  //计算时间加权的累计价格,256位中,前112位用来存整数,后112位用来存小数,多的32位用来存溢出的值

  if(timeElapsed>0&&_reserve0!=0&&_reserve1!=0){

  //*never overflows,and+overflow is desired

  price0CumulativeLast+=uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0))*timeElapsed;

  price1CumulativeLast+=uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1))*timeElapsed;

  }

  //更新reserve值

  reserve0=uint112(balance0);

  reserve1=uint112(balance1);

  blockTimestampLast=blockTimestamp;

  emit Sync(reserve0,reserve1);

  }

  //if fee is on,mint liquidity equivalent to 1/6th of the growth in sqrt(k)

  //如果收费,增发流动性相当于sqrt(k)增长的1/6

  function _mintFee(uint112 _reserve0,uint112 _reserve1)private returns(bool feeOn){

  //获取接收手续费的地址

  address feeTo=IUniswapV2Factory(factory).feeTo();

  //手续费接收者不为0地址

  feeOn=feeTo!=address(0);

  uint _kLast=kLast;//gas savings

  //手续费接收者不为0地址

  if(feeOn){

  if(_kLast!=0){

  uint rootK=Math.sqrt(uint(_reserve0).mul(_reserve1));

  uint rootKLast=Math.sqrt(_kLast);

  if(rootK>rootKLast){

  uint numerator=totalSupply.mul(rootK.sub(rootKLast));

  uint denominator=rootK.mul(5).add(rootKLast);

  uint liquidity=numerator/denominator;

  if(liquidity>0)_mint(feeTo,liquidity);

  }

  }

  }

  //手续费接收者为0,并且kLast不为0

  else if(_kLast!=0){

  kLast=0;

  }

  }

  //this low-level function should be called from a contract which performs important safety checks

  //这个低级函数应该从执行重要安全检查的合约中调用

  function mint(address to)external lock returns(uint liquidity){

  (uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings

  //合约里两种token的当前的balance

  uint balance0=IERC20(token0).balanceOf(address(this));

  uint balance1=IERC20(token1).balanceOf(address(this));

  //获得当前balance和上一次缓存的余额的差值

  //因为balance是动态变化的,reserve是静态变化的

  uint amount0=balance0.sub(_reserve0);

  uint amount1=balance1.sub(_reserve1);

  //计算手续费

  bool feeOn=_mintFee(_reserve0,_reserve1);

  //gas节省,必须在此处定义,因为totalSupply可以在_mintFee中更新

  //totalSupply是pair的凭证

  uint _totalSupply=totalSupply;//gas savings,must be defined here since totalSupply can update in _mintFee

  if(_totalSupply==0){

  //第一次铸币,也就是第一次注入流动性,值为根号k减去MINIMUM_LIQUIDITY,防止数据溢出

  liquidity=Math.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);

  //把MINIMUM_LIQUIDITY赋给地址0,永久锁住

  _mint(address(0),MINIMUM_LIQUIDITY);//permanently lock the first MINIMUM_LIQUIDITY tokens

  }else{

  //计算增量的token占总池子的比例,作为新铸币的数量

  //木桶法则,按最少的来,按当前投入的占池子总的比例增发

  liquidity=Math.min(amount0.mul(_totalSupply)/_reserve0,amount1.mul(_totalSupply)/_reserve1);

  }

  require(liquidity>0,'UniswapV2:INSUFFICIENT_LIQUIDITY_MINTED');

  //铸币,修改to的token数量及totalsupply

  //给to地址发凭证,同时pair合约的totalSupply增发同等的凭证

  _mint(to,liquidity);

  //更新时间加权平均价格

  _update(balance0,balance1,_reserve0,_reserve1);

  if(feeOn)kLast=uint(reserve0).mul(reserve1);//reserve0 and reserve1 are up-to-date

  emit Mint(msg.sender,amount0,amount1);

  }

  //this low-level function should be called from a contract which performs important safety checks

  function burn(address to)external lock returns(uint amount0,uint amount1){

  (uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings

  address _token0=token0;//gas savings

  address _token1=token1;//gas savings

  uint balance0=IERC20(_token0).balanceOf(address(this));

  uint balance1=IERC20(_token1).balanceOf(address(this));

  uint liquidity=balanceOf[address(this)];

  bool feeOn=_mintFee(_reserve0,_reserve1);

  uint _totalSupply=totalSupply;//gas savings,must be defined here since totalSupply can update in _mintFee

  //计算返回的amount0/1

  amount0=liquidity.mul(balance0)/_totalSupply;//using balances ensures pro-rata distribution

  amount1=liquidity.mul(balance1)/_totalSupply;//using balances ensures pro-rata distribution

  require(amount0>0&&amount1>0,'UniswapV2:INSUFFICIENT_LIQUIDITY_BURNED');

  _burn(address(this),liquidity);

  //_token0/1给to转amount0/1

  _safeTransfer(_token0,to,amount0);

  _safeTransfer(_token1,to,amount1);

  //获取转账后的balance

  balance0=IERC20(_token0).balanceOf(address(this));

  balance1=IERC20(_token1).balanceOf(address(this));

  //更新reserve0,reserve1和时间戳

  _update(balance0,balance1,_reserve0,_reserve1);

  if(feeOn)kLast=uint(reserve0).mul(reserve1);//reserve0 and reserve1 are up-to-date

  emit Burn(msg.sender,amount0,amount1,to);

  }

  //this low-level function should be called from a contract which performs important safety checks

  //交易函数

  //可以是token0-->token1,

  //也可以是token1-->token0

  //但参数中:amount0Out和amount1Out中有一个值是0

  function swap(

  uint amount0Out,

  uint amount1Out,

  address to,

  bytes calldata data

  )external lock

  {

  require(amount0Out>0||amount1Out>0,'UniswapV2:INSUFFICIENT_OUTPUT_AMOUNT');

  (uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings

  require(amount0Out<_reserve0&&amount1Out<_reserve1,'UniswapV2:INSUFFICIENT_LIQUIDITY');

  uint balance0;

  uint balance1;

  {//scope for _token{0,1},avoids stack too deep errors

  address _token0=token0;

  address _token1=token1;

  require(to!=_token0&&to!=_token1,'UniswapV2:INVALID_TO');

  //划转操作

  if(amount0Out>0)_safeTransfer(_token0,to,amount0Out);//optimistically transfer tokens

  if(amount1Out>0)_safeTransfer(_token1,to,amount1Out);//optimistically transfer tokens

  if(data.length>0)IUniswapV2Callee(to).uniswapV2Call(msg.sender,amount0Out,amount1Out,data);

  balance0=IERC20(_token0).balanceOf(address(this));

  balance1=IERC20(_token1).balanceOf(address(this));

  }

  uint amount0In=balance0>_reserve0-amount0Out?balance0-(_reserve0-amount0Out):0;

  uint amount1In=balance1>_reserve1-amount1Out?balance1-(_reserve1-amount1Out):0;

  require(amount0In>0||amount1In>0,'UniswapV2:INSUFFICIENT_INPUT_AMOUNT');

  {//scope for reserve{0,1}Adjusted,avoids stack too deep errors

  //防止数据溢出校验

  uint balance0Adjusted=balance0.mul(1000).sub(amount0In.mul(3));

  uint balance1Adjusted=balance1.mul(1000).sub(amount1In.mul(3));

  require(balance0Adjusted.mul(balance1Adjusted)>=uint(_reserve0).mul(_reserve1).mul(1000**2),'UniswapV2:K');

  }

  //更新

  _update(balance0,balance1,_reserve0,_reserve1);

  emit Swap(msg.sender,amount0In,amount1In,amount0Out,amount1Out,to);

  }

  //force balances to match reserves

  //强制balance以匹配储备

  function skim(address to)external lock{

  address _token0=token0;//gas savings

  address _token1=token1;//gas savings

  _safeTransfer(_token0,to,IERC20(_token0).balanceOf(address(this)).sub(reserve0));

  _safeTransfer(_token1,to,IERC20(_token1).balanceOf(address(this)).sub(reserve1));

  }

  //force reserves to match balances

  //强制储备以匹配balance

  function sync()external lock{

  _update(IERC20(token0).balanceOf(address(this)),IERC20(token1).balanceOf(address(this)),reserve0,reserve1);

  }

  }

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
区块链矿机的挖币模式开发源码示例(go语言版)
区块链矿机的挖币模式开发涉及到如何选择适合的挖矿算法和实现挖矿过程的相关功能。
23 0
区块链聚合交易所平台开发源码实例分析
区块链聚合交易所平台开发源码实例分析
44 0
区块链互助系统模式开发规则 | 区块链互助系统开发源码示例
区块链互助系统是一种基于区块链技术的分布式互助平台,它采用去中心化的方式,通过智能合约来实现互助计划,所有信息对全网络公开透明。在区块链互助系统中,每个会员都可以获得一个惟一的身份地址,可以记录其在互助系统中的参与情况和贡献值。同时,系统会根据会员的参与情况和贡献值,给予相应的奖励和优惠政策。
28 0
区块链金融风控系统开发解决方案(源码demo示例)
区块链金融风控系统是一种利用区块链技术建立的金融风控系统,旨在通过多种手段来保证金融交易的安全性和可信性。区块链技术具有去中心化、开放性、自治性和匿名性等特点,这些特性使得它能够保证数据的安全性和隐私性,并使得交易记录能够被全部追溯。同时,区块链技术还能够实现可编程性,从而使得金融交易能够按照事先设定的规则自动执行。
47 0
区块链农场养殖类游戏模式玩法及开发源码示例
区块链农场养殖游戏是一个去中心化的虚拟农场游戏,玩家可以在游戏中体验种植、养殖的乐趣。游戏中的农场是一个数字资产,可以用来购买土地、种子、化肥、农药等物品,并通过种植、养殖动物获得收益。
48 0
区块链众筹项目DAO开发(规则及源码分析)
区块链众筹项目的DAO(数据访问对象)是指一个独立的组织,负责管理众筹项目的数据和资源。它的主要职责是监督项目进展,记录交易和事务,确保所有交易和数据都被准确地记录和跟踪。
54 0
区块链游戏DAO模式开发步骤详情 |(源码demo实例分析)
区块链游戏的 DAO 模式开发是指利用 DAO 模式进行游戏开发和管理,其中 DAO 代表着“抵抗者”的意思。这种开发模式颠覆了传统游戏开发中的中心化管理方式,通过去除中介和减少不必要的成本来降低游戏开发的成本。
44 0
区块链场外交易平台开发方案设计(源码示例)
区块链场外交易平台是指在区块链系统之外进行交易的平台,也称为非链内交易。这些平台通常为交易双方提供交易确认、结算和交易记录等服务,以减少区块链系统中的交易风险和手续费用。常见的场外交易平台包括币安、BitMEX、Bittrex、Coinbase Pro、Bitfinex、KuCoin、Cryptovoxels、Exmo等。
34 0
+关注
开发V_MrsFu123
APP技术开发:I3570980718
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
IBM区块链技之道
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载