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

v3.0.16 #1318

Merged
merged 2 commits into from
Nov 26, 2024
Merged

v3.0.16 #1318

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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [[v3.0.16](https://github.com/multiversx/mx-sdk-dapp/pull/1318)] - 2024-11-26

- [Updated provider initializer to use useGetAccountFromApi](https://github.com/multiversx/mx-sdk-dapp/pull/1317)

## [Unreleased]

## [[v3.0.15](https://github.com/multiversx/mx-sdk-dapp/pull/1316)] - 2024-11-25
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-dapp",
"version": "3.0.15",
"version": "3.0.16",
"description": "A library to hold the main logic for a dapp on the MultiversX blockchain",
"author": "MultiversX",
"license": "GPL-3.0-or-later",
Expand Down
56 changes: 30 additions & 26 deletions src/components/ProviderInitializer/ProviderInitializer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useRef } from 'react';
import { getNetworkConfigFromApi } from 'apiCalls';
import { getNetworkConfigFromApi, useGetAccountFromApi } from 'apiCalls';
import { useLoginService } from 'hooks/login/useLoginService';
import { useWalletConnectV2Login } from 'hooks/login/useWalletConnectV2Login';
import { useWebViewLogin } from 'hooks/login/useWebViewLogin';
Expand Down Expand Up @@ -41,7 +41,6 @@ import { decodeNativeAuthToken } from 'services/nativeAuth/helpers';
import { LoginMethodsEnum } from 'types/enums.types';
import {
getAddress,
getAccount,
getLatestNonce,
newWalletProvider,
emptyProvider,
Expand Down Expand Up @@ -79,6 +78,11 @@ export function ProviderInitializer() {
const loginService = useLoginService(
nativeAuthConfig ? nativeAuthConfig : false
);
const {
data: account,
isLoading: isAccountLoading,
error: accountError
} = useGetAccountFromApi(address);

const initializedAccountRef = useRef(false);
const dispatch = useDispatch();
Expand Down Expand Up @@ -107,8 +111,8 @@ export function ProviderInitializer() {
}, [tokenLogin?.nativeAuthToken, address]);

useEffect(() => {
fetchAccount();
}, [address, network]);
setupAccount();
}, [account, isAccountLoading]);

useEffect(() => {
// prevent balance double fetching by handling ledgerAccount data separately
Expand Down Expand Up @@ -173,8 +177,17 @@ export function ProviderInitializer() {
}
}

async function fetchAccount() {
dispatch(setIsAccountLoading(true));
async function setupAccount() {
if (isAccountLoading) {
dispatch(setIsAccountLoading(true));
return;
}

if (accountError) {
dispatch(setAccountLoadingError('Failed getting account'));
console.error('Failed getting account ', accountError);
return;
}

if (initializedAccountRef.current) {
// account was recently initialized, skip refetching
Expand All @@ -183,26 +196,17 @@ export function ProviderInitializer() {
return;
}

if (address) {
try {
const account = await getAccount(address);

if (account) {
dispatch(
setAccount({
...account,
address,
nonce: account.nonce.valueOf()
})
);
} else if (!isLoggedIn) {
// Clear the address and publicKey if account is not found
dispatch(setAddress(''));
}
} catch (e) {
dispatch(setAccountLoadingError('Failed getting account'));
console.error('Failed getting account ', e);
}
if (account) {
dispatch(
setAccount({
...account,
address,
nonce: account.nonce.valueOf()
})
);
} else if (!isLoggedIn) {
// Clear the address and publicKey if account is not found
dispatch(setAddress(''));
}

dispatch(setIsAccountLoading(false));
Expand Down
Loading