OSDN Git Service

convert bigNumber string to number
[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 import { matchesUTXO } from '../../filter'
7 import BigNumber from 'bignumber.js'
8
9 export function FixedLimitProfit(account, amountBill, saver) {
10   return new Promise((resolve, reject) => {
11     return listDappUTXO({
12       "program": GetContractArgs().profitProgram,
13       "asset": GetContractArgs().assetDeposited,
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 = matchesUTXO(resp, amountBill)
24       const capitalAmount = result.amount
25       const capitalAsset = result.asset
26       const utxo = result.hash
27
28       if(amountBill > capitalAmount) {
29         throw 'input amount must be smaller or equal to ' + capitalAmount + '.'
30       }else{
31         const input = []
32         const output = []
33
34         const sAmountBill = BigNumber(amountBill).div( 100000000 )
35         const sTotalAmountBill = BigNumber(GetContractArgs().totalAmountBill).div( 100000000 )
36         const multiplyResult = BigNumber( GetContractArgs().totalAmountCapital).multipliedBy( sAmountBill )
37         const gain = multiplyResult.div( sTotalAmountBill ).toNumber()
38
39         if( multiplyResult.isGreaterThan( 9223372036854775807 ) ){
40           throw 'The entered amount is too big, please reduce the amount.'
41         }
42
43         const args = contractArguments(amountBill, saver)
44
45         input.push(spendUTXOAction(utxo))
46         input.push(spendWalletAction(amountBill, GetContractArgs().assetBill))
47
48         if(amountBill < capitalAmount){
49           output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
50           output.push(controlAddressAction(gain, capitalAsset, saver))
51           output.push(controlProgramAction((BigNumber(capitalAmount).minus(gain)).toNumber(), capitalAsset, GetContractArgs().profitProgram))
52         }else{
53           output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
54           output.push(controlAddressAction(capitalAmount, capitalAsset, saver))
55         }
56
57         window.bytom.advancedTransfer(account, input, output, GetContractArgs().gas*10000000, args, 1)
58           .then((resp) => {
59             if(resp.action === 'reject'){
60               reject('user reject the request')
61             }else if(resp.action === 'success'){
62               updateUtxo({"hash": utxo})
63                 .then(()=>{
64                   updateBalances({
65                     "tx_id": resp.message.result.data.transaction_hash,
66                     "address": saver,
67                     "asset": GetContractArgs().assetDeposited,
68                     "amount": amountBill*GetContractArgs().totalAmountCapital/GetContractArgs().totalAmountBill
69                   }).then(()=>{
70                     updateBalances({
71                       "tx_id": resp.message.result.data.transaction_hash,
72                       "address": account.address,
73                       "asset": GetContractArgs().assetBill,
74                       "amount": -amountBill
75                     }).then(()=>{
76                       resolve()
77                     }).catch(err => {
78                       throw err
79                     })
80                   }).catch(err => {
81                     throw err
82                   })
83                   })
84                 .catch(err => {
85                   throw err
86                 })
87             }
88           })
89           .catch(err => {
90             throw err
91           })
92       }
93     }).catch(err => {
94       reject(err)
95     })
96   })
97 }
98
99