OSDN Git Service

update the build-chain-transactions function.
authorZhiting Lin <zlin035@uottawa.ca>
Mon, 14 Oct 2019 07:35:50 +0000 (15:35 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Mon, 14 Oct 2019 07:35:50 +0000 (15:35 +0800)
src/features/transactions/actions.js
src/features/transactions/components/New/NormalTransactionForm.jsx
src/sdk/api/transactions.js

index 4944543..d883334 100644 (file)
@@ -1,5 +1,5 @@
 import uuid from 'uuid'
-import {chainClient} from 'utility/environment'
+import {chainClient, btmID} from 'utility/environment'
 import {parseNonblankJSON} from 'utility/string'
 import {push} from 'react-router-redux'
 import {baseCreateActions, baseListActions} from 'features/shared/actions'
@@ -98,6 +98,8 @@ form.submitForm = (formParams) => function (dispatch) {
     const accountAlias = formParams.accountAlias
     const accountInfo = Object.assign({},  accountAlias!== ''? {alias: accountAlias}: {id: accountId})
 
+    const isBTM = (formParams.assetId === btmID) || (formParams.assetAlias === 'BTM')
+
     return client.accounts.query(accountInfo)
       .then( resp => {
         if(resp.data[0].xpubs.length > 1){
@@ -110,17 +112,32 @@ form.submitForm = (formParams) => function (dispatch) {
         if(!result.data.checkResult){
           throw new Error('PasswordWrong')
         }
-        return client.transactions.build(builderFunction)
+        if(isBTM)
+          return client.transactions.buildChain(builderFunction)
+        else
+          return client.transactions.build(builderFunction)
       })
       .then( tpl => {
-        const body = Object.assign({}, {password: formParams.password, transaction: tpl.data})
-        return client.transactions.sign(body)
+        if(isBTM){
+          const body = Object.assign({}, {password: formParams.password, transactions: tpl.data})
+          return client.transactions.signBatch(body)
+        }
+        else{
+          const body = Object.assign({}, {password: formParams.password, transaction: tpl.data})
+          return client.transactions.sign(body)
+        }
       })
       .then(signed => {
         if(!signed.data.signComplete){
           throw {code: 'F_BTM100'}
         }
-        return client.transactions.submit(signed.data.transaction.rawTransaction)
+        if(isBTM){
+          const rawTransactions = signed.data.transaction.map(tx => tx.rawTransaction)
+          return client.transactions.submitBatch(rawTransactions)
+        }
+        else{
+          return client.transactions.submit(signed.data.transaction.rawTransaction)
+        }
       })
       .then(submitSucceeded)
   }
index 7065c30..67a77c8 100644 (file)
@@ -114,24 +114,17 @@ class NormalTxForm extends React.Component {
 
     const body = {actions, ttl: 1}
     this.connection.request('/build-transaction', body).then(resp => {
-      if (resp.status === 'fail') {
-        this.setState({estimateGas: null})
-        const errorMsg =  resp.code && i18n.exists(`btmError.${resp.code}`) && t(`btmError.${resp.code}`) || resp.msg
-        this.props.showError(new Error(errorMsg))
-        return
-      }
-
       return this.connection.request('/estimate-transaction-gas', {
         transactionTemplate: resp.data
       }).then(resp => {
-        if (resp.status === 'fail') {
-          this.setState({estimateGas: null})
-          const errorMsg =  resp.code && i18n.exists(`btmError.${resp.code}`) && t(`btmError.${resp.code}`) || resp.msg
-          this.props.showError(new Error(errorMsg))
-          return
-        }
         this.setState({estimateGas: Math.ceil(resp.data.totalNeu/100000)*100000})
+      }).catch(err =>{
+        throw err
       })
+    }).catch(err=>{
+      this.setState({estimateGas: null, address: null})
+      const errorMsg =  err.code && i18n.exists(`btmError.${err.code}`) && t(`btmError.${err.code}`) || err.msg
+      this.props.showError(new Error(errorMsg))
     })
   }
 
index 3106e61..d4197c6 100644 (file)
@@ -88,6 +88,21 @@ const transactionsAPI = (client) => {
       ).then(resp => checkForError(resp))
     },
 
+    buildChain: (builderBlock, cb) => {
+      const builder = new TransactionBuilder()
+
+      try {
+        builderBlock(builder)
+      } catch (err) {
+        return Promise.reject(err)
+      }
+
+      return shared.tryCallback(
+        client.request('/build-chain-transactions', builder),
+        cb
+      ).then(resp => checkForError(resp)).catch(errors  => {throw checkForError(errors)})
+    },
+
     buildBatch: (builderBlocks, cb) => {
       const builders = []
       for (let i in builderBlocks) {
@@ -113,9 +128,10 @@ const transactionsAPI = (client) => {
         cb
       ),
 
-    signBatch: (templates, cb) => finalizeBatch(templates)
-      // TODO: merge batch errors from finalizeBatch
-      .then(finalized => client.signer.signBatch(finalized.successes, cb)),
+    signBatch: (template, cb) => finalize(template)
+      .then(finalized => client.request('/sign-transactions', finalized ).then(resp => checkForError(resp)).catch(errors  => {throw checkForError(errors)}),
+        cb
+      ),
 
     submit: (signed, cb) => shared.tryCallback(
       client.request('/submit-transaction', {'raw_transaction': signed}).then(resp => checkForError(resp)),
@@ -123,8 +139,8 @@ const transactionsAPI = (client) => {
     ),
 
     submitBatch: (signed, cb) => shared.tryCallback(
-      client.request('/submit-transaction', {transactions: signed})
-            .then(resp => new shared.BatchResponse(resp)),
+      client.request('/submit-transactions', {'raw_transactions': signed}).then(resp => checkForError(resp))
+        .catch(errors  => {throw checkForError(errors)}),
       cb
     ),