2 spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction,
3 updateBalances, updateUtxo, listDappUTXO, contractArguments
5 import GetContractArgs from '../../constants'
6 import { matchesUTXO } from '../../filter'
7 import BigNumber from 'bignumber.js'
9 export function FixedLimitProfit(account, amountBill, saver) {
10 return new Promise((resolve, reject) => {
12 "program": GetContractArgs().profitProgram,
13 "asset": GetContractArgs().assetDeposited,
19 if(resp.length === 0) {
20 throw 'Empty UTXO info, it might be that the utxo is locked. Please retry after 60s.'
23 const radio = BigNumber( GetContractArgs().totalAmountCapital).div(GetContractArgs().totalAmountBill)
24 const matchesAmount = radio.multipliedBy(amountBill).toNumber()
26 const result = matchesUTXO(resp, matchesAmount)
27 const capitalAmount = result.amount
28 const capitalAsset = result.asset
29 const utxo = result.hash
31 if(matchesAmount > capitalAmount) {
32 throw 'input amount must be smaller or equal to ' + capitalAmount/radio.toNumber() + '.'
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()
42 if( multiplyResult.isGreaterThan( 9223372036854775807 ) ){
43 throw 'The entered amount is too big, please reduce the amount.'
46 const args = contractArguments(amountBill, saver)
48 input.push(spendUTXOAction(utxo))
49 input.push(spendWalletAction(amountBill, GetContractArgs().assetBill))
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))
56 output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
57 output.push(controlAddressAction(capitalAmount, capitalAsset, saver))
60 updateUtxo({"hash": utxo})
62 window.bytom.advancedTransfer(account, input, output, GetContractArgs().gas*10000000, args, 1)
64 if(resp.action === 'reject'){
65 reject('user reject the request')
66 }else if(resp.action === 'success'){
68 "tx_id": resp.message.result.data.transaction_hash,
70 "asset": GetContractArgs().assetDeposited,
71 "amount": amountBill*GetContractArgs().totalAmountCapital/GetContractArgs().totalAmountBill
74 "tx_id": resp.message.result.data.transaction_hash,
75 "address": account.address,
76 "asset": GetContractArgs().assetBill,