From a5ca46b547964a8a0c4cd6aa3cf2c9bb081442e7 Mon Sep 17 00:00:00 2001 From: Manuel Calavera Date: Mon, 16 Sep 2019 21:43:27 -0700 Subject: [PATCH] feat: app and stats store --- package.json | 2 ++ src/store/app.js | 59 ++++++++++++++++++++++++++++++++++++++ src/store/index.js | 8 ++++++ src/store/stats.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 10 +++++++ 5 files changed, 149 insertions(+) create mode 100644 src/store/app.js create mode 100644 src/store/index.js create mode 100644 src/store/stats.js diff --git a/package.json b/package.json index 76b78b5..718631e 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,8 @@ "i18next": "^17.0.13", "jsonfile": "^5.0.0", "mz": "^2.7.0", + "mobx": "^5.13.0", + "mobx-react-lite": "^1.4.1", "node-fetch": "^2.6.0", "promisify-child-process": "^3.1.1", "prop-types": "^15.7.2", diff --git a/src/store/app.js b/src/store/app.js new file mode 100644 index 0000000..a8522e8 --- /dev/null +++ b/src/store/app.js @@ -0,0 +1,59 @@ +import LocalStore from 'electron-store'; +import { action, observable } from 'mobx'; + +const localStore = new LocalStore(); + +class AppStore { + @observable depositPublicKey = localStore.get('depositPublicKey', ''); + + @observable screen = 'whatYouNeed'; + + @observable alertType = ''; + + @observable locale = 'en'; + + @observable secretKey = ''; + + @observable gb = [20]; + + @observable state = 'disabled'; + + @action.bound + setScreen(screen) { + this.screen = screen; + } + + @action.bound + showAlert(alertType) { + this.alertType = alertType; + } + + @action.bound + hideAlert() { + this.alertType = null; + } + + @action.bound + setSecretKey(secretKey) { + this.secretKey = secretKey; + } + + @action.bound + setGB(gb) { + this.gb = gb; + } + + @action.bound + setDepositPublicKey(key) { + this.depositPublicKey = key; + } + + @action.bound + setState(state) { + this.state = state; + } +} + +const store = new AppStore(); + +export default store; diff --git a/src/store/index.js b/src/store/index.js new file mode 100644 index 0000000..fc6ea89 --- /dev/null +++ b/src/store/index.js @@ -0,0 +1,8 @@ +import { configure } from 'mobx'; + +configure({ + enforceActions: 'observed', +}); + +export { default as AppStore } from './app'; +export { default as StatsStore } from './stats'; diff --git a/src/store/stats.js b/src/store/stats.js new file mode 100644 index 0000000..5bc1590 --- /dev/null +++ b/src/store/stats.js @@ -0,0 +1,70 @@ +import LocalStore from 'electron-store'; +import { Connection, PublicKey } from '@solana/web3.js'; +import { action, observable, flow } from 'mobx'; +import url from '../url'; +import Replicator from '../replicator'; +import AppStore from './app'; + +const DEFAULT_LAMPORTS = 10000; +const localStore = new LocalStore(); + +class StatsStore { + constructor() { + this.connection = new Connection(url); + this.replicator = new Replicator(this.connection); + } + + @observable stats = { + transactionCount: 0, + totalMined: localStore.get('totalMined', 0), + newMined: 0, + totalSupply: 0, + depositMinimumLamports: localStore.get( + 'depositMinimumLamports', + DEFAULT_LAMPORTS + ), + depositPublicKeyBalance: '', + }; + + clusterRestart() { + this.replicator.clusterRestart(); + this.stats.transactionCount = 0; + setTimeout(() => this.updateStats()); + } + + @action.bound + updateStats = flow(function* updateStats() { + const { transactionCount, depositMinimumLamports } = this.stats; + const newTransactionCount = yield this.connection.getTransactionCount(); + + if (newTransactionCount < transactionCount / 2) { + this.clusterRestart(); + return; + } + + this.stats.totalSupply = yield this.connection.getTotalSupply(); + const newMined = yield this.replicator.adjustedReplicatorBalance(); + + if (newMined > depositMinimumLamports) { + const success = yield this.replicator.depositMiningRewards( + new PublicKey(AppStore.depositPublicKey), + newMined + ); + if (success) { + this.stats.totalMined += newMined; + localStore.set('totalMined', this.stats.totalMined); + } + } + + const balance = yield this.connection.getBalance( + new PublicKey(AppStore.depositPublicKey) + ); + this.stats.depositPublicKeyBalance = `Account Balance: ${balance} lamports`; + this.stats.newMined = newMined; + this.stats.transactionCount = newTransactionCount; + }); +} + +const store = new StatsStore(); + +export default store; diff --git a/yarn.lock b/yarn.lock index 703d45c..1dfc9aa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7808,6 +7808,16 @@ mkdirp@0.5.1, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1: dependencies: minimist "0.0.8" +mobx-react-lite@^1.4.1: + version "1.4.1" + resolved "https://registry.yarnpkg.com/mobx-react-lite/-/mobx-react-lite-1.4.1.tgz#7307a45901f97f9a07ebed32b654235507644e1a" + integrity sha512-XmM+gzNv+GyXZYDLZMIGox3DufIiKULYgJsLhQj0U6Wlf+J5jv/h7limrL0aS1bEUtNilG62g9nwTeQ0KHzLFg== + +mobx@^5.13.0: + version "5.13.0" + resolved "https://registry.yarnpkg.com/mobx/-/mobx-5.13.0.tgz#0fd68f10aa5ff2d146a4ed9e145b53337cfbca59" + integrity sha512-eSAntMSMNj0PFL705rgv+aB/z1RjNqDnFEpBe18yQVreXTWiVgIrmBUXzjnJfuba+eo4eAk6zi+/gXQkSUea8A== + modify-values@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"