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: multiple chain setting #431

Merged
merged 9 commits into from
Oct 11, 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
6 changes: 6 additions & 0 deletions .changeset/cold-eggs-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@blocto/sdk': patch
---

fix story tesnet url, fix multiple chain setting, update polygon url

13 changes: 7 additions & 6 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
"mode": "exit",
"tag": "beta",
"initialVersions": {
"@blocto/aptos-wallet-adapter-plugin": "0.2.10",
"@blocto/aptos-wallet-adapter-plugin": "0.2.11-beta.0",
"@blocto/connectkit-connector": "0.2.3",
"@blocto/rainbowkit-connector": "2.0.2",
"@blocto/wagmi-connector": "2.0.4",
"@blocto/web3-react-connector": "1.0.8",
"@blocto/rainbowkit-connector": "2.0.3-beta.0",
"@blocto/wagmi-connector": "2.0.5-beta.0",
"@blocto/web3-react-connector": "1.0.9-beta.0",
"@blocto/web3modal-connector": "0.1.4",
"@blocto/sdk": "0.10.3",
"@blocto/sdk": "0.10.4-beta.1",
"@blocto/dappauth": "2.2.2",
"eslint-config-custom": "0.0.0",
"tsconfig": "0.0.0"
},
"changesets": [
"nasty-cats-greet"
"nasty-cats-greet",
"yellow-rockets-judge"
]
}
5 changes: 5 additions & 0 deletions .changeset/yellow-rockets-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blocto/sdk': patch
---

fix multiple chain setting
6 changes: 6 additions & 0 deletions packages/blocto-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @blocto/sdk

## 0.10.4-beta.1

### Patch Changes

- 2e2164e: fix multiple chain setting

## 0.10.4-beta.0

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/blocto-sdk/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
'^.+\\.(ts|tsx)?$': 'ts-jest',
'^.+\\.(js|jsx)$': 'babel-jest',
},
transformIgnorePatterns: ['node_modules/(?!variables/.*)'],
testPathIgnorePatterns: ['node_modules/(?!variables/.*)', '/fixtures/', '\\.d\\.ts$'],
automock: false,
resetMocks: false,
};
2 changes: 1 addition & 1 deletion packages/blocto-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@blocto/sdk",
"version": "0.10.4-beta.0",
"version": "0.10.4-beta.1",
"repository": "[email protected]:portto/blocto-sdk.git",
"author": "Chiaki.C",
"license": "MIT",
Expand Down
132 changes: 132 additions & 0 deletions packages/blocto-sdk/src/__tests__/ethereum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import EthereumProvider from '../providers/ethereum';
import { getEvmSupport } from '../lib/getEvmSupport';
import { getEvmSupportList } from './fixtures/getEvmSupport';
jest.mock('../lib/getEvmSupport');

describe('Testing BloctoSDK ethereum provider initialization and network loading', () => {
beforeEach(() => {
(getEvmSupport as jest.Mock).mockResolvedValue(getEvmSupportList);
});

test('should initialize with unloadedNetwork', () => {
const ethereumWithSwitchable = new EthereumProvider({
defaultChainId: '0xaa36a7',
switchableChains: [
{
chainId: '0xaa36a7',
rpcUrls: ['https://ethereum-sepolia.blockpi.network/v1/rpc/public'],
},
{
chainId: '0x61',
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
},
],
});

expect(ethereumWithSwitchable['_blocto'].unloadedNetwork).toBeDefined();
expect(
ethereumWithSwitchable['_blocto']?.unloadedNetwork?.[0].chainId
).toBe('0xaa36a7');
expect(
ethereumWithSwitchable['_blocto']?.unloadedNetwork?.[1].chainId
).toBe('0x61');
expect(ethereumWithSwitchable['_blocto'].unloadedNetwork?.length).toBe(2);
});

test('get support chain list', async () => {
const ethereum = new EthereumProvider({
chainId: '0xaa36a7',
rpc: 'https://ethereum-sepolia.blockpi.network/v1/rpc/public',
});
const supportedChains = await ethereum.supportChainList();
expect(supportedChains).toContainEqual({
chainId: '11155111',
chainName: 'Sepolia',
});
});

test('should add chain and switch to it', async () => {
const ethereum = new EthereumProvider({
chainId: '0xaa36a7',
rpc: 'https://ethereum-sepolia.blockpi.network/v1/rpc/public',
});
await expect(
ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x61' }],
})
).rejects.toThrow(
'Unrecognized chain ID "97". Try adding the chain using wallet_addEthereumChain first.'
);

await ethereum.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: '0x61',
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
},
],
});
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x61' }],
});
expect(ethereum.chainId).toBe('0x61');
});

test('create sdk instance with switchableChains and switch to it', async () => {
const ethereum = new EthereumProvider({
defaultChainId: '0xaa36a7',
switchableChains: [
{
chainId: '0xaa36a7',
rpcUrls: ['https://ethereum-sepolia.blockpi.network/v1/rpc/public'],
},
{
chainId: '0x61',
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
},
],
});
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x61' }],
});
expect(ethereum.chainId).toBe('0x61');
});
test('create sdk instance with switchableChains and call eth_accounts', async () => {
const ethereum = new EthereumProvider({
defaultChainId: '0xaa36a7',
switchableChains: [
{
chainId: '0xaa36a7',
rpcUrls: ['https://ethereum-sepolia.blockpi.network/v1/rpc/public'],
},
{
chainId: '0x61',
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
},
],
});
// Trigger the loading of switchable networks
await ethereum.request({
method: 'eth_accounts',
});
expect(ethereum.chainId).toBe('0xaa36a7');
// should remove unloadedNetwork after loading
expect(ethereum['_blocto'].unloadedNetwork).toBeUndefined();
});

test('should not call loadSwitchableNetwork if unloadedNetwork is empty', async () => {
const ethereum = new EthereumProvider({
chainId: '0xaa36a7',
rpc: 'https://ethereum-sepolia.blockpi.network/v1/rpc/public',
});
const loadSwitchableNetworkSpy = jest.spyOn(ethereum, 'loadSwitchableNetwork');

await ethereum.request({ method: 'eth_accounts' });

expect(loadSwitchableNetworkSpy).not.toHaveBeenCalled();
});
});
Loading
Loading