Skip to content

Commit

Permalink
Added faucet and issue token
Browse files Browse the repository at this point in the history
  • Loading branch information
razvantomegea committed Aug 27, 2024
1 parent 87eb921 commit e870f39
Show file tree
Hide file tree
Showing 32 changed files with 364 additions and 204 deletions.
65 changes: 7 additions & 58 deletions src/components/AxiosInterceptor/AxiosInterceptor.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React, { useEffect, useRef, useState } from 'react';
import axios from 'axios';
import { useSelector } from 'react-redux';
import React, { useEffect, useState } from 'react';
import { useGetAccount } from 'lib';
import { networkSelector } from 'redux/selectors';
import {
handleError,
parseJwt,
useAuthToken,
useSetNativeAuthInterceptors,
useSetResponseInterceptors
} from './helpers';
Expand All @@ -16,14 +11,9 @@ export const AxiosInterceptor = ({ children }: React.PropsWithChildren) => {
useSetNativeAuthInterceptors();
const { address } = useGetAccount();
const isLoggedIn = Boolean(address);
const { token, fetchToken } = useAuthToken();
const {
activeNetwork: { accessToken: hasAccessToken }
} = useSelector(networkSelector);
const [interceptorsReady, setInterceptorsReady] = useState(false);
const { setResponseInterceptors, responseId, axiosErrorUrl } =
const { setResponseInterceptors, axiosErrorUrl } =
useSetResponseInterceptors();
const timeoutRef = useRef<any>();

useEffect(() => {
handleError(axiosErrorUrl);
Expand All @@ -34,53 +24,12 @@ export const AxiosInterceptor = ({ children }: React.PropsWithChildren) => {
setInterceptorsReady(true);
};

const configureAxios = async () => {
if (!hasAccessToken) {
return setReady();
}

if (!token) {
const newToken = await fetchToken();
setNativeAuthTokenInterceptors(newToken);
return setReady();
}

const { exp: tokenTimestamp } = parseJwt(token);
const hasTimestamp = tokenTimestamp !== undefined;

if (!hasTimestamp) {
const newToken = await fetchToken();
setNativeAuthTokenInterceptors(newToken);
return setReady();
}

const now = Math.floor(Date.now() / 1000);
const fetchNextTokenSec = tokenTimestamp - now - 60;

if (fetchNextTokenSec > 0 && !interceptorsReady) {
setNativeAuthTokenInterceptors(token);
return setReady();
}

timeoutRef.current = setTimeout(async () => {
axios.interceptors.request.eject(responseId);
const newToken = await fetchToken();
setNativeAuthTokenInterceptors(newToken);
}, fetchNextTokenSec * 1000);

return () => {
clearTimeout(timeoutRef.current);
};
};

useEffect(() => {
configureAxios();
}, [token]);

useEffect(() => {
const accessToken = token || nativeAuthToken;
setNativeAuthTokenInterceptors(accessToken);
}, [nativeAuthToken, token, isLoggedIn]);
if (nativeAuthToken) {
setNativeAuthTokenInterceptors(nativeAuthToken);
setReady();
}
}, [nativeAuthToken, isLoggedIn]);

return interceptorsReady ? <>{children}</> : null;
};
27 changes: 2 additions & 25 deletions src/components/AxiosInterceptor/helpers/handleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,7 @@ export const handleError = (axiosErrorUrl: string) => {

const hasWalletVersion = walletVersion != null;

if (!hasWalletVersion) {
if (IS_DEVELOPMENT) {
createNotification();
}
return;
}

try {
const erdAddressRegex = new RegExp(/erd1\w+/, 'g');
const hashRegex = new RegExp(/([a-z0-9]){64}/, 'g');

let request = axiosErrorUrl.replace(erdAddressRegex, 'erd1...');
request = request.replace(hashRegex, 'hash...');

if ((window as any).ga) {
(window as any).ga(
'send',
'event',
'failed-request',
request,
walletVersion
);
}

if (!hasWalletVersion && IS_DEVELOPMENT) {
createNotification();
} catch {}
}
};
1 change: 0 additions & 1 deletion src/components/AxiosInterceptor/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './parseJwt';
export * from './handleError';
export * from './useAuthToken';
export * from './useSetNativeAuthInterceptors';
export * from './useSetResponseInterceptors';
66 changes: 0 additions & 66 deletions src/components/AxiosInterceptor/helpers/useAuthToken.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ export const useSetNativeAuthInterceptors = () => {
}
}

if (config.method === 'post') {
config.headers.set('X-Timestamp', Date.now().toString());
}

if (!bearerToken) {
return config;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/NetworkSwitcher/NetworkSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useSelector } from 'react-redux';
import { networks } from 'config';
import { networkSelector } from 'redux/selectors';
import { Dropdown, DropdownOption } from '../Dropdown';
import { useRefreshNativeAuthTokenForNetwork } from './hooks';
import { Dropdown, DropdownOption } from '../Dropdown';

export const NetworkSwitcher = () => {
const { activeNetwork } = useSelector(networkSelector);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { SignableMessage } from '@multiversx/sdk-core/out';
import { useDispatch } from 'react-redux';
import {
useAuthToken,
useSetNativeAuthInterceptors
} from 'components/AxiosInterceptor/helpers';
import { useSetNativeAuthInterceptors } from 'components/AxiosInterceptor/helpers';
import { networks } from 'config';
import { useLoginService } from 'lib';
import { useGetNativeAuthConfig } from 'pages/Unlock/hooks';
import { changeNetwork } from 'redux/slices';
import { useLoginService } from 'lib';

export const useRefreshNativeAuthTokenForNetwork = () => {
const nativeAuthConfig = useGetNativeAuthConfig();
const loginService = useLoginService(nativeAuthConfig);
const dispatch = useDispatch();
const { fetchToken, setToken } = useAuthToken();
const { setNativeAuthTokenInterceptors } = useSetNativeAuthInterceptors();

return async ({
Expand All @@ -34,11 +30,6 @@ export const useRefreshNativeAuthTokenForNetwork = () => {
}

try {
if (foundNetwork.accessToken) {
const token = await fetchToken(foundNetwork.extrasApi);
setNativeAuthTokenInterceptors(token);
}

const nativeAuthToken = await loginService.refreshNativeAuthTokenLogin({
signMessageCallback,
nativeAuthClientConfig: {
Expand All @@ -48,10 +39,7 @@ export const useRefreshNativeAuthTokenForNetwork = () => {
}
});

if (!foundNetwork.accessToken) {
setToken(undefined);
setNativeAuthTokenInterceptors(nativeAuthToken);
}
setNativeAuthTokenInterceptors(nativeAuthToken);
} catch (error) {
console.error('Could not refresh nativeAuth token', error);
}
Expand Down
1 change: 0 additions & 1 deletion src/config/config.devnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const networks: NetworkType[] = [
default: false,
id: EnvironmentsEnum.mainnet,
name: 'Mainnet',
accessToken: true,
apiAddress: 'https://api.multiversx.com',
gatewayUrl: '',
extrasApi: 'https://extras-api.multiversx.com',
Expand Down
1 change: 0 additions & 1 deletion src/config/config.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const networks: NetworkType[] = [
default: false,
id: EnvironmentsEnum.mainnet,
name: 'Mainnet',
accessToken: true,
apiAddress: 'https://api.multiversx.com',
gatewayUrl: '',
extrasApi: 'https://extras-api.multiversx.com',
Expand Down
1 change: 0 additions & 1 deletion src/config/config.mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const networks: NetworkType[] = [
default: true,
id: EnvironmentsEnum.mainnet,
name: 'Mainnet',
accessToken: true,
apiAddress: 'https://api.multiversx.com',
gatewayUrl: '',
extrasApi: 'https://extras-api.multiversx.com',
Expand Down
1 change: 0 additions & 1 deletion src/config/config.sovereign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const networks: NetworkType[] = [
default: false,
id: EnvironmentsEnum.mainnet,
name: 'Mainnet',
accessToken: true,
apiAddress: 'https://api.multiversx.com',
gatewayUrl: '',
extrasApi: 'https://extras-api.multiversx.com',
Expand Down
1 change: 0 additions & 1 deletion src/config/config.testnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const networks: NetworkType[] = [
default: false,
id: EnvironmentsEnum.mainnet,
name: 'Mainnet',
accessToken: true,
apiAddress: 'https://api.multiversx.com',
gatewayUrl: '',
extrasApi: 'https://extras-api.multiversx.com',
Expand Down
4 changes: 4 additions & 0 deletions src/lib/sdkDapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ export { sendBatchTransactions } from '@multiversx/sdk-dapp/services/transaction
export { useAxiosInterceptorContext } from '@multiversx/sdk-dapp/wrappers/AxiosInterceptorContext';
export { storage } from '@multiversx/sdk-dapp/utils/storage';
export { addNewCustomToast } from '@multiversx/sdk-dapp/utils/toasts';
export {
maxDecimals,
stringIsFloat
} from '@multiversx/sdk-dapp/utils/validation';
17 changes: 13 additions & 4 deletions src/localConstants/dataTestIds.enum.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
export enum DataTestIdsEnum {
accessPass = 'accessPass',
accessWalletBtn = 'accessWalletBtn',
activeNetwork = 'activeNetwork',
addTokenBtn = 'addTokenBtn',
amountError = 'amountError',
amountInput = 'amountInput',
requestTokensButton = 'requestTokensButton',
availableAmount = 'availableAmount',
backToWordsButton = 'backToWordsButton',
captcha = 'captcha',
cancelBtn = 'cancelBtn',
activeNetwork = 'activeNetwork',
cancelSignMessageBtn = 'cancelSignMessageBtn',
captcha = 'captcha',
check = 'check',
checkNetwork = 'checkNetwork',
closeButton = 'closeButton',
Expand All @@ -32,15 +31,21 @@ export enum DataTestIdsEnum {
gasLimitInput = 'gasLimitInput',
goToCheckMnemonic = 'goToCheckMnemonic',
goToDownloadButton = 'goToDownloadButton',
issueTokenBtn = 'issueTokenBtn',
keystoreBtn = 'keystoreBtn',
logoutBtn = 'logoutBtn',
mintedValueError = 'mintedValueError',
mintedValueInput = 'mintedValueInput',
mnemonicCheck = 'mnemonicCheck',
mnemonicInput = 'mnemonicInput',
mnemonicWord = 'mnemonicWord',
mnemonicWords = 'mnemonicWords',
mnemonicsDisclaimer = 'mnemonicsDisclaimer',
modalSubtitle = 'modalSubtitle',
modalTitle = 'modalTitle',
networkSwitcher = 'networkSwitcher',
numDecimalsError = 'numDecimalsError',
numDecimalsInput = 'numDecimalsInput',
password = 'password',
passwordError = 'passwordError',
passwordRepeat = 'passwordRepeat',
Expand All @@ -50,7 +55,7 @@ export enum DataTestIdsEnum {
receiverInput = 'receiverInput',
recoverWalletBtn = 'recoverWalletBtn',
removeTokenBtn = 'removeTokenBtn',
networkSwitcher = 'networkSwitcher',
requestTokensButton = 'requestTokensButton',
sendBtn = 'sendBtn',
sendEsdtTypeInput = 'sendEsdtTypeInput',
sendNFtTypeInput = 'sendNFtTypeInput',
Expand All @@ -61,6 +66,10 @@ export enum DataTestIdsEnum {
sovereignTransferBtn = 'sovereignTransferBtn',
submitButton = 'submitButton',
tokenError = 'tokenError',
tokenNameError = 'tokenNameError',
tokenNameInput = 'tokenNameInput',
tokenTickerError = 'tokenTickerError',
tokenTickerInput = 'tokenTickerInput',
transactionToastTitle = 'transactionToastTitle',
unlockPage = 'unlockPage',
userAddress = 'userAddress',
Expand Down
2 changes: 1 addition & 1 deletion src/localConstants/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export const WALLET_FILE = 'walletFile';
export const MAX_ALLOWED_TRANSACTIONS_TO_SIGN = 5;
export const CUSTOM_TOAST_DEFAULT_DURATION = 5000;
export const SOVEREIGN_TRANSFER_GAS_LIMIT = 20_000_000;
export const ACCESS_TOKEN_KEY: any = 'accessToken';
export const TOKEN_KEY: any = 'token';
3 changes: 2 additions & 1 deletion src/localConstants/routes/routeNames.enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export enum RouteNamesEnum {
logout = '/logout',
sign = '/sign',
signMessage = '/sign-message',
sovereignTransfer = '/sovereign-transfer'
sovereignTransfer = '/sovereign-transfer',
issueToken = '/issue-token'
}

export enum HooksPageEnum {
Expand Down
Loading

0 comments on commit e870f39

Please sign in to comment.