|
@@ -3,12 +3,15 @@
|
|
|
* @type {{}}
|
|
* @type {{}}
|
|
|
*/
|
|
*/
|
|
|
import tools from "@/common/js/tools";
|
|
import tools from "@/common/js/tools";
|
|
|
|
|
+import bitkeepTron from "@/common/wallet/bitkeep-wallet/bitkeep-tron";
|
|
|
|
|
+import {ethers} from "ethers";
|
|
|
|
|
|
|
|
let tokenpocketBnb = {}
|
|
let tokenpocketBnb = {}
|
|
|
|
|
|
|
|
let contractArr=[
|
|
let contractArr=[
|
|
|
'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
|
|
'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
|
|
|
];
|
|
];
|
|
|
|
|
+const chainId='0x38'
|
|
|
/**
|
|
/**
|
|
|
* 钱包初始校验
|
|
* 钱包初始校验
|
|
|
* @returns {*}
|
|
* @returns {*}
|
|
@@ -40,6 +43,7 @@ tokenpocketBnb.getAccounts=async function (){
|
|
|
try {
|
|
try {
|
|
|
window.ethereum.request({ method: 'eth_requestAccounts'})
|
|
window.ethereum.request({ method: 'eth_requestAccounts'})
|
|
|
.then((address)=>{
|
|
.then((address)=>{
|
|
|
|
|
+ console.log('address:',address)
|
|
|
if(address.length>0){
|
|
if(address.length>0){
|
|
|
uni.setStorageSync('myAddress',address[0])
|
|
uni.setStorageSync('myAddress',address[0])
|
|
|
resolve(address[0])
|
|
resolve(address[0])
|
|
@@ -56,6 +60,7 @@ tokenpocketBnb.getAccounts=async function (){
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 获取我的余额
|
|
* 获取我的余额
|
|
|
* @param selectedAddress
|
|
* @param selectedAddress
|
|
@@ -65,9 +70,20 @@ tokenpocketBnb.getBalance=async function (selectedAddress) {
|
|
|
if(!selectedAddress){
|
|
if(!selectedAddress){
|
|
|
selectedAddress=bitkeepTron.getMyAddress();
|
|
selectedAddress=bitkeepTron.getMyAddress();
|
|
|
}
|
|
}
|
|
|
- let balance = await tronWeb.trx.getBalance(selectedAddress);
|
|
|
|
|
- balance=balance/1000000;
|
|
|
|
|
- return balance
|
|
|
|
|
|
|
+ return new Promise( (resolve, reject) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ window.ethereum.request({ method: 'eth_getBalance',params:[selectedAddress]})
|
|
|
|
|
+ .then((balance)=>{
|
|
|
|
|
+ balance= ethers.utils.formatEther(balance.toString())
|
|
|
|
|
+ resolve(balance)
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch((e)=>{
|
|
|
|
|
+ resolve(0)
|
|
|
|
|
+ })
|
|
|
|
|
+ }catch (e) {
|
|
|
|
|
+ reject(e)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -93,6 +109,8 @@ tokenpocketBnb.getTokenBalance=async function (selectedAddress,contractType) {
|
|
|
return balance;
|
|
return balance;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 获取合约信息
|
|
* 获取合约信息
|
|
|
* @param contractType
|
|
* @param contractType
|
|
@@ -126,20 +144,52 @@ tokenpocketBnb.getContractData=function (to,money,contractType){
|
|
|
return data;
|
|
return data;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 获取gasPrice
|
|
|
|
|
+ * @returns {Promise<unknown>}
|
|
|
|
|
+ */
|
|
|
|
|
+tokenpocketBnb.getGasPrice= async function (){
|
|
|
|
|
+ let gasPrice =await window.ethereum.request({ method: 'eth_gasPrice',params:[]})
|
|
|
|
|
+ if(!gasPrice){
|
|
|
|
|
+ gasPrice='0x0'
|
|
|
|
|
+ }
|
|
|
|
|
+ return gasPrice;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 获取燃料数量
|
|
|
|
|
+ * @param data
|
|
|
|
|
+ * @returns {Promise<unknown>}
|
|
|
|
|
+ */
|
|
|
|
|
+tokenpocketBnb.getEstimateGas= async function (data){
|
|
|
|
|
+ let estimateGas =await window.ethereum.request({ method: 'eth_estimateGas',params:[data]})
|
|
|
|
|
+ console.log('estimateGas:'+estimateGas)
|
|
|
|
|
+ if(!estimateGas){
|
|
|
|
|
+ estimateGas=100000
|
|
|
|
|
+ }
|
|
|
|
|
+ return estimateGas;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 转账接口
|
|
* 转账接口
|
|
|
* @param to
|
|
* @param to
|
|
|
* @param money
|
|
* @param money
|
|
|
* @returns {{}}
|
|
* @returns {{}}
|
|
|
*/
|
|
*/
|
|
|
-tokenpocketBnb.getTransactionData=function (to,money){
|
|
|
|
|
|
|
+tokenpocketBnb.getTransactionData=async function (to,money){
|
|
|
let data={};
|
|
let data={};
|
|
|
|
|
+ data.data='0x0';
|
|
|
data.to=to;
|
|
data.to=to;
|
|
|
data.from=tokenpocketBnb.getMyAddress();
|
|
data.from=tokenpocketBnb.getMyAddress();
|
|
|
|
|
+ data.gasPrice=await tokenpocketBnb.getGasPrice();
|
|
|
|
|
+ data.gasLimit=await tokenpocketBnb.getEstimateGas();
|
|
|
|
|
+ data.chainId=chainId;
|
|
|
if(tools.isDevelopment()){
|
|
if(tools.isDevelopment()){
|
|
|
money=0.001;
|
|
money=0.001;
|
|
|
}
|
|
}
|
|
|
- data.value=money;
|
|
|
|
|
|
|
+ data.value=ethers.utils.parseEther(money.toString()).toString();
|
|
|
|
|
+ console.log('value:'+data.value)
|
|
|
return data;
|
|
return data;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -151,30 +201,13 @@ tokenpocketBnb.getTransactionData=function (to,money){
|
|
|
tokenpocketBnb.sendTransaction=function (data){
|
|
tokenpocketBnb.sendTransaction=function (data){
|
|
|
return new Promise(async (resolve, reject) => {
|
|
return new Promise(async (resolve, reject) => {
|
|
|
try {
|
|
try {
|
|
|
- let broastTx={};
|
|
|
|
|
- if (data.contract) {
|
|
|
|
|
- let decimal = 18
|
|
|
|
|
- let Contract = await tronWeb.contract().at(data.contract)
|
|
|
|
|
- const decimalCall = Contract.decimals || Contract.DECIMALS;
|
|
|
|
|
- if (decimalCall) {
|
|
|
|
|
- decimal = await decimalCall().call()
|
|
|
|
|
- }
|
|
|
|
|
- let txid = await Contract.transfer(
|
|
|
|
|
- data.to,
|
|
|
|
|
- tronWeb.toHex(data.value * Math.pow(10, decimal))
|
|
|
|
|
- ).send()
|
|
|
|
|
- if(txid){
|
|
|
|
|
- broastTx.txid=txid
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- let tx = await tronWeb.transactionBuilder.sendTrx(data.to, data.value * Math.pow(10, 6), data.from);
|
|
|
|
|
- // sign 签名
|
|
|
|
|
- let signedTx = await tronWeb.trx.sign(tx);
|
|
|
|
|
- // broadcast 广播
|
|
|
|
|
- broastTx = await tronWeb.trx.sendRawTransaction(signedTx);
|
|
|
|
|
- }
|
|
|
|
|
- if(broastTx.txid){
|
|
|
|
|
- resolve(broastTx.txid)
|
|
|
|
|
|
|
+ let txHash = await window.ethereum.request({
|
|
|
|
|
+ method: 'eth_sendTransaction',
|
|
|
|
|
+ params: [data],
|
|
|
|
|
+ });
|
|
|
|
|
+ console.log('txHash:'+txHash)
|
|
|
|
|
+ if(txHash){
|
|
|
|
|
+ resolve(txHash)
|
|
|
}else {
|
|
}else {
|
|
|
reject(false)
|
|
reject(false)
|
|
|
}
|
|
}
|
|
@@ -184,4 +217,6 @@ tokenpocketBnb.sendTransaction=function (data){
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
export default tokenpocketBnb
|
|
export default tokenpocketBnb
|