Skip to content

Commit

Permalink
fix: update starknetkit to be compatible with starknet-react v3
Browse files Browse the repository at this point in the history
  • Loading branch information
bluecco committed Jul 1, 2024
1 parent a03939d commit bf871d2
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 199 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
"dev": "vite build --watch"
},
"dependencies": {
"@starknet-io/get-starknet": "^4.0.0",
"@starknet-io/types-js": "^0.7.7",
"@trpc/client": "^10.38.1",
"@trpc/server": "^10.38.1",
"@walletconnect/sign-client": "^2.11.0",
Expand Down Expand Up @@ -99,6 +101,8 @@
"prettier-plugin-import-sort": "^0.0.7",
"semantic-release": "^21.1.1",
"starknet-types": "^0.7.2",
"starknet4": "npm:[email protected]",
"starknet5": "npm:[email protected]",
"svelte": "^4.0.0",
"svelte-check": "^3.5.1",
"svelte-preprocess": "^5.0.4",
Expand All @@ -109,9 +113,7 @@
"vite-plugin-dts": "^3.0.0",
"vitest": "^1.6.0",
"ws": "^8.8.1",
"zod": "^3.20.6",
"starknet4": "npm:[email protected]",
"starknet5": "npm:[email protected]"
"zod": "^3.20.6"
},
"peerDependencies": {
"starknet": "^6.9.0"
Expand Down
39 changes: 39 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
"timezone": "Europe/London",
"rebaseWhen": "never",
"ignoreDeps": [
"starknet-types",
"starknet",
"starknet4-deprecated",
"starknet4",
"starknet5",
"get-starknet-core",
"get-starknet-coreV3"
"get-starknet-coreV3",
"starknet-types",
"@starknet-io/types-js",
"@starknet-io/get-starknet"
],
"packageRules": [
{
Expand Down
48 changes: 37 additions & 11 deletions src/connectors/argentMobile/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { type AccountChangeEventHandler } from "get-starknet-core"
import { constants } from "starknet"
import { Permission, type StarknetWindowObject } from "starknet-types"
import {
AccountInterface,
ProviderInterface,
ProviderOptions,
WalletAccount,
constants,
} from "starknet"
import {
Permission,
RequestFnCall,
RpcMessage,
RpcTypeToMessageMap,
type StarknetWindowObject,
} from "starknet-types"
import {
ConnectorNotConnectedError,
ConnectorNotFoundError,
Expand Down Expand Up @@ -110,20 +122,17 @@ export class ArgentMobileConnector extends Connector {
this._wallet = null
}

async account(): Promise<string | null> {
async account(
provider: ProviderOptions | ProviderInterface,
): Promise<AccountInterface> {
if (!this._wallet) {
throw new ConnectorNotConnectedError()
}

const [account] = await this._wallet.request({
type: "wallet_requestAccounts",
params: { silent_mode: true },
})

return account ?? null
return new WalletAccount(provider, this._wallet)
}

async chainId(): Promise<constants.StarknetChainId> {
async chainId(): Promise<bigint> {
if (!this._wallet) {
throw new ConnectorNotConnectedError()
}
Expand All @@ -132,7 +141,24 @@ export class ArgentMobileConnector extends Connector {
type: "wallet_requestChainId",
})

return getStarknetChainId(chainId)
const hexChainId = getStarknetChainId(chainId)
return BigInt(hexChainId)
}

async request<T extends RpcMessage["type"]>(
call: RequestFnCall<T>,
): Promise<RpcTypeToMessageMap[T]["result"]> {
this.ensureWallet()

if (!this._wallet) {
throw new ConnectorNotConnectedError()
}

try {
return await this._wallet.request(call)
} catch {
throw new UserRejectedRequestError()
}
}

// needed, methods required by starknet-react. Otherwise an exception is throwd
Expand Down
31 changes: 20 additions & 11 deletions src/connectors/connector.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import EventEmitter from "eventemitter3"
import { constants } from "starknet"
import type { StarknetWindowObject } from "starknet-types"
import { AccountInterface, ProviderInterface, ProviderOptions } from "starknet"
import type {
RequestFnCall,
RpcMessage,
RpcTypeToMessageMap,
StarknetWindowObject,
} from "starknet-types"

/** Connector icons, as base64 encoded svg. */
export type ConnectorIcons = {
/** Dark-mode icon. */
dark?: string
/** Light-mode icon. */
light?: string
}
export type ConnectorIcons = StarknetWindowObject["icon"]

/** Connector data. */
export type ConnectorData = {
/** Connector account. */
account?: string
/** Connector network. */
chainId?: constants.StarknetChainId
chainId?: bigint
}

/** Connector events. */
Expand Down Expand Up @@ -45,9 +45,18 @@ export abstract class Connector extends EventEmitter<ConnectorEvents> {
/** Disconnect wallet. */
abstract disconnect(): Promise<void>
/** Get current account silently. Return null if the account is not authorized */
abstract account(): Promise<string | null>
abstract account(
provider: ProviderOptions | ProviderInterface,
): Promise<AccountInterface>
/** Get current chain id. */
abstract chainId(): Promise<constants.StarknetChainId>
abstract chainId(): Promise<bigint>
/** Create request call to wallet */
abstract request<T extends RpcMessage["type"]>(
call: RequestFnCall<T>,
): Promise<RpcTypeToMessageMap[T]["result"]>
}

export abstract class StarknetkitConnector extends Connector {
/** Connector StarknetWindowObject */
abstract get wallet(): StarknetWindowObject
}
Loading

0 comments on commit bf871d2

Please sign in to comment.