spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction,
updateBalances, updateUtxo, listDappUTXO, contractArguments
} from '../../bytom'
-import GetContractArgs from "../../constants";
+import GetContractArgs from '../../constants'
+import { matchesUTXO } from '../../filter'
+import BigNumber from 'bignumber.js'
export function FixedLimitProfit(account, amountBill, saver) {
+ return new Promise((resolve, reject) => {
return listDappUTXO({
"program": GetContractArgs().profitProgram,
- "asset": GetContractArgs().assetDeposited
+ "asset": GetContractArgs().assetDeposited,
+ "sort": {
+ "by":"amount",
+ "order":"desc"
+ }
}).then(resp => {
-
if(resp.length === 0) {
- throw 'cannot load UTXO info.'
- return
+ throw 'Empty UTXO info, it might be that the utxo is locked. Please retry after 60s.'
+ }else if(amountBill < 100000000){
+ throw 'Please enter an amount bigger or equal than 100000000.'
}
- const capitalAmount = resp.amount
- const capitalAsset = resp.asset
- const utxo = resp.hash
+ const radio = BigNumber( GetContractArgs().radio )
+ const profitAmount = radio.multipliedBy(amountBill).toNumber()
- if(amountBill > capitalAmount) {
- throw 'input amount must be smaller or equal to ' + capitalAmount + '.'
+ const result = matchesUTXO(resp, profitAmount)
+ const capitalAmount = result.amount
+ const capitalAsset = result.asset
+ const utxo = result.hash
+
+ if(profitAmount > capitalAmount) {
+ throw 'input amount must be smaller or equal to ' + capitalAmount/radio.toNumber() + '.'
}else{
const input = []
const output = []
- const sAmountBill = amountBill/100000000
- const sTotalAmountBill = totalAmountBill/100000000
- const gain = totalAmountCapital*sAmountBill/sTotalAmountBill
+ const sAmountBill = BigNumber(amountBill).div( 100000000 )
+ const sTotalAmountBill = BigNumber(GetContractArgs().totalAmountBill).div( 100000000 )
+ const multiplyResult = BigNumber( GetContractArgs().totalAmountCapital).multipliedBy( sAmountBill )
+ const gain = multiplyResult.div( sTotalAmountBill ).toNumber()
+
+ if( multiplyResult.isGreaterThan( 9223372036854775807 ) ){
+ throw 'The entered amount is too big, please reduce the amount.'
+ }
const args = contractArguments(amountBill, saver)
input.push(spendUTXOAction(utxo))
input.push(spendWalletAction(amountBill, GetContractArgs().assetBill))
- if(amountBill < capitalAmount){
+ if( gain < capitalAmount ){
output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
output.push(controlAddressAction(gain, capitalAsset, saver))
- output.push(controlProgramAction((capitalAmount - gain), capitalAsset, GetContractArgs().profitProgram))
+ output.push(controlProgramAction((BigNumber(capitalAmount).minus(gain)).toNumber(), capitalAsset, GetContractArgs().profitProgram))
}else{
output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
output.push(controlAddressAction(capitalAmount, capitalAsset, saver))
}
- window.bytom.advancedTransfer(account, input, output, GetContractArgs().gas*10000000, args, 1)
- .then((resp) => {
- if(resp.action === 'reject'){
- reject('user reject the request')
- }else if(resp.action === 'success'){
- updateUtxo({"hash": utxo})
- .then(()=>{
+ updateUtxo({"hash": utxo})
+ .then(()=>{
+ window.bytom.advancedTransfer(input, output, GetContractArgs().gas*10000000, args, 1)
+ .then((resp) => {
+ if(resp.action === 'reject'){
+ reject('user reject the request')
+ }else if(resp.action === 'success'){
+ const transactionHash = resp.message.result.data.transaction_hash
updateBalances({
+ "tx_id": transactionHash,
"address": saver,
"asset": GetContractArgs().assetDeposited,
- "amount": amountBill*GetContractArgs().totalAmountCapital/GetContractArgs().totalAmountBill
+ "amount": profitAmount
}).then(()=>{
updateBalances({
+ "tx_id": transactionHash,
"address": account.address,
"asset": GetContractArgs().assetBill,
"amount": -amountBill
}).catch(err => {
throw err
})
- })
- .catch(err => {
- throw err
- })
- }
- })
+ }
+ })
+ .catch(err => {
+ throw err
+ })
+ })
.catch(err => {
throw err
})
}).catch(err => {
reject(err)
})
+ })
}