Skip to content

Commit

Permalink
4.1.0 Release - Master (#174)
Browse files Browse the repository at this point in the history
* 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 <abarnard@protonmail.com>
Co-authored-by: Arseniy Ivanov <138289+freeatnet@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 4, 2022
1 parent 8202a07 commit b2a4794
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
LimitRules,
EnhancedConfig
} from './interfaces'
import { isLocalStorageAvailable } from './utilities'

const DEFAULT_APP_NAME = 'unknown'
const DEFAULT_APP_VERSION = 'unknown'
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -119,6 +123,7 @@ export type Network =
| 'xdai'
| 'bsc-main'
| 'matic-main'
| 'fantom-main'
| 'local'

export type Status =
Expand Down
5 changes: 3 additions & 2 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
}

Expand Down
23 changes: 23 additions & 0 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit b2a4794

Please sign in to comment.