OSDN Git Service

update and seperate the code in to submitContract
authorZhiting Lin <zlin035@uottawa.ca>
Mon, 1 Apr 2019 09:07:09 +0000 (17:07 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Mon, 1 Apr 2019 09:07:09 +0000 (17:07 +0800)
contracts/configure.json.js
src/components/layout/account/action.js
src/components/layout/account/index.jsx
src/components/layout/profit/action.js
src/components/layout/profit/index.jsx
src/components/layout/save/action.js
src/components/layout/save/index.jsx
src/components/util/api.js [moved from src/components/bytom.js with 61% similarity]
src/components/util/bytomAction.js [new file with mode: 0644]
src/components/util/submitContract.js [new file with mode: 0644]

index a5dea32..31a9daa 100644 (file)
@@ -1,26 +1,26 @@
 var config = {
   "solonet": {
-    "depositProgram": "2091194ddbf3614cafbadb1274c33e61afd4d5044c6ec4c30f8202980199c30083160014c800033d5e94de5f22e23a6d3cbeaed87b55bd640600204aa9d101050010a5d4e8203310d9951697418af3cdbe7a9cdde1dc49bb5439503dacb33828d6c9ef5af5a24dfc01567a64f5010000c358797ca153795579a19a6957790400e1f5059653790400e1f505967c00a07c00a09a69c358797c9f91616429010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec169515879c2515a79c16952c3597994c251005a79895979895879895779895679895579890274787e008901c07ec16963f0010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec16951c3c2515a79c16963fc010000567a567aae7cac747800c0",
-    "profitProgram": "2091194ddbf3614cafbadb1274c33e61afd4d5044c6ec4c30f8202980199c30083160014c800033d5e94de5f22e23a6d3cbeaed87b55bd640600204aa9d101050010a5d4e820666f298d34806a6db0528c3b3d081bc00fa58aa393e7c42f90d67eb7db2a524f4c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac747800c0",
-    "assetDeposited": "3310d9951697418af3cdbe7a9cdde1dc49bb5439503dacb33828d6c9ef5af5a2",
-    "assetBill": "666f298d34806a6db0528c3b3d081bc00fa58aa393e7c42f90d67eb7db2a524f",
+    "depositProgram": "207c171cafa127bb405295e37cdbfbddbf2d39079872bd2f5cd95fb21a07147ec51600143705562ee78b51846b7aec9e2eadc5dcfef5c08f0600204aa9d101050010a5d4e820bf19b9726f8229cc3a894af3a93caea8b05c0605a5ebba21018f6a2cc85e02084dfc01567a64f5010000c358797ca153795579a19a6957790400e1f5059653790400e1f505967c00a07c00a09a69c358797c9f91616429010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec169515879c2515a79c16952c3597994c251005a79895979895879895779895679895579890274787e008901c07ec16963f0010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec16951c3c2515a79c16963fc010000567a567aae7cac747800c0",
+    "profitProgram": "207c171cafa127bb405295e37cdbfbddbf2d39079872bd2f5cd95fb21a07147ec51600143705562ee78b51846b7aec9e2eadc5dcfef5c08f0600204aa9d101050010a5d4e82034d60be23a1858ed0d1f9c62a4a18185c2b35c068ca9e948d91fd62e7d47a8aa4c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac747800c0",
+    "assetDeposited": "bf19b9726f8229cc3a894af3a93caea8b05c0605a5ebba21018f6a2cc85e0208",
+    "assetBill": "34d60be23a1858ed0d1f9c62a4a18185c2b35c068ca9e948d91fd62e7d47a8aa",
     "totalAmountBill": 1000000000000,
     "totalAmountCapital": 2000000000000,
     "dueBlockHeight": "",
     "expireBlockHeight": "",
-    "banker": "0014c800033d5e94de5f22e23a6d3cbeaed87b55bd64",
+    "banker": "00143705562ee78b51846b7aec9e2eadc5dcfef5c08f",
     "gas": 0.4
   },
   "testnet":{
-    "depositProgram": "20f39af759065598406ca988f0dd79af9175dd7adcbe019317a2d605578b1597ac1600147211ec12410ce8bd0d71cab0a29be3ea61c71eb103c8260203da240203da2402060080f420e6b50600407a10f35a2000d38a1c946e8cba1a69493240f281cd925002a43b81f516c4391b5fb2ffdacd4d4302597a64370200005479cda069c35b790400e1f5059600a05c797ba19a53795579a19a695a790400e1f5059653790400e1f505967800a07800a09a6955797b957c9600a069c35b797c9f9161645b010000005b79c2547951005e79895d79895c79895b7989597989587989537a894ca4587a64980000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c967600a069c3787c9f91616481000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696393000000005b795479515b79c16951c3c2515d79c16963a4000000557acd9f69577a577aae7cac890274787e008901c07ec169515b79c2515d79c16952c35c7994c251005d79895c79895b79895a79895979895879895779895679895579890274787e008901c07ec1696332020000005b79c2547951005e79895d79895c79895b7989597989587989537a894ca4587a64980000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c967600a069c3787c9f91616481000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696393000000005b795479515b79c16951c3c2515d79c16963a4000000557acd9f69577a577aae7cac890274787e008901c07ec16951c3c2515d79c1696343020000547acd9f69587a587aae7cac747800c0",
-    "profitProgram": "20f39af759065598406ca988f0dd79af9175dd7adcbe019317a2d605578b1597ac1600147211ec12410ce8bd0d71cab0a29be3ea61c71eb103c8260203da2402060080f420e6b50600407a10f35a20f855baf98778a892bad0371f5afca845191824dc8584585d566fbbc8ef1f304c4ca4587a64980000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c967600a069c3787c9f91616481000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696393000000005b795479515b79c16951c3c2515d79c16963a4000000557acd9f69577a577aae7cac747800c0",
+    "depositProgram": "20f39af759065598406ca988f0dd79af9175dd7adcbe019317a2d605578b1597ac1600147211ec12410ce8bd0d71cab0a29be3ea61c71eb1060040e59c30120600a0724e18092000d38a1c946e8cba1a69493240f281cd925002a43b81f516c4391b5fb2ffdacd4dfc01567a64f5010000c358797ca153795579a19a6957790400e1f5059653790400e1f505967c00a07c00a09a69c358797c9f91616429010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec169515879c2515a79c16952c3597994c251005a79895979895879895779895679895579890274787e008901c07ec16963f0010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec16951c3c2515a79c16963fc010000567a567aae7cac747800c0",
+    "profitProgram": "20f39af759065598406ca988f0dd79af9175dd7adcbe019317a2d605578b1597ac1600147211ec12410ce8bd0d71cab0a29be3ea61c71eb1060040e59c30120600a0724e180920f855baf98778a892bad0371f5afca845191824dc8584585d566fbbc8ef1f304c4c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac747800c0",
     "assetDeposited": "00d38a1c946e8cba1a69493240f281cd925002a43b81f516c4391b5fb2ffdacd",
     "assetBill": "f855baf98778a892bad0371f5afca845191824dc8584585d566fbbc8ef1f304c",
-    "totalAmountBill": 100000000000000,
-    "totalAmountCapital": 200000000000000,
-    "dueBlockHeight": 140506,
-    "expireBlockHeight": 140506,
+    "totalAmountBill": 10000000000000,
+    "totalAmountCapital": 20000000000000,
+    "dueBlockHeight": 0,
+    "expireBlockHeight": 0,
     "banker": "00147211ec12410ce8bd0d71cab0a29be3ea61c71eb1",
     "gas": 0.4
   }
-}
\ No newline at end of file
+}
index fd5241e..3c375c2 100644 (file)
@@ -1,4 +1,4 @@
-import { listAddress } from '../../bytom'
+import { listAddress } from '../../util/api'
 
 
 import GetContractArgs from "../../constants";
index 05477d7..80f54e3 100644 (file)
@@ -5,7 +5,7 @@ import { connect } from "react-redux"
 import jdenticon from "jdenticon"
 import action from "./action";
 import GetContractArgs from "../../constants";
-import { listBalances } from '../../bytom'
+import { listBalances } from '../../util/api'
 
 class Account extends Component {
   constructor(props) {
index cd1e626..f67958e 100644 (file)
@@ -1,25 +1,41 @@
 import {
   spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction,
-  updateBalances, updateUtxo, listDappUTXO, contractArguments
-} from '../../bytom'
+  contractArguments
+} from '../../util/bytomAction'
+import {
+  updateBalances, listDappUTXO
+} from '../../util/api'
+import { submitContract } from '../../util/submitContract'
 import GetContractArgs from '../../constants'
 import { matchesUTXO } from '../../filter'
 import BigNumber from 'bignumber.js'
 
 export function FixedLimitProfit(account, amountBill, saver) {
+  const object = {
+    address: saver,
+    amount: amountBill,
+    parameter: [saver, amountBill, account]
+  }
+  return submitContract(listDepositUTXO, createContractTransaction, updateDatatbaseBalance, object)
+}
+
+function listDepositUTXO() {
+  return listDappUTXO({
+    "program": GetContractArgs().profitProgram,
+    "asset": GetContractArgs().assetDeposited,
+    "sort": {
+      "by":"amount",
+      "order":"desc"
+    }
+  })
+}
+
+function createContractTransaction(resp, 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.'
+        reject( '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.'
+        reject( 'Please enter an amount bigger or equal than 100000000.')
       }
 
       const radio = BigNumber( GetContractArgs().radio )
@@ -31,18 +47,18 @@ export function FixedLimitProfit(account, amountBill, saver) {
       const utxo = result.hash
 
       if(profitAmount > capitalAmount) {
-        throw 'input amount must be smaller or equal to ' + capitalAmount/radio.toNumber() + '.'
-      }else{
+        reject( 'input amount must be smaller or equal to ' + capitalAmount/radio.toNumber() + '.')
+      }else {
         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 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.'
+        if (multiplyResult.isGreaterThan(9223372036854775807)) {
+          reject( 'The entered amount is too big, please reduce the amount.')
         }
 
         const args = contractArguments(amountBill, saver)
@@ -50,56 +66,42 @@ export function FixedLimitProfit(account, amountBill, saver) {
         input.push(spendUTXOAction(utxo))
         input.push(spendWalletAction(amountBill, GetContractArgs().assetBill))
 
-        if( gain < capitalAmount ){
-          output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
+        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 ))
+        } 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
-          })
+        resolve({
+          input,
+          output,
+          args,
+          utxo
+        })
       }
-    }).catch(err => {
-      reject(err)
-    })
   })
 }
 
-
+function updateDatatbaseBalance(resp, saver, amountBill, account){
+  const transactionHash = resp.message.result.data.transaction_hash
+  const radio = BigNumber( GetContractArgs().radio )
+  const profitAmount = radio.multipliedBy(amountBill).toNumber()
+  return updateBalances({
+    "tx_id": transactionHash,
+    "address": saver,
+    "asset": GetContractArgs().assetDeposited,
+    "amount": profitAmount
+  }).then(()=>{
+    return updateBalances({
+      "tx_id": transactionHash,
+      "address": account.address,
+      "asset": GetContractArgs().assetBill,
+      "amount": -amountBill
+    })
+  }).catch(err => {
+    throw err
+  })
+}
index 01c4601..c851275 100644 (file)
@@ -44,19 +44,27 @@ class Profit extends React.Component {
     const account = this.state.account
     const address = event.target.address.value
 
+    this.refs.btn.setAttribute("disabled", "disabled")
+    this.setState({
+      error:'',
+      msg: ''
+    })
+
     FixedLimitProfit(account, amount, address)
       .then(()=> {
+        this.refs.btn.removeAttribute("disabled");
         this.setState({
           error:'',
           msg:`Submit success!!! you spent ${amount} bill asset, and gain ${BigNumber(amount).multipliedBy(GetContractArgs().radio).toNumber()} deposit asset.`
         })
       }).catch(err => {
-      this.setState({
-        error: err,
-        msg:''
+        this.refs.btn.removeAttribute("disabled");
+        this.setState({
+          error: err,
+          msg:''
+        })
       })
-    })
-  }
+    }
 
   render() {
     return (
@@ -88,7 +96,7 @@ class Profit extends React.Component {
               onChange={this.handleInputChange} />
           </div>
           <p>Fee:  {GetContractArgs().gas} BTM</p>
-          <button type="submit" className="btn btn-primary">Profit to address</button>
+          <button ref='btn' type="submit" className="btn btn-primary">Profit to address</button>
           {this.state.msg && <div className="alert alert-success mt-4" role="alert">
             {this.state.msg}
           </div>}
index ea49419..07cb11c 100644 (file)
@@ -1,92 +1,94 @@
 import {
   spendUTXOAction, spendWalletAction, controlProgramAction,
-  controlAddressAction, listDappUTXO, updateUtxo, updateBalances,
-  contractArguments
-} from '../../bytom'
+  controlAddressAction, contractArguments
+} from '../../util/bytomAction'
+import { listDappUTXO, updateBalances
+} from '../../util/api'
 import GetContractArgs from '../../constants'
 import { matchesUTXO } from '../../filter'
+import { submitContract } from '../../util/submitContract'
 import BigNumber from 'bignumber.js'
 
 export function FixedLimitDeposit(amount, address) {
-  return new Promise((resolve, reject) => {
-    return listDappUTXO({
-      "program": GetContractArgs().depositProgram,
-      "asset": GetContractArgs().assetBill,
-      "sort": {
-        "by":"amount",
-        "order":"desc"
-      }
-    }).then(resp => {
-      const limit =  GetContractArgs().radio * 100000000
-      if(resp.length === 0) {
-        throw 'Empty UTXO info, it might be that the utxo is locked. Please retry after 60s.'
-      }else if(amount < limit){
-        throw `Please enter an amount bigger or equal than ${limit}.`
-      }
+  const object = {
+    address: address,
+    amount: amount,
+    parameter: [amount, address]
+  }
+  return submitContract(listDepositUTXO, createContractTransaction, updateDatatbaseBalance, object)
+}
+
+function listDepositUTXO() {
+  return listDappUTXO({
+    "program": GetContractArgs().depositProgram,
+    "asset": GetContractArgs().assetBill,
+    "sort": {
+      "by":"amount",
+      "order":"desc"
+    }
+  })
+}
 
-      const result = matchesUTXO(resp, amount)
-      const billAmount = result.amount
-      const billAsset = result.asset
-      const utxo = result.hash
+function createContractTransaction(resp, amount, address){
+  return new Promise((resolve, reject) => {
+    //utxo pre calculation
+    const limit = GetContractArgs().radio * 100000000
+    if (resp.length === 0) {
+      reject( 'Empty UTXO info, it might be that the utxo is locked. Please retry after 60s.')
+    } else if (amount < limit) {
+      reject( `Please enter an amount bigger or equal than ${limit}.`)
+    }
 
-      if(amount > billAmount){
-        throw 'input amount must be smaller or equal to ' + billAmount +'.'
-      }else{
-        const input = []
-        const output = []
+    const result = matchesUTXO(resp, amount)
+    const billAmount = result.amount
+    const billAsset = result.asset
+    const utxo = result.hash
 
-        const args = contractArguments(amount, address)
+    //contract calculation
+    if (amount > billAmount) {
+      reject('input amount must be smaller or equal to ' + billAmount + '.')
+    } else {
+      const input = []
+      const output = []
 
-        input.push(spendUTXOAction(utxo))
-        input.push(spendWalletAction(amount, GetContractArgs().assetDeposited))
+      const args = contractArguments(amount, address)
 
-        if(amount < billAmount){
-          output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
-          output.push(controlAddressAction(amount, billAsset, address))
-          output.push(controlProgramAction((BigNumber(billAmount).minus(BigNumber(amount))).toNumber(), billAsset, GetContractArgs().depositProgram))
-        }else{
-          output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
-          output.push(controlAddressAction(billAmount, billAsset, address))
-        }
+      input.push(spendUTXOAction(utxo))
+      input.push(spendWalletAction(amount, GetContractArgs().assetDeposited))
 
-        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'){
-                updateBalances({
-                  "tx_id": resp.message.result.data.transaction_hash,
-                  address,
-                  "asset": GetContractArgs().assetDeposited,
-                  "amount": -amount
-                }).then(()=>{
-                  updateBalances({
-                    "tx_id": resp.message.result.data.transaction_hash,
-                    address,
-                    "asset": GetContractArgs().assetBill,
-                    "amount": amount
-                  }).then(()=>{
-                    resolve()
-                  }).catch(err => {
-                    throw err
-                  })
-                }).catch(err => {
-                  throw err
-                })
-              }
-            })
-            .catch(err => {
-              throw err
-            })
-          })
-          .catch(err => {
-            throw err
-          })
+      if (amount < billAmount) {
+        output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
+        output.push(controlAddressAction(amount, billAsset, address))
+        output.push(controlProgramAction((BigNumber(billAmount).minus(BigNumber(amount))).toNumber(), billAsset, GetContractArgs().depositProgram))
+      } else {
+        output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
+        output.push(controlAddressAction(billAmount, billAsset, address))
       }
-    }).catch(err => {
-      reject(err)
+
+      resolve({
+        input,
+        output,
+        args,
+        utxo
+      })
+    }
+  })
+}
+
+function updateDatatbaseBalance(resp, amount, address){
+ return updateBalances({
+    "tx_id": resp.message.result.data.transaction_hash,
+    address,
+    "asset": GetContractArgs().assetDeposited,
+    "amount": -amount
+  }).then(()=>{
+    return updateBalances({
+      "tx_id": resp.message.result.data.transaction_hash,
+      address,
+      "asset": GetContractArgs().assetBill,
+      "amount": amount
     })
+  }).catch(err => {
+    throw err
   })
 }
index 21e3d8d..928a652 100644 (file)
@@ -43,13 +43,21 @@ class Save extends React.Component {
     const account = this.state.account
     const address = account.address
 
+    this.refs.btn.setAttribute("disabled", "disabled")
+    this.setState({
+      error:'',
+      msg: ''
+    })
+
     FixedLimitDeposit(amount, address)
       .then(()=> {
+          this.refs.btn.removeAttribute("disabled");
           this.setState({
             error:'',
             msg:`Submit success!!! you spent ${amount} deposite asset,and gain ${amount} bill asset.`
           })
         }).catch(err => {
+          this.refs.btn.removeAttribute("disabled");
           this.setState({
             error:err,
             msg: ''
@@ -78,7 +86,7 @@ class Save extends React.Component {
               onChange={this.handleInputChange} />
           </div>
           <p>Fee:  {GetContractArgs().gas} BTM</p>
-          <button type="submit" className="btn btn-primary">Spend Asset</button>
+          <button ref="btn" type="submit" className="btn btn-primary">Spend Asset</button>
 
           {this.state.msg && <div className="alert alert-success mt-4" role="alert">
             {this.state.msg}
similarity index 61%
rename from src/components/bytom.js
rename to src/components/util/api.js
index 9fb2194..15f06ed 100644 (file)
@@ -1,61 +1,13 @@
-import axios from 'axios'
-
-export function spendUTXOAction(utxo){
-  return {
-    "type": "spend_utxo",
-    "output_id": utxo
-  }
-}
-
-export function contractArguments(amount, address){
-  return [
-    {
-      "type": "integer",
-      "value": amount
-    },
-    {
-      "type": "address",
-      "value": address
-    },
-    {
-      "type": "data",
-      "value": ""
-    }
-  ]
-}
-
-export function spendWalletAction(amount, asset){
-  return {
-    "amount": amount,
-    "asset": asset,
-    "type": "spend_wallet"
-  }
-}
-
-export function controlProgramAction(amount, asset, program){
-  return {
-    "amount": amount,
-    "asset": asset,
-    "control_program": program,
-    "type": "control_program"
-  }
-}
-
-export function controlAddressAction(amount, asset, address){
-  return {
-    "amount": amount,
-    "asset": asset,
-    "address": address,
-    "type": "control_address"
-  }
-}
+import axios from "axios/index";
 
+//Api call using bytomAPI sdk
 export function listAddress(guid)
 {
   return bytomAPI.sdk.accounts.listAddressUseServer(guid)
     .then(resp => resp[0])
 }
 
+//Api call from Buffer server
 export function listDappUTXO(params)
 {
   let url
@@ -120,4 +72,4 @@ function post(url, params){
       throw response.data.msg
     }
   })
-}
+}
\ No newline at end of file
diff --git a/src/components/util/bytomAction.js b/src/components/util/bytomAction.js
new file mode 100644 (file)
index 0000000..50e96e9
--- /dev/null
@@ -0,0 +1,51 @@
+export function spendUTXOAction(utxo){
+  return {
+    "type": "spend_utxo",
+    "output_id": utxo
+  }
+}
+
+export function contractArguments(amount, address){
+  return [
+    {
+      "type": "integer",
+      "value": amount
+    },
+    {
+      "type": "address",
+      "value": address
+    },
+    {
+      "type": "data",
+      "value": ""
+    }
+  ]
+}
+
+export function spendWalletAction(amount, asset){
+  return {
+    "amount": amount,
+    "asset": asset,
+    "type": "spend_wallet"
+  }
+}
+
+export function controlProgramAction(amount, asset, program){
+  return {
+    "amount": amount,
+    "asset": asset,
+    "control_program": program,
+    "type": "control_program"
+  }
+}
+
+export function controlAddressAction(amount, asset, address){
+  return {
+    "amount": amount,
+    "asset": asset,
+    "address": address,
+    "type": "control_address"
+  }
+}
+
+
diff --git a/src/components/util/submitContract.js b/src/components/util/submitContract.js
new file mode 100644 (file)
index 0000000..b2c9f6e
--- /dev/null
@@ -0,0 +1,54 @@
+import GetContractArgs from "../constants";
+import { updateUtxo } from './api'
+
+export function submitContract(listDepositUTXO, createContractTransaction, updateDatatbaseBalance, object) {
+  const address = object.address
+  const amount = object.amount
+  const updateParameters = object.parameter
+
+  return new Promise((resolve, reject) => {
+    //list available utxo
+    return listDepositUTXO().then(resp => {
+
+      //create the Contract Transaction
+      return createContractTransaction(resp, amount, address).then(object =>{
+        const input = object.input
+        const output = object.output
+        const args = object.args
+
+        const utxo = object.utxo
+
+        //Lock UTXO
+        return updateUtxo({"hash": utxo})
+          .then(()=>{
+
+            //Transactions
+            window.bytom.advancedTransfer(input, output, GetContractArgs().gas*100000000, args, 1)
+              .then((resp) => {
+                if(resp.action === 'reject'){
+                  reject('user reject the request')
+                }else if(resp.action === 'success'){
+
+                  //Update Balance
+                  return updateDatatbaseBalance(resp, ...updateParameters).then(()=>{
+                    resolve()
+                  }).catch(err => {
+                    throw err
+                  })
+                }
+              })
+              .catch(err => {
+                throw err
+              })
+          })
+          .catch(err => {
+            throw err
+          })
+      }).catch(err => {
+        throw err
+      })
+    }).catch(err => {
+      reject(err)
+    })
+  })
+}