OSDN Git Service

update BigNumber.js error correction.
[bytom/Bytom-Dapp-Demo.git] / src / components / layout / profit / action.js
index 60ca3f0..cd1e626 100644 (file)
@@ -17,16 +17,21 @@ export function FixedLimitProfit(account, amountBill, saver) {
       }
     }).then(resp => {
       if(resp.length === 0) {
-        throw 'cannot load UTXO info.'
+        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 result = matchesUTXO(resp, amountBill)
+      const radio = BigNumber( GetContractArgs().radio )
+      const profitAmount = radio.multipliedBy(amountBill).toNumber()
+
+      const result = matchesUTXO(resp, profitAmount)
       const capitalAmount = result.amount
       const capitalAsset = result.asset
       const utxo = result.hash
 
-      if(amountBill > capitalAmount) {
-        throw 'input amount must be smaller or equal to ' + capitalAmount + '.'
+      if(profitAmount > capitalAmount) {
+        throw 'input amount must be smaller or equal to ' + capitalAmount/radio.toNumber() + '.'
       }else{
         const input = []
         const output = []
@@ -34,7 +39,7 @@ export function FixedLimitProfit(account, amountBill, saver) {
         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 )
+        const gain = multiplyResult.div( sTotalAmountBill ).toNumber()
 
         if( multiplyResult.isGreaterThan( 9223372036854775807 ) ){
           throw 'The entered amount is too big, please reduce the amount.'
@@ -45,30 +50,31 @@ export function FixedLimitProfit(account, 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((BigNumber(capitalAmount).minus(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": resp.message.result.data.transaction_hash,
+                    "tx_id": transactionHash,
                     "address": saver,
                     "asset": GetContractArgs().assetDeposited,
-                    "amount": amountBill*GetContractArgs().totalAmountCapital/GetContractArgs().totalAmountBill
+                    "amount": profitAmount
                   }).then(()=>{
                     updateBalances({
-                      "tx_id": resp.message.result.data.transaction_hash,
+                      "tx_id": transactionHash,
                       "address": account.address,
                       "asset": GetContractArgs().assetBill,
                       "amount": -amountBill
@@ -80,12 +86,12 @@ export function FixedLimitProfit(account, amountBill, saver) {
                   }).catch(err => {
                     throw err
                   })
-                  })
-                .catch(err => {
-                  throw err
-                })
-            }
-          })
+                }
+              })
+              .catch(err => {
+                throw err
+              })
+            })
           .catch(err => {
             throw err
           })