数字货币去中心化交易所开发详情版丨数字货币去中心化交易所系统开发(原生开发)丨数字货币去中心化交易所现成源码模板

简介:  区块链技术是利用块链式数据结构来验证与存储数据、利用分布式节点共识算法来生成和更新数据、利用密码学的方式保证数据传输和访问的安全、利用由自动化脚本代码组成的智能合约来编程和操作数据的一种全新的分布式基础架构与计算范式。

  区块链是一个去中心化的分布式数据库,该数据库由一串使用密码学方法产生的数据区块有序连接而成,区块中包含有一定时间内产生的无法被篡改的数据记录信息。

  区块链技术是利用块链式数据结构来验证与存储数据、利用分布式节点共识算法来生成和更新数据、利用密码学的方式保证数据传输和访问的安全、利用由自动化脚本代码组成的智能合约来编程和操作数据的一种全新的分布式基础架构与计算范式。

  区块链技术模型自下而上包括数据层、网络层、共识层、激励层、合约层和应用层。每一层分别具备一项核心功能,不同层级之间相互配合,共同构建一个去中心的价值传输体系。

  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;

  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

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

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

  uint public price0CumulativeLast;

  uint public price1CumulativeLast;

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

  uint private unlocked=1;

  modifier lock(){

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

  unlocked=0;

  _;

  unlocked=1;

  }

  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{

  (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);

  constructor()public{

  factory=msg.sender;

  }

  //called once by the factory at time of deployment

  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

  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

  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;

  }

  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)

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

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

  feeOn=feeTo!=address(0);

  uint _kLast=kLast;//gas savings

  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);

  }

  }

  }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

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

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

  uint amount0=balance0.sub(_reserve0);

  uint amount1=balance1.sub(_reserve1);

  bool feeOn=_mintFee(_reserve0,_reserve1);

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

  if(_totalSupply==0){

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

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

  }else{

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

  }

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

  _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=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);

  _safeTransfer(_token0,to,amount0);

  _safeTransfer(_token1,to,amount1);

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

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

  _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

  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

  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

  function sync()external lock{

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

  }

  }

相关文章
|
12月前
|
供应链 安全 物联网
区块链去中心化交易所源码|去中心化交易系统开发
随着区块链技术的发展,应用的扩展,区块链软件开√发也随之应用到物联网、供应链管理等领域,其中包含区块链交Y所系统,区块链去中心化交Y所,依托于区块链技术,具有去中心化、匿名性、信息不可纂改等特点
|
10月前
|
算法 区块链
去中心化DAPP交易所系统开发方案与指南
去中心化带来的透明交易,不仅仅是简单地向参与者展示交易信息,更是为参与者提供了保障合法权益的机制。
|
算法 区块链 数据安全/隐私保护
区块链去中心化交易所系统开发成熟技术|开发指南与流程
Web3算法革命将会在多个领域产生深远的影响。首先,它将会对数据安全和隐私保护产生积极的作用
|
存储 监控 安全
区块链交易所开发、 数字货币交易所可二开添加定制化功能
区块链交易所开发是一项复杂的工程,需要技术团队精通多种技术领域,包括但不限于区块链技术、加密算法、交易系统设计、安全性和风险管理。开发一个成功的区块链交易所需要考虑到许多因素,包括用户体验、交易效率、安全性、合规性和可扩展性。首先,交易所的开发需要考虑用户体验。
|
区块链 数据库 开发者
数字货币去中心化交易所系统开发(详细功能)/案例设计/程序逻辑/成熟技术丨数字货币去中心化交易所开发源码项目
区块链技术,也被称之为分布式账本技术,是一种互联网数据库技术,其特点是去中心化、公开透明,让每个人均可参与数据库记录。区块链技术不是一个单项的技术,而是一个集成了多方面研究成果基础之上的综合性技术系统。There are three indispensable core technologies:consensus mechanism,Cryptography principle and distributed data storage.
DAPP去中心化交易所系统开发详细功能丨DAPP去中心化钱包系统开发规则详细/成熟技术/源码说明
 A smart contract is a computer program that runs on a blockchain. Programs include functions and data (also known as variables or parameters), which operate on data. The data used by the function needs to be stored in the computer's memory
|
安全 API 区块链
区块链钱包交易所系统开发详细逻辑丨数字货币交易所钱包系统开发(开发案例)及源码部署
  在区块链中,每个块包含了一定数量的交易信息和该块的唯一标识符,同时还包含了前一个块的哈希值。这样的设计保证了区块之间的顺序和完整性,一旦一个块被添加到区块链中,它就不可更改。这使得区块链成为一个安全可信的分布式账本,可用于记录和验证各种类型的交易。
|
存储 安全 前端开发
中心化交易平台开发:如何构建一个有效的数字货币交易所系统
随着加密货币市场的飞速增长,许多企业都在寻找有效的解决方案,以使其加密货币交易项目取得成功。而在这里,UI/UX 的作用无疑是巨大的。系统的运行方式完全取决于界面的简洁性、导航的有效性和用户旅程的顺畅性。 对于那些选择构建集中式加密交换系统的人来说,设计尤为重要。人们经常在没有丰富交易经验的情况下使用此类平台,因此应尽可能清晰直观。但是,如何为观众提供既简单又有效的交流方式呢? 这篇文章将解释集中交换,提供一些示例,并揭示如何设计这样一个系统来应对最常见的 UI/UX 挑战。
中心化交易平台开发:如何构建一个有效的数字货币交易所系统
|
PyTorch 区块链 算法框架/工具
数字货币交易所系统开发(详细方案)丨数字货币交易所系统开发(逻辑源码)
  从技术角度分析,区块链让数字资产价值流转的每一个节点都公开透明、有迹可循且不可篡改,这将会让Web3.0时代的一切交易变得更加真实可信
|
Java 区块链 C++
去中心化交易所开发运营版丨去中心化交易所系统开发(成熟及技术)丨去中心化交易所系统源码详细
  智能合约是对协议的翻译,区块链开发者用JAVA、C++和其他编程语言编写脚本,不会引起歧义或误解。这段代码翻译了一组自动执行和验证的规则。