Skip to content

Commit

Permalink
refactor: use node instead whole sdk where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed May 27, 2024
1 parent a530534 commit 9359b5c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 50 deletions.
5 changes: 2 additions & 3 deletions src/store/modules/accounts/hdWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ export default {
},

actions: {
async isAccountUsed({ rootState: { sdk } }, address) {
const { api } = sdk.then ? await sdk : sdk;
return api.getAccountByPubkey(address).then(() => true, () => false);
async isAccountUsed({ rootGetters: { node } }, address) {
return node.getAccountByPubkey(address).then(() => true, () => false);
},

async checkPreviousAndCreate({ dispatch, rootGetters }) {
Expand Down
5 changes: 2 additions & 3 deletions src/store/modules/accounts/hdWalletRemote.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ export default {
},

actions: {
async isAccountUsed({ rootState: { sdk } }, address) {
const { api } = sdk.then ? await sdk : sdk;
return api.getAccountByPubkey(address).then(() => true, () => false);
async isAccountUsed({ rootGetters: { node } }, address) {
return node.getAccountByPubkey(address).then(() => true, () => false);
},

async checkPreviousAndCreate({ dispatch, rootGetters }) {
Expand Down
49 changes: 22 additions & 27 deletions src/store/plugins/ui/observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,19 @@ export default (store) => {
options,
);

const sdk$ = watchAsObservable(({ sdk }) => (sdk && sdk.then ? null : sdk), { immediate: true })
.pipe(
pluck('newValue'),
);
const node$ = watchAsObservable((state, { node }) => node, { immediate: true })
.pipe(pluck('newValue'));

const defaultAccountInfo = { balance: BigNumber(0), nonce: 0 };
const getAccountInfo = memoize((address) => sdk$
const getAccountInfo = memoize((address) => node$
.pipe(
switchMap((sdk) => timer(0, 3000).pipe(map(() => sdk))),
switchMap((sdk) => (sdk
? sdk.api.getAccountByPubkey(address).catch((error) => {
if (!isAccountNotFoundError(error)) {
handleUnknownError(error);
}
return defaultAccountInfo;
})
: Promise.resolve(defaultAccountInfo))),
switchMap((node) => timer(0, 3000).pipe(map(() => node))),
switchMap((node) => node.getAccountByPubkey(address).catch((error) => {
if (!isAccountNotFoundError(error)) {
handleUnknownError(error);
}
return defaultAccountInfo;
})),
map((aci) => ({ ...aci, balance: BigNumber(aci.balance).shiftedBy(-MAGNITUDE) })),
multicast(new BehaviorSubject(defaultAccountInfo)),
refCountDelay(1000),
Expand Down Expand Up @@ -75,15 +71,15 @@ export default (store) => {
tx,
});

const createSdkObservable = (func, def) => sdk$
const createNodeObservable = (func, def) => node$
.pipe(
switchMap((sdk) => timer(0, 30000).pipe(map(() => sdk))),
switchMap(async (sdk) => (sdk ? func(sdk) : def)),
switchMap((node) => timer(0, 30000).pipe(map(() => node))),
switchMap(async (node) => func(node)),
multicast(new BehaviorSubject(def)),
refCountDelay(1000),
);
const topBlockHeight$ = createSdkObservable(
async (sdk) => (await sdk.api.getTopHeader()).height,
const topBlockHeight$ = createNodeObservable(
async (node) => (await node.getTopHeader()).height,
0,
);
const middlewareStatus$ = watchAsObservable(
Expand Down Expand Up @@ -143,7 +139,7 @@ export default (store) => {
let transactions = {};
let transactionsByAddress = {};

sdk$.subscribe(() => {
node$.subscribe(() => {
transactions = {};
transactionsByAddress = {};
});
Expand All @@ -156,18 +152,17 @@ export default (store) => {
: Promise.resolve(tx));

const getTransaction = (transactionHash$) => combineLatest([
activeAccountAddress$, topBlockHeight$, transactionHash$, sdk$,
activeAccountAddress$, topBlockHeight$, transactionHash$, node$,
])
.pipe(
switchMap(async ([address, height, hash, sdk]) => {
switchMap(async ([address, height, hash, node]) => {
let tx;
if (transactions[hash]) {
tx = transactions[hash];
} else {
if (!sdk) return null;
tx = await sdk.api.getTransactionByHash(hash);
tx = await node.getTransactionByHash(hash);
tx.pending = tx.blockHash === 'none';
tx.time = !tx.pending && (await sdk.api.getMicroBlockHeaderByHash(tx.blockHash)).time;
tx.time = !tx.pending && (await node.getMicroBlockHeaderByHash(tx.blockHash)).time;
tx = normalizeTransaction(tx);
if (!tx.pending) registerTx(tx);
}
Expand All @@ -191,7 +186,7 @@ export default (store) => {
};

const fetchPendingTransactions = async (address) => (
await store.state.sdk.api.getPendingAccountTransactionsByPubkey(address)
await store.getters.node.getPendingAccountTransactionsByPubkey(address)
.then((r) => r.transactions)
.catch((error) => {
if (!isAccountNotFoundError(error)) {
Expand All @@ -215,7 +210,7 @@ export default (store) => {
),
),
activeAccountAddress$,
sdk$.pipe(filter((sdk) => sdk)),
node$,
])
.pipe(
switchMap(([mode, address]) => concat(of(-1), interval(5000)).pipe(
Expand Down
3 changes: 1 addition & 2 deletions src/store/plugins/ui/veeValidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ export default (store) => {
let lastPromiseCallback;
const checkNameDebounced = debounce(
async (name, expectedNameState) => {
const sdk = await Promise.resolve(store.state.sdk);
try {
const nameEntry = await sdk.api.getNameEntryByName(name);
const nameEntry = await store.getters.node.getNameEntryByName(name);
lastPromiseCallback.resolve(({
[NAME_STATES.REGISTERED]: true,
[NAME_STATES.REGISTERED_ADDRESS]: !!getAddressByNameEntry(nameEntry),
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/specs/transfer/sign-transaction.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('Sign transaction', () => {
url.searchParams.append('callback', 'about:blank');
cy
.viewport('iphone-se2')
.visit(url.href.replace('http://localhost', ''), { login: true });
.visit(url.href.replace('http://localhost', ''), { login: 'wallet-empty' });
cy.get('button').contains('Confirm').click();
const skipButton = cy.get('.modal-plain .ae-button.primary');
cy.matchImage();
Expand Down
44 changes: 30 additions & 14 deletions tests/e2e/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })

// wallet-empty: cool fruit obvious since project earth bread second cereal slender escape zone

Cypress.Commands.overwrite('visit', (originalFn, url, {
isDesktop, login, state, ...options
} = {}) => originalFn(
Expand All @@ -37,34 +39,48 @@ Cypress.Commands.overwrite('visit', (originalFn, url, {
migrations: Object.fromEntries(Cypress._.times(6, (i) => [i, true])),
accounts: {
list: [{
address: login === 'wallet-2'
? 'ak_s7gnBTaTm9n5iTFNVSnMBZMhrrBB42Q3BwpTdjoMuXhWyD5bN'
: 'ak_8eAGBq1jP4dLsmnmgnSzRBxSh5SU1AVsgbCwSQcXZVwwB6c1t',
address: {
'wallet-2': 'ak_s7gnBTaTm9n5iTFNVSnMBZMhrrBB42Q3BwpTdjoMuXhWyD5bN',
'wallet-empty': 'ak_2ujJ8N4GdKapdE2a7aEy4Da3pfPdV7EtJdaA7BUpJ8uqgkQdEB',
}[login] || 'ak_8eAGBq1jP4dLsmnmgnSzRBxSh5SU1AVsgbCwSQcXZVwwB6c1t',
source: { type: 'hd-wallet', idx: 0 },
}, {
address: login === 'wallet-2'
? 'ak_25sDUczTzeuBdWyx1Mvu7yYEhXA8BCJCXB3Gpqf2QHFGBHD7x1'
: 'ak_DNRWW4KcJyHed5b8fNizFkVb6zqykC6eFQokWgsBJLLyKdaiC',
address: {
'wallet-2': 'ak_25sDUczTzeuBdWyx1Mvu7yYEhXA8BCJCXB3Gpqf2QHFGBHD7x1',
'wallet-empty': 'ak_2Mz7EqTRdmGfns7fvLYfLFLoKyXj8jbfHbfwERfwFZgoZs4Z3T',
}[login] || 'ak_DNRWW4KcJyHed5b8fNizFkVb6zqykC6eFQokWgsBJLLyKdaiC',
source: { type: 'hd-wallet', idx: 1 },
}],
hdWallet: {
encryptedWallet: {
privateKey: {
type: 'Uint8Array',
data: login === 'wallet-2' ? [
164, 149, 84, 122, 149, 196, 223, 138, 199, 181, 109, 213, 57, 88, 2, 8,
121, 252, 27, 191, 76, 128, 160, 221, 22, 203, 246, 35, 229, 27, 123, 103,
] : [
data: {
'wallet-2': [
164, 149, 84, 122, 149, 196, 223, 138, 199, 181, 109, 213, 57, 88, 2, 8,
121, 252, 27, 191, 76, 128, 160, 221, 22, 203, 246, 35, 229, 27, 123, 103,
],
'wallet-empty': [
79, 241, 192, 71, 206, 58, 206, 111, 90, 91, 25, 192, 36, 116, 216, 106,
169, 147, 123, 249, 98, 36, 103, 218, 218, 185, 212, 154, 132, 81, 47, 81,
],
}[login] || [
68, 182, 66, 150, 5, 164, 0, 122, 49, 168, 211, 214, 215, 21, 209, 252,
2, 87, 156, 34, 80, 47, 210, 39, 41, 57, 114, 132, 76, 133, 95, 152,
],
},
chainCode: {
type: 'Uint8Array',
data: login === 'wallet-2' ? [
42, 13, 63, 81, 84, 2, 206, 219, 11, 177, 177, 44, 192, 40, 41, 217,
110, 46, 91, 134, 140, 180, 191, 115, 6, 101, 65, 65, 46, 209, 132, 189,
] : [
data: {
'wallet-2': [
42, 13, 63, 81, 84, 2, 206, 219, 11, 177, 177, 44, 192, 40, 41, 217,
110, 46, 91, 134, 140, 180, 191, 115, 6, 101, 65, 65, 46, 209, 132, 189,
],
'wallet-empty': [
38, 36, 63, 103, 245, 78, 22, 185, 23, 255, 210, 194, 42, 6, 160, 112,
72, 80, 146, 122, 226, 151, 217, 254, 41, 32, 78, 114, 224, 209, 215, 217,
],
}[login] || [
239, 237, 223, 34, 108, 6, 11, 247, 234, 38, 22, 33, 129, 121, 252, 96,
45, 95, 234, 210, 221, 187, 26, 114, 144, 126, 68, 68, 154, 133, 75, 225,
],
Expand Down

0 comments on commit 9359b5c

Please sign in to comment.