Skip to content

Commit

Permalink
Merge pull request #379 from blocto/feat/add-wagmi-v2
Browse files Browse the repository at this point in the history
Feat/add wagmi v2
  • Loading branch information
q20274982 authored Jan 18, 2024
2 parents c31446a + 7dc3168 commit 7ace922
Show file tree
Hide file tree
Showing 14 changed files with 1,027 additions and 252 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-colts-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blocto/wagmi-connector': major
---

blocto connector support wagmi v2
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Setup Node.js 16
- name: Setup Node.js 18
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18

- name: Install Dependencies
run: yarn
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ jobs:
restore-keys: |
${{ runner.os }}-turbo-
- name: Setup Node.js 16
- name: Setup Node.js 18
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18

- name: Install Dependencies
run: yarn
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
restore-keys: |
${{ runner.os }}-turbo-
- name: Setup Node.js 16
- name: Setup Node.js 18
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18

- name: Install dependencies
run: yarn
Expand Down
2 changes: 1 addition & 1 deletion adapters/connectkit-connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"/dist"
],
"dependencies": {
"@blocto/wagmi-connector": "^1.3.1"
"@blocto/wagmi-connector": "npm:@blocto/wagmi-connector@^1.3.1"
},
"peerDependencies": {
"connectkit": "^1.5.3",
Expand Down
2 changes: 1 addition & 1 deletion adapters/rainbowkit-connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@rainbow-me/rainbowkit": "^1.0.8",
"@blocto/wagmi-connector": "^1.3.1"
"@blocto/wagmi-connector": "npm:@blocto/wagmi-connector@^1.3.1"
},
"devDependencies": {
"@types/jest": "^29.5.2",
Expand Down
14 changes: 9 additions & 5 deletions adapters/wagmi-connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
},
"scripts": {
"build": "tsc && vite build",
"lint": "eslint src --ext .ts"
"lint": "eslint src --ext .ts",
"test": "vitest run --coverage"
},
"devDependencies": {
"@types/node": "^20.2.5",
"@vitest/coverage-v8": "^1.2.0",
"@wagmi/chains": "^1.8.0",
"@wagmi/core": "^2.2.0",
"typescript": "^5.1.3",
"viem": "^2.x",
"vite": "^4.3.9",
"vite-plugin-dts": "^2.3.0",
"@wagmi/core": "^1.3.9",
"viem": "^1.5.4"
"vitest": "^1.2.0"
},
"files": [
"/dist"
Expand All @@ -31,7 +35,7 @@
"@blocto/sdk": "^0.9.1"
},
"peerDependencies": {
"@wagmi/core": ">=1",
"viem": ">=1"
"@wagmi/core": "^2.2.0",
"viem": "2.x"
}
}
147 changes: 147 additions & 0 deletions adapters/wagmi-connector/src/connector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* @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 { normalizeChainId } from '@wagmi/core';
import { blocto } from './index';

vi.mock('viem');

describe('blocto-connector', () => {
let connector: any;
beforeEach(() => {
const config = createConfig({
chains: [polygonMumbai, arbitrumGoerli],
pollingInterval: 100,
storage: null,
transports: {
[polygonMumbai.id]: viem.http(),
[arbitrumGoerli.id]: viem.http(),
},
});

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

afterEach(() => {
connector = null;
});

test('setup', () => {
expect(connector.name).toEqual('Blocto');
});

test('connect', async () => {
const chainId = 1;
const accounts = ['0xc61B4Aa62E5FD40cceB08C602Eb5D157b257b49a'];
const provider = {
request: vi.fn().mockResolvedValue(accounts),
};
connector.getProvider = vi.fn().mockResolvedValue(provider);
connector.getAccounts = vi.fn().mockResolvedValue(accounts);
connector.getChainId = vi.fn().mockResolvedValue(chainId);

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

expect(result).toEqual({ accounts, chainId });
expect(connector.getProvider).toHaveBeenCalledWith({ chainId });
expect(provider.request).toHaveBeenCalledWith({
method: 'eth_requestAccounts',
});
});

test('disconnect', async () => {
const provider = {
request: vi.fn().mockResolvedValue(undefined),
};
connector.getProvider = vi.fn().mockResolvedValue(provider);

await connector.disconnect();

expect(connector.getProvider).toHaveBeenCalled();
expect(provider.request).toHaveBeenCalledWith({
method: 'wallet_disconnect',
});
});

test('getAccounts', async () => {
const accounts = ['0xc61B4Aa62E5FD40cceB08C602Eb5D157b257b49a'];
const provider = {
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();

expect(result).toEqual(['0xc61B4Aa62E5FD40cceB08C602Eb5D157b257b49a']);
expect(connector.getProvider).toHaveBeenCalled();
expect(provider.request).toHaveBeenCalledWith({ method: 'eth_accounts' });
});

test('getChainId', async () => {
const chainId = '0x1';
const provider = {
chainId: undefined,
request: vi.fn().mockResolvedValue(chainId),
};
connector.getProvider = vi.fn().mockResolvedValue(provider);

const result = await connector.getChainId();

expect(result).toEqual(normalizeChainId(chainId));
expect(connector.getProvider).toHaveBeenCalled();
expect(provider.request).toHaveBeenCalledWith({ method: 'eth_chainId' });
});

test('isAuthorized', async () => {
const accounts = ['0xc61B4Aa62E5FD40cceB08C602Eb5D157b257b49a'];
connector.getAccounts = vi.fn().mockResolvedValue(accounts);

const result = await connector.isAuthorized();

expect(result).toEqual(false);
});

test('switchChain', async () => {
const chainId = arbitrumGoerli.id;
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);
vi.spyOn(viem, 'numberToHex').mockReturnValue(viem.numberToHex(chainId));

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

expect(connector.getProvider).toHaveBeenCalled();
expect(provider.request).toHaveBeenCalledWith({
method: 'wallet_addEthereumChain',
params: [
{
chainId: viem.numberToHex(chainId),
rpcUrls: arbitrumGoerli.rpcUrls.default.http,
},
],
});
expect(provider.request).toHaveBeenCalledWith({
method: 'wallet_switchEthereumChain',
params: [
{
chainId: viem.numberToHex(chainId),
},
],
});
expect(chain.id).toEqual(chainId);
});
});
Loading

0 comments on commit 7ace922

Please sign in to comment.