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

简介:   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");

  }

  }

目录
打赏
0
0
0
0
36
分享
相关文章
Kubernetes 集群的监控与维护策略
【4月更文挑战第23天】 在微服务架构日益盛行的当下,容器编排工具如 Kubernetes 成为了运维工作的重要环节。然而,随着集群规模的增长和复杂性的提升,如何确保 Kubernetes 集群的高效稳定运行成为了一大挑战。本文将深入探讨 Kubernetes 集群的监控要点、常见问题及解决方案,并提出一系列切实可行的维护策略,旨在帮助运维人员有效管理和维护 Kubernetes 环境,保障服务的持续可用性和性能优化。
Hbase入门(二)——安装与配置
本文讲述如何安装,部署,启停HBase集群,如何通过命令行对Hbase进行基本操作。 并介绍Hbase的配置文件。 在安装前需要将所有先决条件安装完成。
1084 0
Hbase入门(二)——安装与配置
如何设计一个监控平台(上篇)
在大型分布式微服务场景下,各个服务版本快速迭代,各类业务规模不断膨胀,同时监控的场景也在不断的发生变化,线上故障随时可能发生,各个平台错综复杂,如何保证线上服务稳定运行,同时提升运维效率,降低运维成本成了监控平台的挑战。
如何设计一个监控平台(上篇)
Eclipse创建Spring项目
本文介绍了在Eclipse中创建Spring项目的步骤,包括如何配置Tomcat服务器、创建项目、部署项目到Tomcat以及添加Spring框架所需的JAR包。
247 1
Eclipse创建Spring项目
dapp/swap去中心化交易所系统开发
DApp/Swap去中心化交易所系统基于区块链技术,通过智能合约实现数字资产的去中心化交易。其开发流程涵盖项目规划、智能合约编写与审计、前后端开发、系统测试部署及维护升级等多个环节,需确保系统合规、安全,并提供良好用户体验。同时,建立技术支持与活跃社区,促进用户交流与反馈。
IDEA如何对比不同分支某个文件的差异
【9月更文挑战第28天】该指南介绍了在IDEA中使用Git工具窗口进行分支对比的方法。首先,通过底部工具栏或菜单打开Git窗口;接着,在“Branches”选项卡中查看所有分支;然后选择要对比的分支和文件,并通过右键菜单启动对比;最后,在“Diff”视图中查看详细差异,包括新增和删除内容的颜色标记。此外,还提供了使用内置终端执行`git diff`命令进行对比的可选方法。
1590 4
如何在Java中实现智能合约与区块链集成
如何在Java中实现智能合约与区块链集成
怎样通过java用web3j查询以太坊交易信息?
刚开始使用web3j,我有一些基本的麻烦。 我已经可以成功如何获得一个EthBlock,并检索里面的所有信息。我想看看这个块中的交易列表,我该怎么做? 我可以调用: List transactions = ethBlock.getBlock().getTransactions(); 我应该能够浏览这个列表并获得有关每笔交易的信息。
6635 0
《Solidity 简易速速上手小册》第9章:DApp 开发与 Solidity 集成(2024 最新版)(上)
《Solidity 简易速速上手小册》第9章:DApp 开发与 Solidity 集成(2024 最新版)
180 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问