OSDN Git Service

Add the Utxo best match function.
authorZhiting Lin <zlin035@uottawa.ca>
Wed, 20 Feb 2019 10:19:04 +0000 (18:19 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Wed, 20 Feb 2019 10:19:04 +0000 (18:19 +0800)
src/components/bytom.js
src/components/filter.js [new file with mode: 0644]
src/components/layout/profit/action.js
src/components/layout/save/action.js

index 6b28e71..9fb2194 100644 (file)
@@ -66,7 +66,7 @@ export function listDappUTXO(params)
     default:
       url = "/dapp/list-utxos"
   }
-  return post(url, params).then(resp => resp.data[0])
+  return post(url, params).then(resp => resp.data)
 }
 
 export function updateUtxo(params)
diff --git a/src/components/filter.js b/src/components/filter.js
new file mode 100644 (file)
index 0000000..05f76d0
--- /dev/null
@@ -0,0 +1,21 @@
+export function matchesUTXO(array, amount){
+  let result = array[0]
+  const length = array.length
+  if( length > 0 ){
+    for(let i = 0; i < length; i++){
+      const resp = array[i]
+      if( resp.amount === amount ){
+        result = resp
+        break
+      }else if( resp.amount < amount ){
+        if(i > 0){
+          result = array[i-1]
+        }
+        break
+      }else if( i === length-1 ){
+        result =  array[length-1]
+      }
+    }
+  }
+  return result
+}
\ No newline at end of file
index 4928405..dabae4a 100644 (file)
@@ -2,7 +2,8 @@ import {
   spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction,
   updateBalances, updateUtxo, listDappUTXO, contractArguments
 } from '../../bytom'
-import GetContractArgs from "../../constants";
+import GetContractArgs from '../../constants'
+import { matchesUTXO } from '../../filter'
 
 export function FixedLimitProfit(account, amountBill, saver) {
   return new Promise((resolve, reject) => {
@@ -14,13 +15,14 @@ export function FixedLimitProfit(account, amountBill, saver) {
         "order":"desc"
       }
     }).then(resp => {
-      if(!resp) {
+      if(resp.length === 0) {
         throw 'cannot load UTXO info.'
       }
 
-      const capitalAmount = resp.amount
-      const capitalAsset = resp.asset
-      const utxo = resp.hash
+      const result = matchesUTXO(resp, amountBill)
+      const capitalAmount = result.amount
+      const capitalAsset = result.asset
+      const utxo = result.hash
 
       if(amountBill > capitalAmount) {
         throw 'input amount must be smaller or equal to ' + capitalAmount + '.'
index 1d5324f..daf0731 100644 (file)
@@ -3,7 +3,7 @@ import {
   controlAddressAction, listDappUTXO, updateUtxo, updateBalances,
   contractArguments
 } from '../../bytom'
-import GetContractArgs from "../../constants";
+import GetContractArgs from '../../constants'
 
 export function FixedLimitDeposit(account, amount, address) {
   return new Promise((resolve, reject) => {
@@ -15,13 +15,14 @@ export function FixedLimitDeposit(account, amount, address) {
         "order":"desc"
       }
     }).then(resp => {
-      if(!resp) {
+      if(resp.length === 0) {
         throw 'cannot load UTXO info.'
       }
 
-      const billAmount = resp.amount
-      const billAsset = resp.asset
-      const utxo = resp.hash
+      const result = resp[0]
+      const billAmount = result.amount
+      const billAsset = result.asset
+      const utxo = result.hash
 
       if(amount > billAmount){
         throw 'input amount must be smaller or equal to ' + billAmount +'.'