A simple deposit and profit bytom dapp demo
Revision | c8cf82822c21d5cad751132df1a1a787c981de47 (tree) |
---|---|
Zeit | 2019-02-20 19:19:04 |
Autor | Zhiting Lin <zlin035@uott...> |
Commiter | Zhiting Lin |
Add the Utxo best match function.
@@ -66,7 +66,7 @@ export function listDappUTXO(params) | ||
66 | 66 | default: |
67 | 67 | url = "/dapp/list-utxos" |
68 | 68 | } |
69 | - return post(url, params).then(resp => resp.data[0]) | |
69 | + return post(url, params).then(resp => resp.data) | |
70 | 70 | } |
71 | 71 | |
72 | 72 | export function updateUtxo(params) |
@@ -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 |
@@ -2,7 +2,8 @@ import { | ||
2 | 2 | spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction, |
3 | 3 | updateBalances, updateUtxo, listDappUTXO, contractArguments |
4 | 4 | } from '../../bytom' |
5 | -import GetContractArgs from "../../constants"; | |
5 | +import GetContractArgs from '../../constants' | |
6 | +import { matchesUTXO } from '../../filter' | |
6 | 7 | |
7 | 8 | export function FixedLimitProfit(account, amountBill, saver) { |
8 | 9 | return new Promise((resolve, reject) => { |
@@ -14,13 +15,14 @@ export function FixedLimitProfit(account, amountBill, saver) { | ||
14 | 15 | "order":"desc" |
15 | 16 | } |
16 | 17 | }).then(resp => { |
17 | - if(!resp) { | |
18 | + if(resp.length === 0) { | |
18 | 19 | throw 'cannot load UTXO info.' |
19 | 20 | } |
20 | 21 | |
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 | |
24 | 26 | |
25 | 27 | if(amountBill > capitalAmount) { |
26 | 28 | throw 'input amount must be smaller or equal to ' + capitalAmount + '.' |
@@ -3,7 +3,7 @@ import { | ||
3 | 3 | controlAddressAction, listDappUTXO, updateUtxo, updateBalances, |
4 | 4 | contractArguments |
5 | 5 | } from '../../bytom' |
6 | -import GetContractArgs from "../../constants"; | |
6 | +import GetContractArgs from '../../constants' | |
7 | 7 | |
8 | 8 | export function FixedLimitDeposit(account, amount, address) { |
9 | 9 | return new Promise((resolve, reject) => { |
@@ -15,13 +15,14 @@ export function FixedLimitDeposit(account, amount, address) { | ||
15 | 15 | "order":"desc" |
16 | 16 | } |
17 | 17 | }).then(resp => { |
18 | - if(!resp) { | |
18 | + if(resp.length === 0) { | |
19 | 19 | throw 'cannot load UTXO info.' |
20 | 20 | } |
21 | 21 | |
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 | |
25 | 26 | |
26 | 27 | if(amount > billAmount){ |
27 | 28 | throw 'input amount must be smaller or equal to ' + billAmount +'.' |