Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions Class 1/mingle/commonUtils.js
Original file line number Diff line number Diff line change
@@ -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}

//启动程序

31 changes: 31 additions & 0 deletions Class 1/mingle/erc20.js
Original file line number Diff line number Diff line change
@@ -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'
}
];
128 changes: 128 additions & 0 deletions Class 1/mingle/ethToTokens.js
Original file line number Diff line number Diff line change
@@ -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()
30 changes: 30 additions & 0 deletions Class 1/mingle/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading