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

initialize SCWStateManager with dapp chainIds #1134

Merged
merged 2 commits into from
Mar 18, 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
1 change: 1 addition & 0 deletions packages/wallet-sdk/src/sign/scw/SCWSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class SCWSigner implements Signer {
this.puc = options.puc;
this.keyManager = new SCWKeyManager();
this.stateManager = new SCWStateManager({
appChainIds: this.appChainIds,
updateListener: {
onAccountsUpdate: (...args) => options.updateListener.onAccountsUpdate(this, ...args),
onChainUpdate: (...args) => options.updateListener.onChainUpdate(this, ...args),
Expand Down
31 changes: 31 additions & 0 deletions packages/wallet-sdk/src/sign/scw/SCWStateManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('SCWStateManager', () => {

beforeEach(() => {
stateManager = new SCWStateManager({
appChainIds: [DEFAULT_CHAIN.id],
updateListener: {
onAccountsUpdate: jest.fn(),
onChainUpdate: jest.fn(),
Expand All @@ -22,6 +23,35 @@ describe('SCWStateManager', () => {
stateManager.clear();
});

describe('fallback to appChainIds[0]', () => {
const appChainIds = [10];

let stateManager: SCWStateManager;
beforeEach(() => {
stateManager = new SCWStateManager({
appChainIds,
updateListener: {
onAccountsUpdate: jest.fn(),
onChainUpdate: jest.fn(),
},
});
});
it('should use the first chain id from appChainIds as the active chain', () => {
expect(stateManager.activeChain.id).toBe(appChainIds[0]);
});

it('should use the first chain id from appChainIds as the active chain when appChainIds is empty', () => {
stateManager = new SCWStateManager({
appChainIds: [],
updateListener: {
onAccountsUpdate: jest.fn(),
onChainUpdate: jest.fn(),
},
});
expect(stateManager.activeChain.id).toBe(1);
});
});

describe('switchChain', () => {
beforeEach(() => {
stateManager.updateAvailableChains(AVAILABLE_CHAINS);
Expand Down Expand Up @@ -64,6 +94,7 @@ describe('SCWStateManager', () => {

beforeEach(() => {
stateManager = new SCWStateManager({
appChainIds: [1],
updateListener: {
onAccountsUpdate: jest.fn(),
onChainUpdate: chainUpdatedListener,
Expand Down
4 changes: 2 additions & 2 deletions packages/wallet-sdk/src/sign/scw/SCWStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class SCWStateManager {
return this._activeChain;
}

constructor(options: { updateListener: StateUpdateListener }) {
constructor(options: { appChainIds: number[]; updateListener: StateUpdateListener }) {
this.updateListener = options.updateListener;

this.availableChains = this.loadItemFromStorage(AVAILABLE_CHAINS_STORAGE_KEY);
Expand All @@ -41,7 +41,7 @@ export class SCWStateManager {
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit would place updateListeners after _activeChain and _accounts are set


this._accounts = accounts || [];
this._activeChain = chain || { id: 1 };
this._activeChain = chain || { id: options.appChainIds?.[0] ?? 1 };
}

updateAccounts(accounts: AddressString[]) {
Expand Down
Loading