Skip to content

Commit

Permalink
Feat: run detect and create tokens when new block arrives
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamboster committed Sep 18, 2024
1 parent 2e09e12 commit 9317ac2
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export type SilentPushEvent = {
notification_type?: string;
ab?: any;
tokenAddress?: string | null;
chain?: string;
coin?: string;
network?: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ export const ImportLedgerWalletModal = () => {
}),
);
}
dispatch(startUpdateAllKeyAndWalletStatus({force: true}));
dispatch(
startUpdateAllKeyAndWalletStatus({
context: 'importLedger',
force: true,
}),
);
}
dispatch(AppActions.importLedgerModalToggled(false));
};
Expand Down
9 changes: 8 additions & 1 deletion src/navigation/tabs/home/HomeRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,17 @@ const HomeRoot = () => {
try {
await dispatch(startGetRates({}));
await Promise.all([
dispatch(startUpdateAllKeyAndWalletStatus({force: true})),
dispatch(
startUpdateAllKeyAndWalletStatus({
context: 'homeRootOnRefresh',
force: true,
createTokenWalletWithFunds: true,
}),
),
dispatch(requestBrazeContentRefresh()),
sleep(1000),
]);
await sleep(2000);
dispatch(updatePortfolioBalance());
} catch (err) {
dispatch(showBottomNotificationModal(BalanceUpdateError()));
Expand Down
19 changes: 16 additions & 3 deletions src/store/app/app.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,8 +977,16 @@ export const setEmailNotifications =
};

const _startUpdateAllKeyAndWalletStatus = debounce(
async dispatch => {
dispatch(startUpdateAllKeyAndWalletStatus({force: true}));
async (dispatch, chain, tokenAddress) => {
dispatch(
startUpdateAllKeyAndWalletStatus({
context: 'newBlockEvent',
force: true,
createTokenWalletWithFunds: true,
chain,
tokenAddress,
}),
);
DeviceEventEmitter.emit(DeviceEmitterEvents.WALLET_LOAD_HISTORY);
},
5000,
Expand Down Expand Up @@ -1062,7 +1070,12 @@ export const handleBwsEvent =
break;
case 'NewBlock':
if (response.network && response.network === 'livenet') {
_startUpdateAllKeyAndWalletStatus(dispatch);
// Chain and tokenAddress are passed to check if a new token received funds on that network and create the wallet if necessary
_startUpdateAllKeyAndWalletStatus(
dispatch,
response.chain,
response.tokenAddress,
);
}
break;
case 'TxProposalAcceptedBy':
Expand Down
33 changes: 27 additions & 6 deletions src/store/wallet/effects/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,17 +816,37 @@ export const getDecryptPassword =
};

export const detectAndCreateTokensForEachEvmWallet =
({key, force}: {key: Key; force?: boolean}): Effect<Promise<void>> =>
({
key,
force,
chain,
tokenAddress,
}: {
key: Key;
force?: boolean;
chain?: string;
tokenAddress?: string;
}): Effect<Promise<void>> =>
async dispatch => {
try {
dispatch(
LogActions.info('starting [detectAndCreateTokensForEachEvmWallet]'),
LogActions.info('Starting [detectAndCreateTokensForEachEvmWallet]'),
);

const evmWalletsToCheck = key.wallets.filter(
w =>
IsEVMChain(w.chain) && !IsERCToken(w.currencyAbbreviation, w.chain),
);
const evmWalletsToCheck = key.wallets.filter(w => {
const isEVMChain = IsEVMChain(w.chain);
const isNotERCToken = !IsERCToken(w.currencyAbbreviation, w.chain);
const matchesChain =
!chain || (w.chain && chain.toLowerCase() === w.chain.toLowerCase());
const notAlreadyCreated =
!tokenAddress ||
!w.tokens ||
!cloneDeep(w.tokens).some(t =>
t?.toLowerCase().includes(tokenAddress.toLowerCase()),
);

return isEVMChain && isNotERCToken && matchesChain && notAlreadyCreated;
});

for (const [index, w] of evmWalletsToCheck.entries()) {
if (w.chain && w.receiveAddress) {
Expand All @@ -846,6 +866,7 @@ export const detectAndCreateTokensForEachEvmWallet =
token.includes(erc20Token.token_address),
)) &&
!erc20Token.possible_spam &&
erc20Token.verified_contract &&
erc20Token.balance &&
erc20Token.decimals &&
parseFloat(erc20Token.balance) /
Expand Down
7 changes: 6 additions & 1 deletion src/store/wallet/effects/import/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,12 @@ export const startMigration =

// update store with token rates from coin gecko and update balances
await dispatch(startGetRates({force: true}));
await dispatch(startUpdateAllKeyAndWalletStatus({force: true}));
await dispatch(
startUpdateAllKeyAndWalletStatus({
context: 'startMigration',
force: true,
}),
);
dispatch(
LogActions.info(
'[startMigration] - success migration keys and wallets',
Expand Down
2 changes: 1 addition & 1 deletion src/store/wallet/effects/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const startWalletStoreInit =
await dispatch(startGetRates({init: true})); // populate rates and alternative currency list

if (Object.keys(WALLET.keys).length) {
dispatch(startUpdateAllKeyAndWalletStatus({}));
dispatch(startUpdateAllKeyAndWalletStatus({context: 'init'}));
}

dispatch(WalletActions.successWalletStoreInit());
Expand Down
51 changes: 49 additions & 2 deletions src/store/wallet/effects/status/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,13 +627,34 @@ export const startUpdateAllWalletStatusForKey =
);
};

type UpdateAllKeyAndWalletStatusContext =
| 'homeRootOnRefresh'
| 'importLedger'
| 'init'
| 'newBlockEvent'
| 'startMigration';

export const startUpdateAllKeyAndWalletStatus =
({force}: {force?: boolean}): Effect =>
({
context,
force,
createTokenWalletWithFunds,
chain,
tokenAddress,
}: {
context?: UpdateAllKeyAndWalletStatusContext;
force?: boolean;
createTokenWalletWithFunds?: boolean;
chain?: string;
tokenAddress?: string;
}): Effect =>
async (dispatch, getState) => {
return new Promise(async (resolve, reject) => {
try {
dispatch(
LogActions.info('starting [startUpdateAllKeyAndWalletStatus]'),
LogActions.info(
`Starting [startUpdateAllKeyAndWalletStatus]. Context: ${context}`,
),
);
const {
WALLET: {keys: _keys, balanceCacheKey},
Expand All @@ -651,6 +672,32 @@ export const startUpdateAllKeyAndWalletStatus =

const [readOnlyKeys, keys] = _.partition(_keys, 'isReadOnly');

if (createTokenWalletWithFunds) {
LogActions.debug(
`Checking for new token with funds.${
context ? ' Context: ' + context + '. ' : ''
}${chain ? ' Chain: ' + chain + '. ' : ''}${
tokenAddress ? ' Contract address: ' + tokenAddress + '. ' : ''
}`,
);
for (const [index, k] of keys.entries()) {
try {
await dispatch(
detectAndCreateTokensForEachEvmWallet({
key: k,
chain,
tokenAddress,
}),
);
} catch (error) {
dispatch(
LogActions.info(
'Error trying to detectAndCreateTokensForEachEvmWallet. Continue anyway.',
),
);
}
}
}
await Promise.all([
dispatch(startUpdateAllWalletStatusForKeys({keys, force})),
dispatch(
Expand Down

0 comments on commit 9317ac2

Please sign in to comment.