OSDN Git Service

error handle for empty utxo information
[bytom/Bytom-Dapp-Demo.git] / src / components / layout / profit / action.js
1 import {
2   spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction,
3   updateBalances, updateUtxo, listDappUTXO, contractArguments
4 } from '../../bytom'
5 import GetContractArgs from "../../constants";
6
7 export function FixedLimitProfit(account, amountBill, saver) {
8     return listDappUTXO({
9       "program": GetContractArgs().profitProgram,
10       "asset": GetContractArgs().assetDeposited
11     }).then(resp => {
12
13       if(!resp) {
14         throw 'cannot load UTXO info.'
15       }
16
17       const capitalAmount = resp.amount
18       const capitalAsset = resp.asset
19       const utxo = resp.hash
20
21       if(amountBill > capitalAmount) {
22         throw 'input amount must be smaller or equal to ' + capitalAmount + '.'
23       }else{
24         const input = []
25         const output = []
26
27         const sAmountBill = amountBill/100000000
28         const sTotalAmountBill = totalAmountBill/100000000
29         const gain = totalAmountCapital*sAmountBill/sTotalAmountBill
30
31         const args = contractArguments(amountBill, saver)
32
33         input.push(spendUTXOAction(utxo))
34         input.push(spendWalletAction(amountBill, GetContractArgs().assetBill))
35
36         if(amountBill < capitalAmount){
37           output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
38           output.push(controlAddressAction(gain, capitalAsset, saver))
39           output.push(controlProgramAction((capitalAmount - gain), capitalAsset, GetContractArgs().profitProgram))
40         }else{
41           output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
42           output.push(controlAddressAction(capitalAmount, capitalAsset, saver))
43         }
44
45         window.bytom.advancedTransfer(account, input, output, GetContractArgs().gas*10000000, args, 1)
46           .then((resp) => {
47             if(resp.action === 'reject'){
48               reject('user reject the request')
49             }else if(resp.action === 'success'){
50               updateUtxo({"hash": utxo})
51                 .then(()=>{
52                   updateBalances({
53                     "address": saver,
54                     "asset": GetContractArgs().assetDeposited,
55                     "amount": amountBill*GetContractArgs().totalAmountCapital/GetContractArgs().totalAmountBill
56                   }).then(()=>{
57                     updateBalances({
58                       "address": account.address,
59                       "asset": GetContractArgs().assetBill,
60                       "amount": -amountBill
61                     }).then(()=>{
62                       resolve()
63                     }).catch(err => {
64                       throw err
65                     })
66                   }).catch(err => {
67                     throw err
68                   })
69                   })
70                 .catch(err => {
71                   throw err
72                 })
73             }
74           })
75           .catch(err => {
76             throw err
77           })
78       }
79     }).catch(err => {
80       reject(err)
81     })
82 }
83
84