OSDN Git Service

removed update-base from font-end
[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 {profitProgram, assetDeposited, assetBill, gas, banker, totalAmountBill, totalAmountCapital} from "../../constants";
6
7 export function FixedLimitProfit(account, amountBill, saver) {
8   return new Promise((resolve, reject) => {
9     return listDappUTXO({
10       "program": profitProgram,
11       "asset": assetDeposited
12     }).then(resp => {
13
14       const capitalAmount = resp.amount
15       const capitalAsset = resp.asset
16       const utxo = resp.hash
17
18       if(amountBill > capitalAmount) {
19         throw 'input amount must be smaller or equal to ' + capitalAmount + '.'
20       }else{
21         const input = []
22         const output = []
23
24         const sAmountBill = amountBill/100000000
25         const sTotalAmountBill = totalAmountBill/100000000
26         const gain = totalAmountCapital*sAmountBill/sTotalAmountBill
27
28         const args = contractArguments(amountBill, saver)
29
30         input.push(spendUTXOAction(utxo))
31         input.push(spendWalletAction(amountBill, assetBill))
32
33         if(amountBill < capitalAmount){
34           output.push(controlProgramAction(amountBill, assetBill, banker ))
35           output.push(controlAddressAction(gain, capitalAsset, saver))
36           output.push(controlProgramAction((capitalAmount - gain), capitalAsset, profitProgram))
37         }else{
38           output.push(controlProgramAction(amountBill, assetBill, banker ))
39           output.push(controlAddressAction(capitalAmount, capitalAsset, saver))
40         }
41
42         window.bytom.advancedTransfer(account, input, output, gas*10000000, args)
43           .then((resp) => {
44             if(resp.action === 'reject'){
45               reject('user reject the request')
46             }else if(resp.action === 'success'){
47               updateUtxo({"hash": utxo})
48                 .then(()=>{
49                   updateBalances({
50                     "address": saver,
51                     "asset": assetDeposited,
52                     "amount": amountBill*totalAmountCapital/totalAmountBill
53                   }).then(()=>{
54                     updateBalances({
55                       "address": account.address,
56                       "asset": assetBill,
57                       "amount": -amountBill
58                     }).then(()=>{
59                       resolve()
60                     }).catch(err => {
61                       throw err
62                     })
63                   }).catch(err => {
64                     throw err
65                   })
66                 })
67                 .catch(err => {
68                   throw err
69                 })
70             }
71           })
72           .catch(err => {
73             throw err
74           })
75       }
76     }).catch(err => {
77       reject(err)
78     })
79   })
80 }