1、交易数据的签名方式

部分展示:

const myDexExec = require('../dex/myDexExec'); // 引入fs模块
const accounts = require('../util/accounts.json');
const uniRouterABI = require('../abi/uniRouter.json');
const Web3 = require('web3');
const uniABI = require('../abi/uni.json');
const uniFactoryABI = require('../abi/uniFactory.json');
const uniPairABI = require('../abi/uniPair.json');
const ethABI = require('../abi/WETH.json')
const config = require('../config');
const BigNumber = require("bignumber.js");
const Tx = require('ethereumjs-tx');
const util = require('../util/util');
const web3 = new Web3('https://rinkeby.infura.io/v3/45192e3bb296499a93ef0a73c0eb159a');//母账号转账
let symbol, amountToken, toAddress, amountEth;
symbol = 'uni';
amountToken = 1;//向每个子账户转账的 uni 币数量
amountEth = 0.1;//向每个子账户转账的 eth 数量
let addr = new Array();
// uniFACTORY = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f
const uniRouterAddress = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D';
const uniTokenAddress = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984';
const WETHAddress = '0xc778417e063141139fce010982780140aa0cd5ab';
const uniFactoryAddr= '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f';
const uniPairAddress = '0x4E99615101cCBB83A462dC4DE2bc1362EF1365e5';(async function () {       for (let i in accounts) {console.log('------------向子账户转账------------');await transferAccounts(accounts[i]);  // 母账户向所有子账户转账console.log('------------转账完成----------------')console.log('------------添加流动性--------------');await AddLiquidity(accounts[i]);  //账号添加流动性console.log('------------流动性完成--------------');console.log('------------账号提币----------------');await WithdrawMoney(accounts[i]);  //账号提币console.log('------------提币完成----------------');}})();async function transferAccounts(accounts){//母账户向所有子账户转账(uni,eth)toAddress = accounts.address;let addrLenth = addr.push(toAddress);if (addrLenth < 3) {//uni币转账let tokenTransResult = await myDexExec.transaction(symbol, amountToken, toAddress);if(!tokenTransResult){console.log(' uni 转账失败!! ');return;}//查询 uin 币的余额let uniBalancer = await myDexExec.getBalanceFromSymbol(symbol,toAddress);console.log(toAddress + " 成功---uni余额:" + uniBalancer);//eth币转账let ethTransResult = await myDexExec.transactionEth(amountEth, toAddress);if (!ethTransResult) {console.log(' eth 转账失败!! ')}//查询 ETH 币的余额let ETHbalancer = await myDexExec.getEthBalance(toAddress);console.log(toAddress + "成功---ETH余额:" + ETHbalancer);}}async function AddLiquidity(accounts){//账号添加流动性let toAddress = accounts.address;let Router = new web3.eth.Contract(uniRouterABI, uniRouterAddress);let Factory = new web3.eth.Contract(uniFactoryABI,uniFactoryAddr);//通过工厂对象调用工厂合约里面的 getPair()方法,传入 eth 和 uni 币的地址,得到交易对的地址 let pairAddress = await Factory.methods.getPair(WETHAddress,uniTokenAddress).call();//通过 unisawppair 合约的 abi 和通过工厂合约得到的交易对地址创建 pair 的实例let uniPair = new web3.eth.Contract(uniPairABI,pairAddress);//调取 Pair 合约的 getReserves()方法返回 reserve0、reserve1、blockTimestampLast 信息let ReservesAllMessage = await uniPair.methods.getReserves().call();//获取reserve0、reserve1 用以计算流动性//此处需要做字典排序let reserve0 = ReservesAllMessage['_reserve0'];let reserve1 = ReservesAllMessage['_reserve1'];let blockTimestampLast = ReservesAllMessage['_blockTimestampLast'];//调用 router 合约的 quote 方法,传入指定的 amoutA 数量通过流动性计算出返回的 amoutB 的数量const amout_A = 0.000000001;let decimals = new BigNumber(10**18);//做大数处理let amoutA =web3.utils.toHex(decimals.times(amout_A))let amoutB = await Router.methods.quote(amoutA,reserve1,reserve0).call();console.log('流动性计算出的另一种币的数量: '+ amoutB);//添加流动性函数 addLiquidityETH() 需要如下参数,在签名数据时需要携带ETH数量const tokenAddr = uniTokenAddress;const amountTokenDesired = amoutB;const amountTokenMin = amoutB-1;const amountETHMin = amoutA-1;const to = uniRouterAddress;const deadline = Date.now() + 600;let data = Router.methods.addLiquidityETH(tokenAddr,amountTokenDesired,amountTokenMin,amountETHMin,to,deadline).encodeABI();let nonce = await web3.eth.getTransactionCount(config.walletAddr);let getGasPrice = 1000000000;//1gas为1*10**9let txDataA = {nonce: web3.utils.toHex(nonce),//钱包地址的noncegasLimit: web3.utils.toHex(400000),gasPrice: web3.utils.toHex(getGasPrice),to: uniRouterAddress,//这笔交易的接收地址,为router合约from: toAddress,//交易的发起地址data: data,/编码过后的交易数据value:amoutA,//携带的ETH数量}let hash = await myDexExec.sendSigned(txDataA);//调用签名函数console.log(new Date().toLocaleString(),'transfer',hash);console.log('流动性添加后返回的哈希:' + hash);let result = '';let desc;do {let data = await myDexExec.checkTrade(hash);if (data && data.status) {desc = data.status;result = true;}if (!result) {await util.sleep(300);}} while (!result);return desc;
}async function WithdrawMoney(accounts){ // 账号提币//母账户向所有子账户提币(转账(uni,eth))const amount = 0.005;const toaddr = '0x88ded3010c9E9B2b2D1914B07C0d674281952d19';const uniTokenAddress = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984';let addrs = new Array();//账户地址let keys = new Array();//私钥let fromAddress = accounts.address;//获取的账户地址       let pKey = accounts.privateKey;//获取的私钥let addrLenth0 = addrs.push(fromAddress);//将 key 对的地址 fromAddress 存到一个指定的数组let keysLenth = keys.push(pKey)//将 key 存到一个指定的数组if (addrLenth0 < 3) {console.log("提币地址数组长度: "+ addrLenth0 + ' ---本次获取的账户地址为: ' + fromAddress);console.log('对应私钥 key 的数组长度:' + keysLenth + ' ---本次获取的私钥为: ' + pKey);//uni币转账let tokenTransResult = await transactionToken(fromAddress, amount, toaddr,uniTokenAddress,pKey);if(!tokenTransResult){console.log(' uni 转账失败!! ');return;}//查询 uin 币的余额let uniBalancer = await myDexExec.getBalanceFromSymbol(symbol,fromAddress);console.log(`${fromAddress} 提币_uni 余额:${uniBalancer}`);//eth币转账let ethTransResult = await transactionETH(fromAddress,amount,toaddr,pKey);if (!ethTransResult) {console.log(' eth 转账失败!! ')}//查询 ETH 币的余额let ETHbalancer = await myDexExec.getEthBalance(fromAddress);console.log(`${fromAddress} 提币_ETH余额:${ETHbalancer}`);}}async function transactionToken(fromAddress,amount,toaddr,tokenAddress,pKey){  //uni 交易(从 fromAddress 转 amount 个数量到 toaddr 地址,接受 amount 数量的币的币地址为 tokenAddress)      const uniAddress = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984';let decimals = new BigNumber(10**18);let amountTrans = web3.utils.toHex(decimals.times(amount));let uniContract = new web3.eth.Contract(uniABI, uniAddress);let data = uniContract.methods.transfer(toaddr,amountTrans).encodeABI();let nonce = await web3.eth.getTransactionCount(fromAddress);let getGasPrice = 1000000000;let txData = {nonce: web3.utils.toHex(nonce),gasLimit: web3.utils.toHex(400000),gasPrice: web3.utils.toHex(getGasPrice),to: tokenAddress,//需要调取合约,是币的合约地址from: fromAddress,data: data,}let hash = await sendSigned(txData,pKey);//调用签名函数console.log(new Date().toLocaleString(),'transfer',hash);let result = '';let desc;do{let data = await myDexExec.checkTrade(hash);if (data && data.status) {desc = data.status;result = true;}if (!result) {await util.sleep(300);}}while(!result);return desc;
}async function transactionETH(fromAddress,amount,toAddress,pKey){//ETH交易(从 fromAddress 地址转 amount 个数量到 toAddress 地址)// const toddress = '0x88ded3010c9E9B2b2D1914B07C0d674281952d19';let decimals = new BigNumber(10**18);let amountTrans = web3.utils.toHex(decimals.times(amount));let nonce = await web3.eth.getTransactionCount(fromAddress);let getGasPrice = 1000000000;let txData = {nonce: Web3.utils.toHex(nonce),gasLimit: web3.utils.toHex(400000),gasPrice: web3.utils.toHex(getGasPrice),to: toAddress,//ETH转账不用调取合约,直接是接收地址from:fromAddress,value:amountTrans,}let hash = await sendSigned(txData,pKey);//调用签名函数console.log(new Date().toLocaleString(),'transactionETH',hash);let result = '';let desc;do {let data = await myDexExec.checkTrade(hash);//交易完成检查,不检查会出现 nonce 相等的情况从而导致交易失败if (data && data.status) {desc = data.status;result = true;}if (!result) {await util.sleep(300);}} while (!result);return desc;
}async function sendSigned(txData, pKey) {//签名函数let privateKeys = Buffer.from(pKey,'hex');let transaction = new Tx.Transaction(txData,{chain: 'rinkeby'});transaction.sign(privateKeys);let serializedTx = transaction.serialize().toString('hex');return new Promise(function (resolve,reject){//获取的原始私钥如果含有 ‘0x’ 会出现类型长度的错误web3.eth.sendSignedTransaction('0x' + serializedTx,function(err,info){if (err) {return reject(err);}return resolve(info);})})
}

2、批量钱包地址的产生


const Wallet = require('ethereumjs-wallet');
const path = require('path');
let accounts = require('../util/accounts.json');
let saveFile = require('../util/saveFile');(async function () {console.log('-----现在开始筹建钱包地址---私钥----- ')for(let i=0; i<10; i++) {let ethWallet = Wallet.default.generate(false);let addr = ethWallet.getAddressString()console.log('地址: ' + addr);let keyString = ethWallet.getPrivateKeyString();let addrKey = keyString.substring(2);//去掉‘0x’console.log('对应的私钥:' + addrKey);let accountKey = Object.keys(accounts);let index = accountKey.length;accounts[`account${index}`] = {"address": addr,"privateKey": addrKey}}await saveFile.writeLogFile(path.resolve("./src/util/accounts.json"), JSON.stringify(accounts))console.log('---已完成---创建:');
})();

调用uniswap在测试网Rinkeby上发起签名交易(批量钱包地址生成)相关推荐

  1. 以太坊钱包开发系列3 - 展示钱包信息及发起签名交易

    最新内容会更新在主站深入浅出区块链社区 原文链接:使用 ethers.js 开发以太坊 Web 钱包 3 - 展示钱包信息及发起签名交易) 以太坊去中心化网页钱包开发系列,将从零开始开发出一个可以实际 ...

  2. 如何参与 Rainbow 的测试网?

    Rainbow 很荣幸成为Polygon Developer Support Program资助的项目之一,是一个以保险为核心的去中心化金融市场基础设施.目的是开发基于区块链技术的开放式保险协议应用, ...

  3. 关于测试网 gladios 节点公开部署

    亲爱的Aresians,我们正式地邀请您参与测试网 gladios 节点部署.上个月,我们向社区公开招募节点,大家热情地报名.有爱好者提议,开放节点的数量,让更多的Aresians参与其中. 对此,团 ...

  4. 全面理解EOS——2.加入EOS主网和测试网

    有了EOSIO软件,如何加入EOS的主网及测试网络?不同步EOS主网和测试网络全部数据,如何快速与EOS主网及测试网交互?如果想获得EOS主网及测试网络上所有的块信息,又该如何? 在线查看EOS网络信 ...

  5. 容器单机编排工具 Docker Compose,swap 限制,配置默认的CFS调度程序,Stress-ng 压力测试工具,docker官网镜像上传,及阿里云镜像上传,Docker 的资源限制

    目录 Docker Compose介绍   有版本问题 一键生成 docker-compose.yml docker官网镜像上传,及阿里云镜像上传 Docker 之分布式仓库 Harbor Docke ...

  6. 电脑上传网速怎么测试软件,宽带上传速度怎么测试 教你如何看电脑宽带上传速度...

    网速一般分为下载速度和上传速度,一般我们测算网速只是测试下载速度,只要电脑从网上下载速度快,用户通常比较满意.但你知道吗?上传速度也是值得关注的,尤其是在云网络时代,很多朋友都要传文件到网盘或者视频网 ...

  7. linux网络测速qerf,最新可用linux/centos 7上网络测速|测试网速脚本

    最新可用linux/centos 7上网络测速|测试网速脚本!centos 7上测试网速的一个小工具speedtest! Speedtest.net强大而知名的全球宽带网络速度测试网站,采用Flash ...

  8. 如何在Mac上测试宽带的网速(上传和下载速度)和网络质量

    在mac电脑上怎么测试网速有多快?在使用mac电脑时,想测试一下自己的网络下载速度,或上传速度时,怎么测速?一起来看看吧. 有时我们新安装了宽带后,比如100M.200M甚至千兆的宽带,想要检测下宽带 ...

  9. linux服务器上测试网速

    Linux界面都是命令行,也可以测试网速,用的是开源测试软件speedtest,linux上的版本是基于python的. 因为是下载githup版本库上的软件,linux上要安装git客户端,最简单的 ...

最新文章

  1. 怎样构建中文文本标注工具?(附工具、代码、论文等资源)
  2. my understanding for love
  3. 一次性汇总了 30+ 字符串常用处理方法
  4. mysql导入表结构命令是_mysql,命令导入\导出表结构或数据
  5. java 反射操作字段_x86上的Java最终字段没有操作?
  6. python以垂直方式输出hello world_python3提问:垂直输出Hello World,全部代码不超过2行....
  7. Docker 架构原理及简单使用
  8. python画柱状图-python plotly画柱状图代码实例
  9. 根据配置文件的值创建不同对象
  10. mysql中DateTime、Date、Time、TimeStamp区别
  11. 爆强的一句话,工作之余放松放松!
  12. 汇编语言集成编译器android,辰灿汇编语言集成开发环境
  13. 如何在线将flac格式转换成mp3音频
  14. Elasticsearch: 运用 Pinned query 来提高文档的排名 (7.5发行版新功能)
  15. Excel 图表与数据透视表制作
  16. Android api升级到31 导致的兼容性问题
  17. (加快设计)推荐一个SketchUp的3D模型库
  18. 你的软件也能挣钱 共享软件走向国际指南
  19. ssm+mysql+养老院信息管理系统 毕业设计-附源码181550
  20. java设计老鼠游戏_construct2制作小游戏——捉老鼠小游戏

热门文章

  1. 华为操作系统鸿蒙是linux,华为鸿蒙到底是个什么操作系统?
  2. ISME:南农张瑞福组揭示芽孢杆菌通过代谢互作刺激常驻根际微生物促进植物生长!...
  3. 20220414在MT6739的Android10系统下开启导航栏NavigationBar(虚拟按键)
  4. 关于jq工具安装shell脚本
  5. 计算机毕业设计Java学科竞赛管理系统(源码+系统+mysql数据库+Lw文档)
  6. win10+NVIDIA GTX 960M+CUDA 8.0+cudnn6.0安装
  7. python eol error错误
  8. 机器视觉系列(四)——相机部分(精简版)
  9. sqoop导出 建表_hive中orc表sqoop导出到mysql
  10. python发微信提醒天气冷了注意保暖_给客户发天气变冷注意保暖短信 提醒客户注意保暖的温馨句子...