|
|
@@ -0,0 +1,187 @@
|
|
|
+/**
|
|
|
+ * bitkeep钱包的tron
|
|
|
+ * @type {{}}
|
|
|
+ */
|
|
|
+import tools from "@/common/js/tools";
|
|
|
+
|
|
|
+let tokenpocketBnb = {}
|
|
|
+
|
|
|
+let contractArr=[
|
|
|
+ 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
|
|
|
+];
|
|
|
+/**
|
|
|
+ * 钱包初始校验
|
|
|
+ * @returns {*}
|
|
|
+ */
|
|
|
+tokenpocketBnb.isInstall=function (){
|
|
|
+ console.log('---------------------------')
|
|
|
+ console.log(window.ethereum)
|
|
|
+ if(!window.ethereum){
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ console.log(window.ethereum.isTokenPocket)
|
|
|
+ return typeof window.ethereum.isTokenPocket !== 'undefined';
|
|
|
+
|
|
|
+}
|
|
|
+/**
|
|
|
+ * 获取我登陆的地址
|
|
|
+ * @returns {any}
|
|
|
+ */
|
|
|
+tokenpocketBnb.getMyAddress=function () {
|
|
|
+ return uni.getStorageSync('myAddress')
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取基本账户地址
|
|
|
+ * @returns {Promise<unknown>}
|
|
|
+ */
|
|
|
+tokenpocketBnb.getAccounts=async function (){
|
|
|
+ return new Promise( (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ window.ethereum.request({ method: 'eth_requestAccounts'})
|
|
|
+ .then((address)=>{
|
|
|
+ if(address.length>0){
|
|
|
+ uni.setStorageSync('myAddress',address[0])
|
|
|
+ resolve(address[0])
|
|
|
+ }else {
|
|
|
+ resolve(false)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch((e)=>{
|
|
|
+ console.log(e)
|
|
|
+ })
|
|
|
+ }catch (e) {
|
|
|
+ reject(e)
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取我的余额
|
|
|
+ * @param selectedAddress
|
|
|
+ * @returns {Promise<unknown>}
|
|
|
+ */
|
|
|
+tokenpocketBnb.getBalance=async function (selectedAddress) {
|
|
|
+ if(!selectedAddress){
|
|
|
+ selectedAddress=bitkeepTron.getMyAddress();
|
|
|
+ }
|
|
|
+ let balance = await tronWeb.trx.getBalance(selectedAddress);
|
|
|
+ balance=balance/1000000;
|
|
|
+ return balance
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取合约数量
|
|
|
+ * @param selectedAddress
|
|
|
+ * @param contractType
|
|
|
+ * @returns {Promise<number>}
|
|
|
+ */
|
|
|
+tokenpocketBnb.getTokenBalance=async function (selectedAddress,contractType) {
|
|
|
+ if(!selectedAddress){
|
|
|
+ selectedAddress=tokenpocketBnb.getMyAddress();
|
|
|
+ }
|
|
|
+ let tokenBalance = await tronWeb.transactionBuilder.triggerConstantContract(
|
|
|
+ contractArr[contractType],
|
|
|
+ "balanceOf(address)",
|
|
|
+ {},
|
|
|
+ [{ type: 'address', value: selectedAddress }],
|
|
|
+ tronWeb.defaultAddress.base58,
|
|
|
+ );
|
|
|
+ let balance = tokenBalance.constant_result[0];
|
|
|
+ balance= parseInt(balance, 16);
|
|
|
+ balance=balance/1000000;
|
|
|
+ return balance;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取合约信息
|
|
|
+ * @param contractType
|
|
|
+ * @returns {Promise<boolean|*>}
|
|
|
+ */
|
|
|
+tokenpocketBnb.getContract=async function (contractType) {
|
|
|
+ let contractData = await tronWeb.trx.getContract(contractArr[contractType])
|
|
|
+ if(contractData.abi){
|
|
|
+ return contractData.abi
|
|
|
+ }else {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 合约转账
|
|
|
+ * @param to
|
|
|
+ * @param money
|
|
|
+ * @param contractType
|
|
|
+ */
|
|
|
+tokenpocketBnb.getContractData=function (to,money,contractType){
|
|
|
+ let data={};
|
|
|
+ data.contract=contractArr[contractType];
|
|
|
+ data.to=to;
|
|
|
+ data.from=tokenpocketBnb.getMyAddress();
|
|
|
+ if(tools.isDevelopment()){
|
|
|
+ money=0.001;
|
|
|
+ }
|
|
|
+ data.value=money;
|
|
|
+ return data;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 转账接口
|
|
|
+ * @param to
|
|
|
+ * @param money
|
|
|
+ * @returns {{}}
|
|
|
+ */
|
|
|
+tokenpocketBnb.getTransactionData=function (to,money){
|
|
|
+ let data={};
|
|
|
+ data.to=to;
|
|
|
+ data.from=tokenpocketBnb.getMyAddress();
|
|
|
+ if(tools.isDevelopment()){
|
|
|
+ money=0.001;
|
|
|
+ }
|
|
|
+ data.value=money;
|
|
|
+ return data;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 转账
|
|
|
+ * @param data
|
|
|
+ * @returns {Promise<unknown>}
|
|
|
+ */
|
|
|
+tokenpocketBnb.sendTransaction=function (data){
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ 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)
|
|
|
+ }else {
|
|
|
+ reject(false)
|
|
|
+ }
|
|
|
+ }catch (e) {
|
|
|
+ reject(false)
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+export default tokenpocketBnb
|