Skip to content

Commit e230f47

Browse files
authored
Merge pull request #22 from blocknative/feature/sdk-instance
Add SDK instance property to wallet
2 parents f38af7e + 61fb5b0 commit e230f47

File tree

7 files changed

+64
-67
lines changed

7 files changed

+64
-67
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const options = {
9090
address: Function, // Called with the current account address of the user [String]
9191
network: Function, // Called with the current network id the users' wallet is connected to [Number]
9292
balance: Function, // Called with the current balance in `wei` of the users' current account address [String]
93-
wallet: Function // Called with the users' current selected wallet [Object]: {provider: Object, name: String}
93+
wallet: Function // Called with the users' current selected wallet [Object]: {provider: Object, name: String, instance: Object (if a sdk wallet)}
9494
},
9595
modules: {
9696
walletSelect: {
@@ -245,7 +245,13 @@ import Onboard from "bnc-onboard"
245245

246246
// PICK AND CHOOSE MODULES
247247

248-
const { portis, squarelink, dapper, metamask, fortmatic } = Onboard.modules.select
248+
const {
249+
portis,
250+
squarelink,
251+
dapper,
252+
metamask,
253+
fortmatic
254+
} = Onboard.modules.select
249255

250256
const onboard = Onboard.init({
251257
// ...
@@ -256,12 +262,12 @@ const onboard = Onboard.init({
256262
"Please select the wallet that you would like to use with this Dapp",
257263
wallets: {
258264
desktop: [
259-
portis({ apiKey: 'sdda-w2-ds3', networkId: 1 }),
260-
squarelink({ apiKey: 'sdda-w2-ds3', networkId: 1 }),
265+
portis({ apiKey: "sdda-w2-ds3", networkId: 1 }),
266+
squarelink({ apiKey: "sdda-w2-ds3", networkId: 1 }),
261267
dapper(),
262-
metmask(),
268+
metmask()
263269
],
264-
mobile: [fortmatic({apiKey: 'sd-3d3-d', networkId: 1})]
270+
mobile: [fortmatic({ apiKey: "sd-3d3-d", networkId: 1 })]
265271
}
266272
}
267273
//....
@@ -278,7 +284,7 @@ const onboard = Onboard.init({
278284
walletSelect: Onboard.modules.select.defaults({
279285
portisInit: { apiKey: "Your portis key here" },
280286
networkId: 4
281-
}),
287+
})
282288
// ...
283289
}
284290
})

src/modules/select/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ function defaults({
1919
const desktopModules = [metamask(), dapper()]
2020
const mobileModules = [coinbase(), trust()]
2121

22-
if (squarelinkInit) {
23-
desktopModules.push(squarelink({ ...squarelinkInit, networkId }))
24-
mobileModules.push(squarelink({ ...squarelinkInit, networkId }))
25-
}
26-
2722
if (portisInit) {
2823
desktopModules.push(portis({ ...portisInit, networkId }))
2924
mobileModules.push(portis({ ...portisInit, networkId }))
@@ -43,6 +38,11 @@ function defaults({
4338
)
4439
}
4540

41+
if (squarelinkInit) {
42+
desktopModules.push(squarelink({ ...squarelinkInit, networkId }))
43+
mobileModules.push(squarelink({ ...squarelinkInit, networkId }))
44+
}
45+
4646
return {
4747
heading: heading || "Select a Wallet",
4848
description:

src/modules/select/wallets/fortmatic.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ function fortmatic(options) {
2727
name: "Fortmatic",
2828
iconSrc: fortmaticIcon,
2929
wallet: ({ BigNumber }) => {
30-
const fortmatic = new Fortmatic(
30+
const instance = new Fortmatic(
3131
apiKey,
3232
networkId === 1 ? undefined : networkName(networkId)
3333
)
34-
const provider = fortmatic.getProvider()
34+
const provider = instance.getProvider()
3535

3636
return {
3737
provider,
38+
instance,
3839
interface: {
3940
name: "Fortmatic",
40-
connect: fortmatic.user.login,
41+
connect: instance.user.login,
4142
address: {
4243
get: () => Promise.resolve(provider.account)
4344
},
@@ -47,7 +48,7 @@ function fortmatic(options) {
4748
balance: {
4849
get: () =>
4950
provider.account &&
50-
fortmatic.user.getBalances().then(res =>
51+
instance.user.getBalances().then(res =>
5152
res[0]
5253
? BigNumber(res[0].crypto_amount)
5354
.times(BigNumber("1000000000000000000"))

src/modules/select/wallets/portis.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,25 @@ function portis(options) {
2626
name: "Portis",
2727
iconSrc: portisIcon,
2828
wallet: ({ BigNumber }) => {
29-
const portis = new Portis(apiKey, networkName(networkId))
30-
const { provider } = portis
29+
const instance = new Portis(apiKey, networkName(networkId))
30+
const { provider } = instance
3131

3232
return {
3333
provider,
34+
instance,
3435
interface: {
3536
name: "Portis",
3637
connect: provider.enable,
3738
address: {
3839
onChange: func => {
39-
portis.onLogin(address => {
40+
instance.onLogin(address => {
4041
func(address)
4142
provider.address = address
4243
})
4344
}
4445
},
4546
network: {
46-
get: () => Promise.resolve(portis.config.network.chainId)
47+
get: () => Promise.resolve(instance.config.network.chainId)
4748
},
4849
balance: {
4950
get: () =>

src/modules/select/wallets/squarelink.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import Squarelink from "squarelink"
22

3-
import { networkName } from "../../../utilities"
3+
import { networkName, networkToId } from "../../../utilities"
44
import sqlkIcon from "../wallet-icons/icon-squarelink.svg"
55

66
function squarelink(options) {
77
if (!options || typeof options !== "object") {
8-
throw new Error("An options object is required to initialize squarelink module")
8+
throw new Error(
9+
"An options object is required to initialize squarelink module"
10+
)
911
}
1012

1113
const { apiKey, networkId } = options
@@ -16,7 +18,7 @@ function squarelink(options) {
1618
)
1719
}
1820

19-
if (!network || typeof networkId !== "number") {
21+
if (!networkId || typeof networkId !== "number") {
2022
throw new Error(
2123
"A network of type number is required to initialize squarelink module"
2224
)
@@ -26,42 +28,43 @@ function squarelink(options) {
2628
name: "Squarelink",
2729
iconSrc: sqlkIcon,
2830
wallet: ({ BigNumber }) => {
29-
const sqlk = new Squarelink(apiKey, networkName(networkId), { useSync: true })
30-
const provider = sqlk.getProviderSync()
31+
const instance = new Squarelink(apiKey, networkName(networkId), {
32+
useSync: true
33+
})
34+
35+
const provider = instance.getProviderSync()
3136

3237
return {
3338
provider,
39+
instance,
3440
interface: {
3541
name: "Squarelink",
3642
connect: provider.enable,
3743
address: {
38-
get: () => Promise.resolve(provider.accounts[0])
44+
get: () => Promise.resolve(instance.accounts[0])
3945
},
4046
network: {
41-
get: () => Promise.resolve(networkId)
47+
get: () => Promise.resolve(networkToId(instance.network))
4248
},
4349
balance: {
4450
get: () =>
45-
new Promise(resolve => {
46-
if (!provider.accounts.length) {
47-
resolve(null)
48-
return
49-
}
50-
51-
provider.sendAsync(
52-
{
53-
method: "eth_getBalance",
54-
params: [
55-
provider.accounts[0],
56-
"latest"
57-
],
58-
id: 1
59-
},
60-
(e, res) => {
61-
resolve(BigNumber(res.result).toString(10))
51+
new Promise(resolve => {
52+
if (!instance.accounts.length) {
53+
resolve(null)
54+
return
6255
}
63-
)
64-
})
56+
57+
provider.sendAsync(
58+
{
59+
method: "eth_getBalance",
60+
params: [instance.accounts[0], "latest"],
61+
id: 1
62+
},
63+
(e, res) => {
64+
resolve(BigNumber(res.result).toString(10))
65+
}
66+
)
67+
})
6568
}
6669
}
6770
}

src/views/WalletSelect.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@
5858
}
5959
6060
function handleWalletSelect(module) {
61-
const { provider, interface: selectedWalletInterface } = module.wallet({
61+
const {
62+
provider,
63+
interface: selectedWalletInterface,
64+
instance
65+
} = module.wallet({
6266
getProviderName,
6367
createLegacyProviderInterface,
6468
createModernProviderInterface,
@@ -89,6 +93,7 @@
8993
9094
wallet.set({
9195
provider,
96+
instance,
9297
name: module.name,
9398
connect: selectedWalletInterface.connect
9499
});

yarn.lock

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,25 +2439,6 @@ eth-json-rpc-middleware@^4.1.4:
24392439
pify "^3.0.0"
24402440
safe-event-emitter "^1.0.1"
24412441

2442-
"eth-json-rpc-middleware@github:walletconnect/eth-json-rpc-middleware":
2443-
version "4.1.3"
2444-
resolved "https://codeload.github.com/walletconnect/eth-json-rpc-middleware/tar.gz/dac43613b9f1bf9d45455dcb98cbb8f676f61a8a"
2445-
dependencies:
2446-
btoa "^1.2.1"
2447-
clone "^2.1.1"
2448-
eth-query "^2.1.2"
2449-
eth-sig-util "^1.4.2"
2450-
ethereumjs-block "^1.6.0"
2451-
ethereumjs-tx "^1.3.3"
2452-
ethereumjs-util "^5.1.2"
2453-
ethereumjs-vm "^2.6.0"
2454-
fetch-ponyfill "^4.0.0"
2455-
json-rpc-engine "^5.0.0"
2456-
json-rpc-error "^2.0.0"
2457-
json-stable-stringify "^1.0.1"
2458-
pify "^3.0.0"
2459-
safe-event-emitter "^1.0.1"
2460-
24612442
eth-query@^2.0.2, eth-query@^2.1.0, eth-query@^2.1.2:
24622443
version "2.1.2"
24632444
resolved "https://registry.yarnpkg.com/eth-query/-/eth-query-2.1.2.tgz#d6741d9000106b51510c72db92d6365456a6da5e"
@@ -5300,7 +5281,7 @@ vlq@^0.2.2:
53005281
eth-block-tracker "^4.2.0"
53015282
eth-json-rpc-filters "^4.0.2"
53025283
eth-json-rpc-infura "^3.1.0"
5303-
eth-json-rpc-middleware "^4.1.1"
5284+
eth-json-rpc-middleware "github:walletconnect/eth-json-rpc-middleware"
53045285
eth-sig-util "^1.4.2"
53055286
ethereumjs-block "^1.2.2"
53065287
ethereumjs-tx "^1.2.0"

0 commit comments

Comments
 (0)