OSDN Git Service

update BigNumber.js error correction.
[bytom/Bytom-Dapp-Demo.git] / src / components / layout / save / action.js
1 import {
2   spendUTXOAction, spendWalletAction, controlProgramAction,
3   controlAddressAction, listDappUTXO, updateUtxo, updateBalances,
4   contractArguments
5 } from '../../bytom'
6 import GetContractArgs from '../../constants'
7 import { matchesUTXO } from '../../filter'
8 import BigNumber from 'bignumber.js'
9
10 export function FixedLimitDeposit(amount, address) {
11   return new Promise((resolve, reject) => {
12     return listDappUTXO({
13       "program": GetContractArgs().depositProgram,
14       "asset": GetContractArgs().assetBill,
15       "sort": {
16         "by":"amount",
17         "order":"desc"
18       }
19     }).then(resp => {
20       const limit =  GetContractArgs().radio * 100000000
21       if(resp.length === 0) {
22         throw 'Empty UTXO info, it might be that the utxo is locked. Please retry after 60s.'
23       }else if(amount < limit){
24         throw `Please enter an amount bigger or equal than ${limit}.`
25       }
26
27       const result = matchesUTXO(resp, amount)
28       const billAmount = result.amount
29       const billAsset = result.asset
30       const utxo = result.hash
31
32       if(amount > billAmount){
33         throw 'input amount must be smaller or equal to ' + billAmount +'.'
34       }else{
35         const input = []
36         const output = []
37
38         const args = contractArguments(amount, address)
39
40         input.push(spendUTXOAction(utxo))
41         input.push(spendWalletAction(amount, GetContractArgs().assetDeposited))
42
43         if(amount < billAmount){
44           output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
45           output.push(controlAddressAction(amount, billAsset, address))
46           output.push(controlProgramAction((BigNumber(billAmount).minus(BigNumber(amount))).toNumber(), billAsset, GetContractArgs().depositProgram))
47         }else{
48           output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
49           output.push(controlAddressAction(billAmount, billAsset, address))
50         }
51
52         updateUtxo({"hash": utxo})
53           .then(()=>{
54           window.bytom.advancedTransfer(input, output, GetContractArgs().gas*10000000, args, 1)
55             .then((resp) => {
56               if(resp.action === 'reject'){
57                 reject('user reject the request')
58               }else if(resp.action === 'success'){
59                 updateBalances({
60                   "tx_id": resp.message.result.data.transaction_hash,
61                   address,
62                   "asset": GetContractArgs().assetDeposited,
63                   "amount": -amount
64                 }).then(()=>{
65                   updateBalances({
66                     "tx_id": resp.message.result.data.transaction_hash,
67                     address,
68                     "asset": GetContractArgs().assetBill,
69                     "amount": amount
70                   }).then(()=>{
71                     resolve()
72                   }).catch(err => {
73                     throw err
74                   })
75                 }).catch(err => {
76                   throw err
77                 })
78               }
79             })
80             .catch(err => {
81               throw err
82             })
83           })
84           .catch(err => {
85             throw err
86           })
87       }
88     }).catch(err => {
89       reject(err)
90     })
91   })
92 }