aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/local_storage
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-11-22 04:03:08 +0800
committerFabio Berger <me@fabioberger.com>2017-11-22 04:03:08 +0800
commit3660ba28d73d70d08bf14c33ef680e5ef3ec7f3b (patch)
treef101656799da807489253e17bea7abfaea90b62d /packages/website/ts/local_storage
parent037f466e1f80f635b48f3235258402e2ce75fb7b (diff)
downloaddexon-sol-tools-3660ba28d73d70d08bf14c33ef680e5ef3ec7f3b.tar.gz
dexon-sol-tools-3660ba28d73d70d08bf14c33ef680e5ef3ec7f3b.tar.zst
dexon-sol-tools-3660ba28d73d70d08bf14c33ef680e5ef3ec7f3b.zip
Add website to mono repo, update packages to align with existing sub-packages, use new subscribeAsync 0x.js method
Diffstat (limited to 'packages/website/ts/local_storage')
-rw-r--r--packages/website/ts/local_storage/local_storage.ts35
-rw-r--r--packages/website/ts/local_storage/tracked_token_storage.ts56
-rw-r--r--packages/website/ts/local_storage/trade_history_storage.tsx82
3 files changed, 173 insertions, 0 deletions
diff --git a/packages/website/ts/local_storage/local_storage.ts b/packages/website/ts/local_storage/local_storage.ts
new file mode 100644
index 000000000..d94e6877b
--- /dev/null
+++ b/packages/website/ts/local_storage/local_storage.ts
@@ -0,0 +1,35 @@
+import * as _ from 'lodash';
+
+export const localStorage = {
+ doesExist() {
+ return !!window.localStorage;
+ },
+ getItemIfExists(key: string): string {
+ if (!this.doesExist) {
+ return undefined;
+ }
+ const item = window.localStorage.getItem(key);
+ if (_.isNull(item) || item === 'undefined') {
+ return '';
+ }
+ return item;
+ },
+ setItem(key: string, value: string) {
+ if (!this.doesExist || _.isUndefined(value)) {
+ return;
+ }
+ window.localStorage.setItem(key, value);
+ },
+ removeItem(key: string) {
+ if (!this.doesExist) {
+ return;
+ }
+ window.localStorage.removeItem(key);
+ },
+ getAllKeys(): string[] {
+ if (!this.doesExist) {
+ return [];
+ }
+ return _.keys(window.localStorage);
+ },
+};
diff --git a/packages/website/ts/local_storage/tracked_token_storage.ts b/packages/website/ts/local_storage/tracked_token_storage.ts
new file mode 100644
index 000000000..0b54a66e0
--- /dev/null
+++ b/packages/website/ts/local_storage/tracked_token_storage.ts
@@ -0,0 +1,56 @@
+import * as _ from 'lodash';
+import {Token, TrackedTokensByNetworkId} from 'ts/types';
+import {localStorage} from 'ts/local_storage/local_storage';
+
+const TRACKED_TOKENS_KEY = 'trackedTokens';
+
+export const trackedTokenStorage = {
+ addTrackedTokenToUser(userAddress: string, networkId: number, token: Token) {
+ const trackedTokensByUserAddress = this.getTrackedTokensByUserAddress();
+ let trackedTokensByNetworkId = trackedTokensByUserAddress[userAddress];
+ if (_.isUndefined(trackedTokensByNetworkId)) {
+ trackedTokensByNetworkId = {};
+ }
+ const trackedTokens = !_.isUndefined(trackedTokensByNetworkId[networkId]) ?
+ trackedTokensByNetworkId[networkId] :
+ [];
+ trackedTokens.push(token);
+ trackedTokensByNetworkId[networkId] = trackedTokens;
+ trackedTokensByUserAddress[userAddress] = trackedTokensByNetworkId;
+ const trackedTokensByUserAddressJSONString = JSON.stringify(trackedTokensByUserAddress);
+ localStorage.setItem(TRACKED_TOKENS_KEY, trackedTokensByUserAddressJSONString);
+ },
+ getTrackedTokensByUserAddress(): TrackedTokensByNetworkId {
+ const trackedTokensJSONString = localStorage.getItemIfExists(TRACKED_TOKENS_KEY);
+ if (_.isEmpty(trackedTokensJSONString)) {
+ return {};
+ }
+ const trackedTokensByUserAddress = JSON.parse(trackedTokensJSONString);
+ return trackedTokensByUserAddress;
+ },
+ getTrackedTokensIfExists(userAddress: string, networkId: number): Token[] {
+ const trackedTokensJSONString = localStorage.getItemIfExists(TRACKED_TOKENS_KEY);
+ if (_.isEmpty(trackedTokensJSONString)) {
+ return undefined;
+ }
+ const trackedTokensByUserAddress = JSON.parse(trackedTokensJSONString);
+ const trackedTokensByNetworkId = trackedTokensByUserAddress[userAddress];
+ if (_.isUndefined(trackedTokensByNetworkId)) {
+ return undefined;
+ }
+ const trackedTokens = trackedTokensByNetworkId[networkId];
+ return trackedTokens;
+ },
+ removeTrackedToken(userAddress: string, networkId: number, tokenAddress: string) {
+ const trackedTokensByUserAddress = this.getTrackedTokensByUserAddress();
+ const trackedTokensByNetworkId = trackedTokensByUserAddress[userAddress];
+ const trackedTokens = trackedTokensByNetworkId[networkId];
+ const remainingTrackedTokens = _.filter(trackedTokens, (token: Token) => {
+ return token.address !== tokenAddress;
+ });
+ trackedTokensByNetworkId[networkId] = remainingTrackedTokens;
+ trackedTokensByUserAddress[userAddress] = trackedTokensByNetworkId;
+ const trackedTokensByUserAddressJSONString = JSON.stringify(trackedTokensByUserAddress);
+ localStorage.setItem(TRACKED_TOKENS_KEY, trackedTokensByUserAddressJSONString);
+ },
+};
diff --git a/packages/website/ts/local_storage/trade_history_storage.tsx b/packages/website/ts/local_storage/trade_history_storage.tsx
new file mode 100644
index 000000000..415b380b7
--- /dev/null
+++ b/packages/website/ts/local_storage/trade_history_storage.tsx
@@ -0,0 +1,82 @@
+import * as _ from 'lodash';
+import {Fill} from 'ts/types';
+import {configs} from 'ts/utils/configs';
+import {constants} from 'ts/utils/constants';
+import {localStorage} from 'ts/local_storage/local_storage';
+import ethUtil = require('ethereumjs-util');
+import BigNumber from 'bignumber.js';
+
+const FILLS_KEY = 'fills';
+const FILLS_LATEST_BLOCK = 'fillsLatestBlock';
+const FILL_CLEAR_KEY = 'lastClearFillDate';
+
+export const tradeHistoryStorage = {
+ // Clear all fill related localStorage if we've updated the config variable in an update
+ // that introduced a backward incompatible change requiring the user to re-fetch the fills from
+ // the blockchain
+ clearIfRequired() {
+ const lastClearFillDate = localStorage.getItemIfExists(FILL_CLEAR_KEY);
+ if (lastClearFillDate !== configs.lastLocalStorageFillClearanceDate) {
+ const localStorageKeys = localStorage.getAllKeys();
+ _.each(localStorageKeys, key => {
+ if (_.startsWith(key, `${FILLS_KEY}-`) || _.startsWith(key, `${FILLS_LATEST_BLOCK}-`)) {
+ localStorage.removeItem(key);
+ }
+ });
+ }
+ localStorage.setItem(FILL_CLEAR_KEY, configs.lastLocalStorageFillClearanceDate);
+ },
+ addFillToUser(userAddress: string, networkId: number, fill: Fill) {
+ const fillsByHash = this.getUserFillsByHash(userAddress, networkId);
+ const fillHash = this._getFillHash(fill);
+ const doesFillExist = !_.isUndefined(fillsByHash[fillHash]);
+ if (doesFillExist) {
+ return;
+ }
+ fillsByHash[fillHash] = fill;
+ const userFillsJSONString = JSON.stringify(fillsByHash);
+ const userFillsKey = this._getUserFillsKey(userAddress, networkId);
+ localStorage.setItem(userFillsKey, userFillsJSONString);
+ },
+ getUserFillsByHash(userAddress: string, networkId: number): {[fillHash: string]: Fill} {
+ const userFillsKey = this._getUserFillsKey(userAddress, networkId);
+ const userFillsJSONString = localStorage.getItemIfExists(userFillsKey);
+ if (_.isEmpty(userFillsJSONString)) {
+ return {};
+ }
+ const userFillsByHash = JSON.parse(userFillsJSONString);
+ _.each(userFillsByHash, (fill, hash) => {
+ fill.paidMakerFee = new BigNumber(fill.paidMakerFee);
+ fill.paidTakerFee = new BigNumber(fill.paidTakerFee);
+ fill.filledTakerTokenAmount = new BigNumber(fill.filledTakerTokenAmount);
+ fill.filledMakerTokenAmount = new BigNumber(fill.filledMakerTokenAmount);
+ });
+ return userFillsByHash;
+ },
+ getFillsLatestBlock(userAddress: string, networkId: number): number {
+ const userFillsLatestBlockKey = this._getFillsLatestBlockKey(userAddress, networkId);
+ const blockNumberStr = localStorage.getItemIfExists(userFillsLatestBlockKey);
+ if (_.isEmpty(blockNumberStr)) {
+ return constants.GENESIS_ORDER_BLOCK_BY_NETWORK_ID[networkId];
+ }
+ const blockNumber = _.parseInt(blockNumberStr);
+ return blockNumber;
+ },
+ setFillsLatestBlock(userAddress: string, networkId: number, blockNumber: number) {
+ const userFillsLatestBlockKey = this._getFillsLatestBlockKey(userAddress, networkId);
+ localStorage.setItem(userFillsLatestBlockKey, `${blockNumber}`);
+ },
+ _getUserFillsKey(userAddress: string, networkId: number) {
+ const userFillsKey = `${FILLS_KEY}-${userAddress}-${networkId}`;
+ return userFillsKey;
+ },
+ _getFillsLatestBlockKey(userAddress: string, networkId: number) {
+ const userFillsLatestBlockKey = `${FILLS_LATEST_BLOCK}-${userAddress}-${networkId}`;
+ return userFillsLatestBlockKey;
+ },
+ _getFillHash(fill: Fill): string {
+ const fillJSON = JSON.stringify(fill);
+ const fillHash = ethUtil.sha256(fillJSON);
+ return fillHash.toString('hex');
+ },
+};