Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: lazy loading modal solana selector #98

Merged
merged 29 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9395c69
add: lazy loading modal solana selector
LukassF Feb 21, 2024
5678d3c
add: fetch all wallets and change type
LukassF Feb 22, 2024
5bab9f5
fix: version
LukassF Feb 22, 2024
bb51fc0
add: solana change event listener and emitter
LukassF Feb 22, 2024
bba5a77
fix: listeners
LukassF Feb 22, 2024
f26cbb6
Merge branch 'main' into solana_selector_lazy
LukassF Feb 22, 2024
bd6a67e
fix; pull main
LukassF Feb 22, 2024
de1f563
fix: decoupling types
LukassF Feb 22, 2024
11fce53
fix: partial and moved getSolanaWalletsList method to dedicated file
LukassF Feb 23, 2024
e012fc4
fix: remove partial
LukassF Feb 23, 2024
ae9b42a
remove redundant imports
LukassF Feb 23, 2024
7cff49a
add: showing error in modal
LukassF Feb 23, 2024
cdd47df
add: polkadot event emitter
LukassF Feb 23, 2024
6f25b32
fix type on connect emit
NorbertBodziony Feb 23, 2024
dad6ed4
fix hooks on connect
NorbertBodziony Feb 23, 2024
492aeb3
fix: modal display error
LukassF Feb 26, 2024
d7ecc4c
fix: linter errors
LukassF Feb 26, 2024
01004bd
fix: hooks in solana selector
LukassF Feb 26, 2024
532d76c
Merge branch 'main' into solana_selector_lazy
LukassF Feb 26, 2024
3024c96
fix: reject rather than throw error
LukassF Feb 26, 2024
0790def
remove: console log
LukassF Feb 26, 2024
cf5764e
add: external modal for solana
LukassF Feb 26, 2024
3bb97c4
fix: added listener to the newly created app when disconnecting
LukassF Feb 27, 2024
44295fb
fix: update wallets list on disconnect
LukassF Feb 27, 2024
262958d
add try catch and remove filter on modal
NorbertBodziony Feb 27, 2024
d4c6d5b
fix solana
NorbertBodziony Feb 27, 2024
8953340
add try catch blocks
LukassF Feb 27, 2024
299798c
fix: polkadot detection
LukassF Feb 27, 2024
64474f3
fix: polkadot detection v2
LukassF Feb 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sdk/apps/modal-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
"dependencies": {
"@mysten/sui.js": "^0.42.0",
"@mysten/wallet-adapter-wallet-standard": "^0.8.0",
"@nightlylabs/wallet-selector-solana": "0.2.7",
"@nightlylabs/wallet-selector-solana": "0.3.0",
"@nightlylabs/nightly-connect-solana": "0.0.29",
"@nightlylabs/nightly-connect-sui": "0.0.29",
"@nightlylabs/wallet-selector-sui": "0.2.7",
"@nightlylabs/nightly-connect-polkadot": "0.0.14",
"@nightlylabs/wallet-selector-polkadot": "0.2.0",
"@nightlylabs/wallet-selector-polkadot": "0.2.2",
"@nightlylabs/wallet-selector-base": "^0.4.0",
"@polkadot/extension-inject": "^0.46.5",
"@polkadot/api": "^10.10.1",
"@solana/web3.js": "^1.77.2",
Expand All @@ -38,4 +39,4 @@
"engines": {
"node": ">=16.8"
}
}
}
17 changes: 10 additions & 7 deletions sdk/apps/modal-example/src/routes/aleph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export default function Polkadot() {
)

adapter.canEagerConnect().then((canEagerConnect: boolean) => {
console.log('canEagerConnect', canEagerConnect)
setEager(canEagerConnect)
})
setAdapter(adapter)
Expand Down Expand Up @@ -67,12 +66,16 @@ export default function Polkadot() {
fallback={
<button
onClick={async () => {
console.log(getPolkadotWallets())
await adapter()!.connect()
const accounts = await adapter()!.accounts.get()
console.log(accounts)
setPublicKey(accounts[0].address)
console.log('adapter', adapter())
try {
console.log(getPolkadotWallets())
await adapter()!.connect()
const accounts = await adapter()!.accounts.get()
console.log(accounts)
setPublicKey(accounts[0].address)
console.log('adapter', adapter())
} catch (err) {
console.log(err)
}
}}>
Connect
</button>
Expand Down
18 changes: 11 additions & 7 deletions sdk/apps/modal-example/src/routes/alephCustom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function Polkadot() {
},
network: 'AlephZero'
},
true, // change this to false to test disabling eager connect
{}, // change this to false to test disabling eager connect
document.getElementById('modalAnchor'),
{
variablesOverride: {
Expand Down Expand Up @@ -97,12 +97,16 @@ export default function Polkadot() {
fallback={
<button
onClick={async () => {
console.log(getPolkadotWallets())
await adapter()!.connect()
const accounts = await adapter()!.accounts.get()
console.log(accounts)
setPublicKey(accounts[0].address)
console.log('adapter', adapter())
try {
console.log(getPolkadotWallets())
await adapter()!.connect()
const accounts = await adapter()!.accounts.get()
console.log(accounts)
setPublicKey(accounts[0].address)
console.log('adapter', adapter())
} catch (err) {
console.log(err)
}
}}>
Connect
</button>
Expand Down
24 changes: 15 additions & 9 deletions sdk/apps/modal-example/src/routes/alephInitOnConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function Polkadot() {
const provider = new WsProvider('wss://ws.test.azero.dev/')

onMount(async () => {
const adapter = NightlyConnectAdapter.buildWithInitOnConnect(
const adapter = NightlyConnectAdapter.buildLazy(
{
appMetadata: {
name: 'NC TEST AlephZero',
Expand All @@ -23,10 +23,12 @@ export default function Polkadot() {
},
network: 'AlephZero'
},
true, // change this to false to test disabling eager connect
{ initOnConnect: true }, // change this to false to test disabling eager connect
document.getElementById('modalAnchor')
)

adapter.on('connect', (a) => {
console.log('adapter connected', a)
})
setAdapter(adapter)

ApiPromise.create({
Expand All @@ -45,12 +47,16 @@ export default function Polkadot() {
fallback={
<button
onClick={async () => {
console.log(getPolkadotWallets())
await adapter()!.connect()
const accounts = await adapter()!.accounts.get()
console.log(accounts)
setPublicKey(accounts[0].address)
console.log('adapter', adapter())
try {
console.log(getPolkadotWallets())
await adapter()!.connect()
const accounts = await adapter()!.accounts.get()
console.log(accounts)
setPublicKey(accounts[0].address)
console.log('adapter', adapter())
} catch (err) {
console.log(err)
}
}}>
Connect
</button>
Expand Down
155 changes: 155 additions & 0 deletions sdk/apps/modal-example/src/routes/externalModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { createEffect, createSignal, onMount, Show } from 'solid-js'
import { Title } from 'solid-start'
import { NightlyConnectAdapter } from '@nightlylabs/wallet-selector-solana'
import { Connection, PublicKey, SystemProgram, Transaction as SolanaTx } from '@solana/web3.js'
import toast from 'solid-toast'
import { AppInitData, NightlyConnectSelectorModal } from '@nightlylabs/wallet-selector-base'
import { SOLANA_NETWORK } from '@nightlylabs/nightly-connect-solana'

const connection = new Connection('https://api.devnet.solana.com')

export default function SolanaExternalModal() {
const [adapter, setAdapter] = createSignal<NightlyConnectAdapter>()
const [modal, setModal] = createSignal<NightlyConnectSelectorModal>()
const [eager, setEager] = createSignal(false)
const [publicKey, setPublicKey] = createSignal<PublicKey>()
onMount(async () => {
const appInitData: AppInitData = {
appMetadata: {
name: 'NCTestSolana',
description: 'Nightly Connect Test',
icon: 'https://docs.nightly.app/img/logo.png',
additionalInfo: 'Courtesy of Nightly Connect team'
},
persistent: true
}

const adapter = await NightlyConnectAdapter.build(
appInitData,
{ disableModal: true },
document.getElementById('modalAnchor')
)

const modal = new NightlyConnectSelectorModal(
adapter.walletsList,
appInitData.url ?? 'https://nc2.nightly.app',
{
name: SOLANA_NETWORK,
icon: 'https://assets.coingecko.com/coins/images/4128/small/solana.png'
},
document.getElementById('modalAnchor')
)

setModal(modal)

adapter.on('connect', (pk) => {
modal.closeModal()
setPublicKey(pk)
})

adapter.on('disconnect', () => {
modal.walletsList = adapter.walletsList
setPublicKey(undefined)
})

adapter.canEagerConnect().then((canEagerConnect) => {
setEager(canEagerConnect)
})

setAdapter(adapter)
})

createEffect(() => {
if (eager()) {
adapter()
?.connect()
.then(
() => {
console.log('connect resolved successfully')
},
() => {
console.log('connect rejected')
}
)
}
})

return (
<main>
<Title>Solana with External Modal Example</Title>
<div id="modalAnchor" />
<Show
when={!!publicKey()}
fallback={
<button
onClick={() => {
if (adapter()?.connecting) {
console.log('Cannot connect while connecting')
return
}

if (adapter()?.connected) {
return
}

modal()?.openModal(adapter()?.sessionId ?? undefined, async (walletName) => {
try {
modal()?.setStandardWalletConnectProgress(true)
await adapter()?.connectToWallet(walletName)
} catch (err) {
modal()?.setStandardWalletConnectProgress(false)
console.log('error')
modal()?.closeModal()
}
})
}}>
Connect
</button>
}>
<h1>Current public key: {publicKey()!.toString()}</h1>
<button
onClick={async () => {
try {
const ix = SystemProgram.transfer({
fromPubkey: adapter()!.publicKey!,
lamports: 1e6,
toPubkey: new PublicKey('147oKbjwGDHEthw7sRKNrzYiRiGqYksk1ravTMFkpAnv')
})
const tx = new SolanaTx().add(ix).add(ix).add(ix).add(ix).add(ix)
const a = await connection.getRecentBlockhash()
tx.recentBlockhash = a.blockhash
tx.feePayer = adapter()!.publicKey!
const signedTx = await adapter()!.signTransaction!(tx)
await connection.sendRawTransaction(signedTx!.serialize())

toast.success('Transaction was signed and sent!')
} catch (e) {
toast.error("Error: couldn't sign and send transaction!")
console.log(e)
}
}}>
Send 0.005 SOL
</button>
<button
onClick={async () => {
try {
await adapter()!.signMessage!(new TextEncoder().encode('I love Nightly'))

toast.success('Message was signed!')
} catch (e) {
toast.error("Error: couldn't sign message!")
console.log(e)
}
}}>
Sign message
</button>
<button
onClick={() => {
adapter()?.disconnect()
}}>
Disconnect
</button>
</Show>
</main>
)
}
3 changes: 3 additions & 0 deletions sdk/apps/modal-example/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default function Home() {
<A href="/solanaInitOnConnect">
<button>Solana - init app on connect</button>
</A>
<A href="/externalModal">
<button>Solana - external modal</button>
</A>
<A href="/sui">
<button>Sui</button>
</A>
Expand Down
2 changes: 1 addition & 1 deletion sdk/apps/modal-example/src/routes/solana.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function Solana() {
additionalInfo: 'Courtesy of Nightly Connect team'
}
},
true,
{},
document.getElementById('modalAnchor')
).then((adapter) => {
adapter.on('connect', (pk) => {
Expand Down
4 changes: 2 additions & 2 deletions sdk/apps/modal-example/src/routes/solanaInitOnConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function SolanaLazy() {
const [adapter, setAdapter] = createSignal<NightlyConnectAdapter>()
const [publicKey, setPublicKey] = createSignal<PublicKey>()
onMount(() => {
const adapter = NightlyConnectAdapter.buildWithInitOnConnect(
const adapter = NightlyConnectAdapter.buildLazy(
{
appMetadata: {
name: 'NCTestSolana',
Expand All @@ -20,7 +20,7 @@ export default function SolanaLazy() {
},
url: 'https://nc2.nightly.app'
},
true,
{ initOnConnect: true },
document.getElementById('modalAnchor')
)

Expand Down
2 changes: 1 addition & 1 deletion sdk/apps/modal-example/src/routes/solanaLazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function SolanaLazy() {
},
url: 'https://nc2.nightly.app'
},
true,
{},
document.getElementById('modalAnchor')
)

Expand Down
4 changes: 2 additions & 2 deletions sdk/packages/modal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nightlylabs/wallet-selector-modal",
"version": "0.2.0",
"version": "0.2.1",
"type": "module",
"exports": {
".": {
Expand Down Expand Up @@ -55,4 +55,4 @@
"typescript": "^5.0.2",
"vite": "^4.3.9"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const Error: Story = (args: NightlyModalArgs) => {

if (!args.sessionId)
setTimeout(() => {
updateArgs({ timeoutError: true })
updateArgs({ timeoutError: 'error' })
}, 5000)

return html`<nightly-desktop-main
Expand Down
Loading
Loading