Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
feat: app and stats store
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-calavera committed Sep 17, 2019
1 parent 88ff168 commit a5ca46b
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
59 changes: 59 additions & 0 deletions src/store/app.js
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { configure } from 'mobx';

configure({
enforceActions: 'observed',
});

export { default as AppStore } from './app';
export { default as StatsStore } from './stats';
70 changes: 70 additions & 0 deletions src/store/stats.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7808,6 +7808,16 @@ [email protected], 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"
Expand Down

0 comments on commit a5ca46b

Please sign in to comment.