OSDN Git Service

rework on the transactions actions
authorZhiting Lin <zlin035@uottawa.ca>
Thu, 17 May 2018 07:01:32 +0000 (15:01 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Thu, 17 May 2018 07:01:32 +0000 (15:01 +0800)
src/features/transactions/actions.js
src/features/transactions/components/New/AdvancedTransactionForm.jsx
src/sdk/api/transactions.js

index 9347954..89d6745 100644 (file)
@@ -97,30 +97,19 @@ function preprocessTransaction(formParams) {
 }
 
 form.submitForm = (formParams) => function (dispatch) {
-  const connection = chainClient().connection
-
-  const processed = preprocessTransaction(formParams)
-  const buildPromise = connection.request('/build-transaction', {actions: processed.actions})
-
-  const signAndSubmitTransaction = (transaction, password) => {
-    return connection.request('/sign-transaction', {
-      password,
-      transaction
-    }).then(resp => {
-      if (resp.status === 'fail') {
-        throw new Error(resp.msg)
-      }
+  const client = chainClient()
 
-      const rawTransaction = resp.data.transaction.rawTransaction
-      return connection.request('/submit-transaction', {rawTransaction})
-    }).then(dealSignSubmitResp)
-  }
+  const buildPromise = (formParams.state.showAdvanced && formParams.signTransaction) ? null :
+    client.transactions.build(builder => {
+      const processed = preprocessTransaction(formParams)
 
-  const dealSignSubmitResp = resp => {
-    if (resp.status === 'fail') {
-      throw new Error(resp.msg)
-    }
+      builder.actions = processed.actions
+      if (processed.baseTransaction) {
+        builder.baseTransaction = processed.baseTransaction
+      }
+    })
 
+  const submitSucceeded = () => {
     dispatch(form.created())
     dispatch(push({
       pathname: '/transactions',
@@ -133,92 +122,63 @@ form.submitForm = (formParams) => function (dispatch) {
   // normal transactions
   if(formParams.form === 'normalTx'){
     return buildPromise
-      .then((resp) => {
-        if (resp.status === 'fail') {
-          throw new Error(resp.msg)
-        }
-
-        const body = Object.assign({}, {password: formParams.password, 'transaction': resp.data})
-        return connection.request('/sign-transaction', body)
+      .then(tpl => {
+        const body = Object.assign({}, {password: formParams.password, transaction: tpl.data})
+        return client.transactions.sign(body)
       })
-      .then(signResp => {
-        if (signResp.status === 'fail') {
-          throw new Error(signResp.msg)
-        }
-        else if(!signResp.data.signComplete){
+      .then(signed => {
+        if(!signed.data.signComplete){
           throw new Error('Signature failed, it might be your password is wrong.')
         }
-
-        const rawTransaction = signResp.data.transaction.rawTransaction
-        return connection.request('/submit-transaction', {rawTransaction})
-      }).then(dealSignSubmitResp)
+        return client.transactions.submit(signed.data.transaction.rawTransaction)
+      })
+      .then(submitSucceeded)
   }
   //advanced transactions
   else{
-    if ( formParams.state.showAdvanced
-      && formParams.baseTransaction
-      && formParams.submitAction == 'submit') {
-      const transaction = JSON.parse(formParams.baseTransaction)
-      return signAndSubmitTransaction(transaction, formParams.password)
-    }
-
-    if (formParams.state.showAdvanced
-      && formParams.baseTransaction
-      && formParams.submitAction !== 'submit') {
-      const transaction = JSON.parse(formParams.baseTransaction)
-      return connection.request('/sign-transaction', {
-        password: formParams.password,
-        transaction
-      }).then(resp => {
-        if (resp.status === 'fail') {
-          throw new Error(resp.msg)
-        }
+    if (formParams.submitAction == 'submit') {
+      const signAndSubmitTransaction = (transaction) => {
+        const body = Object.assign({}, {password: formParams.password, transaction: transaction})
+        return client.transactions.sign(body)
+          .then( signed => client.transactions.submit(signed.data.transaction.rawTransaction) )
+          .then(submitSucceeded)
+      }
 
-        const id = uuid.v4()
-        dispatch({
-          type: 'GENERATED_TX_HEX',
-          generated: {
-            id: id,
-            hex: JSON.stringify(resp.data.transaction),
-          },
-        })
-        dispatch(push(`/transactions/generated/${id}`))
-      })
-    }
+      if( formParams.state.showAdvanced
+        && formParams.signTransaction ){
+        const transaction = JSON.parse(formParams.signTransaction)
+        return signAndSubmitTransaction(transaction)
+      }
 
-    if (formParams.submitAction == 'submit') {
       return buildPromise
-        .then((resp) => {
-          if (resp.status === 'fail') {
-            throw new Error(resp.msg)
-          }
+        .then(tpl => signAndSubmitTransaction(tpl.data))
+    }
 
-          return signAndSubmitTransaction(resp.data, formParams.password)
+    // submitAction == 'generate'
+    const signAndSubmitGeneratedTransaction = (transaction) => {
+      const body = Object.assign({}, {password: formParams.password, transaction: transaction})
+      return client.transactions.sign(body)
+        .then(resp => {
+          const id = uuid.v4()
+          dispatch({
+            type: 'GENERATED_TX_HEX',
+            generated: {
+              id: id,
+              hex: JSON.stringify(resp.data.transaction),
+            },
+          })
+          dispatch(push(`/transactions/generated/${id}`))
         })
     }
 
-    // submitAction == 'generate'
-    return buildPromise.then(resp => {
-      if (resp.status === 'fail') {
-        throw new Error(resp.msg)
-      }
+    if (formParams.state.showAdvanced
+      && formParams.signTransaction) {
+      const transaction = JSON.parse(formParams.signTransaction)
+      return signAndSubmitGeneratedTransaction(transaction)
+    }
 
-      const body = Object.assign({}, {password: formParams.password, 'transaction': resp.data})
-      return connection.request('/sign-transaction', body)
-    }).then(resp => {
-      if (resp.status === 'fail') {
-        throw new Error(resp.msg)
-      }
-      const id = uuid.v4()
-      dispatch({
-        type: 'GENERATED_TX_HEX',
-        generated: {
-          id: id,
-          hex: JSON.stringify(resp.data.transaction),
-        },
-      })
-      dispatch(push(`/transactions/generated/${id}`))
-    })
+    return buildPromise
+      .then(resp => signAndSubmitGeneratedTransaction(resp.data))
   }
 }
 
index 9a6926f..523010b 100644 (file)
@@ -74,7 +74,7 @@ class AdvancedTxForm extends React.Component {
 
   render() {
     const {
-      fields: {baseTransaction, actions, submitAction, password},
+      fields: {signTransaction, actions, submitAction, password},
       error,
       handleSubmit,
       submitting
@@ -82,7 +82,7 @@ class AdvancedTxForm extends React.Component {
     const lang = this.props.lang
 
     let submitLabel = lang === 'zh' ? '提交交易' : 'Submit transaction'
-    const hasBaseTransaction = ((baseTransaction.value || '').trim()).length > 0
+    const hasBaseTransaction = ((signTransaction.value || '').trim()).length > 0
     if (submitAction.value == 'generate' && !hasBaseTransaction) {
       submitLabel = lang === 'zh' ? '生成交易JSON' : 'Generate transaction JSON'
     }
@@ -138,7 +138,7 @@ class AdvancedTxForm extends React.Component {
             <TextField
               title={lang === 'zh' ? '带签名交易' : 'To sign transaction'}
               placeholder={lang === 'zh' ? '在这里复制交易 HEX ...' : 'Paste transaction hex here...'}
-              fieldProps={baseTransaction}
+              fieldProps={signTransaction}
               autoFocus={true}/>
 
             <FieldLabel>{lang === 'zh' ? '交易构建类型' : 'Transaction Build Type'}</FieldLabel>
@@ -207,12 +207,12 @@ const validate = (values, props) => {
   const lang = props.lang
 
   // Base transaction
-  let baseTx = (values.baseTransaction || '').trim()
+  let baseTx = (values.signTransaction || '').trim()
   try {
     JSON.parse(baseTx)
   } catch (e) {
     if (baseTx && e) {
-      errors.baseTransaction = ( lang === 'zh' ? '请使用JSON字符来签名交易' : 'To sign transaction must be a JSON string.' )
+      errors.signTransaction = ( lang === 'zh' ? '请使用JSON字符来签名交易' : 'To sign transaction must be a JSON string.' )
     }
   }
 
@@ -233,7 +233,7 @@ export default BaseNew.connect(
   reduxForm({
     form: 'AdvancedTransactionForm',
     fields: [
-      'baseTransaction',
+      'signTransaction',
       'actions[].accountId',
       'actions[].accountAlias',
       'actions[].assetId',
index 05cd0a4..a651cbf 100644 (file)
@@ -9,6 +9,8 @@ function checkForError(resp) {
       errors.formatErrMsg(resp, ''),
       {body: resp}
     )
+  }else if (resp.status === 'fail') {
+    throw new Error(resp.msg)
   }
   return resp
 }
@@ -81,9 +83,9 @@ const transactionsAPI = (client) => {
       }
 
       return shared.tryCallback(
-        client.request('/build-transaction', builder, true),
+        client.request('/build-transaction', builder),
         cb
-      )
+      ).then(resp => checkForError(resp))
     },
 
     buildBatch: (builderBlocks, cb) => {
@@ -102,14 +104,16 @@ const transactionsAPI = (client) => {
     },
 
     sign: (template, cb) => finalize(template)
-      .then(finalized => client.signer.sign(finalized, cb)),
+      .then(finalized => client.request('/sign-transaction', finalized ).then(resp => checkForError(resp)),
+        cb
+      ),
 
     signBatch: (templates, cb) => finalizeBatch(templates)
       // TODO: merge batch errors from finalizeBatch
       .then(finalized => client.signer.signBatch(finalized.successes, cb)),
 
     submit: (signed, cb) => shared.tryCallback(
-      client.request('/submit-transaction', {transactions: [signed]}).then(resp => checkForError(resp[0])),
+      client.request('/submit-transaction', {'raw_transaction': signed}).then(resp => checkForError(resp)),
       cb
     ),