-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: accounts and wallets separation (#2885)
- Loading branch information
1 parent
13398fb
commit c49fa39
Showing
318 changed files
with
2,883 additions
and
1,956 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
projects: | ||
SubQuery Governance: | ||
schema: https://subquery-governance-polkadot-prod.novasama-tech.org | ||
include: | ||
- src/renderer/shared/api/governance/** | ||
|
||
SubQuery Proxy: | ||
schema: https://subquery-proxy-polkadot-prod.novasama-tech.org | ||
include: | ||
- src/renderer/entities/multisig/** | ||
- src/renderer/entities/proxy/** | ||
|
||
SubQuery History: | ||
schema: https://subquery-history-polkadot-prod.novasama-tech.org | ||
|
||
SubQuery Collectives: | ||
schema: https://subquery-collectives-polkadot-prod.novasama-tech.org | ||
include: | ||
- src/renderer/domains/collectives/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { allSettled, fork } from 'effector'; | ||
|
||
import { CryptoType, SigningType } from '@/shared/core'; | ||
import { createAccountId } from '@/shared/mocks'; | ||
|
||
import { accountsDomainModel } from './model'; | ||
import { type AnyAccount, type AnyAccountDraft } from './types'; | ||
|
||
const accounts: AnyAccount[] = [ | ||
{ | ||
id: 'test', | ||
type: 'chain', | ||
accountId: createAccountId('1'), | ||
chainId: '0x01', | ||
name: '', | ||
walletId: 0, | ||
signingType: SigningType.POLKADOT_VAULT, | ||
cryptoType: CryptoType.SR25519, | ||
}, | ||
{ | ||
id: 'test 2', | ||
type: 'universal', | ||
accountId: createAccountId('2'), | ||
name: '', | ||
walletId: 0, | ||
signingType: SigningType.POLKADOT_VAULT, | ||
cryptoType: CryptoType.SR25519, | ||
}, | ||
]; | ||
|
||
describe('accounts model', () => { | ||
it('should populate accounts', async () => { | ||
const scope = fork({ | ||
handlers: [[accountsDomainModel.populate, () => accounts]], | ||
}); | ||
|
||
expect(scope.getState(accountsDomainModel.$populated)).toEqual(false); | ||
|
||
await allSettled(accountsDomainModel.populate, { scope }); | ||
|
||
expect(scope.getState(accountsDomainModel.$list)).toEqual(accounts); | ||
expect(scope.getState(accountsDomainModel.$populated)).toEqual(true); | ||
}); | ||
|
||
it('should create new accounts', async () => { | ||
const scope = fork({ | ||
handlers: [[accountsDomainModel.createAccounts, (accounts: AnyAccount[]) => accounts]], | ||
}); | ||
|
||
await allSettled(accountsDomainModel.createAccounts, { scope, params: accounts }); | ||
|
||
expect(scope.getState(accountsDomainModel.$list)).toEqual(accounts); | ||
}); | ||
|
||
it('should successfully update account', async () => { | ||
const scope = fork({ | ||
values: [[accountsDomainModel.__test.$list, accounts]], | ||
handlers: [[accountsDomainModel.__test.updateAccountFx, () => true]], | ||
}); | ||
|
||
const draft: AnyAccountDraft = { | ||
accountId: createAccountId('1'), | ||
chainId: '0x01', | ||
walletId: 0, | ||
type: 'chain', | ||
name: 'test', | ||
}; | ||
|
||
await allSettled(accountsDomainModel.updateAccount, { | ||
scope, | ||
params: draft, | ||
}); | ||
|
||
expect(scope.getState(accountsDomainModel.$list)).toEqual([{ ...accounts[0], ...draft }, accounts[1]]); | ||
}); | ||
|
||
it('should skip update if account is not defined', async () => { | ||
const scope = fork({ | ||
values: [[accountsDomainModel.__test.$list, accounts]], | ||
handlers: [[accountsDomainModel.__test.updateAccountFx, () => false]], | ||
}); | ||
|
||
const draft: AnyAccountDraft = { | ||
accountId: createAccountId('3'), | ||
chainId: '0x01', | ||
walletId: 0, | ||
type: 'chain', | ||
name: 'test', | ||
}; | ||
|
||
await allSettled(accountsDomainModel.updateAccount, { | ||
scope, | ||
params: draft, | ||
}); | ||
|
||
expect(scope.getState(accountsDomainModel.$list)).toEqual(accounts); | ||
}); | ||
|
||
it('should create delete accounts', async () => { | ||
const scope = fork({ | ||
handlers: [ | ||
[accountsDomainModel.populate, () => accounts], | ||
[accountsDomainModel.deleteAccounts, (accounts: AnyAccount[]) => accounts], | ||
], | ||
}); | ||
|
||
await allSettled(accountsDomainModel.populate, { scope }); | ||
await allSettled(accountsDomainModel.deleteAccounts, { scope, params: accounts.slice(0, 1) }); | ||
|
||
expect(scope.getState(accountsDomainModel.$list)).toEqual(accounts.slice(1, 2)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { attach, createEffect, createStore, restore, sample } from 'effector'; | ||
import { once, readonly } from 'patronum'; | ||
|
||
import { storageService } from '@/shared/api/storage'; | ||
import { merge, nonNullable, nullable } from '@/shared/lib/utils'; | ||
|
||
import { accountsService } from './service'; | ||
import { type AnyAccount, type AnyAccountDraft } from './types'; | ||
|
||
const $accounts = createStore<AnyAccount[]>([]); | ||
|
||
const $populated = restore( | ||
once($accounts.updates).map(() => true), | ||
false, | ||
); | ||
|
||
const populateFx = createEffect((): Promise<AnyAccount[]> => storageService.accounts2.readAll()); | ||
|
||
const createAccountsFx = createEffect(async (accounts: AnyAccount[]): Promise<AnyAccount[]> => { | ||
return storageService.accounts2 | ||
.createAll(accounts.map(a => ({ ...a, id: accountsService.uniqId(a) }))) | ||
.then(x => x ?? []); | ||
}); | ||
|
||
const updateAccountFx = createEffect(async (account: AnyAccountDraft | null): Promise<boolean> => { | ||
if (nullable(account)) return false; | ||
|
||
const id = accountsService.uniqId(account); | ||
|
||
return storageService.accounts2.update(id, account).then(nonNullable); | ||
}); | ||
|
||
const updateAccount = attach({ | ||
source: $accounts, | ||
mapParams: (draft: AnyAccountDraft, accounts) => { | ||
if (accounts.find(a => accountsService.uniqId(a) === accountsService.uniqId(draft))) { | ||
return draft; | ||
} | ||
|
||
return null; | ||
}, | ||
effect: updateAccountFx, | ||
}); | ||
|
||
const deleteAccountsFx = createEffect(async (accounts: AnyAccount[]) => { | ||
// TODO set correct id | ||
await storageService.accounts2.deleteAll(accounts.map(accountsService.uniqId)); | ||
|
||
return accounts; | ||
}); | ||
|
||
sample({ | ||
clock: populateFx.doneData, | ||
target: $accounts, | ||
}); | ||
|
||
sample({ | ||
clock: createAccountsFx.doneData, | ||
source: $accounts, | ||
fn: (accounts, newAccounts) => | ||
merge({ | ||
a: accounts, | ||
b: newAccounts, | ||
mergeBy: accountsService.uniqId, | ||
}), | ||
target: $accounts, | ||
}); | ||
|
||
sample({ | ||
clock: updateAccount.done, | ||
source: $accounts, | ||
filter: (_, { result: successful }) => successful, | ||
fn: (accounts, { params: draft }) => { | ||
const draftId = accountsService.uniqId(draft); | ||
|
||
return accounts.map(a => | ||
accountsService.uniqId(a) === draftId ? ({ ...a, ...draft } as AnyAccount) : a, | ||
) as AnyAccount[]; | ||
}, | ||
target: $accounts, | ||
}); | ||
|
||
sample({ | ||
clock: deleteAccountsFx.done, | ||
source: $accounts, | ||
fn: (accounts, { result: deletedAccounts }) => { | ||
const deletedIds = deletedAccounts.map(accountsService.uniqId); | ||
|
||
return accounts.filter(a => !deletedIds.includes(accountsService.uniqId(a))); | ||
}, | ||
target: $accounts, | ||
}); | ||
|
||
export const accountsDomainModel = { | ||
$list: readonly($accounts), | ||
$populated: readonly($populated), | ||
|
||
populate: populateFx, | ||
createAccounts: createAccountsFx, | ||
updateAccount, | ||
deleteAccounts: deleteAccountsFx, | ||
|
||
__test: { | ||
$list: $accounts, | ||
updateAccountFx, | ||
}, | ||
}; |
Oops, something went wrong.