Skip to content

Commit 114220a

Browse files
Increase the new block check interval to 1 minute but also keep an async checker if there are pending transactions still. (#90)
1 parent 9cebdbd commit 114220a

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

src/elm/DappInterface/MainModel.elm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ type alias Model =
155155
, governanceState : Eth.Governance.GovernanceState
156156
, errors : List String
157157
, currentTime : Maybe Time.Posix
158+
, lastNewBlockTime : Maybe Time.Posix
158159
, currentTimeZone : Time.Zone
159160
, browserType : Utils.BrowserInfo.BrowserType
160161
, proposeModel : Propose.Model

src/elm/Eth/Transaction.elm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ port module Eth.Transaction exposing
1010
, containsTransactionType
1111
, filteredTransactionsByCToken
1212
, filteredTransactionsByComptroller
13+
, getAllPendingTransactions
1314
, getDefaultOldestPendingTrxTime
1415
, getPendingTransactionsForAccount
1516
, giveNewTrx

src/elm/Main.elm

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import CompoundComponents.Eth.Ethereum as Ethereum exposing (Account(..), AssetA
1414
import CompoundComponents.Eth.Ledger exposing (LedgerAccount(..))
1515
import CompoundComponents.Eth.Network as Network exposing (Network(..), networkId)
1616
import CompoundComponents.Ether.BNTransaction as BNTransaction exposing (BNTransactionMsg)
17-
import CompoundComponents.Functions exposing (handleError)
17+
import CompoundComponents.Functions as Functions exposing (handleError)
1818
import CompoundComponents.Utils.CompoundHtmlAttributes exposing (HrefLinkType(..), class, href, id, target)
1919
import CompoundComponents.Utils.Time
2020
import DappInterface.ClaimCompModal as ClaimCompModal
@@ -41,7 +41,7 @@ import Http
4141
import Json.Decode
4242
import Json.Encode
4343
import Liquidate
44-
import Port exposing (askNetwork, askNewBlock, giveAccountBalance, giveError, giveNewBlock, setGasPrice, setTitle)
44+
import Port exposing (askNetwork, askNewBlock, askNewBlockAsync, giveAccountBalance, giveError, giveNewBlock, setGasPrice, setTitle)
4545
import Preferences exposing (PreferencesMsg(..), preferencesInit, preferencesSubscriptions, preferencesUpdate)
4646
import Repl
4747
import Strings.Translations as Translations
@@ -274,6 +274,7 @@ init { path, configurations, configAbiFiles, dataProviders, apiBaseUrlMap, userA
274274
, governanceState = Eth.Governance.init
275275
, errors = []
276276
, currentTime = Nothing
277+
, lastNewBlockTime = Nothing
277278
, currentTimeZone = Time.utc
278279
, browserType = browserType
279280
, proposeModel = Propose.emptyState
@@ -747,14 +748,48 @@ update msg ({ page, configs, apiBaseUrlMap, account, transactionState, bnTransac
747748

748749
updateCompoundState =
749750
Eth.Compound.handleAccountLiquidityCalculation oracleState compoundState
751+
752+
canCheckForPendingTrxs =
753+
let
754+
pendingTransactions =
755+
Transaction.getAllPendingTransactions model.network model.transactionState.transactions
756+
in
757+
if List.isEmpty pendingTransactions then
758+
Nothing
759+
760+
else
761+
Just True
762+
763+
-- The new block checker in ports now checks every 1 minute
764+
-- but if we have pending transactions, then lets continue
765+
-- to trigger an async block check every 20 seconds.
766+
( lastNewBlockTimestamp, askNewBlockAsyncCmd ) =
767+
Functions.map3
768+
canCheckForPendingTrxs
769+
model.blockNumber
770+
model.lastNewBlockTime
771+
(\_ currentBlockNumber actualLastNewBlockTime ->
772+
if Time.posixToMillis actualLastNewBlockTime + 20000 > Time.posixToMillis time then
773+
( Just time
774+
, askNewBlockAsync currentBlockNumber
775+
)
776+
777+
else
778+
( model.lastNewBlockTime, Cmd.none )
779+
)
780+
|> Maybe.withDefault ( model.lastNewBlockTime, Cmd.none )
750781
in
751782
( { model
752783
| currentTime = Just time
784+
, lastNewBlockTime = lastNewBlockTimestamp
753785
, borrowingContainerState = updatedBorrowingContainerState
754786
, voteModel = updatedVoteModel
755787
, compoundState = updateCompoundState
756788
}
757-
, Cmd.map voteTranslator loadDelegateeCmd
789+
, Cmd.batch
790+
[ Cmd.map voteTranslator loadDelegateeCmd
791+
, askNewBlockAsyncCmd
792+
]
758793
)
759794

760795
CheckVersion time ->
@@ -786,7 +821,12 @@ update msg ({ page, configs, apiBaseUrlMap, account, transactionState, bnTransac
786821
( model, Cmd.none )
787822

788823
SetBlockNumber blockNumber ->
789-
( { model | blockNumber = Just blockNumber }, newBlockCmd apiBaseUrlMap model.network blockNumber model.blockNumber model )
824+
( { model
825+
| blockNumber = Just blockNumber
826+
, lastNewBlockTime = model.currentTime
827+
}
828+
, newBlockCmd apiBaseUrlMap model.network blockNumber model.blockNumber model
829+
)
790830

791831
WrappedTransactionMsg transactionMsg ->
792832
let

src/elm/Port.elm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
port module Port exposing
22
( askNetwork
33
, askNewBlock
4+
, askNewBlockAsync
45
, encodeParameters
56
, giveAccountBalance
67
, giveEncodedExtrinsic
@@ -91,6 +92,14 @@ askNewBlock =
9192
askNewBlockPort {}
9293

9394

95+
port askNewBlockAsyncPort : { blockNumber : Int } -> Cmd msg
96+
97+
98+
askNewBlockAsync : Int -> Cmd msg
99+
askNewBlockAsync currentBlock =
100+
askNewBlockAsyncPort { blockNumber = currentBlock }
101+
102+
94103
port giveNewBlockPort : (Value -> msg) -> Sub msg
95104

96105

src/js/ports.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const PROVIDER_TYPE_SHOW_ACCOUNT = 3;
4747

4848
const ACCOUNT_CHECK_INTERVAL_MS = 2000;
4949
const NETWORK_CHECK_INTERVAL_MS = 4000;
50-
const NEW_BLOCK_CHECK_INTERVAL_MS = 20_000;
50+
const NEW_BLOCK_CHECK_INTERVAL_MS = 60_000;
5151
const SECONDS_PER_BLOCK = 12;
5252
const BLOCKS_PER_DAY = new BN(7200); // 12 seconds per block
5353
const EXP_DECIMALS = 18;
@@ -420,6 +420,21 @@ function subscribeToNewBlocks(app, eth) {
420420

421421
// port askNewBlockPort : {} -> Cmd msg
422422
app.ports.askNewBlockPort.subscribe(newBlockCheckFunction);
423+
424+
// port askNewBlockAsyncPort : { blockNumber : Int } -> Cmd msg
425+
app.ports.askNewBlockAsyncPort.subscribe(({blockNumber}) => {
426+
requestForeground(() => {
427+
getBlockNumber(eth)
428+
.then((blockNumber) => {
429+
if (blockNumber && blockNumber !== previousBlock) {
430+
debug(`New Block: ${blockNumber}`);
431+
app.ports.giveNewBlockPort.send({ block: blockNumber });
432+
previousBlock = blockNumber;
433+
}
434+
})
435+
.catch(reportError(app))
436+
});
437+
});
423438
}
424439

425440
function subscribeToCheckTrxStatus(app, eth) {

0 commit comments

Comments
 (0)