欢迎光临散文网 会员登陆 & 注册

DAPP/LP/DEFI/IDO代币合约燃烧通缩流动性质押NFT挖矿分红系统开发案例源码/项目方案

2023-06-11 17:06 作者:bili_36625761919  | 我要投稿

  区块链是一种将数据区块按照时间顺序组合成的链式结构,是去中心化系统中各节点共享且共同维护的分布式数据账本,具体的:各节点由P2P组网方式相互连通和交互,受激励机制激励贡献自身算力,


  根据数据验证机制及传播协议,执行、验证并传播一段时间内生成的有效交易数据,同时利用Merkle树、哈希算法、时间戳等技术加密、生成数据区块,依据共识算法争夺记账权,最终获得记账权的节点(矿工),将其生成的数据区块链接到区块链主链上并获得相应奖励,其余节点更新区块链账本.


  智能合约一般具有值和状态两个属性,代码中用If-Then和What-If语句预置了合约条款的相应触发场景和响应规则,智能合约经多方共同协定、各自签署后随用户发起的交易(Transaction,Txn)提交,经P2P网络传播、矿工验证后存储在区块链特定区块中,用户得到返回的合约地址及合约接口等信息后即可通过发起交易来调用合约.


  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


DAPP/LP/DEFI/IDO代币合约燃烧通缩流动性质押NFT挖矿分红系统开发案例源码/项目方案的评论 (共 条)

分享到微博请遵守国家法律