OSDN Git Service

seperate the action into normalTx and advancedTX
authorZhiting Lin <zlin035@uottawa.ca>
Wed, 16 May 2018 03:33:22 +0000 (11:33 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Wed, 16 May 2018 03:33:22 +0000 (11:33 +0800)
src/features/transactions/actions.js
src/features/transactions/components/New/AdvancedTransactionForm.jsx
src/features/transactions/components/New/NormalTransactionForm.jsx

index 4bbd3b6..9347954 100644 (file)
@@ -18,15 +18,14 @@ function preprocessTransaction(formParams) {
     actions: copy.actions || [],
   }
 
-  const normalT = formParams
-  if (builder.actions.length == 0) {
+  if (formParams.form === 'normalTx') {
     let gasPrice = 0
-    switch (normalT.gas.type) {
+    switch (formParams.gas.type) {
       case 'Fast':
         gasPrice = formParams.state.estimateGas * 2
         break
       case 'Customize':
-        gasPrice = normalT.gas.price
+        gasPrice = formParams.gas.price
         break
       case 'Standard':
       default:
@@ -35,25 +34,25 @@ function preprocessTransaction(formParams) {
     }
 
     builder.actions.push({
-      accountAlias: normalT.accountAlias,
-      accountId: normalT.accountId,
+      accountAlias: formParams.accountAlias,
+      accountId: formParams.accountId,
       assetAlias: 'BTM',
       amount: Number(gasPrice),
       type: 'spend_account'
     })
     builder.actions.push({
-      accountAlias: normalT.accountAlias,
-      accountId: normalT.accountId,
-      assetAlias: normalT.assetAlias,
-      assetId: normalT.assetId,
-      amount: normalT.amount,
+      accountAlias: formParams.accountAlias,
+      accountId: formParams.accountId,
+      assetAlias: formParams.assetAlias,
+      assetId: formParams.assetId,
+      amount: formParams.amount,
       type: 'spend_account'
     })
     builder.actions.push({
-      address: normalT.address,
-      assetAlias: normalT.assetAlias,
-      assetId: normalT.assetId,
-      amount: normalT.amount,
+      address: formParams.address,
+      assetAlias: formParams.assetAlias,
+      assetId: formParams.assetId,
+      amount: formParams.amount,
       type: 'control_address'
     })
   }
@@ -131,27 +130,85 @@ form.submitForm = (formParams) => function (dispatch) {
     }))
   }
 
-  if (formParams.actions.length !== 0
-    && formParams.state.showAdvanced
-    && formParams.baseTransaction
-    && formParams.submitAction == 'submit') {
-    const transaction = JSON.parse(formParams.baseTransaction)
-    return signAndSubmitTransaction(transaction, formParams.password)
+  // 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(signResp => {
+        if (signResp.status === 'fail') {
+          throw new Error(signResp.msg)
+        }
+        else if(!signResp.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)
   }
+  //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.actions.length !== 0
-    && 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 (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)
+        }
+
+        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.submitAction == 'submit') {
+      return buildPromise
+        .then((resp) => {
+          if (resp.status === 'fail') {
+            throw new Error(resp.msg)
+          }
+
+          return signAndSubmitTransaction(resp.data, formParams.password)
+        })
+    }
+
+    // submitAction == 'generate'
+    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(resp => {
+      if (resp.status === 'fail') {
+        throw new Error(resp.msg)
+      }
       const id = uuid.v4()
       dispatch({
         type: 'GENERATED_TX_HEX',
@@ -163,40 +220,6 @@ form.submitForm = (formParams) => function (dispatch) {
       dispatch(push(`/transactions/generated/${id}`))
     })
   }
-
-  if (formParams.submitAction == 'submit') {
-    return buildPromise
-      .then((resp) => {
-        if (resp.status === 'fail') {
-          throw new Error(resp.msg)
-        }
-
-        return signAndSubmitTransaction(resp.data, formParams.password)
-      })
-  }
-
-  // submitAction == 'generate'
-  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(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}`))
-  })
 }
 
 export default {
index 4b2c1fd..9a6926f 100644 (file)
@@ -54,7 +54,7 @@ class AdvancedTxForm extends React.Component {
 
   submitWithValidation(data) {
     return new Promise((resolve, reject) => {
-      this.props.submitForm(Object.assign({}, data, {state: this.state}))
+      this.props.submitForm(Object.assign({}, data, {state: this.state, form: 'advancedTx'}))
         .catch((err) => {
           const response = {}
 
index aa103b8..dc94b54 100644 (file)
@@ -68,7 +68,7 @@ class NormalTxForm extends React.Component {
 
   submitWithValidation(data) {
     return new Promise((resolve, reject) => {
-      this.props.submitForm(Object.assign({}, data, {state: this.state}))
+      this.props.submitForm(Object.assign({}, data, {state: this.state, form: 'normalTx'}))
         .catch((err) => {
           const response = {}