-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FCL Ethereum Provider
chainChanged
Event (#2096)
- Loading branch information
Showing
8 changed files
with
303 additions
and
24 deletions.
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.