OSDN Git Service

matches Arguement styles, and add the error handle for dapp request
authorZhiting Lin <zlin035@uottawa.ca>
Fri, 15 Feb 2019 07:54:47 +0000 (15:54 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Fri, 15 Feb 2019 07:54:47 +0000 (15:54 +0800)
src/components/bytom.js
src/components/layout/account/action.js
src/components/layout/profit/action.js
src/components/layout/save/action.js

index 5c0d072..f8318c1 100644 (file)
@@ -1,32 +1,29 @@
 import axios from 'axios'
 
-export function spendUTXOAction(utxo, amount, address){
+export function spendUTXOAction(utxo){
   return {
     "type": "spend_utxo",
-    "output_id": utxo,
-    "arguments": [
-      {
-        "type": "integer",
-        "raw_data": {
-          "value": amount
-        }
-      },
-      {
-        "type": "address",
-        "raw_data": {
-          "value": address
-        }
-      },
-      {
-        "type": "data",
-        "raw_data": {
-          "value": ""
-        }
-      }
-    ]
+    "output_id": utxo
   }
 }
 
+export function contractArguments(amount, address){
+  return [
+    {
+      "type": "integer",
+      "value": amount
+    },
+    {
+      "type": "address",
+      "value": address
+    },
+    {
+      "type": "data",
+      "value": ""
+    }
+  ]
+}
+
 export function spendWalletAction(amount, asset){
   return {
     "amount": amount,
@@ -53,28 +50,10 @@ export function controlAddressAction(amount, asset, address){
   }
 }
 
-export function listUTXO(params)
+export function listAddress(guid)
 {
-  const url = "/api/api/v1/btm/q/list-utxos"
-  return axios({
-    method: 'post',
-    url,
-    data: params
-  }).then(response => {
-    return response.data.result.data[0];
-  })
-}
-
-export function listAddress(params)
-{
-  const url = "/api/api/v1/btm//account/list-addresses"
-  return axios({
-    method: 'post',
-    url,
-    data: params
-  }).then(response => {
-    return response.data.result.data[0];
-  })
+  return bytomAPI.sdk.accounts.listAddressUseServer(guid)
+    .then(resp => resp[0])
 }
 
 export function listDappUTXO(params)
@@ -85,7 +64,12 @@ export function listDappUTXO(params)
     url,
     data: params
   }).then(response => {
-    return response.data.result.data[0];
+    if(response.data.code === 200){
+      return response.data.result.data[0];
+    }
+    else {
+      throw response.data.msg
+    }
   })
 }
 
@@ -97,7 +81,11 @@ export function updateBase(params)
     url,
     data: params
   }).then(response => {
-    return response.data.result;
+    if(response.data.code === 200){
+      return response.data.result;
+    }else {
+      throw response.data.msg
+    }
   })
 }
 
@@ -109,7 +97,11 @@ export function updateUtxo(params)
     url,
     data: params
   }).then(response => {
-    return response.data.result;
+    if(response.data.code === 200){
+      return response.data.result;
+    }else{
+      throw response.data.msg
+    }
   })
 }
 
@@ -121,7 +113,11 @@ export function updateBalances(params)
     url,
     data: params
   }).then(response => {
-    return response.data.result;
+    if(response.data.code === 200){
+      return response.data.result;
+    }else{
+      throw response.data.msg
+    }
   })
 }
 
@@ -133,6 +129,10 @@ export function listBalances(params)
     url,
     data: params
   }).then(response => {
-    return response.data.result;
+    if(response.data.code === 200){
+      return response.data.result;
+    }else{
+      throw response.data.msg
+    }
   })
 }
index cb3604b..5fd8756 100644 (file)
@@ -5,7 +5,7 @@ import { assetDeposited, assetBill } from "../../constants";
 
 const updateBalances = (guid = '') => {
   return (dispatch) => {
-    return listAddress({guid})
+    return listAddress(guid)
       .then((resp)=>{
         const balances = resp.balances
         const billBalance = balances.filter(balance => balance.asset === assetBill)
index 85d3a6e..938b36a 100644 (file)
@@ -1,6 +1,6 @@
 import {
   spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction,
-  updateBase, updateBalances, updateUtxo, listDappUTXO
+  updateBase, updateBalances, updateUtxo, listDappUTXO, contractArguments
 } from '../../bytom'
 import {profitProgram, assetDeposited, assetBill, gas, banker, totalAmountBill, totalAmountCapital} from "../../constants";
 
@@ -25,7 +25,9 @@ export function FixedLimitProfit(account, amountBill, saver) {
         const sTotalAmountBill = totalAmountBill/100000000
         const gain = totalAmountCapital*sAmountBill/sTotalAmountBill
 
-        input.push(spendUTXOAction(utxo, amountBill, saver))
+        const args = contractArguments(amountBill, saver)
+
+        input.push(spendUTXOAction(utxo))
         input.push(spendWalletAction(amountBill, assetBill))
 
         if(amountBill < capitalAmount){
@@ -37,7 +39,7 @@ export function FixedLimitProfit(account, amountBill, saver) {
           output.push(controlAddressAction(capitalAmount, capitalAsset, saver))
         }
 
-        window.bytom.advancedTransfer(account, input, output, gas*10000000)
+        window.bytom.advancedTransfer(account, input, output, gas*10000000, args)
           .then((resp) => {
             if(resp.action === 'reject'){
               reject('user reject the request')
@@ -55,9 +57,16 @@ export function FixedLimitProfit(account, amountBill, saver) {
                       "amount": -amountBill
                     }).then(()=>{
                       resolve()
+                    }).catch(err => {
+                      throw err
                     })
+                  }).catch(err => {
+                    throw err
                   })
                 })
+                .catch(err => {
+                  throw err
+                })
             }
           })
           .catch(err => {
index b385e25..159a1bf 100644 (file)
@@ -1,6 +1,7 @@
 import {
   spendUTXOAction, spendWalletAction, controlProgramAction,
-  controlAddressAction, listDappUTXO, updateBase, updateUtxo ,updateBalances
+  controlAddressAction, listDappUTXO, updateBase, updateUtxo ,updateBalances,
+  contractArguments
 } from '../../bytom'
 import {
   depositProgram, profitProgram, assetDeposited, assetBill, gas
@@ -22,7 +23,9 @@ export function FixedLimitDeposit(account, amount, address) {
         const input = []
         const output = []
 
-        input.push(spendUTXOAction(utxo, amount, address))
+        const args = contractArguments(amount, address)
+
+        input.push(spendUTXOAction(utxo))
         input.push(spendWalletAction(amount, assetDeposited))
 
         if(amount < billAmount){
@@ -34,7 +37,7 @@ export function FixedLimitDeposit(account, amount, address) {
           output.push(controlAddressAction(billAmount, billAsset, address))
         }
 
-        window.bytom.advancedTransfer(account, input, output, gas*10000000)
+        window.bytom.advancedTransfer(account, input, output, gas*10000000, args)
           .then((resp) => {
             if(resp.action === 'reject'){
               reject('user reject the request')
@@ -52,9 +55,16 @@ export function FixedLimitDeposit(account, amount, address) {
                       "amount": amount
                     }).then(()=>{
                       resolve()
+                    }).catch(err => {
+                      throw err
                     })
+                  }).catch(err => {
+                    throw err
                   })
                 })
+                .catch(err => {
+                  throw err
+                })
             }
           })
           .catch(err => {