OSDN Git Service

convert bigNumber string to number
[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 BigNumber from 'bignumber.js'
8
9 export function FixedLimitDeposit(account, amount, address) {
10   return new Promise((resolve, reject) => {
11     return listDappUTXO({
12       "program": GetContractArgs().depositProgram,
13       "asset": GetContractArgs().assetBill,
14       "sort": {
15         "by":"amount",
16         "order":"desc"
17       }
18     }).then(resp => {
19       if(resp.length === 0) {
20         throw 'cannot load UTXO info.'
21       }
22
23       const result = resp[0]
24       const billAmount = result.amount
25       const billAsset = result.asset
26       const utxo = result.hash
27
28       if(amount > billAmount){
29         throw 'input amount must be smaller or equal to ' + billAmount +'.'
30       }else{
31         const input = []
32         const output = []
33
34         const args = contractArguments(amount, address)
35
36         input.push(spendUTXOAction(utxo))
37         input.push(spendWalletAction(amount, GetContractArgs().assetDeposited))
38
39         if(amount < billAmount){
40           output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
41           output.push(controlAddressAction(amount, billAsset, address))
42           output.push(controlProgramAction((BigNumber(billAmount).minus(BigNumber(amount))).toNumber(), billAsset, GetContractArgs().depositProgram))
43         }else{
44           output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
45           output.push(controlAddressAction(billAmount, billAsset, address))
46         }
47
48         window.bytom.advancedTransfer(account, input, output, GetContractArgs().gas*10000000, args, 1)
49           .then((resp) => {
50             if(resp.action === 'reject'){
51               reject('user reject the request')
52             }else if(resp.action === 'success'){
53               updateUtxo({"hash": utxo})
54                 .then(()=>{
55                   updateBalances({
56                     "tx_id": resp.message.result.data.transaction_hash,
57                     address,
58                     "asset": GetContractArgs().assetDeposited,
59                     "amount": -amount
60                   }).then(()=>{
61                     updateBalances({
62                       "tx_id": resp.message.result.data.transaction_hash,
63                       address,
64                       "asset": GetContractArgs().assetBill,
65                       "amount": amount
66                     }).then(()=>{
67                       resolve()
68                     }).catch(err => {
69                       throw err
70                     })
71                   }).catch(err => {
72                     throw err
73                   })
74                 })
75                 .catch(err => {
76                   throw err
77                 })
78             }
79           })
80           .catch(err => {
81             throw err
82           })
83       }
84     }).catch(err => {
85       reject(err)
86     })
87   })
88 }