You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.8 KiB
144 lines
3.8 KiB
import abi from './EthAbi.js'
|
|
const Web3 = require('web3');
|
|
var Tx = require('ethereumjs-tx');
|
|
let web3;
|
|
web3 = new Web3(new Web3.providers.HttpProvider("https://http-mainnet-node.huobichain.com"));
|
|
|
|
let eth = {
|
|
//获取主币eth余额
|
|
getBalance: async function(address, type) {
|
|
let balance = await web3.eth.getBalance(address);
|
|
console.log("余额:",balance)
|
|
return Number(balance) / Math.pow(10, 18);
|
|
},
|
|
//获取eth代币币余额
|
|
getTokenBalance: async function(address, contract) {
|
|
var myContract = new web3.eth.Contract(abi, contract, {
|
|
from: address
|
|
});
|
|
var decimals = await myContract.methods.decimals().call();
|
|
var balance = await myContract.methods.balanceOf(address).call();
|
|
|
|
return Number(balance) / Math.pow(10, decimals);
|
|
},
|
|
getGas: async function() {
|
|
const gasPrice = await web3.eth.getGasPrice().then((v) => {
|
|
return v
|
|
});
|
|
|
|
return Number(nubmerJs.strip(gas * gasPrice / 10 ** 18).toFixed(6))
|
|
},
|
|
transaction: async function(fromAddress, toAddress, value, privateKey, success) {
|
|
|
|
var nonce = await web3.eth.getTransactionCount(fromAddress);
|
|
var gas = await web3.eth.estimateGas({
|
|
from: fromAddress
|
|
});
|
|
console.log(11111111111)
|
|
console.log(nonce)
|
|
console.log(fromAddress, toAddress, value, privateKey)
|
|
var txData = {
|
|
chainId: web3.utils.toHex(1899),
|
|
// nonce每次++,以免覆盖之前pending中的交易
|
|
nonce: web3.utils.toHex(nonce++),
|
|
// 设置gasLimit和gasPrice
|
|
gas: web3.utils.toHex(gas),
|
|
gasPrice: web3.utils.toHex(web3.eth.getGasPrice()),
|
|
// 要转账的哪个账号
|
|
to: toAddress,
|
|
// 从哪个账号转
|
|
from: fromAddress,
|
|
// 0.001 以太币
|
|
value: web3.utils.toHex(web3.utils.toWei(value, 'ether'))
|
|
}
|
|
|
|
|
|
|
|
// 引入私钥,并转换为16进制
|
|
|
|
// 用私钥签署交易
|
|
console.log(Buffer.from(privateKey).toString('hex'))
|
|
const tx = new Tx(txData);
|
|
tx.sign(Buffer.from(privateKey, 'hex'));
|
|
|
|
// 序列化
|
|
var serializedTx = tx.serialize().toString('hex');
|
|
|
|
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err,
|
|
hash) {
|
|
console.log('hash' + hash)
|
|
if (!err) {
|
|
success(hash, undefined);
|
|
} else {
|
|
console.log(err,789798)
|
|
success(undefined, err);
|
|
}
|
|
});
|
|
// 获取交易数据
|
|
|
|
|
|
|
|
|
|
},
|
|
sendTokenTransaction: async function(fromAddress, toAddress, value, privateKey, contract, success) {
|
|
|
|
var nonce = await web3.eth.getTransactionCount(fromAddress);
|
|
var gas = await web3.eth.estimateGas({
|
|
from: fromAddress
|
|
});
|
|
|
|
console.log('nonce' + nonce)
|
|
var myContract = new web3.eth.Contract(abi, contract)
|
|
myContract.methods.decimals().call()
|
|
.then(function(decimals) {
|
|
|
|
// 获取交易数据
|
|
console.log(Number(value) * Math.pow(10, decimals))
|
|
var txData = {
|
|
chainId: web3.utils.toHex(128),
|
|
// nonce每次++,以免覆盖之前pending中的交易
|
|
nonce: web3.utils.toHex(nonce++),
|
|
// 设置gasLimit和gasPrice
|
|
gas: web3.utils.toHex(gas),
|
|
gasPrice: web3.utils.toHex(web3.eth.getGasPrice()),
|
|
// 要转账的哪个账号
|
|
to: contract,
|
|
// 从哪个账号转
|
|
from: fromAddress,
|
|
// 0.001 以太币
|
|
value: "0x00",
|
|
data: myContract.methods.transfer(
|
|
toAddress, web3.utils.toWei(value, 'ether')).encodeABI()
|
|
}
|
|
|
|
|
|
// 引入私钥,并转换为16进制
|
|
|
|
// 用私钥签署交易
|
|
|
|
const tx = new Tx(txData, {
|
|
'chain': 'ropsten'
|
|
});
|
|
tx.sign(Buffer.from(privateKey, 'hex'));
|
|
console.log(Buffer.from(privateKey).toString('hex'))
|
|
// 序列化
|
|
var serializedTx = tx.serialize().toString('hex');
|
|
|
|
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(
|
|
err,
|
|
hash) {
|
|
if (!err) {
|
|
success(hash, undefined);
|
|
} else {
|
|
success(undefined, err);
|
|
}
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
export default eth;
|
|
|