Skip to content

Commit

Permalink
initialize SCWStateManager with dapp chainIds (#1134)
Browse files Browse the repository at this point in the history
* initialize SCWStateManager with dapp chainIds

* test
  • Loading branch information
fan-zhang-sv authored Mar 18, 2024
1 parent e71a004 commit baa39e6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
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 {
}

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

updateAccounts(accounts: AddressString[]) {
Expand Down

0 comments on commit baa39e6

Please sign in to comment.