bump frame-sdk version

This commit is contained in:
Tony D'Addeo
2024-12-04 16:21:58 -06:00
committed by lucas-neynar
parent 0196931aed
commit e5a55c463b
3 changed files with 73 additions and 33 deletions

View File

@@ -1,9 +1,13 @@
import sdk from "@farcaster/frame-sdk";
import { SwitchChainError, fromHex, getAddress, numberToHex } from "viem";
import { ChainNotConfiguredError, createConnector } from "wagmi";
import { ChainNotConfiguredError, Connector, createConnector } from "wagmi";
frameConnector.type = "frameConnector" as const;
let accountsChanged: Connector['onAccountsChanged'] | undefined
let chainChanged: Connector['onChainChanged'] | undefined
let disconnect: Connector['onDisconnect'] | undefined
export function frameConnector() {
let connected = true;
@@ -21,6 +25,20 @@ export function frameConnector() {
method: "eth_requestAccounts",
});
if (!accountsChanged) {
accountsChanged = this.onAccountsChanged.bind(this)
// @ts-expect-error - provider type is stricter
provider.on('accountsChanged', accountsChanged)
}
if (!chainChanged) {
chainChanged = this.onChainChanged.bind(this)
provider.on('chainChanged', chainChanged)
}
if (!disconnect) {
disconnect = this.onDisconnect.bind(this)
provider.on('disconnect', disconnect)
}
let currentChainId = await this.getChainId();
if (chainId && currentChainId !== chainId) {
const chain = await this.switchChain!({ chainId });
@@ -35,6 +53,24 @@ export function frameConnector() {
};
},
async disconnect() {
const provider = await this.getProvider()
if (accountsChanged) {
// @ts-expect-error - provider type is stricter
provider.removeListener('accountsChanged', accountsChanged)
accountsChanged = undefined
}
if (chainChanged) {
provider.removeListener('chainChanged', chainChanged)
chainChanged = undefined
}
if (disconnect) {
provider.removeListener('disconnect', disconnect)
disconnect = undefined
}
connected = false;
},
async getAccounts() {
@@ -68,6 +104,8 @@ export function frameConnector() {
params: [{ chainId: numberToHex(chainId) }],
});
// providers should start emitting these events - remove when hosts have upgraded
//
// explicitly emit this event as a workaround for ethereum provider not
// emitting events, can remove once events are flowing
config.emitter.emit("change", { chainId });