From b2a47944ea53c23f96ff05aafe01645412703f43 Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Fri, 4 Feb 2022 14:35:06 -0700 Subject: [PATCH] 4.1.0 Release - Master (#174) * 4.0.0-0.1.0 : Add fantom network #173 * Add fantom network (fantom-main) to Network type in interfaces.ts and networks object in defaults.ts * Update version * 4.0.0-0.1.1 - Test whether localStorage is accessible before performing operations (#172) * Update version * Add gasGwei values Co-authored-by: Aaron Barnard Co-authored-by: Arseniy Ivanov <138289+freeatnet@users.noreply.github.com> --- package.json | 2 +- src/defaults.ts | 3 ++- src/index.ts | 3 ++- src/interfaces.ts | 5 +++++ src/messages.ts | 5 +++-- src/utilities.ts | 23 +++++++++++++++++++++++ 6 files changed, 36 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 4955f42..cecac89 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bnc-sdk", - "version": "4.0.0", + "version": "4.1.0", "description": "SDK to connect to the blocknative backend via a websocket connection", "keywords": [ "ethereum", diff --git a/src/defaults.ts b/src/defaults.ts index 5ef2427..91129c8 100644 --- a/src/defaults.ts +++ b/src/defaults.ts @@ -11,7 +11,8 @@ export const networks: { [key: string]: { [key: string]: string } } = { '42': 'kovan', '56': 'bsc-main', '100': 'xdai', - '137': 'matic-main' + '137': 'matic-main', + '250': 'fantom-main' } } diff --git a/src/index.ts b/src/index.ts index c739671..ed03de3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,6 +32,7 @@ import { LimitRules, EnhancedConfig } from './interfaces' +import { isLocalStorageAvailable } from './utilities' const DEFAULT_APP_NAME = 'unknown' const DEFAULT_APP_VERSION = 'unknown' @@ -114,7 +115,7 @@ class Blocknative { const storageKey = CryptoEs.SHA1(`${dappId} - ${name}`).toString() const storedConnectionId = - typeof window !== 'undefined' && window.localStorage.getItem(storageKey) + isLocalStorageAvailable() && window.localStorage.getItem(storageKey) this._storageKey = storageKey this._connectionId = storedConnectionId || undefined diff --git a/src/interfaces.ts b/src/interfaces.ts index dcf3f9e..fd751b8 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -66,6 +66,10 @@ export interface EthereumTransactionData extends CommonTransactionData { replaceHash?: string counterparty?: string direction?: string + baseFeePerGasGwei?: number + maxPriorityFeePerGasGwei?: number + maxFeePerGasGwei?: number + gasPriceGwei?: number } export interface InternalTransaction { @@ -119,6 +123,7 @@ export type Network = | 'xdai' | 'bsc-main' | 'matic-main' + | 'fantom-main' | 'local' export type Status = diff --git a/src/messages.ts b/src/messages.ts index 0037df8..0339d5b 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -3,7 +3,8 @@ import { last, networkName, wait, - jsonPreserveUndefined + jsonPreserveUndefined, + isLocalStorageAvailable } from './utilities' import { version } from '../package.json' import { Ac, Tx, Emitter, EventObject, TransactionHandler } from './interfaces' @@ -63,7 +64,7 @@ export function handleMessage(this: any, msg: { data: string }): void { } = JSON.parse(msg.data) if (connectionId) { - if (typeof window !== 'undefined') { + if (isLocalStorageAvailable()) { window.localStorage.setItem(this._storageKey, connectionId) } diff --git a/src/utilities.ts b/src/utilities.ts index 476e3f2..cfd605c 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -114,3 +114,26 @@ export function wait(time: number) { export const jsonPreserveUndefined = (k: any, v: any) => v === undefined ? 'undefined' : v + +/** + * Tests if LocalStorage may be used. Accounts for environments where + * LocalStorage is not supported, as well as those where it is blocked. + * + * @returns `true` if LocalStorage is supported and accessible, `false` otherwise. + */ +export function isLocalStorageAvailable(): boolean { + const isSupported = typeof window !== 'undefined' && 'localStorage' in window + + if (isSupported) { + const testKey = '__testLocalStorage' + try { + window.localStorage.setItem(testKey, '1') + window.localStorage.removeItem(testKey) + return true + } catch (err) { + return false + } + } + + return false +}