OSDN Git Service

update BigNumber.js error correction.
[bytom/Bytom-Dapp-Demo.git] / src / components / layout / profit / action.js
index fdbd7d9..cd1e626 100644 (file)
-import { spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction, listUTXO } from '../../bytom'
+import {
+  spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction,
+  updateBalances, updateUtxo, listDappUTXO, contractArguments
+} from '../../bytom'
+import GetContractArgs from '../../constants'
+import { matchesUTXO } from '../../filter'
+import BigNumber from 'bignumber.js'
 
-export function FixedLimitProfit(amountBill, saver) {
+export function FixedLimitProfit(account, amountBill, saver) {
+  return new Promise((resolve, reject) => {
+    return listDappUTXO({
+      "program": GetContractArgs().profitProgram,
+      "asset": GetContractArgs().assetDeposited,
+      "sort": {
+        "by":"amount",
+        "order":"desc"
+      }
+    }).then(resp => {
+      if(resp.length === 0) {
+        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 program = "203f98494c8fcce82a1da8054cda521295333a6c7a6ea3b861a27fd766a5cea5731600140014f19df269f9334bdcb496da6b63b275d49447016401500500c817a8040500e40b540220df4638860378a2203466833c935efa19f513ac3aae2cb52d36cee7fa5010b0794ca4587a64980000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c967600a069c3787c9f91616481000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696393000000005b795479515b79c16951c3c2515d79c16963a4000000557acd9f69577a577aae7cac747800c0"
+      const radio = BigNumber( GetContractArgs().radio )
+      const profitAmount = radio.multipliedBy(amountBill).toNumber()
 
-  listUTXO({
-    "filter": {
-      "script": program,
-      "asset":"bbc81814b304cf4e129582b094672b917d28e1109aab4569697d72f102af07c8"
-    }
-  }).then(resp => {
-    const capitalAmount = resp.amount
-    const capitalAsset = resp.asset
-    const utxo = resp.hash
+      const result = matchesUTXO(resp, profitAmount)
+      const capitalAmount = result.amount
+      const capitalAsset = result.asset
+      const utxo = result.hash
 
-    const gas = 40000000
-    const assetBill = "df4638860378a2203466833c935efa19f513ac3aae2cb52d36cee7fa5010b079"
-    const btm = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
+      if(profitAmount > capitalAmount) {
+        throw 'input amount must be smaller or equal to ' + capitalAmount/radio.toNumber() + '.'
+      }else{
+        const input = []
+        const output = []
 
-    const input = []
-    const output = []
+        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()
 
-    const totalAmountBill = 10000000000
-    const totalAmountCapital = 20000000000
+        if( multiplyResult.isGreaterThan( 9223372036854775807 ) ){
+          throw 'The entered amount is too big, please reduce the amount.'
+        }
 
-    const sAmountBill = amountBill/100000000
-    const sTotalAmountBill = totalAmountBill/100000000
-    const gain = totalAmountCapital*sAmountBill/sTotalAmountBill
+        const args = contractArguments(amountBill, saver)
 
-    const banker = "00140014f19df269f9334bdcb496da6b63b275d49447"
+        input.push(spendUTXOAction(utxo))
+        input.push(spendWalletAction(amountBill, GetContractArgs().assetBill))
 
-    input.push(spendUTXOAction(utxo, amountBill, saver))
-    input.push(spendWalletAction(amountBill, assetBill))
-    input.push(spendWalletAction(gas, btm))
-
-    if(amountBill < capitalAmount){
-      output.push(controlProgramAction(amountBill, assetBill, banker ))
-      output.push(controlAddressAction(gain, capitalAsset, saver))
-      output.push(controlProgramAction((capitalAmount - gain), capitalAsset, program))
-    }else{
-      output.push(controlProgramAction(amountBill, assetBill, banker ))
-      output.push(controlAddressAction(capitalAmount, capitalAsset, saver))
-    }
-
-    window.bytom.advancedTransfer(input, output)
+        if( gain < capitalAmount ){
+          output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
+          output.push(controlAddressAction(gain, capitalAsset, saver))
+          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))
+        }
 
+        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": profitAmount
+                  }).then(()=>{
+                    updateBalances({
+                      "tx_id": transactionHash,
+                      "address": account.address,
+                      "asset": GetContractArgs().assetBill,
+                      "amount": -amountBill
+                    }).then(()=>{
+                      resolve()
+                    }).catch(err => {
+                      throw err
+                    })
+                  }).catch(err => {
+                    throw err
+                  })
+                }
+              })
+              .catch(err => {
+                throw err
+              })
+            })
+          .catch(err => {
+            throw err
+          })
+      }
+    }).catch(err => {
+      reject(err)
+    })
   })
+}
+
 
-}
\ No newline at end of file