aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/local_storage
diff options
context:
space:
mode:
authorHsuan Lee <boczeratul@gmail.com>2019-03-06 17:46:50 +0800
committerHsuan Lee <boczeratul@gmail.com>2019-03-06 17:46:50 +0800
commit35703539d0f2b4ddb3b11d0de8c9634af59ab71f (patch)
treeae3731221dbbb3a6fa40060a8d916cfd3f738289 /packages/website/ts/local_storage
parent92a1fde5b1ecd81b07cdb5bf0c9c1cd3544799db (diff)
downloaddexon-0x-contracts-stable.tar.gz
dexon-0x-contracts-stable.tar.zst
dexon-0x-contracts-stable.zip
Deploy @dexon-foundation/0x.jsstable
Diffstat (limited to 'packages/website/ts/local_storage')
-rw-r--r--packages/website/ts/local_storage/local_storage.ts45
-rw-r--r--packages/website/ts/local_storage/state_storage.ts16
-rw-r--r--packages/website/ts/local_storage/tracked_token_storage.ts71
-rw-r--r--packages/website/ts/local_storage/trade_history_storage.tsx94
4 files changed, 0 insertions, 226 deletions
diff --git a/packages/website/ts/local_storage/local_storage.ts b/packages/website/ts/local_storage/local_storage.ts
deleted file mode 100644
index 13d9ca401..000000000
--- a/packages/website/ts/local_storage/local_storage.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import * as _ from 'lodash';
-
-export const localStorage = {
- doesExist(): boolean {
- return !!window.localStorage;
- },
- getItemIfExists(key: string): string {
- if (!localStorage.doesExist) {
- return undefined;
- }
- const item = window.localStorage.getItem(key);
- if (_.isNull(item) || item === 'undefined') {
- return '';
- }
- return item;
- },
- setItem(key: string, value: string): void {
- if (!localStorage.doesExist || _.isUndefined(value)) {
- return;
- }
- window.localStorage.setItem(key, value);
- },
- removeItem(key: string): void {
- if (!localStorage.doesExist) {
- return;
- }
- window.localStorage.removeItem(key);
- },
- getObject(key: string): object | undefined {
- const item = localStorage.getItemIfExists(key);
- if (item) {
- return JSON.parse(item);
- }
- return undefined;
- },
- setObject(key: string, value: object): void {
- localStorage.setItem(key, JSON.stringify(value));
- },
- getAllKeys(): string[] {
- if (!localStorage.doesExist) {
- return [];
- }
- return _.keys(window.localStorage);
- },
-};
diff --git a/packages/website/ts/local_storage/state_storage.ts b/packages/website/ts/local_storage/state_storage.ts
deleted file mode 100644
index 517784b5b..000000000
--- a/packages/website/ts/local_storage/state_storage.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { localStorage } from 'ts/local_storage/local_storage';
-import { INITIAL_STATE, State } from 'ts/redux/reducer';
-
-const STORAGE_NAME = 'persistedState';
-
-export const stateStorage = {
- saveState(partialState: Partial<State>): void {
- localStorage.setObject(STORAGE_NAME, partialState);
- },
- getPersistedState(): Partial<State> {
- return localStorage.getObject(STORAGE_NAME);
- },
- getPersistedDefaultState(): State {
- return { ...INITIAL_STATE, ...stateStorage.getPersistedState() };
- },
-};
diff --git a/packages/website/ts/local_storage/tracked_token_storage.ts b/packages/website/ts/local_storage/tracked_token_storage.ts
deleted file mode 100644
index b1b579aef..000000000
--- a/packages/website/ts/local_storage/tracked_token_storage.ts
+++ /dev/null
@@ -1,71 +0,0 @@
-import * as _ from 'lodash';
-import { localStorage } from 'ts/local_storage/local_storage';
-import { Token, TokenByAddress, TrackedTokensByUserAddress } from 'ts/types';
-import { configs } from 'ts/utils/configs';
-
-const TRACKED_TOKENS_KEY = 'trackedTokens';
-const TRACKED_TOKENS_CLEAR_KEY = 'lastClearTrackedTokensDate';
-
-export const trackedTokenStorage = {
- // Clear trackedTokens localStorage if we've updated the config variable in an update
- // that introduced a backward incompatible change requiring the tracked tokens to be re-set
- clearIfRequired(): void {
- const lastClearFillDate = localStorage.getItemIfExists(TRACKED_TOKENS_CLEAR_KEY);
- if (lastClearFillDate !== configs.LAST_LOCAL_STORAGE_TRACKED_TOKEN_CLEARANCE_DATE) {
- localStorage.removeItem(TRACKED_TOKENS_KEY);
- }
- localStorage.setItem(TRACKED_TOKENS_CLEAR_KEY, configs.LAST_LOCAL_STORAGE_TRACKED_TOKEN_CLEARANCE_DATE);
- },
- addTrackedTokenToUser(userAddress: string, networkId: number, token: Token): void {
- const trackedTokensByUserAddress = trackedTokenStorage.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(): TrackedTokensByUserAddress {
- const trackedTokensJSONString = localStorage.getItemIfExists(TRACKED_TOKENS_KEY);
- if (_.isEmpty(trackedTokensJSONString)) {
- return {};
- }
- const trackedTokensByUserAddress = JSON.parse(trackedTokensJSONString);
- return trackedTokensByUserAddress;
- },
- getTrackedTokensByAddress(userAddress: string, networkId: number): TokenByAddress {
- const trackedTokensByAddress: TokenByAddress = {};
- const trackedTokensJSONString = localStorage.getItemIfExists(TRACKED_TOKENS_KEY);
- if (_.isEmpty(trackedTokensJSONString)) {
- return trackedTokensByAddress;
- }
- const trackedTokensByUserAddress = JSON.parse(trackedTokensJSONString);
- const trackedTokensByNetworkId = trackedTokensByUserAddress[userAddress];
- if (_.isUndefined(trackedTokensByNetworkId)) {
- return trackedTokensByAddress;
- }
- const trackedTokens = trackedTokensByNetworkId[networkId];
- _.each(trackedTokens, (trackedToken: Token) => {
- trackedTokensByAddress[trackedToken.address] = trackedToken;
- });
- return trackedTokensByAddress;
- },
- removeTrackedToken(userAddress: string, networkId: number, tokenAddress: string): void {
- const trackedTokensByUserAddress = trackedTokenStorage.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
deleted file mode 100644
index ef6a0a1aa..000000000
--- a/packages/website/ts/local_storage/trade_history_storage.tsx
+++ /dev/null
@@ -1,94 +0,0 @@
-import { BigNumber } from '@0x/utils';
-import ethUtil from 'ethereumjs-util';
-import * as _ from 'lodash';
-import { localStorage } from 'ts/local_storage/local_storage';
-import { Fill } from 'ts/types';
-import { configs } from 'ts/utils/configs';
-import { constants } from 'ts/utils/constants';
-
-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(): void {
- const lastClearFillDate = localStorage.getItemIfExists(FILL_CLEAR_KEY);
- if (lastClearFillDate !== configs.LAST_LOCAL_STORAGE_FILL_CLEARANCE_DATE) {
- 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.LAST_LOCAL_STORAGE_FILL_CLEARANCE_DATE);
- },
- addFillToUser(userAddress: string, networkId: number, fill: Fill): void {
- const fillsByHash = tradeHistoryStorage.getUserFillsByHash(userAddress, networkId);
- const fillHash = tradeHistoryStorage._getFillHash(fill);
- const doesFillExist = !_.isUndefined(fillsByHash[fillHash]);
- if (doesFillExist) {
- return; // noop
- }
- fillsByHash[fillHash] = fill;
- const userFillsJSONString = JSON.stringify(fillsByHash);
- const userFillsKey = tradeHistoryStorage._getUserFillsKey(userAddress, networkId);
- localStorage.setItem(userFillsKey, userFillsJSONString);
- },
- removeFillFromUser(userAddress: string, networkId: number, fill: Fill): void {
- const fillsByHash = tradeHistoryStorage.getUserFillsByHash(userAddress, networkId);
- const fillHash = tradeHistoryStorage._getFillHash(fill);
- const doesFillExist = !_.isUndefined(fillsByHash[fillHash]);
- if (!doesFillExist) {
- return; // noop
- }
- delete fillsByHash[fillHash];
- const userFillsJSONString = JSON.stringify(fillsByHash);
- const userFillsKey = tradeHistoryStorage._getUserFillsKey(userAddress, networkId);
- localStorage.setItem(userFillsKey, userFillsJSONString);
- },
- getUserFillsByHash(userAddress: string, networkId: number): { [fillHash: string]: Fill } {
- const userFillsKey = tradeHistoryStorage._getUserFillsKey(userAddress, networkId);
- const userFillsJSONString = localStorage.getItemIfExists(userFillsKey);
- if (_.isEmpty(userFillsJSONString)) {
- return {};
- }
- const userFillsByHash = JSON.parse(userFillsJSONString);
- _.each(userFillsByHash, fill => {
- 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 = tradeHistoryStorage._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): void {
- const userFillsLatestBlockKey = tradeHistoryStorage._getFillsLatestBlockKey(userAddress, networkId);
- localStorage.setItem(userFillsLatestBlockKey, `${blockNumber}`);
- },
- _getUserFillsKey(userAddress: string, networkId: number): string {
- const userFillsKey = `${FILLS_KEY}-${userAddress}-${networkId}`;
- return userFillsKey;
- },
- _getFillsLatestBlockKey(userAddress: string, networkId: number): string {
- 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');
- },
-};