-
Notifications
You must be signed in to change notification settings - Fork 119
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
Implement FCL Ethereum Provider chainChanged
Event
#2096
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
packages/fcl-ethereum-provider/src/network/network-manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import {mockConfig} from "../__mocks__/fcl" | ||
import {NetworkManager} from "./network-manager" | ||
import * as fcl from "@onflow/fcl" | ||
|
||
jest.mock("@onflow/fcl", () => { | ||
return { | ||
getChainId: jest.fn(), | ||
} | ||
}) | ||
|
||
describe("network manager", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test("getChainId should return correct chain id mainnet", async () => { | ||
jest.mocked(fcl.getChainId).mockResolvedValue("mainnet") | ||
|
||
const config = mockConfig() | ||
await config.set({ | ||
"accessNode.api": "https://example.com", | ||
}) | ||
const manager = new NetworkManager(config.mock) | ||
const chainId = await manager.getChainId() | ||
|
||
expect(chainId).toBe(747) | ||
}) | ||
|
||
test("getChainId should return correct chain id testnet", async () => { | ||
jest.mocked(fcl.getChainId).mockResolvedValue("testnet") | ||
|
||
const config = mockConfig() | ||
await config.set({ | ||
"accessNode.api": "https://example.com", | ||
}) | ||
const manager = new NetworkManager(config.mock) | ||
const chainId = await manager.getChainId() | ||
|
||
expect(chainId).toBe(646) | ||
}) | ||
|
||
test("getChainId should throw error on unknown network", async () => { | ||
jest.mocked(fcl.getChainId).mockResolvedValue("unknown") | ||
|
||
const config = mockConfig() | ||
await config.set({ | ||
"accessNode.api": "https://example.com", | ||
}) | ||
|
||
const manager = new NetworkManager(config.mock) | ||
await expect(manager.getChainId()).rejects.toThrow("Unknown network") | ||
}) | ||
|
||
test("getChainId should throw error on error", async () => { | ||
jest.mocked(fcl.getChainId).mockRejectedValue(new Error("error")) | ||
|
||
const config = mockConfig() | ||
await config.set({ | ||
"accessNode.api": "https://example.com", | ||
}) | ||
|
||
const manager = new NetworkManager(config.mock) | ||
await expect(manager.getChainId()).rejects.toThrow("error") | ||
}) | ||
|
||
test("subscribe should return correct chain id", async () => { | ||
jest.mocked(fcl.getChainId).mockResolvedValue("mainnet") | ||
|
||
const config = mockConfig() | ||
await config.set({ | ||
"accessNode.api": "https://example.com", | ||
}) | ||
|
||
const manager = new NetworkManager(config.mock) | ||
const chainId = await new Promise<number>(resolve => { | ||
const unsub = manager.subscribe(id => { | ||
if (id) { | ||
resolve(id) | ||
unsub() | ||
} | ||
}) | ||
}) | ||
|
||
expect(chainId).toBe(747) | ||
}) | ||
|
||
test("subscribe should update chain id", async () => { | ||
jest.mocked(fcl.getChainId).mockResolvedValue("mainnet") | ||
|
||
const config = mockConfig() | ||
await config.set({ | ||
"accessNode.api": "https://example.com", | ||
}) | ||
|
||
const manager = new NetworkManager(config.mock) | ||
|
||
const chainIds: number[] = [] | ||
|
||
const unsub = manager.subscribe(id => { | ||
if (id) { | ||
chainIds.push(id) | ||
} | ||
}) | ||
|
||
await config.set({ | ||
"accessNode.api": "https://example2.com", | ||
}) | ||
|
||
await config.set({ | ||
"accessNode.api": "https://example3.com", | ||
}) | ||
|
||
unsub() | ||
|
||
expect(chainIds).toEqual([747, 747, 747]) | ||
expect(fcl.getChainId).toHaveBeenCalledTimes(3) | ||
}) | ||
|
||
test("should not query chain id multiple times for same access node", async () => { | ||
jest.mocked(fcl.getChainId).mockResolvedValue("mainnet") | ||
|
||
const config = mockConfig() | ||
await config.set({ | ||
"accessNode.api": "https://example.com", | ||
}) | ||
|
||
const manager = new NetworkManager(config.mock) | ||
|
||
await manager.getChainId() | ||
await manager.getChainId() | ||
await manager.getChainId() | ||
|
||
expect(fcl.getChainId).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
88 changes: 88 additions & 0 deletions
88
packages/fcl-ethereum-provider/src/network/network-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {ACCESS_NODE_API_KEY, FLOW_CHAINS, FlowNetwork} from "../constants" | ||
import { | ||
BehaviorSubject, | ||
concat, | ||
distinctUntilChanged, | ||
filter, | ||
firstValueFrom, | ||
from, | ||
map, | ||
of, | ||
Subscription, | ||
switchMap, | ||
} from "../util/observable" | ||
import * as fcl from "@onflow/fcl" | ||
|
||
type ChainIdStore = { | ||
isLoading: boolean | ||
chainId: number | null | ||
error: unknown | null | ||
} | ||
export class NetworkManager { | ||
private $chainIdStore = new BehaviorSubject<ChainIdStore>({ | ||
isLoading: true, | ||
chainId: null, | ||
error: null, | ||
}) | ||
|
||
constructor(config: typeof fcl.config) { | ||
// Map FCL config to behavior subject | ||
const $config = new BehaviorSubject<Record<string, unknown> | null>(null) | ||
config.subscribe((cfg, err) => { | ||
if (err) { | ||
$config.error(err) | ||
} else { | ||
$config.next(cfg) | ||
} | ||
}) | ||
|
||
// Bind $network to chainId | ||
$config | ||
.pipe( | ||
map(cfg => cfg?.[ACCESS_NODE_API_KEY]), | ||
distinctUntilChanged(), | ||
switchMap(accessNode => | ||
concat( | ||
of({isLoading: true} as ChainIdStore), | ||
from( | ||
(async () => { | ||
try { | ||
const flowNetwork = (await fcl.getChainId({ | ||
node: accessNode, | ||
})) as FlowNetwork | ||
if (!(flowNetwork in FLOW_CHAINS)) { | ||
throw new Error("Unknown network") | ||
} | ||
const {eip155ChainId: chainId} = FLOW_CHAINS[flowNetwork] | ||
return {isLoading: false, chainId, error: null} | ||
} catch (error) { | ||
return {isLoading: false, chainId: null, error} | ||
} | ||
})() | ||
) | ||
) | ||
) | ||
) | ||
.subscribe(this.$chainIdStore) | ||
} | ||
|
||
public subscribe(callback: (chainId: number | null) => void): Subscription { | ||
return this.$chainIdStore | ||
.pipe( | ||
filter(x => !x.isLoading && !x.error), | ||
map(x => x.chainId) | ||
) | ||
.subscribe(callback) | ||
} | ||
|
||
public async getChainId(): Promise<number | null> { | ||
const {chainId, error} = await firstValueFrom( | ||
this.$chainIdStore.pipe(filter(x => !x.isLoading)) | ||
) | ||
|
||
if (error) { | ||
throw error | ||
} | ||
return chainId | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you supposed to throw errors in observables vs do something like
$config.next({ error: err })
? Do you want it to terminate?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case errors are fatal, so we should propogate this through the observable chain