OSDN Git Service

update the list vote stauts.
[bytom/Byone.git] / src / models / account.js
index 88a8647..aa5f203 100644 (file)
@@ -1,4 +1,6 @@
 import bytom from './bytom'
+import uuid from 'uuid'
+
 
 let account = {
   setupNet: bytom.setupNet
@@ -6,6 +8,9 @@ let account = {
 
 account.create = function(accountAlias, keyAlias, passwd, success, error) {
   let retPromise = new Promise((resolve, reject) => {
+    if(!keyAlias){
+      keyAlias = `${accountAlias}-key-${uuid.v4()}`
+    }
     bytom.keys
       .create(keyAlias, passwd)
       .then(res => {
@@ -25,29 +30,62 @@ account.create = function(accountAlias, keyAlias, passwd, success, error) {
   return retPromise
 }
 
-const ASSET_BTM =
-  'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
-
-account.balance = function(guid, dstAsset) {
-  if (dstAsset == undefined) {
-    dstAsset = ASSET_BTM
-  }
+account.copy = function(guid) {
+  let retPromise = new Promise((resolve, reject) => {
+    bytom.accounts
+      .copyAccountUseServer(guid, 'btm')
+      .then(ret => {
+        resolve(ret)
+      })
+      .catch(error => {
+        reject(error)
+      })
+  })
+  return retPromise
+}
 
+account.balance = function(guid) {
   let retPromise = new Promise((resolve, reject) => {
     bytom.accounts
       .listAddressUseServer(guid)
       .then(addresses => {
-        let balance = 0
-        addresses.forEach(item => {
-          if (item.balances != null) {
-            item.balances.forEach(asset => {
-              if (asset.asset == dstAsset) {
-                balance += asset.balance
-              }
-            })
+        let balances = []
+        let votes = []
+        addresses.forEach(address => {
+          if (address.balances != null) {
+            balances = balances.concat(address.balances)
+          }
+          if (address.votes != null) {
+            votes = votes.concat(address.votes)
           }
         })
-        resolve(balance / 100000000)
+        let obj = {};
+
+        balances.forEach(function (balance) {
+          if(obj.hasOwnProperty(balance.asset)) {
+            obj[balance.asset].balance = Number(obj[balance.asset].balance) + Number(balance.balance);
+          } else {
+            balance.balance =  Number(balance.balance)
+            obj[balance.asset] = balance;
+            delete obj[balance.asset]['total_received']
+            delete obj[balance.asset]['total_sent']
+            delete obj[balance.asset]['in_btc']
+            delete obj[balance.asset]['in_cny']
+            delete obj[balance.asset]['in_usd']
+          }
+        });
+
+
+        let res = [];
+
+        for(let prop in obj) {
+          res.push(obj[prop]);
+        }
+
+        resolve({
+            balances:res,
+            votes
+          })
       })
       .catch(error => {
         reject(error)
@@ -61,12 +99,18 @@ account.list = function() {
     bytom.accounts
       .listAccountUseServer()
       .then(accounts => {
-        accounts.forEach(account => {
-          this.balance(account.guid).then(balance => {
-            account.balance = balance
-          })
+        Promise.all(accounts.map(async (account) => {
+          try{
+            const balances = await this.balance(account.guid)
+            account.balances = balances
+          }catch (e) {
+            return e
+          }
+        })).then(()=>{
+          resolve(accounts)
+        }).catch(error=>{
+          throw error
         })
-        resolve(accounts)
       })
       .catch(error => {
         reject(error)