From 856be5909c7e87f146d9570669562d68eb6bb27e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8E=E4=BA=AE?= Date: Sun, 31 Oct 2021 21:07:03 +0800 Subject: [PATCH] =?UTF-8?q?mingle=E4=BD=9C=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Class 1/mingle/commonUtils.js | 145 ++++ Class 1/mingle/erc20.js | 31 + Class 1/mingle/ethToTokens.js | 128 +++ Class 1/mingle/package.json | 30 + Class 1/mingle/pancakeAbi/pancake.js | 752 ++++++++++++++++++ .../pancakeSwapETHForExactTokens.js | 36 + .../pancakeSwapExactTokensForETH.js | 33 + .../pancakeSwapExactTokensForTokens.js | 32 + Class 1/mingle/readBalance.js | 44 + Class 1/mingle/send.js | 84 ++ Class 1/mingle/tokenToEth.js | 131 +++ Class 1/mingle/tokenToToken.js | 132 +++ 12 files changed, 1578 insertions(+) create mode 100644 Class 1/mingle/commonUtils.js create mode 100644 Class 1/mingle/erc20.js create mode 100644 Class 1/mingle/ethToTokens.js create mode 100644 Class 1/mingle/package.json create mode 100644 Class 1/mingle/pancakeAbi/pancake.js create mode 100644 Class 1/mingle/pancakeAbi/pancakeSwapETHForExactTokens.js create mode 100644 Class 1/mingle/pancakeAbi/pancakeSwapExactTokensForETH.js create mode 100644 Class 1/mingle/pancakeAbi/pancakeSwapExactTokensForTokens.js create mode 100644 Class 1/mingle/readBalance.js create mode 100644 Class 1/mingle/send.js create mode 100644 Class 1/mingle/tokenToEth.js create mode 100644 Class 1/mingle/tokenToToken.js diff --git a/Class 1/mingle/commonUtils.js b/Class 1/mingle/commonUtils.js new file mode 100644 index 0000000..14e9283 --- /dev/null +++ b/Class 1/mingle/commonUtils.js @@ -0,0 +1,145 @@ +//这里用bsc 链举例子 +//加载web3的库 +const { mainModule } = require('process'); +var Web3 = require('web3'); +//创建 rpc 连接字符串 +var rpcstring = 'https://bsc-dataseed1.binance.org/' +//创建ws连接字符串 +var wstring = 'wss://bsc-ws-node.nariox.org:443'; + +var wscweb3 = new Web3(new Web3.providers.WebsocketProvider(wstring )); +var rpcweb3 = new Web3(new Web3.providers.HttpProvider(rpcstring )); + +//设置web3 使用rpcweb3模式 +web3 = rpcweb3; +//因为是通过网络查询,所以要使用 async模式才能查询,不适用 async 不会使用阻塞模式 +const getBNBBalance= async (address) => +{ + let result = await web3.eth.getBalance(address) + //由于使用的是大数模式,小数点有18位,所以获得的balance 要除以10^18次方才是正确的数据 + //或者使用自带的转换工具 + let balance = web3.utils.fromWei(result.toString(10), getweiname()); + //打印结果 + console.log("地址:" + address +"有" + balance +"个BNB"); + return balance; +} + + +var EthereumTx = require('ethereumjs-tx') + +//调用获取BNB余额 +getBNBBalance("0x088a8384bdC4Ec8702415b9fA838Dff3FD805bFC"); + +//使用该函数之前需要应用 erc20 的 abi接口 + +//通过小数点多少位,转换对应的数据 +function getweiname(tokendecimals = 18) { + weiname = 'ether'; + switch (tokendecimals) { + case 3: + weiname = "Kwei"; + break; + case 6: + weiname = 'mwei'; + break; + case 9: + weiname = 'gwei'; + break; + case 12: + weiname = 'microether '; + break; + case 15: + weiname = 'milliether'; + break; + case 18: + weiname = 'ether'; + break; + + } + return weiname; +} + +//将erc20 取出来 +const erc20 = require('./erc20.js') + +//获得代币数量 +const getTokenBalance = async (tokenaddress,address) => +{ + //创建代币的智能合约函数 + var tokenContract = new web3.eth.Contract(erc20, tokenaddress); + + //调用代币的智能合约获取余额功能 + let result= await tokenContract.methods.balanceOf(address).call(); + //获得代币有多少位小数 + let decimals = await tokenContract.methods.decimals().call(); + weiname = getweiname(decimals); + let tokenbalance = web3.utils.fromWei(result.toString(10), weiname); + + //获得代币的符号 + let symbol = await tokenContract.methods.symbol().call(); + + //打印结果 + console.log("地址:" +address +"有代币:" +symbol +"的数量是:"+tokenbalance); + return tokenbalance; +} + + +var walletaddress= "0x088a8384bdC4Ec8702415b9fA838Dff3FD805bFC" +var lqtLPAddress= "0x0a0c9448De5eD90dCbC56ea0f11E9337c2b3Dc1E" +var wbnbaddress = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c" +var lqtwallet = "0xbd2c43da85d007b0b3cd856fd55c299578d832bc" +//getTokenBalance(wbnbaddress ,walletaddress); +//getTokenBalance(lqtwallet ,walletaddress); +const main = async () => { + //要使用哪部分,就去掉哪部分的注释 + //第一部分,获得bnb的的数量 + getBNBBalance(walletaddress); + //第二部分,获得代币的数量 + getTokenBalance(wbnbaddress, walletaddress); + getTokenBalance(lqtwallet ,walletaddress); + + //第三部分,发送bnb + // send(); + //第四部分,pancake bnb 交换某个代币 + //swap() +} + +//这里是将签名的内容发送到区块链网络中的代码 +const signTransaction = async (fromAddress, toAddress, input, nonceNum, privKey, gasPrice, nbnb, gaslimit) => { + var serializedTx = getEthRawTx(fromAddress, toAddress, input, nonceNum, privKey, gasPrice, nbnb, gaslimit) + + // Comment out these three lines if you don't really want to send the TX right now + console.log(`Attempting to send signed tx: ${serializedTx.toString('hex')}`); + var receipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')); + console.log(`Receipt info: ${JSON.stringify(receipt, null, '\t')}`); + if (receipt.status == true) { + return true; + } + return false; +} + + +//这里是将交易用私钥签名部分 +function getEthRawTx(fromAddress, toAddress, input, nonceNum, privKey, gasPrice, nbnb, gaslimit) { + + var rawTransaction = { + "from": fromAddress, + "nonce": web3.utils.toHex(nonceNum), + "gasLimit": web3.utils.toHex(gaslimit), + "gasPrice": web3.utils.toHex(gasPrice), + "to": toAddress, + "value": web3.utils.toHex(nbnb), + "data": input, //设置num属性 + "chainId": 0x38 //4:Rinkeby, 3:Ropsten, 1:mainnet + }; + + var tx = new EthereumTx(rawTransaction); + tx.sign(privKey); + var serializedTx = tx.serialize(); + return serializedTx; +} + +module.exports = {getBNBBalance,getTokenBalance,getweiname,signTransaction,getEthRawTx} + +//启动程序 + diff --git a/Class 1/mingle/erc20.js b/Class 1/mingle/erc20.js new file mode 100644 index 0000000..75595aa --- /dev/null +++ b/Class 1/mingle/erc20.js @@ -0,0 +1,31 @@ +//这部分单独用一个erc20.js封装 +module.exports=[ { + constant: true, + inputs: [{ name: 'src', type: 'address' }], + name: 'balanceOf', + outputs: [{ name: '', type: 'uint256' }], + payable: false, + stateMutability: 'view', + type: 'function' + }, + { + constant: true, + inputs: [], + name: 'decimals', + outputs: [{ name: '', type: 'uint256' }], + payable: false, + stateMutability: 'view', + type: 'function' + }, + { + constant: true, + inputs: [], + name: 'symbol', + outputs: [ {"internalType": "string", + "name": "", + "type": "string"}], + payable: false, + stateMutability: 'view', + type: 'function' + } +]; \ No newline at end of file diff --git a/Class 1/mingle/ethToTokens.js b/Class 1/mingle/ethToTokens.js new file mode 100644 index 0000000..28bf0e0 --- /dev/null +++ b/Class 1/mingle/ethToTokens.js @@ -0,0 +1,128 @@ +//这里用bsc 链举例子 +//加载web3的库 +const { mainModule } = require('process'); +//将erc20 取出来 +const erc20 = require('./erc20.js'); +var moment = require('moment'); +const { waitForDebugger } = require('inspector'); +const util = require('ethereumjs-util'); +const pancake = require('./pancakeAbi/pancakeSwapETHForExactTokens.js') +const commonUtils = require('./commonUtils.js'); + +var Web3 = require('web3'); +//创建 rpc 连接字符串 +var rpcstring = 'https://bsc-dataseed1.binance.org/' +//创建ws连接字符串 +var wstring = 'wss://bsc-ws-node.nariox.org:443'; + +var wscweb3 = new Web3(new Web3.providers.WebsocketProvider(wstring )); +var rpcweb3 = new Web3(new Web3.providers.HttpProvider(rpcstring )); + +//设置web3 使用rpcweb3模式 +web3 = rpcweb3; +var prikeystring = "" +var walletaddress= "0x088a8384bdC4Ec8702415b9fA838Dff3FD805bFC" +var toAddress = "0x39faba07d76c3ce23e75604abf51b729a51a19cd" +var wbnbaddress = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c" +var lqtwallet = "0xbd2c43da85d007b0b3cd856fd55c299578d832bc" + +var lqtLPAddress= "0x0a0c9448De5eD90dCbC56ea0f11E9337c2b3Dc1E" +var addresspancake = "0x10ed43c718714eb63d5aa57b78b54704e256024e" + + + +prikeystring = prikeystring.replace("0x", "") + +function getPriKey(prikeystring) { + const privKey = new Buffer.from(prikeystring, "hex"); + return privKey; +} + +//这里的privKey 是私钥 +var priKey = getPriKey(prikeystring); + +//获取input内容 +function swaptokeninput(wbnbadddress, toaddress, tokenamountIn, amountOut, tokenaddress, tokendecimals = 18,) { + + weiname = commonUtils.getweiname(tokendecimals); + + var path = [ wbnbadddress,tokenaddress]; + + var amountOutMin = web3.utils.toWei(amountOut.toString(10), weiname); + const now = moment().unix(); + const DEADLINE = now + 60 * 20; //往后延迟20分钟 + + var deadline = (DEADLINE).toString(10); + console.log("inputbefore"); + console.log(pancake[0]); + var input = web3.eth.abi.encodeFunctionCall(pancake[0], [amountOutMin, path, toaddress, deadline]); + console.log(input) + return input; +} + + +const swap = async () => { + + //获得自己的地址 + var fromAddress = "0x" + util.privateToAddress(priKey).toString('hex'); + //要交换的tokenadrress + + var tokenContract = new web3.eth.Contract(erc20, lqtwallet); + + //获得代币有多少位小数 + let decimals = await tokenContract.methods.decimals().call(); + + // 设置交易滑点,直接调用合约可以设置100的滑点,这里设置50的滑点 + var los = 50; + // 假设要购买5个BNB的tokenA + var nbnb = 0.0001; + + //获取交易对代币比例 + let lpTokenBalance = await commonUtils.getTokenBalance(lqtwallet,lqtLPAddress) + let lpBnBBalance = await commonUtils.getBNBBalance(wbnbaddress,lqtLPAddress) + var rate = lpTokenBalance/lpBnBBalance; + var ntoken = (nbnb * (100 - los) / 100 * rate).toFixed(10); + + //获得input 内容 + + + //创建交易执行智能合约 + + + var toAddress = addresspancake + //获得下一次交易的数 + var nonceCnt = await web3.eth.getTransactionCount(fromAddress); + + //交易需要5个BNB + nbnb = web3.utils.toWei((nbnb).toString(10), 'ether'); + //设置gasprice 为 5G wei + var gasPrice = web3.utils.toWei((5).toString(10), 'Gwei'); + //设置 gaslimit 为 420000 + var gaslimit = 420000 + + var input = swaptokeninput(wbnbaddress, fromAddress, nbnb, ntoken, lqtwallet, decimals) + let reslut = await commonUtils.signTransaction(fromAddress, toAddress, input, nonceCnt, priKey, gasPrice, nbnb, gaslimit) + if (reslut) { + console.log("交易成功") + } + else { + console.log("交易失败") + } +} + + + + + + + + + +const main = async () => { + + //console.log("infoUtils:",infoUtils) + //infoUtils.getBNBBalance(wbnbaddress,walletaddress) + swap() +} + +main() \ No newline at end of file diff --git a/Class 1/mingle/package.json b/Class 1/mingle/package.json new file mode 100644 index 0000000..f38e896 --- /dev/null +++ b/Class 1/mingle/package.json @@ -0,0 +1,30 @@ +{ + "name": "watch", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "chalk": "^4.1.2", + "ethereumjs-abi": "^0.6.8", + "ethereumjs-tx": "^1.3.7", + "ethjs-contract": "^0.2.3", + "ethjs-query": "^0.3.8", + "fs": "0.0.1-security", + "got": "^9.6.0", + "httpntlm": "^1.7.7", + "jsonobject": "^2.0.0", + "kafka-node": "^5.0.0", + "line-reader": "^0.4.0", + "moment": "^2.29.1", + "readline": "^1.3.0", + "silly-datetime": "^0.1.2", + "single-line-log": "^1.1.2", + "web3": "^1.3.4", + "web3-eth-abi": "^1.3.4" + } +} diff --git a/Class 1/mingle/pancakeAbi/pancake.js b/Class 1/mingle/pancakeAbi/pancake.js new file mode 100644 index 0000000..15eea84 --- /dev/null +++ b/Class 1/mingle/pancakeAbi/pancake.js @@ -0,0 +1,752 @@ +module.exports = [{ + "inputs": [{ + "internalType": "address", + "name": "_factory", + "type": "address" + }, { + "internalType": "address", + "name": "_WETH", + "type": "address" + }], + "stateMutability": "nonpayable", + "type": "constructor" +}, { + "inputs": [], + "name": "WETH", + "outputs": [{ + "internalType": "address", + "name": "", + "type": "address" + }], + "stateMutability": "view", + "type": "function" +}, { + "inputs": [{ + "internalType": "address", + "name": "tokenA", + "type": "address" + }, { + "internalType": "address", + "name": "tokenB", + "type": "address" + }, { + "internalType": "uint256", + "name": "amountADesired", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountBDesired", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountAMin", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountBMin", + "type": "uint256" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "addLiquidity", + "outputs": [{ + "internalType": "uint256", + "name": "amountA", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountB", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "liquidity", + "type": "uint256" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "address", + "name": "token", + "type": "address" + }, { + "internalType": "uint256", + "name": "amountTokenDesired", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountTokenMin", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountETHMin", + "type": "uint256" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "addLiquidityETH", + "outputs": [{ + "internalType": "uint256", + "name": "amountToken", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountETH", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "liquidity", + "type": "uint256" + }], + "stateMutability": "payable", + "type": "function" +}, { + "inputs": [], + "name": "factory", + "outputs": [{ + "internalType": "address", + "name": "", + "type": "address" + }], + "stateMutability": "view", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountOut", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "reserveIn", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "reserveOut", + "type": "uint256" + }], + "name": "getAmountIn", + "outputs": [{ + "internalType": "uint256", + "name": "amountIn", + "type": "uint256" + }], + "stateMutability": "pure", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountIn", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "reserveIn", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "reserveOut", + "type": "uint256" + }], + "name": "getAmountOut", + "outputs": [{ + "internalType": "uint256", + "name": "amountOut", + "type": "uint256" + }], + "stateMutability": "pure", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountOut", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }], + "name": "getAmountsIn", + "outputs": [{ + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }], + "stateMutability": "view", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountIn", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }], + "name": "getAmountsOut", + "outputs": [{ + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }], + "stateMutability": "view", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountA", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "reserveA", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "reserveB", + "type": "uint256" + }], + "name": "quote", + "outputs": [{ + "internalType": "uint256", + "name": "amountB", + "type": "uint256" + }], + "stateMutability": "pure", + "type": "function" +}, { + "inputs": [{ + "internalType": "address", + "name": "tokenA", + "type": "address" + }, { + "internalType": "address", + "name": "tokenB", + "type": "address" + }, { + "internalType": "uint256", + "name": "liquidity", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountAMin", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountBMin", + "type": "uint256" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "removeLiquidity", + "outputs": [{ + "internalType": "uint256", + "name": "amountA", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountB", + "type": "uint256" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "address", + "name": "token", + "type": "address" + }, { + "internalType": "uint256", + "name": "liquidity", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountTokenMin", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountETHMin", + "type": "uint256" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "removeLiquidityETH", + "outputs": [{ + "internalType": "uint256", + "name": "amountToken", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountETH", + "type": "uint256" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "address", + "name": "token", + "type": "address" + }, { + "internalType": "uint256", + "name": "liquidity", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountTokenMin", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountETHMin", + "type": "uint256" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "removeLiquidityETHSupportingFeeOnTransferTokens", + "outputs": [{ + "internalType": "uint256", + "name": "amountETH", + "type": "uint256" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "address", + "name": "token", + "type": "address" + }, { + "internalType": "uint256", + "name": "liquidity", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountTokenMin", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountETHMin", + "type": "uint256" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }, { + "internalType": "bool", + "name": "approveMax", + "type": "bool" + }, { + "internalType": "uint8", + "name": "v", + "type": "uint8" + }, { + "internalType": "bytes32", + "name": "r", + "type": "bytes32" + }, { + "internalType": "bytes32", + "name": "s", + "type": "bytes32" + }], + "name": "removeLiquidityETHWithPermit", + "outputs": [{ + "internalType": "uint256", + "name": "amountToken", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountETH", + "type": "uint256" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "address", + "name": "token", + "type": "address" + }, { + "internalType": "uint256", + "name": "liquidity", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountTokenMin", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountETHMin", + "type": "uint256" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }, { + "internalType": "bool", + "name": "approveMax", + "type": "bool" + }, { + "internalType": "uint8", + "name": "v", + "type": "uint8" + }, { + "internalType": "bytes32", + "name": "r", + "type": "bytes32" + }, { + "internalType": "bytes32", + "name": "s", + "type": "bytes32" + }], + "name": "removeLiquidityETHWithPermitSupportingFeeOnTransferTokens", + "outputs": [{ + "internalType": "uint256", + "name": "amountETH", + "type": "uint256" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "address", + "name": "tokenA", + "type": "address" + }, { + "internalType": "address", + "name": "tokenB", + "type": "address" + }, { + "internalType": "uint256", + "name": "liquidity", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountAMin", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountBMin", + "type": "uint256" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }, { + "internalType": "bool", + "name": "approveMax", + "type": "bool" + }, { + "internalType": "uint8", + "name": "v", + "type": "uint8" + }, { + "internalType": "bytes32", + "name": "r", + "type": "bytes32" + }, { + "internalType": "bytes32", + "name": "s", + "type": "bytes32" + }], + "name": "removeLiquidityWithPermit", + "outputs": [{ + "internalType": "uint256", + "name": "amountA", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountB", + "type": "uint256" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountOut", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapETHForExactTokens", + "outputs": [{ + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }], + "stateMutability": "payable", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountOutMin", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapExactETHForTokens", + "outputs": [{ + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }], + "stateMutability": "payable", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountOutMin", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapExactETHForTokensSupportingFeeOnTransferTokens", + "outputs": [], + "stateMutability": "payable", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountIn", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountOutMin", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapExactTokensForETH", + "outputs": [{ + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountIn", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountOutMin", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapExactTokensForETHSupportingFeeOnTransferTokens", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountIn", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountOutMin", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapExactTokensForTokens", + "outputs": [{ + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountIn", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountOutMin", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapExactTokensForTokensSupportingFeeOnTransferTokens", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountOut", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountInMax", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapTokensForExactETH", + "outputs": [{ + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{ + "internalType": "uint256", + "name": "amountOut", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountInMax", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapTokensForExactTokens", + "outputs": [{ + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }], + "stateMutability": "nonpayable", + "type": "function" +}, { + "stateMutability": "payable", + "type": "receive" +}] \ No newline at end of file diff --git a/Class 1/mingle/pancakeAbi/pancakeSwapETHForExactTokens.js b/Class 1/mingle/pancakeAbi/pancakeSwapETHForExactTokens.js new file mode 100644 index 0000000..74f10be --- /dev/null +++ b/Class 1/mingle/pancakeAbi/pancakeSwapETHForExactTokens.js @@ -0,0 +1,36 @@ +module.exports = [ + { + "inputs": [ + { + "internalType": "uint256", + "name": "amountOut", + "type": "uint256" + }, + { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + } + ], + "name": "swapETHForExactTokens", + "outputs": [ + { + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + } + ], + "stateMutability": "payable", + "type": "function" +}, +]; diff --git a/Class 1/mingle/pancakeAbi/pancakeSwapExactTokensForETH.js b/Class 1/mingle/pancakeAbi/pancakeSwapExactTokensForETH.js new file mode 100644 index 0000000..ebdc5ff --- /dev/null +++ b/Class 1/mingle/pancakeAbi/pancakeSwapExactTokensForETH.js @@ -0,0 +1,33 @@ +module.exports = [ + { + "inputs": [{ + "internalType": "uint256", + "name": "amountIn", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountOutMin", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapExactTokensForETH", + "outputs": [{ + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }], + "stateMutability": "nonpayable", + "type": "function" +} +] \ No newline at end of file diff --git a/Class 1/mingle/pancakeAbi/pancakeSwapExactTokensForTokens.js b/Class 1/mingle/pancakeAbi/pancakeSwapExactTokensForTokens.js new file mode 100644 index 0000000..393558b --- /dev/null +++ b/Class 1/mingle/pancakeAbi/pancakeSwapExactTokensForTokens.js @@ -0,0 +1,32 @@ +module.exports = [ { + "inputs": [{ + "internalType": "uint256", + "name": "amountIn", + "type": "uint256" + }, { + "internalType": "uint256", + "name": "amountOutMin", + "type": "uint256" + }, { + "internalType": "address[]", + "name": "path", + "type": "address[]" + }, { + "internalType": "address", + "name": "to", + "type": "address" + }, { + "internalType": "uint256", + "name": "deadline", + "type": "uint256" + }], + "name": "swapExactTokensForTokens", + "outputs": [{ + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }], + "stateMutability": "nonpayable", + "type": "function" +} +] \ No newline at end of file diff --git a/Class 1/mingle/readBalance.js b/Class 1/mingle/readBalance.js new file mode 100644 index 0000000..395990c --- /dev/null +++ b/Class 1/mingle/readBalance.js @@ -0,0 +1,44 @@ +//这里用bsc 链举例子 +//加载web3的库 +const { mainModule } = require('process'); +var Web3 = require('web3'); +//创建 rpc 连接字符串 +var rpcstring = 'https://bsc-dataseed1.binance.org/' +//创建ws连接字符串 +var wstring = 'wss://bsc-ws-node.nariox.org:443'; + +var wscweb3 = new Web3(new Web3.providers.WebsocketProvider(wstring )); +var rpcweb3 = new Web3(new Web3.providers.HttpProvider(rpcstring )); + +//设置web3 使用rpcweb3模式 +web3 = rpcweb3; + +const commonUtils = require('./commonUtils.js'); + +var myAddress= "0x088a8384bdC4Ec8702415b9fA838Dff3FD805bFC" +var lqtAddress = "0xbd2c43da85d007b0b3cd856fd55c299578d832bc" +var dndAddress = "0x14c358b573a4ce45364a3dbd84bbb4dae87af034" +var jojoAddress = "0x30938fd95ee9ec20e1c0344520e098ec6c3991d6" +var dytAddress = "0x740623d2c797b7d8d1ecb98e9b4afcf99ec31e14" +var doggyAddress = "0x74926b3d118a63f6958922d3dc05eb9c6e6e00c6" +var cnsAddress = "0xf6cb4ad242bab681effc5de40f7c8ff921a12d63" +var tlmAddress = "0x2222227e22102fe3322098e4cbfe18cfebd57c95" +var phyAddress = "0xae63595ed0bcfddeff2ebb74a20ae96727783a67" +var gbyteAddress = "0xeb34de0c4b2955ce0ff1526cdf735c9e6d249d09" +var tmfAddress = "0x7122f1c0777900b959573132deac9a7209741aa0" +const main = async () => { + + let balance1 = await commonUtils.getTokenBalance(lqtAddress,myAddress) + let balance2 = await commonUtils.getTokenBalance(dndAddress,myAddress) + let balance3 = await commonUtils.getTokenBalance(jojoAddress,myAddress) + let balance4 = await commonUtils.getTokenBalance(dytAddress,myAddress) + let balance5 = await commonUtils.getTokenBalance(doggyAddress,myAddress) + let balance6 = await commonUtils.getTokenBalance(cnsAddress,myAddress) + let balance7 = await commonUtils.getTokenBalance(tlmAddress,myAddress) + let balance8 = await commonUtils.getTokenBalance(phyAddress,myAddress) + let balance9 = await commonUtils.getTokenBalance(gbyteAddress,myAddress) + let balance10 = await commonUtils.getTokenBalance(tmfAddress,myAddress) + +} + +main() \ No newline at end of file diff --git a/Class 1/mingle/send.js b/Class 1/mingle/send.js new file mode 100644 index 0000000..f297ea6 --- /dev/null +++ b/Class 1/mingle/send.js @@ -0,0 +1,84 @@ +//这里用bsc 链举例子 +//加载web3的库 +const { mainModule } = require('process'); +var Web3 = require('web3'); +//创建 rpc 连接字符串 +var rpcstring = 'https://bsc-dataseed1.binance.org/' +//创建ws连接字符串 +var wstring = 'wss://bsc-ws-node.nariox.org:443'; + +var wscweb3 = new Web3(new Web3.providers.WebsocketProvider(wstring )); +var rpcweb3 = new Web3(new Web3.providers.HttpProvider(rpcstring )); + +//设置web3 使用rpcweb3模式 +web3 = rpcweb3; +var prikeystring = "" +var walletaddress= "0x088a8384bdC4Ec8702415b9fA838Dff3FD805bFC" +var toAddress = "" +var wbnbaddress = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c" +var lqtwallet = "0xbd2c43da85d007b0b3cd856fd55c299578d832bc" + + +var EthereumTx = require('ethereumjs-tx') +//const EthereumTx = require('ethereumjs-tx').Transaction +const util = require('ethereumjs-util') +//这里是加载私钥的部分 + + + +prikeystring = prikeystring.replace("0x", "") + +function getPriKey(prikeystring) { + + const privKey = new Buffer.from(prikeystring, "hex"); + return privKey; +} + +//这里的privKey 是私钥 +var priKey = getPriKey(prikeystring); + + +const send = async () => { + //获得自己的地址 + //var fromAddress = "0x" + util.privateToAddress(priKey).toString('hex'); + var fromAddress = walletaddress + //发送给谁 + var toAddress = walletaddress + + var nsendBNB = 0.0001 + //假设交易 0.008个bnb + var nbnb = web3.utils.toWei((nsendBNB).toString(10), 'ether'); + //设置gasprice 为 5G wei + var gasPrice = web3.utils.toWei((5).toString(10), 'Gwei'); + //设置 gaslimit 为 420000 + var gaslimit = 420000 + //没有调用智能合约,将input设置为空 + var input = "" + //获得下一次交易的数 + console.log("发送地址是:" + fromAddress) + var nonceCnt = await web3.eth.getTransactionCount(fromAddress); + let reslut = await signTransaction(fromAddress, toAddress, input, nonceCnt, priKey, gasPrice, nbnb, gaslimit) + if (reslut) { + console.log("交易成功") + } + else { + console.log("交易失败") + } + +} + +const main = async () => { + //要使用哪部分,就去掉哪部分的注释 + //第一部分,获得bnb的的数量 + //getBNBBalance(walletaddress); + //第二部分,获得代币的数量 + //getTokenBalance(wbnbaddress, walletaddress); + //getTokenBalance(lqtwallet ,walletaddress); + + //第三部分,发送bnb + send(); + //第四部分,pancake bnb 交换某个代币 + //swap() +} + +main() \ No newline at end of file diff --git a/Class 1/mingle/tokenToEth.js b/Class 1/mingle/tokenToEth.js new file mode 100644 index 0000000..46c7b79 --- /dev/null +++ b/Class 1/mingle/tokenToEth.js @@ -0,0 +1,131 @@ +//这里用bsc 链举例子 +//加载web3的库 +const { mainModule } = require('process'); +//将erc20 取出来 +const erc20 = require('./erc20.js'); +var moment = require('moment'); +const { waitForDebugger } = require('inspector'); +const util = require('ethereumjs-util'); +const pancake = require('./pancakeAbi/pancakeSwapExactTokensForETH.js') +const commonUtils = require('./commonUtils.js'); + +var Web3 = require('web3'); +//创建 rpc 连接字符串 +var rpcstring = 'https://bsc-dataseed1.binance.org/' +//创建ws连接字符串 +var wstring = 'wss://bsc-ws-node.nariox.org:443'; + +var wscweb3 = new Web3(new Web3.providers.WebsocketProvider(wstring )); +var rpcweb3 = new Web3(new Web3.providers.HttpProvider(rpcstring )); + +//设置web3 使用rpcweb3模式 +web3 = rpcweb3; +var prikeystring = "" +var walletaddress= "0x088a8384bdC4Ec8702415b9fA838Dff3FD805bFC" +var toAddress = "0x088a8384bdC4Ec8702415b9fA838Dff3FD805bFC" +var wbnbaddress = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c" +var lqtwallet = "0xbd2c43da85d007b0b3cd856fd55c299578d832bc" + +var lqtLPAddress= "0x0a0c9448De5eD90dCbC56ea0f11E9337c2b3Dc1E" +var addresspancake = "0x10ed43c718714eb63d5aa57b78b54704e256024e" + + + +prikeystring = prikeystring.replace("0x", "") + +function getPriKey(prikeystring) { + const privKey = new Buffer.from(prikeystring, "hex"); + return privKey; +} + +//这里的privKey 是私钥 +var priKey = getPriKey(prikeystring); + +//获取input内容 +function swaptokeninput(wbnbadddress, toaddress, tokenamountIn, amountOut, tokenaddress, tokendecimals = 18,) { + + weiname = commonUtils.getweiname(tokendecimals); + + var path = [tokenaddress,wbnbadddress]; + + var amountOutMin = web3.utils.toWei(amountOut.toString(10), weiname); + const now = moment().unix(); + const DEADLINE = now + 60 * 20; //往后延迟20分钟 + + var deadline = (DEADLINE).toString(10); + console.log("inputbefore"); + console.log(pancake[0]); + var input = web3.eth.abi.encodeFunctionCall(pancake[0], [ amountOutMin , 0, path, toaddress, deadline]); + console.log(input) + return input; +} + + +const swap = async () => { + + //获得自己的地址 + var fromAddress = "0x" + util.privateToAddress(priKey).toString('hex'); + //要交换的tokenadrress + + var tokenContract = new web3.eth.Contract(erc20, lqtwallet); + + //获得代币有多少位小数 + let decimals = await tokenContract.methods.decimals().call(); + + // 设置交易滑点,直接调用合约可以设置100的滑点,这里设置50的滑点 + var los = 50; + // 假设要购买5个BNB的tokenA + var ntoken = 0.01; + + //获取交易对代币比例 + let lpTokenBalance = await commonUtils.getTokenBalance(lqtwallet,lqtLPAddress) + + let lpBnBBalance = await commonUtils.getBNBBalance(wbnbaddress,lqtLPAddress) + + var rate = lpBnBBalance/lpTokenBalance; + var nbnb = (ntoken * (100 - los) / 100 * rate).toFixed(10); + + + //获得input 内容 + + + //创建交易执行智能合约 + + + var toAddress = addresspancake + //获得下一次交易的数 + var nonceCnt = await web3.eth.getTransactionCount(fromAddress); + + //交易需要5个BNB + nbnb = web3.utils.toWei((nbnb).toString(10), 'ether'); + //设置gasprice 为 5G wei + var gasPrice = web3.utils.toWei((5).toString(10), 'Gwei'); + //设置 gaslimit 为 420000 + var gaslimit = 420000 + + var input = swaptokeninput(wbnbaddress, fromAddress,nbnb , ntoken, lqtwallet, decimals) + let reslut = await commonUtils.signTransaction(fromAddress, toAddress, input, nonceCnt, priKey, gasPrice, 0, gaslimit) + if (reslut) { + console.log("交易成功") + } + else { + console.log("交易失败") + } +} + + + + + + + + + +const main = async () => { + + //console.log("infoUtils:",infoUtils) + //infoUtils.getBNBBalance(wbnbaddress,walletaddress) + swap() +} + +main() \ No newline at end of file diff --git a/Class 1/mingle/tokenToToken.js b/Class 1/mingle/tokenToToken.js new file mode 100644 index 0000000..0ede939 --- /dev/null +++ b/Class 1/mingle/tokenToToken.js @@ -0,0 +1,132 @@ +//这里用bsc 链举例子 +//加载web3的库 +const { mainModule } = require('process'); +//将erc20 取出来 +const erc20 = require('./erc20.js'); +var moment = require('moment'); +const { waitForDebugger } = require('inspector'); +const util = require('ethereumjs-util'); +const pancake = require('./pancakeAbi/pancakeSwapExactTokensForTokens.js') +const commonUtils = require('./commonUtils.js'); + +var Web3 = require('web3'); +//创建 rpc 连接字符串 +var rpcstring = 'https://bsc-dataseed1.binance.org/' +//创建ws连接字符串 +var wstring = 'wss://bsc-ws-node.nariox.org:443'; + +var wscweb3 = new Web3(new Web3.providers.WebsocketProvider(wstring )); +var rpcweb3 = new Web3(new Web3.providers.HttpProvider(rpcstring )); + +//设置web3 使用rpcweb3模式 +web3 = rpcweb3; +var prikeystring = "" + +var wbnbaddress = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c" +var lqtwallet = "0xbd2c43da85d007b0b3cd856fd55c299578d832bc" +var bsudAddress = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56" +var dndAddress = "0x14c358b573a4cE45364a3DBD84BBb4Dae87af034" + +var addresspancake = "0x10ed43c718714eb63d5aa57b78b54704e256024e" + + + +prikeystring = prikeystring.replace("0x", "") + +function getPriKey(prikeystring) { + const privKey = new Buffer.from(prikeystring, "hex"); + return privKey; +} + +//这里的privKey 是私钥 +var priKey = getPriKey(prikeystring); + +//获取input内容 +function swaptokeninput(wbnbadddress, toaddress, nbnb, ntoken, tokenaddress, tokendecimals = 18,) { + + weiname = commonUtils.getweiname(tokendecimals); + + var path = [ lqtwallet, + wbnbaddress, + bsudAddress, + dndAddress]; + + var tokenamountIn = web3.utils.toWei(ntoken.toString(10), weiname); + const now = moment().unix(); + const DEADLINE = now + 60 * 20; //往后延迟20分钟 + + var deadline = (DEADLINE).toString(10); + console.log("inputbefore"); + console.log(pancake[0]); + var input = web3.eth.abi.encodeFunctionCall(pancake[0], [ tokenamountIn,0, path, toaddress, deadline]); + console.log(input) + return input; +} + + +const swap = async () => { + + //获得自己的地址 + var fromAddress = "0x" + util.privateToAddress(priKey).toString('hex'); + //要交换的tokenadrress + + var tokenContract = new web3.eth.Contract(erc20, lqtwallet); + + //获得代币有多少位小数 + let decimals = await tokenContract.methods.decimals().call(); + + // 设置交易滑点,直接调用合约可以设置100的滑点,这里设置50的滑点 + var los = 50; + // 假设要购买5个BNB的tokenA + var nbnb = 0.00001; + + //获取交易对代币比例 + // let lpTokenBalance = await commonUtils.getTokenBalance(lqtwallet,lqtLPAddress) + // let lpBnBBalance = await commonUtils.getBNBBalance(wbnbaddress,lqtLPAddress) + // var rate = lpTokenBalance/lpBnBBalance; + var ntoken = 0.001; + + + //获得input 内容 + + + //创建交易执行智能合约 + + + var toAddress = addresspancake + //获得下一次交易的数 + var nonceCnt = await web3.eth.getTransactionCount(fromAddress); + + //交易需要5个BNB + nbnb = web3.utils.toWei((nbnb).toString(10), 'ether'); + //设置gasprice 为 5G wei + var gasPrice = web3.utils.toWei((5).toString(10), 'Gwei'); + //设置 gaslimit 为 420000 + var gaslimit = 420000 + + var input = swaptokeninput(wbnbaddress, fromAddress, 0, ntoken, lqtwallet, decimals) + let reslut = await commonUtils.signTransaction(fromAddress, toAddress, input, nonceCnt, priKey, gasPrice, nbnb, gaslimit) + if (reslut) { + console.log("交易成功") + } + else { + console.log("交易失败") + } +} + + + + + + + + + +const main = async () => { + + //console.log("infoUtils:",infoUtils) + //infoUtils.getBNBBalance(wbnbaddress,walletaddress) + swap() +} + +main() \ No newline at end of file