去中心化交易所开发丨去中心化交易所系统开发(功能及逻辑)丨去中心化交易所系统源码部署

简介:   Web3.0的主要特征是开放、隐私和去中心化。其开放体现在Web2.0所谓的生态内、生态间界限将被打破,应用之间具有高度的组合性和复合性;Web3.0内部基于不同基础设施的应用之间可以被“跨链”协议解决互联互通问题。

  Web3.0的主要特征是开放、隐私和去中心化。其开放体现在Web2.0所谓的生态内、生态间界限将被打破,应用之间具有高度的组合性和复合性;Web3.0内部基于不同基础设施的应用之间可以被“跨链”协议解决互联互通问题。

  Web 3.0:移动互联网后下一阶段的互联网生态,主要通过区块链等技术手段,实现中心化的网络形态,模拟真实世界的感觉,实现虚拟、实现打破现实边界的互联网——区块链:是分布式节点存储数据的中心化数据结构,节点是提供计算力的计算机,是计算机或服务器,具有服务器端和客户端两种性质。

  springboot使用web3进行代币交换(uniswap):

  import java.math.BigDecimal;

  import java.math.BigInteger;

  import java.util.ArrayList;

  import java.util.List;

  import org.slf4j.Logger;

  import org.slf4j.LoggerFactory;

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.stereotype.Service;

  import org.web3j.abi.FunctionEncoder;

  import org.web3j.abi.TypeReference;

  import org.web3j.abi.datatypes.Address;

  import org.web3j.abi.datatypes.Function;

  import org.web3j.abi.datatypes.StaticArray;

  import org.web3j.abi.datatypes.Type;

  import org.web3j.abi.datatypes.generated.Uint256;

  import org.web3j.crypto.Credentials;

  import org.web3j.crypto.RawTransaction;

  import org.web3j.crypto.TransactionEncoder;

  import org.web3j.protocol.Web3j;

  import org.web3j.protocol.core.methods.response.EthGasPrice;

  import org.web3j.protocol.core.methods.response.EthGetTransactionCount;

  import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;

  import org.web3j.protocol.core.methods.response.EthSendTransaction;

  import org.web3j.protocol.http.HttpService;

  import org.web3j.utils.Convert;

  import org.web3j.utils.Numeric;

  import io.renren.common.utils.R;

  import net.sf.json.JSONObject;

  Service
  public class Uniswap{

  private static final Logger log=LoggerFactory.getLogger(Uniswap.class);

  Autowired

  private EthGas ethGas;

  Autowired

  private EthAccount ethAccount;

  /**

  *

  *param gasLimit gaslimit

  *param gasPrice gas费用

  *param host链路

  *param privateKey自己的私钥

  *param v要兑换的对应代币的

  *param contractAddress如用USDT兑换BTC,则此值是BTC的合约地址

  *param amountOutMin兑换代币的最小的个数

  *param sendTokenContractAddress如用USDT兑换BTC,则此值是USDT的合约地址

  *param approveAddress approve合约地址,设置此值可以合约内直接确认,无需调钱包插件确认

  *return

  */

  public R sendContract(BigInteger gasLimit,BigInteger gasPrice,String host,String privateKey,BigDecimal v,String contractAddress,BigInteger amountOutMin,

  String sendTokenContractAddress,String approveAddress){

  Web3j web3j=Web3j.build(new HttpService(host));

  try{

  Credentials credentials=Credentials.create(privateKey);

  String fromAddress=credentials.getAddress();

  //获取交易次数

  EthGetTransactionCount ethGetTransactionCount=ethAccount.getTradeCount(web3j,privateKey);

  //错误返回

  if(ethGetTransactionCount.hasError()){

  return R.error(ethGetTransactionCount.getError().getMessage());

  }

  BigInteger nonce=ethGetTransactionCount.getTransactionCount();

  //合约函数参数

  List<Address>addList=new ArrayList<Address>();

  addList.add(new Address(sendTokenContractAddress));

  addList.add(new Address(approveAddress));

  StaticArray<Address>address=new StaticArray<Address>(Address.class,addList){

  };

  //BigInteger amountOutMin=Numeric.toBigInt("0x8d5ff4d17003f1000");

  if(gasPrice.compareTo(BigInteger.valueOf(0))!=1){//没有输入gas时查询当前链路的gas

  EthGasPrice ethGasPrice=ethGas.getGasPrice(web3j);

  if(ethGasPrice.hasError()){

  return R.error(ethGasPrice.getError().getMessage());

  }

  gasPrice=ethGasPrice.getGasPrice();

  }

  //创建inputdata

  String data=createSwapMethod(amountOutMin,address,addList.size(),new Address(fromAddress),

  new Uint256(System.currentTimeMillis()/1000+300));

  //EthEstimateGas ethEstimateGas=ethGas.getEthEstimateGas(web3j,fromAddress,contractAddress,data);

  //if(ethEstimateGas.hasError()){

  //return R.error(ethEstimateGas.getError().getMessage());

  //}

  //BigInteger gasLimit=BigInteger.valueOf(910000);

  BigInteger value=Convert.toWei(String.valueOf(v.toString()),Convert.Unit.ETHER).toBigInteger();

  RawTransaction rawTransaction=RawTransaction.createTransaction(nonce,gasPrice,gasLimit,contractAddress,

  value,data);

  byte[]signedMessage=TransactionEncoder.signMessage(rawTransaction,credentials);

  String hexValue=Numeric.toHexString(signedMessage);

  log.info("hexValue:{}",hexValue);

  //发送交易

  EthSendTransaction ethSendTransaction=web3j.ethSendRawTransaction(hexValue).sendAsync().get();

  if(ethSendTransaction.hasError()){

  return R.error(ethSendTransaction.getError().getMessage());

  }

  log.info("transactionHash:{}",JSONObject.fromObject(ethSendTransaction));

  String transactionHash=ethSendTransaction.getTransactionHash();

  log.info("transactionHash:{}",transactionHash);

  EthGetTransactionReceipt ethGetTransactionReceipt=web3j.ethGetTransactionReceipt(transactionHash)

  .sendAsync().get();

  log.info("EthGetTransactionReceipt:{}",JSONObject.fromObject(ethGetTransactionReceipt));

  if(ethGetTransactionReceipt.hasError()){

  return R.error(ethGetTransactionReceipt.getError().getMessage());

  }

  return R.ok(transactionHash);

  }catch(Exception e){

  //TODO:handle exception

  web3j.shutdown();

  return R.error(e.getMessage());

  }finally{

  web3j.shutdown();

  }

  }

  SuppressWarnings("rawtypes")

  public static String createSwapMethod(BigInteger amountOutMin,StaticArray<Address>addressArray,int size,

  Address to,Uint256 deadline){

  List<Type>parametersList=new ArrayList<Type>();

  parametersList.add(new Uint256(amountOutMin));

  parametersList.add(new Uint256(128));

  parametersList.add(to);

  parametersList.add(deadline);

  parametersList.add(new Uint256(BigInteger.valueOf(size)));

  parametersList.add(addressArray);

  List<TypeReference<?>>outList=new ArrayList<>();

  Function function=new Function("swapExactETHForTokens",parametersList,outList);

  String encodedFunction=FunctionEncoder.encode(function);

  System.out.println(encodedFunction);

  //函数编码不正确,先替换

  return encodedFunction.replace("0x8fd067c2","0x7ff36ab5");

  }

  }

相关文章
|
8月前
|
供应链 安全 物联网
区块链去中心化交易所源码|去中心化交易系统开发
随着区块链技术的发展,应用的扩展,区块链软件开√发也随之应用到物联网、供应链管理等领域,其中包含区块链交Y所系统,区块链去中心化交Y所,依托于区块链技术,具有去中心化、匿名性、信息不可纂改等特点
|
9月前
|
安全 区块链
DAPP去中心化系统开发|DAPP质押模式系统开发(方案需求)
智能合约是一种以代码形式存在的合约,旨在在去中心化网络上执行和实施
|
5月前
|
存储 区块链 数据库
DAPP质押代币模式系统开发技术方案
相比之下,集中式计算则是将大部分计算功能从本地或者远程进行集中计算
|
6月前
|
算法 区块链
去中心化DAPP交易所系统开发方案与指南
去中心化带来的透明交易,不仅仅是简单地向参与者展示交易信息,更是为参与者提供了保障合法权益的机制。
|
8月前
|
存储 安全 BI
智能合约交易平台开发方案部署:实现安全高效的去中心化交易
随着区块链技术的发展和应用,智能合约交易平台作为区块链领域的重要组成部分,为各行业提供了去中心化的交易解决方案。本文将深入探讨智能合约交易平台的开发方案,旨在实现安高效的交易,提升交易的透明度和信任度,推动区块链技术在商业领域的广泛应用。
|
8月前
|
算法 区块链 数据安全/隐私保护
区块链去中心化交易所系统开发成熟技术|开发指南与流程
Web3算法革命将会在多个领域产生深远的影响。首先,它将会对数据安全和隐私保护产生积极的作用
|
9月前
|
Rust JavaScript 前端开发
|
9月前
|
网络安全 区块链
DAPP去中心化项目系统开发|DAPP质押流程分析
去中心化融需要去中心化的发行方式与之匹配package com.hou.test1;
|
10月前
|
存储 TensorFlow 区块链
去中心化交易所开发运营版丨去中心化交易所系统开发(案例项目)/方案设计/源码详细
  智能合约dapp开发技术是一种基于区块链技术的开发技术,它可以帮助开发者快速高效地开发出功能强大、可靠性高的dapp(去中心化应用)。dapp定制开发技术则是用于为dapp开发者提供更好的定制化开发服务,帮助开发者更快捷地构建出功能强大、可靠性高的dapp。
|
10月前
|
区块链 数据库 开发者
数字货币去中心化交易所系统开发(详细功能)/案例设计/程序逻辑/成熟技术丨数字货币去中心化交易所开发源码项目
区块链技术,也被称之为分布式账本技术,是一种互联网数据库技术,其特点是去中心化、公开透明,让每个人均可参与数据库记录。区块链技术不是一个单项的技术,而是一个集成了多方面研究成果基础之上的综合性技术系统。There are three indispensable core technologies:consensus mechanism,Cryptography principle and distributed data storage.