Skip to content

Commit 41d8777

Browse files
committed
Add dynamic imports to check modules
1 parent 06fadfb commit 41d8777

19 files changed

+367
-683
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"@babel/cli": "^7.7.0",
2525
"@babel/core": "^7.5.5",
2626
"@babel/preset-env": "^7.5.5",
27-
"@joseph184/rollup-plugin-node-builtins": "^2.1.4",
2827
"@pyoner/svelte-ts-preprocess": "^1.2.1",
2928
"@rollup/plugin-json": "^4.0.0",
3029
"@types/node": "^12.12.3",
@@ -33,7 +32,6 @@
3332
"rollup": "^1.12.0",
3433
"rollup-plugin-commonjs": "^10.0.0",
3534
"rollup-plugin-img": "^1.1.0",
36-
"rollup-plugin-node-globals": "^1.4.0",
3735
"rollup-plugin-node-resolve": "^5.2.0",
3836
"rollup-plugin-svelte": "^5.0.3",
3937
"rollup-plugin-typescript2": "0.21.0",
@@ -50,7 +48,8 @@
5048
"bowser": "^2.5.2",
5149
"fortmatic": "^0.8.2",
5250
"promise-cancelable": "^2.1.1",
53-
"regenerator-runtime": "^0.13.3"
51+
"regenerator-runtime": "^0.13.3",
52+
"squarelink": "^1.1.4"
5453
},
5554
"scripts": {
5655
"build": "rimraf dist && rollup -c && babel dist/cjs --out-dir dist/cjs && babel dist/esm --out-dir dist/esm",

rollup.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import resolve from 'rollup-plugin-node-resolve'
33
import json from '@rollup/plugin-json'
44
import image from 'rollup-plugin-img'
55
import commonjs from 'rollup-plugin-commonjs'
6-
import globals from 'rollup-plugin-node-globals'
7-
import builtins from '@joseph184/rollup-plugin-node-builtins'
86
import typescript from 'rollup-plugin-typescript2'
97

108
import {

src/components/Wallets.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import { fade } from 'svelte/transition'
33
import Button from '../elements/Button.svelte'
44
import IconButton from '../elements/IconButton.svelte'
5-
import { SelectModalData, WalletModule } from '../interfaces'
6-
export let modalData: SelectModalData
5+
import { WalletSelectModalData, WalletModule } from '../interfaces'
6+
export let modalData: WalletSelectModalData
77
export let handleWalletSelect: (wallet: WalletModule) => void
88
99
let showingAllWalletModules: boolean = false

src/interfaces.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface Subscriptions {
1515

1616
interface Modules {
1717
walletSelect: WalletSelectModule
18-
walletReady: WalletReadyModule[]
18+
walletCheck: WalletCheckModule[]
1919
}
2020

2121
export interface WalletSelectModule {
@@ -24,14 +24,14 @@ export interface WalletSelectModule {
2424
wallets: WalletModule[]
2525
}
2626

27-
export interface WalletReadyModule {
27+
export interface WalletCheckModule {
2828
(stateAndHelpers: StateAndHelpers):
29-
| ReadyModal
29+
| WalletCheckModal
3030
| undefined
31-
| Promise<ReadyModal | undefined>
31+
| Promise<WalletCheckModal | undefined>
3232
}
3333

34-
export interface ReadyModal {
34+
export interface WalletCheckModal {
3535
img?: string
3636
heading: string
3737
description: string
@@ -46,7 +46,7 @@ export interface ReadyModal {
4646
icon?: string
4747
}
4848

49-
export interface SelectModalData {
49+
export interface WalletSelectModalData {
5050
heading: string
5151
description: string
5252
primaryWallets: WalletModule[]
@@ -122,11 +122,6 @@ export interface Wallet {
122122
loading?: () => Promise<undefined>
123123
}
124124

125-
export interface ReadyDefaultsOptions {
126-
networkId: number
127-
minimumBalance?: string
128-
}
129-
130125
export interface SdkWalletOptions {
131126
apiKey: string
132127
networkId: number
@@ -144,11 +139,17 @@ export interface WalletInit {
144139
networkId?: number
145140
}
146141

142+
export interface WalletCheckInit {
143+
name: string
144+
networkId?: number
145+
minimumBalance?: string
146+
}
147+
147148
export interface WalletSelectFunction {
148149
(autoSelectWallet?: string): Promise<boolean>
149150
}
150151

151-
interface WalletReady {
152+
interface WalletCheck {
152153
(): Promise<boolean>
153154
}
154155

@@ -166,7 +167,7 @@ export interface ConfigOptions {
166167

167168
export interface API {
168169
walletSelect: WalletSelectFunction
169-
walletReady: WalletReady
170+
walletCheck: WalletCheck
170171
config: Config
171172
getState: GetState
172173
}
@@ -205,8 +206,8 @@ export interface AppState {
205206
autoSelectWallet: string
206207
walletSelectInProgress: boolean
207208
walletSelectCompleted: boolean
208-
walletReadyInProgress: boolean
209-
walletReadyCompleted: boolean
209+
walletCheckInProgress: boolean
210+
walletCheckCompleted: boolean
210211
}
211212

212213
export interface QuerablePromise extends CancelablePromise {

src/modules/ready/balance.ts renamed to src/modules/check/balance.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
import { ReadyModal, StateAndHelpers } from '../../interfaces'
1+
import { WalletCheckModal, StateAndHelpers } from '../../interfaces'
2+
import { validateType } from '../../validation'
3+
4+
function balance(options: {
5+
minimumBalance: string
6+
}): (currentState: StateAndHelpers) => WalletCheckModal | undefined {
7+
validateType({ name: 'balance options', value: options, type: 'object' })
8+
9+
const { minimumBalance } = options
10+
11+
validateType({
12+
name: 'minimumBalance',
13+
value: minimumBalance,
14+
type: 'string'
15+
})
216

3-
function balance(
4-
minimum: string
5-
): (currentState: StateAndHelpers) => ReadyModal | undefined {
617
return (StateAndHelpers: StateAndHelpers) => {
718
const { balance, BigNumber } = StateAndHelpers
819
// if balance is less than minimum
9-
if (BigNumber(balance).lt(BigNumber(minimum || 0))) {
20+
if (BigNumber(balance).lt(BigNumber(minimumBalance || 0))) {
1021
return {
1122
heading: 'Get Some ETH',
1223
description: `Your current account has less than the necessary minimum balance of ${BigNumber(
13-
minimum
24+
minimumBalance
1425
)
1526
.div(BigNumber('1000000000000000000'))
1627
.toString(10)} ETH.`,

src/modules/ready/connect.ts renamed to src/modules/check/connect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ReadyModal, StateAndHelpers, Wallet } from '../../interfaces'
1+
import { WalletCheckModal, StateAndHelpers } from '../../interfaces'
22

33
function connect() {
4-
return (stateAndHelpers: StateAndHelpers): ReadyModal | undefined => {
4+
return (stateAndHelpers: StateAndHelpers): WalletCheckModal | undefined => {
55
const { wallet, address } = stateAndHelpers
66
if (!address && wallet && wallet.name) {
77
return {

src/modules/check/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { validateWalletCheckInit } from '../../validation'
2+
import { WalletCheckModule, WalletCheckInit } from '../../interfaces'
3+
4+
function walletChecks(
5+
walletCheckInit: WalletCheckInit[]
6+
): never | Promise<WalletCheckModule[]> {
7+
validateWalletCheckInit(walletCheckInit)
8+
9+
return Promise.all(
10+
walletCheckInit.map(init => {
11+
const { name, ...initParams } = init
12+
13+
return getModule(name).then((module: any) => module.default(initParams))
14+
})
15+
)
16+
}
17+
18+
function getModule(name: string): Promise<any> {
19+
switch (name) {
20+
case 'connect':
21+
return import('./connect')
22+
case 'network':
23+
return import('./network')
24+
case 'balance':
25+
return import('./balance')
26+
default:
27+
return Promise.reject('invalid wallet check name')
28+
}
29+
}
30+
31+
export default walletChecks

src/modules/ready/network.ts renamed to src/modules/check/network.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { networkName } from '../../utilities'
2-
import { WalletReadyModule, StateAndHelpers } from '../../interfaces'
2+
import { validateType } from '../../validation'
3+
import { WalletCheckModule, StateAndHelpers } from '../../interfaces'
34

4-
function network(correctNetwork: number): WalletReadyModule {
5-
if (!correctNetwork || typeof correctNetwork !== 'number') {
6-
throw new Error(
7-
'Network module must be called with a property of value that is of type Number'
8-
)
9-
}
5+
function network(options: { networkId: number }): WalletCheckModule | never {
6+
validateType({ name: 'network options', value: options, type: 'object' })
7+
8+
const { networkId } = options
9+
validateType({ name: 'networkId', value: networkId, type: 'number' })
1010

1111
return (stateAndHelpers: StateAndHelpers) => {
1212
const { network, walletSelect, exit } = stateAndHelpers
1313

14-
if (network != (correctNetwork || '1')) {
14+
if (network != networkId) {
1515
return {
1616
heading: 'You Must Change Networks',
1717
description: `We've detected that you need to switch your wallet's network from <b>${networkName(
18-
Number(network)
18+
network
1919
)}</b> to <b>${networkName(
20-
Number(correctNetwork)
20+
networkId
2121
)} network</b> for this Dapp. <br><br> <i style="font-size: inherit; font-family: inherit;">*Some wallets may not support changing networks. If you can not change networks in your wallet you may consider switching to a different wallet.</i>`,
2222
eventCode: 'networkFail',
2323
button: {

src/modules/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import select from './select'
2-
import ready from './ready'
2+
import check from './check'
33

44
export default {
55
select,
6-
ready
6+
check
77
}

src/modules/ready/index.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)