OSDN Git Service

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