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

Fix/ adjust getAddress #396

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/empty-candles-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blocto/wagmi-connector': patch
---

adjust the usage of getAddress function
109 changes: 89 additions & 20 deletions adapters/wagmi-connector/src/connector.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/**
* @vitest-environment jsdom
*/

import { expect, test, describe, vi, beforeEach, afterEach } from 'vitest';
import { createConfig } from '@wagmi/core';
import { polygonMumbai, arbitrumGoerli } from '@wagmi/chains';
import * as viem from 'viem';
import { SwitchChainError, http, numberToHex } from 'viem';
import { normalizeChainId } from '@wagmi/core';
import { blocto } from './index';
import { blocto } from './';

vi.mock('viem');
const walletProvider = {
on: vi.fn(),
};

describe('blocto-connector', () => {
let connector: any;
Expand All @@ -19,13 +17,11 @@ describe('blocto-connector', () => {
pollingInterval: 100,
storage: null,
transports: {
[polygonMumbai.id]: viem.http(),
[arbitrumGoerli.id]: viem.http(),
[polygonMumbai.id]: http(),
[arbitrumGoerli.id]: http(),
},
});

const connectorFn = blocto();
connector = config._internal.connectors.setup(connectorFn);
connector = config._internal.connectors.setup(blocto());
});

afterEach(() => {
Expand Down Expand Up @@ -55,6 +51,19 @@ describe('blocto-connector', () => {
});
});

test('catch error when user decline to connect', () => {
const chainId = 1;
const userRejectedRequest = new Error('User rejected request');
const provider = {
request: vi.fn().mockImplementation(() => {
throw userRejectedRequest;
}),
};
connector.getProvider = vi.fn().mockResolvedValue(provider);

expect(connector.connect({ chainId })).rejects.toThrow(userRejectedRequest);
});

test('disconnect', async () => {
const provider = {
request: vi.fn().mockResolvedValue(undefined),
Expand All @@ -75,7 +84,6 @@ describe('blocto-connector', () => {
request: vi.fn().mockResolvedValue(accounts),
};
connector.getProvider = vi.fn().mockResolvedValue(provider);
vi.spyOn(viem, 'getAddress').mockImplementation((x) => x as `0x${string}`);

const result = await connector.getAccounts();

Expand All @@ -99,6 +107,30 @@ describe('blocto-connector', () => {
expect(provider.request).toHaveBeenCalledWith({ method: 'eth_chainId' });
});

test('getProvider', async () => {
vi.mock('@blocto/sdk', () => ({
default: vi.fn().mockImplementation(() => ({
ethereum: walletProvider,
})),
}));

const chainId = 1;
const result = await connector.getProvider({ chainId });

expect(result).toEqual(walletProvider);
expect(walletProvider.on).toHaveBeenCalledWith(
'accountsChanged',
expect.any(Function)
);
expect(walletProvider.on).toHaveBeenCalledWith(
'chainChanged',
expect.any(Function)
);
expect(walletProvider.on).toHaveBeenCalledWith(
'disconnect',
expect.any(Function)
);
});
test('isAuthorized', async () => {
const accounts = ['0xc61B4Aa62E5FD40cceB08C602Eb5D157b257b49a'];
connector.getAccounts = vi.fn().mockResolvedValue(accounts);
Expand All @@ -120,7 +152,6 @@ describe('blocto-connector', () => {
),
};
connector.getProvider = vi.fn().mockResolvedValue(provider);
vi.spyOn(viem, 'numberToHex').mockReturnValue(viem.numberToHex(chainId));

const chain = await connector.switchChain({ chainId });

Expand All @@ -129,19 +160,57 @@ describe('blocto-connector', () => {
method: 'wallet_addEthereumChain',
params: [
{
chainId: viem.numberToHex(chainId),
chainId: numberToHex(chainId),
rpcUrls: arbitrumGoerli.rpcUrls.default.http,
},
],
});
expect(provider.request).toHaveBeenCalledWith({
method: 'wallet_switchEthereumChain',
params: [
{
chainId: viem.numberToHex(chainId),
},
],
params: [{ chainId: numberToHex(chainId) }],
});
expect(chain.id).toEqual(chainId);
});

test('catch error when switching to unConfigured chain', async () => {
const unConfiguredChainId = 111111;
const provider = {
request: vi.fn().mockResolvedValue(undefined),
supportChainList: vi.fn().mockResolvedValue(
[polygonMumbai, arbitrumGoerli].map(({ id, name }) => ({
chainId: id,
chainName: name,
}))
),
};
connector.getProvider = vi.fn().mockResolvedValue(provider);
const expectError = new SwitchChainError(
new Error(`Chain not in config: ${numberToHex(unConfiguredChainId)}`)
);

expect(
connector.switchChain({ chainId: unConfiguredChainId })
).rejects.toThrow(expectError);
});

test('catch error when switching to blocto unsupported chain', async () => {
const unsupportedChainId = arbitrumGoerli.id;
const provider = {
request: vi.fn().mockResolvedValue(undefined),
supportChainList: vi.fn().mockResolvedValue(
[polygonMumbai].map(({ id, name }) => ({
chainId: id,
chainName: name,
}))
),
};
connector.getProvider = vi.fn().mockResolvedValue(provider);
const expectError = new SwitchChainError(
new Error(`Blocto unsupported chain: ${numberToHex(unsupportedChainId)}`)
);

expect(
connector.switchChain({ chainId: unsupportedChainId })
).rejects.toThrow(expectError);
});
});
3 changes: 1 addition & 2 deletions adapters/wagmi-connector/src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function blocto({ appId }: BloctoParameters = {}) {
method: 'eth_accounts',
})) as string[];

return accounts.map(getAddress);
return accounts.map((x) => getAddress(x));
},
async getChainId() {
const provider = await this.getProvider();
Expand All @@ -89,7 +89,6 @@ export function blocto({ appId }: BloctoParameters = {}) {
};

walletProvider = new BloctoSDK({ ethereum, appId })?.ethereum;

if (!walletProvider) {
throw new Error('Blocto SDK is not initialized.');
}
Expand Down
Loading