Browse Source

以太坊余额修复

master
vee 4 years ago
parent
commit
e012d854ce
  1. 7
      pages/menu/sendToken/enPassword/index.vue
  2. 4
      pages/menu/sendToken/transfer/index.vue
  3. 12
      unpackage/dist/dev/app-plus/app-service.js
  4. 73
      utils/EthUtil.js
  5. 2
      utils/SystemConfiguration.js
  6. 2
      utils/TokenUtil.js
  7. 1
      utils/WalletUtil.js

7
pages/menu/sendToken/enPassword/index.vue

@ -46,9 +46,11 @@
} }
}, },
transaction() { transaction() {
console.log(this.transInfoPass.privateKey)
let that=this
if (this.transInfoPass.type === 'ETH') { if (this.transInfoPass.type === 'ETH') {
this.$EthUtil.transaction(this.transInfoPass.fromAddress, this.transInfoPass.privateKey, this.$EthUtil.transaction(this.transInfoPass.fromAddress,
this.transInfoPass.toAddress, this.transInfoPass.amount, this.transInfoPass.contractAddress, this.transInfoPass.toAddress, this.transInfoPass.amount.toString(), this.transInfoPass.privateKey.substring(2,this.transInfoPass.privateKey.length), this.transInfoPass.contractAddress,
function(hash, err) { function(hash, err) {
if (!err) { if (!err) {
@ -64,7 +66,6 @@
}) })
}, 1000) }, 1000)
} else { } else {
console.log(err, 'cuowu') console.log(err, 'cuowu')
uni.showToast({ uni.showToast({

4
pages/menu/sendToken/transfer/index.vue

@ -444,7 +444,9 @@
this.$EthUtil.getGas().then((res) => { this.$EthUtil.getGas().then((res) => {
console.log(res, 'ETH') console.log(res, 'ETH')
this.$EthUtil.getGas().then((res) => { this.$EthUtil.getGas().then((res) => {
that.transInfoPass.fee = res * 61000 / Math.pow(10, 6) that.transInfoPass.fee = res * 61000 / Math.pow(10, 18)
that.showBottom = true;
that.show = true;
}) })
}) })

12
unpackage/dist/dev/app-plus/app-service.js

File diff suppressed because one or more lines are too long

73
utils/EthUtil.js

@ -13,12 +13,18 @@ if (typeof web3 !== 'undefined') {
let eth = { let eth = {
//获取主币eth余额 //获取主币eth余额
getBalance: async function(address) { // getBalance: async function(address) {
console.log("查询余额:",address) // console.log("查询余额:",address)
let balance = await web3.eth.getBalance(address); // let balance = 0;
console.log("余额:",balance) // try{
return Number(balance) / Math.pow(10, 18); // web3 = new Web3(new Web3.providers.HttpProvider(systemConfiguration.constant.ethNode));
}, // balance = await web3.eth.getBalance(address);
// console.log("余额:",balance,9999999999999)
// }catch(e){
// console.log("余额:",e,9999999999999)
// }
// return Number(balance) / Math.pow(10, 18);
// },
// //获取eth代币币余额 // //获取eth代币币余额
// getTokenBalance: async function(address, contract) { // getTokenBalance: async function(address, contract) {
// console.log(address, contract) // console.log(address, contract)
@ -31,6 +37,7 @@ let eth = {
// return Number(balance) / Math.pow(10, decimals); // return Number(balance) / Math.pow(10, decimals);
// }, // },
getGas: async function() { getGas: async function() {
web3 = new Web3(new Web3.providers.HttpProvider(systemConfiguration.constant.ethNode));
const gasPrice = await web3.eth.getGasPrice().then((v) => { const gasPrice = await web3.eth.getGasPrice().then((v) => {
return v return v
}); });
@ -38,28 +45,28 @@ let eth = {
return gasPrice; return gasPrice;
}, },
// //获取主币eth余额 //获取主币eth余额
// getBalance: async function(address) { getBalance: async function(address) {
// web3 = new Web3(); web3 = new Web3();
// const data = { const data = {
// 'jsonrpc': '2.0', 'jsonrpc': '2.0',
// 'id': '1', 'id': '1',
// 'method': 'eth_getBalance', 'method': 'eth_getBalance',
// 'params': [address, "latest"] 'params': [address, "latest"]
// }; };
// let res = await uni.request({ let res = await uni.request({
// url: systemConfiguration.constant.ethNode, //仅为示例,并非真实接口地址。 url: systemConfiguration.constant.ethNode, //仅为示例,并非真实接口地址。
// method: 'POST', method: 'POST',
// data: data, data: data,
// dataType: 'json' dataType: 'json'
// }); });
// try { try {
// let balance = Number(web3.utils.hexToNumberString(res[1].data.result)) / Math.pow(10, 18) let balance = Number(web3.utils.hexToNumberString(res[1].data.result)) / Math.pow(10, 18)
// return balance return balance
// } catch (e) { } catch (e) {
// return 0; return 0;
// } }
// }, },
addPreZero: function(num) { addPreZero: function(num) {
let t = (num + '').length, let t = (num + '').length,
s = ''; s = '';
@ -100,25 +107,26 @@ let eth = {
sendTransaction: async function(fromAddress, toAddress, value, privateKey, success) { sendTransaction: async function(fromAddress, toAddress, value, privateKey, success) {
var nonce = await web3.eth.getTransactionCount(fromAddress); var nonce = await web3.eth.getTransactionCount(fromAddress);
var gas = await web3.eth.estimateGas({ var gas = await web3.eth.estimateGas({
from: fromAddress from: fromAddress
}); });
console.log(11111111111) console.log(11111111111)
console.log(nonce) console.log(nonce)
console.log(fromAddress, toAddress, value, privateKey) console.log(fromAddress, toAddress, value, privateKey,web3.eth.getGasPrice())
var txData = { var txData = {
chainId: web3.utils.toHex(1899),
// nonce每次++,以免覆盖之前pending中的交易 // nonce每次++,以免覆盖之前pending中的交易
nonce: web3.utils.toHex(nonce++), nonce: web3.utils.toHex(nonce++),
// 设置gasLimit和gasPrice // 设置gasLimit和gasPrice
gas: web3.utils.toHex(gas), gasLimit: web3.utils.toHex(600000),
gasPrice: web3.utils.toHex(web3.eth.getGasPrice()), gasPrice: web3.utils.toHex(web3.eth.getGasPrice()),
// 要转账的哪个账号 // 要转账的哪个账号
to: toAddress, to: toAddress,
// 从哪个账号转 // 从哪个账号转
from: fromAddress, from: fromAddress,
// 0.001 以太币 // 0.001 以太币
value: web3.utils.toHex(web3.utils.toWei(value, 'ether')) value: web3.utils.toHex(web3.utils.toWei(value, 'ether')),
chainId:systemConfiguration.constant.ethChainId
} }
@ -126,6 +134,7 @@ let eth = {
// 引入私钥,并转换为16进制 // 引入私钥,并转换为16进制
// 用私钥签署交易 // 用私钥签署交易
console.log(txData,55555)
console.log(Buffer.from(privateKey).toString('hex')) console.log(Buffer.from(privateKey).toString('hex'))
const tx = new Tx(txData); const tx = new Tx(txData);
tx.sign(Buffer.from(privateKey, 'hex')); tx.sign(Buffer.from(privateKey, 'hex'));

2
utils/SystemConfiguration.js

@ -5,11 +5,13 @@ const constant = isTest ? {
serverUrl: 'http://wallet-api.weirui0755.com', serverUrl: 'http://wallet-api.weirui0755.com',
coinTickerWs:'ws://wallet-quartz.weirui0755.com/websocket', coinTickerWs:'ws://wallet-quartz.weirui0755.com/websocket',
ethNode:'http://47.245.25.82:8545', ethNode:'http://47.245.25.82:8545',
ethChainId:1
} : { } : {
//服务端连接 //服务端连接
serverUrl: 'http://wallet-api.weirui0755.com', serverUrl: 'http://wallet-api.weirui0755.com',
coinTickerWs:'ws://wallet-quartz.weirui0755.com/websocket', coinTickerWs:'ws://wallet-quartz.weirui0755.com/websocket',
ethNode:'http://47.245.25.82:8545', ethNode:'http://47.245.25.82:8545',
ethChainId:3
} }
export default { export default {

2
utils/TokenUtil.js

@ -92,7 +92,7 @@ let token = {
let wallet = new ethers.Wallet(privateKey); let wallet = new ethers.Wallet(privateKey);
let address = wallet.address; let address = wallet.address;
return { return {
'privateKey': privateKey, 'privateKey': wallet.privateKey,
'address': address 'address': address
}; };
}, },

1
utils/WalletUtil.js

@ -235,6 +235,7 @@ let WalletUtil = {
case 'ETH': case 'ETH':
console.log("进来ETH",wallet.balance ) console.log("进来ETH",wallet.balance )
wallet.balance = await EthUtil.getBalance(wallet.address); wallet.balance = await EthUtil.getBalance(wallet.address);
wallet.balance=wallet.balance>0?wallet.balance.toFixed(6):wallet.balance
console.log("进来ETH",wallet.balance ) console.log("进来ETH",wallet.balance )
for (let i = 1; i < wallet.coinList.length; i++) { for (let i = 1; i < wallet.coinList.length; i++) {
wallet.coinList[i].balance = await EthUtil.getTokenBalance(wallet.address, wallet.coinList[i].contractAddress); wallet.coinList[i].balance = await EthUtil.getTokenBalance(wallet.address, wallet.coinList[i].contractAddress);

Loading…
Cancel
Save