OSDN Git Service

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