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

Feat/e2e with event #18

Draft
wants to merge 30 commits into
base: feat/e2e
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
93f3705
chore: add changeset for MetaMask detection fix
khanti42 Oct 28, 2024
997aaae
Merge pull request #274 from Consensys/fix/get-starknet-v4-metamask-d…
fracek Oct 28, 2024
0807b88
chore: version bump
github-actions[bot] Oct 28, 2024
51d9464
Merge pull request #275 from starknet-io/changeset-release/develop
fracek Oct 29, 2024
3cae449
Merge branch 'develop' into feat/e2e
khanti42 Oct 31, 2024
e2f3442
chore: revamp mm wallet
stanleyyconsensys Nov 1, 2024
0e6a000
chore: rollback pnpm-lock.yaml
khanti42 Nov 5, 2024
8ce0cfd
chore: fix pnpm-lock
khanti42 Nov 5, 2024
39b9e13
Merge branch 'chore/revamp-mmvitualwallet' into feat/e2e
khanti42 Nov 5, 2024
dcca68a
chore: merged
khanti42 Nov 5, 2024
1360dfe
feat: add event management
khanti42 Nov 6, 2024
d2fee96
chore: add changeset for MetaMask dynamic loading fix
khanti42 Nov 6, 2024
600b4a7
chore: fix pnpm-lock
khanti42 Nov 6, 2024
4669396
Merge pull request #276 from Consensys/chore/revamp-mmvitualwallet
fracek Nov 15, 2024
25326a7
chore: version bump
github-actions[bot] Nov 15, 2024
f7131db
Merge pull request #277 from starknet-io/changeset-release/develop
fracek Nov 27, 2024
34e44b6
fix: allows discovery of virtual wallet support independently
khanti42 Dec 5, 2024
0263b88
chore: added changeset
khanti42 Dec 5, 2024
7144af2
fix: await for all hasSupport to haapen
khanti42 Dec 6, 2024
2cccbbb
feat: allow filtering by ids or names
khanti42 Dec 6, 2024
187bf96
Merge pull request #280 from Consensys/fix/virtual-wallet-discovery
fracek Dec 9, 2024
356a6fd
chore: version bump
github-actions[bot] Dec 9, 2024
441a220
Merge pull request #281 from starknet-io/changeset-release/develop
fracek Dec 9, 2024
1818adf
feat: add event support on MM
stanleyyconsensys Dec 11, 2024
d35f27c
chore: update comment text
stanleyyconsensys Dec 11, 2024
5887ee6
chore: fix comment text
stanleyyconsensys Dec 11, 2024
3f030eb
chore: resolve comment
stanleyyconsensys Dec 13, 2024
8e1dd66
Merge branch 'feat/e2e-with-event' into feat/add-mm-event-subscriptio…
khanti42 Dec 16, 2024
9894852
Merge pull request #19 from Consensys/feat/add-mm-event-subscription-…
khanti42 Dec 16, 2024
121a4e1
fix: event with new get-starknet swo management
khanti42 Dec 16, 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
2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@starknet-io/get-starknet-example",
"version": "4.0.2",
"version": "4.0.3",
"private": true,
"type": "module",
"scripts": {
Expand Down
115 changes: 90 additions & 25 deletions e2e/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,62 @@ import {
connect,
disconnect,
} from "@starknet-io/get-starknet"
import { useState } from "react"
import { WalletAccount, constants, hash } from "starknet"
import { useEffect, useState } from "react"
import { WalletAccount, constants } from "starknet"

// adjust path as needed

type Chains = Record<string, string>

const chains: Chains = {
[constants.StarknetChainId.SN_MAIN]: "main",
[constants.StarknetChainId.SN_SEPOLIA]: "testnet",
}

function App() {
const [walletName, setWalletName] = useState("")
const [wallet, setWallet] = useState<StarknetWindowObject | null>(null)
const [walletAccount, setWalletAccount] = useState<WalletAccount | null>(null)
const [walletAddress, setWalletAddress] = useState<string | null>(null)
const [result, setResult] = useState<string | boolean>("") // State to hold the result for display
const [walletChainId, setWalletChainId] = useState<string | null>(null)
const [result, setResult] = useState<string | boolean>("")

async function handleConnect(options?: ConnectOptions) {
const selectedWalletSWO = await connect(options)

if (selectedWalletSWO) {
const myFrontendProviderUrl =
"https://free-rpc.nethermind.io/sepolia-juno/v0_7"
"https://free-rpc.nethermind.io/mainnet-juno/v0_7"
const myWalletAccount = new WalletAccount(
{ nodeUrl: myFrontendProviderUrl },
selectedWalletSWO,
)
setWalletAccount(myWalletAccount)
setWallet(selectedWalletSWO)
setWalletChainId(
await selectedWalletSWO.request({
type: "wallet_requestChainId",
}),
)
setWalletAddress(myWalletAccount.address)
setWalletName(selectedWalletSWO?.name || "")
// Set the wallet globally if needed
window.wallet = myWalletAccount
window.swo = selectedWalletSWO
// myWalletAccount.walletProvider.on("networkChanged", (res,acc) => {
// console.log(res,acc)
// })
}

setWalletName(selectedWalletSWO?.name || "")
}

async function handleDisconnect(options?: DisconnectOptions) {
await disconnect(options)
setWalletName("")
setWallet(null)
setWalletAddress(null)
setResult("") // Clear result on disconnect
setWalletAccount(null)
setWalletChainId(null)
setResult("")
}

const tokenToWatch = {
Expand All @@ -55,6 +76,28 @@ function App() {
},
}

useEffect(() => {
if (!wallet) return

const handleNetworkChange = (chainId: any, accounts: any) => {
setWalletAddress(accounts[0])
setWalletChainId(chainId)
}

const handleAccountChange = (accounts: any) => {
setWalletAddress(accounts[0])
}

wallet.on("networkChanged", handleNetworkChange)
wallet.on("accountsChanged", handleAccountChange)

// Manual cleanup strategy: reset listeners on new `walletAccount`
// return () => {
// walletAccount.onNetworkChanged(() => {}) // Replaces the listener with a no-op
// walletAccount.onAccountChange(() => {}) // Replaces the listener with a no-op
// }
}, [wallet])

const methods = [
{
name: "wallet_watchAsset",
Expand Down Expand Up @@ -91,10 +134,20 @@ function App() {
accountWallet: async () => {
if (walletAccount) {
try {
const targetChainId =
walletChainId === constants.StarknetChainId.SN_SEPOLIA
? constants.StarknetChainId.SN_MAIN
: constants.StarknetChainId.SN_SEPOLIA

const response = await walletAccount.switchStarknetChain(
constants.StarknetChainId.SN_SEPOLIA,
targetChainId,
)
setResult(response) // Display plain text
setWalletChainId(targetChainId) // Update the local state
const accounts = await walletAccount?.requestAccounts()
if (accounts) {
setWalletAddress(accounts[0])
}
} catch (error: any) {
console.log(error)
setResult(`Error: ${error.message}`)
Expand All @@ -104,11 +157,21 @@ function App() {
rpc: async () => {
if (wallet) {
try {
const targetChainId =
walletChainId === constants.StarknetChainId.SN_SEPOLIA
? constants.StarknetChainId.SN_MAIN
: constants.StarknetChainId.SN_SEPOLIA

const response = await wallet.request({
type: "wallet_switchStarknetChain",
params: { chainId: constants.StarknetChainId.SN_SEPOLIA },
params: { chainId: targetChainId },
})
setResult(response) // Display plain text
setWalletChainId(targetChainId) // Update the local state
const accounts = await walletAccount?.requestAccounts()
if (accounts) {
setWalletAddress(accounts[0])
}
} catch (error: any) {
console.log(error)
setResult(`Error: ${error.message}`)
Expand All @@ -119,17 +182,18 @@ function App() {
{
name: "wallet_requestChainId",
label: "Request Chain ID",
accountWallet: async () => {
if (walletAccount) {
try {
const response = await walletAccount.getChainId()
setResult(response) // Display plain text
} catch (error: any) {
console.log(error)
setResult(`Error: ${error.message}`)
}
}
},
accountWallet: null,
// accountWallet: async () => {
// if (walletAccount) {
// try {
// const response = await walletAccount.getChainId()
// setResult(response) // Display plain text
// } catch (error: any) {
// console.log(error)
// setResult(`Error: ${error.message}`)
// }
// }
// },
rpc: async () => {
if (wallet) {
try {
Expand Down Expand Up @@ -192,7 +256,6 @@ function App() {
label: "Add Invoke Transaction",
accountWallet: null, // Not implemented
rpc: async () => {
console.log(walletAddress)
if (wallet && walletAddress) {
try {
const response = await wallet.request({
Expand Down Expand Up @@ -374,11 +437,13 @@ function App() {
</div>
</div>

{walletName && (
{walletName && walletChainId && (
<div>
<h2>
Selected Wallet: <pre>{walletName}</pre>
</h2>
<h4>
<pre>Wallet: {walletName}</pre>
<pre>Chain: {chains[walletChainId]}</pre>
<pre>Address: {walletAddress}</pre>
</h4>
<div className="method-table">
<div className="table-header">
<span>Method Name</span>
Expand Down
18 changes: 18 additions & 0 deletions example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# @starknet-io/get-starknet-example

## 4.0.5

### Patch Changes

- @starknet-io/get-starknet@4.0.5

## 4.0.4

### Patch Changes

- @starknet-io/get-starknet@4.0.4

## 4.0.3

### Patch Changes

- @starknet-io/get-starknet@4.0.3

## 4.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@starknet-io/get-starknet-example",
"version": "4.0.2",
"version": "4.0.5",
"private": true,
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
"prettier-plugin-packagejson": "^2.2.18",
"prettier-plugin-svelte": "^2.7.0"
},
"packageManager": "pnpm@9.1.1+sha512.14e915759c11f77eac07faba4d019c193ec8637229e62ec99eefb7cf3c3b75c64447882b7c485142451ee3a6b408059cdfb7b7fa0341b975f12d0f7629c71195"
"packageManager": "pnpm@8.0.0"
}
21 changes: 21 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# @starknet-io/get-starknet-core

## 4.0.5

### Patch Changes

- 0263b88: Decouple Virtual Wallet Discovery for async workflow

## 4.0.4

### Patch Changes

- d2fee96: Fix loading MetaMask Virtual Wallet dynamically in WalletAccount and
add support for RPC APIs (wallet_supportedWalletApi and wallet_supportedSpecs)
in get-starknet v4 integration.

## 4.0.3

### Patch Changes

- 93f3705: Fix MetaMask provider detection and handling in get-starknet v4
integration.

## 4.0.2

### Patch Changes
Expand Down
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@starknet-io/get-starknet-core",
"version": "4.0.2",
"version": "4.0.5",
"keywords": [
"starknet",
"starkware",
Expand Down Expand Up @@ -31,7 +31,8 @@
},
"dependencies": {
"@module-federation/runtime": "^0.1.2",
"@starknet-io/types-js": "^0.7.7"
"@starknet-io/types-js": "^0.7.7",
"async-mutex": "^0.5.0"
},
"devDependencies": {
"c8": "^7.12.0",
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { sortBy } from "./wallet/sort"
import {
initiateVirtualWallets,
resolveVirtualWallet,
virtualWallets,
} from "./wallet/virtualWallets"
import { Permission, type StarknetWindowObject } from "@starknet-io/types-js"

Expand Down Expand Up @@ -116,6 +117,29 @@ export function getStarknet(

return firstAuthorizedWallet
},
discoverVirtualWallets: async (
walletNamesOrIds: string[] = [],
): Promise<void> => {
const walletNamesOrIdsSet = new Set(walletNamesOrIds)

const virtualWalletToDiscover =
walletNamesOrIdsSet.size > 0
? virtualWallets.filter(
(virtualWallet) =>
walletNamesOrIdsSet.has(virtualWallet.name) ||
walletNamesOrIdsSet.has(virtualWallet.id),
)
: virtualWallets

await Promise.all(
virtualWalletToDiscover.map(async (virtualWallet) => {
const hasSupport = await virtualWallet.hasSupport(windowObject)
if (hasSupport) {
windowObject[virtualWallet.windowKey] = virtualWallet
}
}),
)
},
enable: async (inputWallet, options) => {
let wallet: StarknetWindowObject
if (isVirtualWallet(inputWallet)) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface GetStarknetResult {
) => Promise<StarknetWindowObject[]> // Returns only preauthorized wallets available in the window object
getDiscoveryWallets: (options?: GetWalletOptions) => Promise<WalletProvider[]> // Returns all wallets in existence (from discovery file)
getLastConnectedWallet: () => Promise<StarknetWindowObject | null | undefined> // Returns the last wallet connected when it's still connected
discoverVirtualWallets: () => Promise<void> // Discovers the virtual wallets by calling their hasSupport methods
enable: (
wallet: StarknetWindowObject | VirtualWallet,
options?: RequestAccountsParameters,
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/wallet/virtualWallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const virtualWallets: VirtualWallet[] = [metaMaskVirtualWallet]

function initiateVirtualWallets(windowObject: Record<string, unknown>) {
virtualWallets.forEach(async (virtualWallet) => {
const hasSupport = await virtualWallet.hasSupport(windowObject)
if (hasSupport) {
windowObject[virtualWallet.windowKey] = virtualWallet
if (!(virtualWallet.windowKey in windowObject)) {
const hasSupport = await virtualWallet.hasSupport(windowObject)
if (hasSupport) {
windowObject[virtualWallet.windowKey] = virtualWallet
}
}
})
}
Expand All @@ -28,4 +30,4 @@ async function resolveVirtualWallet(
return wallet
}

export { initiateVirtualWallets, resolveVirtualWallet }
export { initiateVirtualWallets, resolveVirtualWallet, virtualWallets }
Loading