• R/O
  • HTTP
  • SSH
  • HTTPS

Bytom-Dapp-Demo: Commit

A simple deposit and profit bytom dapp demo


Commit MetaInfo

Revisionc8cf82822c21d5cad751132df1a1a787c981de47 (tree)
Zeit2019-02-20 19:19:04
AutorZhiting Lin <zlin035@uott...>
CommiterZhiting Lin

Log Message

Add the Utxo best match function.

Ändern Zusammenfassung

Diff

--- a/src/components/bytom.js
+++ b/src/components/bytom.js
@@ -66,7 +66,7 @@ export function listDappUTXO(params)
6666 default:
6767 url = "/dapp/list-utxos"
6868 }
69- return post(url, params).then(resp => resp.data[0])
69+ return post(url, params).then(resp => resp.data)
7070 }
7171
7272 export function updateUtxo(params)
--- /dev/null
+++ b/src/components/filter.js
@@ -0,0 +1,21 @@
1+export function matchesUTXO(array, amount){
2+ let result = array[0]
3+ const length = array.length
4+ if( length > 0 ){
5+ for(let i = 0; i < length; i++){
6+ const resp = array[i]
7+ if( resp.amount === amount ){
8+ result = resp
9+ break
10+ }else if( resp.amount < amount ){
11+ if(i > 0){
12+ result = array[i-1]
13+ }
14+ break
15+ }else if( i === length-1 ){
16+ result = array[length-1]
17+ }
18+ }
19+ }
20+ return result
21+}
\ No newline at end of file
--- a/src/components/layout/profit/action.js
+++ b/src/components/layout/profit/action.js
@@ -2,7 +2,8 @@ import {
22 spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction,
33 updateBalances, updateUtxo, listDappUTXO, contractArguments
44 } from '../../bytom'
5-import GetContractArgs from "../../constants";
5+import GetContractArgs from '../../constants'
6+import { matchesUTXO } from '../../filter'
67
78 export function FixedLimitProfit(account, amountBill, saver) {
89 return new Promise((resolve, reject) => {
@@ -14,13 +15,14 @@ export function FixedLimitProfit(account, amountBill, saver) {
1415 "order":"desc"
1516 }
1617 }).then(resp => {
17- if(!resp) {
18+ if(resp.length === 0) {
1819 throw 'cannot load UTXO info.'
1920 }
2021
21- const capitalAmount = resp.amount
22- const capitalAsset = resp.asset
23- const utxo = resp.hash
22+ const result = matchesUTXO(resp, amountBill)
23+ const capitalAmount = result.amount
24+ const capitalAsset = result.asset
25+ const utxo = result.hash
2426
2527 if(amountBill > capitalAmount) {
2628 throw 'input amount must be smaller or equal to ' + capitalAmount + '.'
--- a/src/components/layout/save/action.js
+++ b/src/components/layout/save/action.js
@@ -3,7 +3,7 @@ import {
33 controlAddressAction, listDappUTXO, updateUtxo, updateBalances,
44 contractArguments
55 } from '../../bytom'
6-import GetContractArgs from "../../constants";
6+import GetContractArgs from '../../constants'
77
88 export function FixedLimitDeposit(account, amount, address) {
99 return new Promise((resolve, reject) => {
@@ -15,13 +15,14 @@ export function FixedLimitDeposit(account, amount, address) {
1515 "order":"desc"
1616 }
1717 }).then(resp => {
18- if(!resp) {
18+ if(resp.length === 0) {
1919 throw 'cannot load UTXO info.'
2020 }
2121
22- const billAmount = resp.amount
23- const billAsset = resp.asset
24- const utxo = resp.hash
22+ const result = resp[0]
23+ const billAmount = result.amount
24+ const billAsset = result.asset
25+ const utxo = result.hash
2526
2627 if(amount > billAmount){
2728 throw 'input amount must be smaller or equal to ' + billAmount +'.'
Show on old repository browser