OSDN Git Service

fix: use the new SDK
authorj <coffce404@gmail.com>
Wed, 28 Jul 2021 09:52:42 +0000 (17:52 +0800)
committerj <coffce404@gmail.com>
Wed, 28 Jul 2021 09:52:42 +0000 (17:52 +0800)
src/models/query.js
src/models/transaction.js
src/models/wallet.js [new file with mode: 0644]
src/store/store.js
src/utils/Bytom.js
src/utils/constants.js
src/views/home.vue

index bf735aa..248e005 100644 (file)
@@ -1,13 +1,16 @@
 import bytom from "./bytom";
+import { wallet } from './wallet'
 
 let query = {};
 
 query.chainStatus = function() {
-  return bytom.query.getVoteStatus();
+  return wallet.getVoteStatus()
+  // return bytom.query.getVoteStatus();
 };
 
 query.blockStatus = function() {
-  return bytom.query.getblockcount();
+  // return bytom.query.getblockcount();
+  return wallet.getChainStatus()
 };
 
 export default query;
index 4027097..f40ccb8 100644 (file)
@@ -1,16 +1,22 @@
 import bytom from "./bytom";
 import { snakeize } from "@/utils/utils";
 import { networks } from '@/utils/constants'
-
+import { wallet } from './wallet'
 
 let transaction = {};
 
-transaction.list = function(guid, asset_id, start, limit, tx_types) {
+transaction.list = function(address, asset_id, start, limit, tx_types) {
   const filter = {asset_id}
   if(tx_types){
     filter.tx_types = tx_types
   }
-  return bytom.transaction.list(guid, filter, null, start, limit);
+  // return bytom.transaction.list(guid, filter, null, start, limit);
+  return wallet.getTransactions({
+    address,
+    limit,
+    start,
+    filter
+  })
 };
 
 transaction.listDelayTransaction = function(address, start, limit, network) {
@@ -19,9 +25,9 @@ transaction.listDelayTransaction = function(address, start, limit, network) {
   }
 
   let baseURL = networks[network]
-  if(network==='testnet'){
-    baseURL = baseURL+':3008'
-  }
+  // if(network==='testnet'){
+  //   baseURL = baseURL+':3008'
+  // }
   return bytom.transaction.listDelayTransaction(baseURL, address, start, limit);
 };
 
@@ -29,8 +35,9 @@ transaction.convertArgument = function(argArray) {
   let fn = function asyncConvert(object){
     const type = object.type
     const value = object.value
-    return bytom.transaction.convertArgument(type, value)
-      .then(resp => resp.value);
+    // return bytom.transaction.convertArgument(type, value)
+    //   .then(resp => resp.value);
+    return wallet.convertArgument(type, value)
   };
 
   let actionFunction = argArray.map(fn)
@@ -38,25 +45,35 @@ transaction.convertArgument = function(argArray) {
 };
 
 transaction.chainStatus = function() {
-  return bytom.query.getblockcount();
+  return wallet.getChainStatus()
+  // return bytom.query.getblockcount();
 };
 
 transaction.asset = function(asset_id) {
-  return bytom.query.asset(asset_id);
+  return wallet.getAsset(asset_id)
+  // return bytom.query.asset(asset_id);
 };
 
 transaction.build = function(address, to, asset, amount, fee, confirmations) {
-  let retPromise = new Promise((resolve, reject) => {
-    bytom.transaction
-      .buildPayment(address, to, asset, amount.toString(), confirmations)
-      .then(res => {
-        resolve(res);
-      })
-      .catch(error => {
-        reject(error);
-      });
-  });
-  return retPromise;
+  return wallet.buildPayment({
+    address,
+    asset,
+    confirmations,
+    recipients: {
+      [to]: amount.toString()
+    }
+  })
+  // let retPromise = new Promise((resolve, reject) => {
+  //   bytom.transaction
+  //     .buildPayment(address, to, asset, amount.toString(), confirmations)
+  //     .then(res => {
+  //       resolve(res);
+  //     })
+  //     .catch(error => {
+  //       reject(error);
+  //     });
+  // });
+  // return retPromise;
 };
 
 transaction.buildCrossChain = function(address, to, asset, amount, confirmations) {
@@ -74,31 +91,45 @@ transaction.buildCrossChain = function(address, to, asset, amount, confirmations
 };
 
 transaction.buildVote = function(address, vote, amount, confirmations, memo) {
-  let retPromise = new Promise((resolve, reject) => {
-    bytom.transaction
-      .buildVote(address, vote, amount.toString(), confirmations, memo)
-      .then(res => {
-        resolve(res);
-      })
-      .catch(error => {
-        reject(error);
-      });
-  });
-  return retPromise;
+  return wallet.buildVote({
+    address,
+    amount: amount.toString(),
+    vote,
+    confirmations,
+    memo
+  })
+  // let retPromise = new Promise((resolve, reject) => {
+  //   bytom.transaction
+  //     .buildVote(address, vote, amount.toString(), confirmations, memo)
+  //     .then(res => {
+  //       resolve(res);
+  //     })
+  //     .catch(error => {
+  //       reject(error);
+  //     });
+  // });
+  // return retPromise;
 };
 
 transaction.buildVeto = function(address, vote, amount, confirmations, memo) {
-  let retPromise = new Promise((resolve, reject) => {
-    bytom.transaction
-      .buildVeto(address, vote, amount.toString(), confirmations, memo)
-      .then(res => {
-        resolve(res);
-      })
-      .catch(error => {
-        reject(error);
-      });
-  });
-  return retPromise;
+  return wallet.buildVeto({
+    address,
+    vote,
+    amount: amount.toString(),
+    confirmations,
+    memo
+  })
+  // let retPromise = new Promise((resolve, reject) => {
+  //   bytom.transaction
+  //     .buildVeto(address, vote, amount.toString(), confirmations, memo)
+  //     .then(res => {
+  //       resolve(res);
+  //     })
+  //     .catch(error => {
+  //       reject(error);
+  //     });
+  // });
+  // return retPromise;
 };
 
 transaction.buildTransaction = function(address, inputs, outputs, gas, confirmations) {
@@ -133,19 +164,19 @@ transaction.signTransaction = function(address, transaction, password, context)
   return retPromise;
 };
 
-transaction.decodeTransaction = function(rawTx) {
-  let retPromise = new Promise((resolve, reject) => {
-    bytom.transaction
-      .decodeTransaction(rawTx)
-      .then(res => {
-        resolve(res);
-      })
-      .catch(error => {
-        reject(error);
-      });
-  });
-  return retPromise;
-};
+// transaction.decodeTransaction = function(rawTx) {
+//   let retPromise = new Promise((resolve, reject) => {
+//     bytom.transaction
+//       .decodeTransaction(rawTx)
+//       .then(res => {
+//         resolve(res);
+//       })
+//       .catch(error => {
+//         reject(error);
+//       });
+//   });
+//   return retPromise;
+// };
 
 transaction.transfer = function(transaction, password, address, context) {
   let retPromise = new Promise((resolve, reject) => {
@@ -153,9 +184,23 @@ transaction.transfer = function(transaction, password, address, context) {
       const {to, asset, amount, confirmations} = transaction
       // transfer to multi address
       if (typeof to === 'object') {
-        return bytom.transaction.buildPayment(address, to, asset, null, confirmations)
+        return wallet.buildPayment({
+          address,
+          recipients: to,
+          asset,
+          confirmations
+        })
+        // return bytom.transaction.buildPayment(address, to, asset, null, confirmations)
       } else {
-        return bytom.transaction.buildPayment(address, to.trim(), asset, amount.toString(), confirmations)
+        return wallet.buildPayment({
+          address,
+          recipients: {
+            [to.trim()]: amount.toString()
+          },
+          asset,
+          confirmations
+        })
+        // return bytom.transaction.buildPayment(address, to.trim(), asset, amount.toString(), confirmations)
       }
     }
     build().then(result => {
@@ -185,8 +230,13 @@ function signSubmit (txObject, password, address, context) {
           context
         )
           .then(ret => {
-            return bytom.transaction
-              .submitPayment(address, ret.raw_transaction, ret.signatures)
+            return wallet.submitPayment({
+              address,
+              rawTransaction: ret.raw_transaction,
+              signatures: ret.signatures
+            })
+            // return bytom.transaction
+            //   .submitPayment(address, ret.raw_transaction, ret.signatures)
               .then(res3 => {
                 const object = {
                   transactionHash: res3.txHash
@@ -208,8 +258,15 @@ function signSubmit (txObject, password, address, context) {
 transaction.signMessage = function(message, password, address, context) {
   const keyArray = context.bytom.keychain.findByAddress(address, context.bytom.settings.network);
   const key = typeof keyArray.keystore ==='string'? keyArray.keystore :JSON.stringify(keyArray.keystore);
-
-  return bytom.keys.signMessageJsPromise(message, password,key);
+  // return bytom.keys.signMessageJsPromise(message, password,key);
+  return new Promise((resolve, reject) => {
+    try {
+      const data = wallet.signMessage(message, password, key)
+      resolve(data)
+    } catch(e) {
+      reject(e)
+    }
+  })
 };
 
 transaction.advancedTransfer = function(address, transaction, password, arrayData, context) {
@@ -225,8 +282,13 @@ transaction.advancedTransfer = function(address, transaction, password, arrayDat
         if(arrayData){
           signatures[0] = arrayData
         }
-        bytom.transaction
-          .submitPayment(address, ret.raw_transaction, signatures)
+        wallet.submitPayment({
+          address,
+          rawTransaction: ret.raw_transaction,
+          signatures
+        })
+        // bytom.transaction
+        //   .submitPayment(address, ret.raw_transaction, signatures)
           .then(res3 => {
             const object ={
               transactionHash: res3.txHash
@@ -252,14 +314,23 @@ function signTx(address, transaction, password, context){
     throw 'Account not found.'
   }else{
     const key = JSON.stringify(keyArray.keystore)
-    return bytom.transaction._signTransactionJsPromise(transaction,
-      password,
-      key)
+    return new Promise((resolve, reject) => {
+      try {
+        const data = wallet.signTransaction(transaction, password, key)
+        resolve(data)
+      } catch(e) {
+        reject(e)
+      }
+    })
+    // return bytom.transaction._signTransactionJsPromise(transaction,
+    //   password,
+    //   key)
   }
 }
 
 transaction.estimateFee = function(address, asset_amounts, confirmations){
-  return bytom.transaction.estimateFee(address, asset_amounts, confirmations)
+  return wallet.estimateTxFee(address, asset_amounts, confirmations)
+  // return bytom.transaction.estimateFee(address, asset_amounts, confirmations)
 }
 
 export default transaction;
diff --git a/src/models/wallet.js b/src/models/wallet.js
new file mode 100644 (file)
index 0000000..4fc798c
--- /dev/null
@@ -0,0 +1,27 @@
+import { Wallet } from 'bytom'
+import bytom from './bytom'
+import { networks } from '../utils/constants'
+
+export const wallet = new Wallet({
+  url: {
+    bytom: networks.testnet,
+    bytom1: networks.mainnet,
+    vapor: networks.mainnet
+  },
+  chain: 'bytom'
+})
+
+export function setNet (net) {
+  wallet.setOptions({
+    url: {
+      bytom: networks.testnet,
+      bytom1: networks[net],
+      vapor: networks[net]
+    }
+  })
+}
+
+export function setChain (chain) {
+  wallet.setOptions({ chain })
+}
+
index 44af521..fadc20c 100644 (file)
@@ -38,7 +38,7 @@ const getters = {
     messages:state => state.prompt.data.messages || [],
 };
 
-export const store = new Vuex.Store({
+export const store = window.store = new Vuex.Store({
     state,
     getters,
     mutations,
index 5c4f511..adba97e 100644 (file)
@@ -9,6 +9,7 @@ export default class BytomObj {
       this.settings = Settings.placeholder();
       this.keychain = Keychain.placeholder();
       this.histories = [];
+      window.__bytom = this
     }
 
     static placeholder(){ return new BytomObj(); }
index 00c82a0..cd6deee 100644 (file)
@@ -22,14 +22,16 @@ export const camelize = (object) => {
 
 export const networks = {
   mainnet:'https://bcapi.movapi.com',
-  testnet: localStorage.getItem('testnet') || 'http://52.83.11.181'
+  testnet: localStorage.getItem('testnet') || 'https://test-bcapi.movapi.com'
 }
 
 export const networksJS = {
-  mainnetbytom: `${networks['mainnet']}/bytom/v3/`,
+  mainnetbytom: `${networks['testnet']}/bytom2/v1/`,
   mainnetvapor: `${networks['mainnet']}/vapor/v3/` ,
-  testnetbytom: `${networks['testnet']}:4000/bytom/v3/`,
-  testnetvapor: `${networks['testnet']}:3000/vapor/v3/` ,
+  mainnetbytom1: `${networks['mainnet']}/bytom/v3/`,
+  testnetbytom: `${networks['testnet']}/bytom2/v1/`,
+  testnetvapor: `${networks['testnet']}/vapor/v3/` ,
+  testnetbytom1: `${networks['testnet']}/bytom/v3/`,
 }
 
 
index 7cc58b2..27af89d 100644 (file)
@@ -410,6 +410,7 @@ export default {
                   symbol: "BTM",
                   decimals: 8
                 } ,
+                balance: 0,
                 availableBalance: 0,
                 inBtc: "0",
                 inCny: "0",
@@ -449,6 +450,7 @@ export default {
             }else{
               addr = newVal.address
             }
+            console.log('currentAccount change', this.netType)
             this.refreshBalance(addr)
         },
     },
@@ -469,7 +471,7 @@ export default {
             const vpAddress = this.currentAccount.vpAddress
             if(!vpAddress){
               const bytom = this.bytom.clone();
-              return account.copy(this.currentAccount.guid).then(accounts => {
+              return account.copy(this.currentAccount.bytom1.guid).then(accounts => {
                 //update currentAccount
                 bytom.currentAccount.vpAddress = accounts.address
                 //update AccountList
@@ -550,7 +552,7 @@ export default {
 
           account.setupNet(`${this.net}${newNetType}`)
           if(this.isVapor && !this.currentAccount.vpAddress){
-            account.copy(this.currentAccount.guid).then(accounts => {
+            account.copy(this.currentAccount.bytom1.guid).then(accounts => {
               //update currentAccount
               bytom.currentAccount.vpAddress = accounts.address
               //update AccountList