OSDN Git Service

add estimated gas
authorYongfeng LI <wliyongfeng@gmail.com>
Fri, 20 Apr 2018 06:36:20 +0000 (14:36 +0800)
committerYongfeng LI <wliyongfeng@gmail.com>
Fri, 20 Apr 2018 06:36:20 +0000 (14:36 +0800)
src/features/transactions/components/New/New.jsx
src/features/transactions/components/New/New.scss
src/utility/buildInOutDisplay.js

index 7bf1252..3c4949a 100644 (file)
@@ -15,18 +15,18 @@ import ActionItem from './FormActionItem'
 import React from 'react'
 import styles from './New.scss'
 import balanceActions from 'features/balances/actions'
-import {normalizeBTMAmountUnit} from 'utility/buildInOutDisplay'
+import {normalizeBTMAmountUnit, normalizeBTM} from 'utility/buildInOutDisplay'
 
 const rangeOptions = [
   {
     label: 'Standard',
     label_zh: '标准',
-    value: '20000000'
+    ratio: 1
   },
   {
     label: 'Fast',
     label_zh: '快速',
-    value: '25000000'
+    ratio: 2
   },
   {
     label: 'Customize',
@@ -157,6 +157,7 @@ class Form extends React.Component {
     const noAsset = !assetAlias && !assetId
 
     if (!address || !amount || noAccount || noAsset) {
+      this.setState({estimateGas: null})
       return
     }
 
@@ -191,9 +192,12 @@ class Form extends React.Component {
       }
 
       return this.connection.request('/estimate-transaction-gas', {
-        rawTransaction: resp.data.rawTransaction
+        transactionTemplate: resp.data
       }).then(resp => {
-        window.console.log(resp)
+        if (resp.status === 'fail') {
+          return
+        }
+        this.setState({estimateGas: resp.data.totalNeu})
       })
     })
   }
@@ -312,8 +316,12 @@ class Form extends React.Component {
                   </label>
                 </td>
                 <td>
-                  {option.label == normalTransaction.gas.type.value && option.label !== 'Customize'
-                  && normalizeBTMAmountUnit(btmID, option.value, this.props.btmAmountUnit)}
+                  {
+                    option.label == normalTransaction.gas.type.value && option.label !== 'Customize'
+                    && ((lang === 'zh' ? '估算' : 'estimated') + '   ' + normalizeBTMAmountUnit(btmID,
+                      option.ratio * (this.state.estimateGas || Math.pow(10, 5)),
+                      this.props.btmAmountUnit))
+                  }
                   {
                     option.label === 'Customize' && normalTransaction.gas.type.value === 'Customize' &&
                     <div>
@@ -477,41 +485,42 @@ export default BaseNew.connect(
   (dispatch) => ({
     didLoadAutocomplete: () => dispatch(balanceActions.didLoadAutocomplete),
     fetchAll: (cb) => dispatch(balanceActions.fetchAll(cb)),
+    showError: err => dispatch({type: 'ERROR', payload: err}),
     ...BaseNew.mapDispatchToProps('transaction')(dispatch)
   }),
   reduxForm({
-    form: 'NewTransactionForm',
-    fields: [
-      'baseTransaction',
-      'actions[].accountId',
-      'actions[].accountAlias',
-      'actions[].assetId',
-      'actions[].assetAlias',
-      'actions[].amount',
-      'actions[].receiver',
-      'actions[].outputId',
-      'actions[].referenceData',
-      'actions[].type',
-      'actions[].address',
-      'actions[].password',
-      'normalTransaction.accountAlias',
-      'normalTransaction.accountId',
-      'normalTransaction.amount',
-      'normalTransaction.assetAlias',
-      'normalTransaction.assetId',
-      'normalTransaction.gas',
-      'normalTransaction.gas.type',
-      'normalTransaction.gas.price',
-      'normalTransaction.address',
-      'submitAction',
-      'password'
-    ],
-    validate,
-    touchOnChange: true,
-    initialValues: {
-      submitAction: 'submit',
-    },
-  }
+      form: 'NewTransactionForm',
+      fields: [
+        'baseTransaction',
+        'actions[].accountId',
+        'actions[].accountAlias',
+        'actions[].assetId',
+        'actions[].assetAlias',
+        'actions[].amount',
+        'actions[].receiver',
+        'actions[].outputId',
+        'actions[].referenceData',
+        'actions[].type',
+        'actions[].address',
+        'actions[].password',
+        'normalTransaction.accountAlias',
+        'normalTransaction.accountId',
+        'normalTransaction.amount',
+        'normalTransaction.assetAlias',
+        'normalTransaction.assetId',
+        'normalTransaction.gas',
+        'normalTransaction.gas.type',
+        'normalTransaction.gas.price',
+        'normalTransaction.address',
+        'submitAction',
+        'password'
+      ],
+      validate,
+      touchOnChange: true,
+      initialValues: {
+        submitAction: 'submit',
+      },
+    }
   )(Form)
 )
 
index 6770c3f..7f1eda2 100644 (file)
   margin-top: $gutter-size;
 }
 
-.title{
-  color:  $text-strong-color;
+.title {
+  color: $text-strong-color;
   font-size: $font-size-btn-lg;
 }
 
+.estimateGas {
+  margin-left: 10px;
+  font-size: 8px;
+  color: #747c89;
+}
+
 .main {
   border: 1px solid $border-color;
   padding: $gutter-size;
@@ -55,7 +61,7 @@
   flex-direction: column;
   border-radius: 3px;
   height: 150px;
-  input{
+  input {
     margin: 0 5px;
   }
   & > label {
index 726da18..74e55ef 100644 (file)
@@ -1,3 +1,5 @@
+import {btmAmountUnit} from '../features/core/reducers'
+
 const mappings = {
   id: 'ID',
   type: 'Type',
@@ -176,6 +178,17 @@ export const normalizeGlobalBTMAmount = (assetID, amount, btmAmountUnit) => {
   return amount
 }
 
+export const normalizeBTM = (amount, btmAmountUnit) => {
+  switch (btmAmountUnit){
+    case 'BTM':
+      return formatIntNumToPosDecimal(amount, 8)+' BTM'
+    case 'mBTM':
+      return formatIntNumToPosDecimal(amount, 5)+' mBTM'
+    case 'NEU':
+      return amount+' NEU'
+  }
+}
+
 export function formatBTMAmount(value, pos)  {
   if (!value) {
     return value