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

Setup eth accounts handler #2079

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 packages/fcl-ethereum-provider/src/create-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function createProvider(config: {
gateway?: string
}): Eip1193Provider {
const accountManager = new AccountManager(config.user)
const rpcProcessor = new RpcProcessor()
const rpcProcessor = new RpcProcessor(accountManager)
const eventProcessor = new EventDispatcher()
const provider = new FclEthereumProvider(rpcProcessor, eventProcessor)
return provider
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {ethAccounts} from "./eth-accounts"
import {AccountManager} from "../../accounts/account-manager"

describe("ethAccounts handler", () => {
let accountManagerMock: jest.Mocked<AccountManager>

beforeEach(() => {
accountManagerMock = {
getAccounts: jest.fn(),
subscribe: jest.fn(),
} as unknown as jest.Mocked<AccountManager>
});

it("should return accounts from the AccountManager", () => {
accountManagerMock.getAccounts.mockReturnValue(["0x1234...", "0x5678..."])

const accounts = ethAccounts(accountManagerMock)

expect(accounts).toEqual(["0x1234...", "0x5678..."])
expect(accountManagerMock.getAccounts).toHaveBeenCalled()
});

it("should return an empty array if no accounts are available", () => {
accountManagerMock.getAccounts.mockReturnValue([])

const accounts = ethAccounts(accountManagerMock)

expect(accounts).toEqual([])
expect(accountManagerMock.getAccounts).toHaveBeenCalled()
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function eth_accounts() {
throw new Error("Not implemented")
import {AccountManager} from "../../accounts/account-manager"

export function ethAccounts(accountManager: AccountManager): string[] {
return accountManager.getAccounts()
}

export function ethRequestAccounts() {
throw new Error("Not implemented")
}
11 changes: 5 additions & 6 deletions packages/fcl-ethereum-provider/src/rpc/rpc-processor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {ProviderRequest} from "../types/provider"
import {eth_accounts} from "./handlers/eth-accounts"

const handlers = {
eth_accounts,
}
import {ethAccounts} from "./handlers/eth-accounts"
import {AccountManager} from "../accounts/account-manager"

export class RpcProcessor {
constructor() {}
constructor(private accountManager: AccountManager) {}

async handleRequest({method, params}: ProviderRequest): Promise<any> {
switch (method) {
case "eth_accounts":
return ethAccounts(this.accountManager)
case "eth_requestAccounts":
throw new Error("Not implemented")
default:
Expand Down
Loading