Skip to content

Commit

Permalink
Account slice (#4)
Browse files Browse the repository at this point in the history
Add slices and services
  • Loading branch information
arhtudormorar authored Jul 10, 2024
1 parent 39cdd2a commit a73883b
Show file tree
Hide file tree
Showing 98 changed files with 3,379 additions and 739 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
transform: {
'^.+\\.(ts|js|tsx|jsx)$': ['@swc/jest']
},
setupFilesAfterEnv: ['<rootDir>/src/setupTests.js'],
transformIgnorePatterns: ['node_modules/(^.+\\\\.(ts|js|tsx|jsx)$)'],
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
Expand Down
27 changes: 19 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@multiversx/sdk-dapp-core",
"version": "0.0.0-alpha.1",
"main": "out/index.js",
"module": "out/index.mjs",
"module": "out/index.js",
"types": "out/index.d.ts",
"description": "A library to hold core logic for building TypeScript dApps on the MultiversX blockchain",
"author": "MultiversX",
Expand All @@ -20,32 +20,43 @@
},
"scripts": {
"compile": "tsc && tsc-alias",
"compile-next": "rimraf out && tsc --p tsconfig.next.json && tsc-alias --project tsconfig.next.json",
"build-esbuild": "rimraf out && node esbuild.js",
"publish-verdaccio": "npm unpublish --registry http://localhost:4873 @multiversx/[email protected] && rimraf out && yarn compile && npm publish --registry http://localhost:4873",
"build": "yarn build-esbuild && yarn compile",
"test": "jest"
"test": "jest",
"compile-next": "rimraf out && tsc --p tsconfig.next.json && tsc-alias --project tsconfig.next.json"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@multiversx/sdk-web-wallet-cross-window-provider": "0.1.3",
"@multiversx/sdk-native-auth-client": "^1.0.8",
"@multiversx/sdk-web-wallet-cross-window-provider": "0.2.0",
"@types/lodash": "^4.17.4",
"isomorphic-fetch": "^3.0.0",
"lodash": "^4.17.21",
"zustand": "^4.4.7"
},
"peerDependencies": {
"@multiversx/sdk-core": ">= 13.0.0",
"@multiversx/sdk-dapp-utils": "^0.0.1",
"@multiversx/sdk-core": ">= 12.18.0",
"axios": ">=1.6.5"
"axios": ">=1.6.5",
"bignumber.js": "9.x"
},
"resolutions": {
"string-width": "4.1.0"
},
"devDependencies": {
"@multiversx/sdk-core": ">= 13.0.0",
"@multiversx/sdk-dapp-utils": "^0.0.1",
"@multiversx/sdk-core": ">= 12.18.0",
"@swc/core": "^1.4.17",
"@swc/jest": "^0.2.36",
"@types/node": "20.12.8",
"@typescript-eslint/eslint-plugin": "7.8.0",
"@typescript-eslint/parser": "7.8.0",
"axios": ">=1.6.5",
"axios-mock-adapter": "^1.22.0",
"bignumber.js": "9.x",
"esbuild": "^0.21.1",
"eslint": "9.1.1",
"eslint-config-prettier": "9.1.0",
Expand All @@ -59,7 +70,7 @@
"immer": "^10.1.1",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"lit": "^3.1.3",
"msw": "1.3.1",
"prettier": "3.2.5",
"protobufjs": "^7.3.0",
"react": "^18.3.1",
Expand Down
11 changes: 11 additions & 0 deletions src/__mocks__/accountConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { fallbackNetworkConfigurations } from 'constants/network';
import { EnvironmentsEnum } from 'types';

export const testAddress =
'erd1dm9uxpf5awkn7uhju7zjn9lde0dhahy0qaxqqlu26xcuuw27qqrsqfmej3';

export const testNetwork =
fallbackNetworkConfigurations[EnvironmentsEnum.devnet];

export const testReceiver =
'erd1qqqqqqqqqqqqqpgqp699jngundfqw07d8jzkepucvpzush6k3wvqyc44rx';
10 changes: 10 additions & 0 deletions src/__mocks__/data/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const account = {
address: 'erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx',
balance: '116893786890813785912',
nonce: 12320,
shard: 0,
rootHash: 'wICKVeNpCg/TsBRyRyZMMMhcW1KENpAbopfinRVyENQ=',
txCount: 12655,
scrCount: 14084,
developerReward: '0'
};
6 changes: 6 additions & 0 deletions src/__mocks__/data/blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const blocks = [
{
hash: 'fff67d31476ad920d53093a3a4c2178e198179b35656eeefa419107fa718b780',
timestamp: 1671204768
}
];
16 changes: 16 additions & 0 deletions src/__mocks__/data/dappConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const dappConfig = {
id: 'devnet',
name: 'Devnet',
egldLabel: 'xEGLD',
decimals: '4',
egldDenomination: '18',
gasPerDataByte: '1500',
apiTimeout: '4000',
walletConnectDeepLink:
'https://maiar.page.link/?apn=com.elrond.maiar.wallet&isi=1519405832&ibi=com.elrond.maiar.wallet&link=https://xportal.com/',
walletConnectBridgeAddresses: ['https://bridge.walletconnect.org'],
walletAddress: 'https://devnet-wallet.multiversx.com',
apiAddress: 'https://devnet-api.multiversx.com',
explorerAddress: 'http://devnet-explorer.multiversx.com',
chainId: 'D'
};
7 changes: 7 additions & 0 deletions src/__mocks__/data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './account';
export * from './blocks';
export * from './networkConfig';
export * from './dappConfig';
export * from './socketResponse';
export * from './websocketConfig';
export * from './blocks';
28 changes: 28 additions & 0 deletions src/__mocks__/data/networkConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const networkConfig = {
data: {
config: {
erd_adaptivity: 'false',
erd_chain_id: 'D',
erd_denomination: 18,
erd_gas_per_data_byte: 1500,
erd_gas_price_modifier: '0.01',
erd_hysteresis: '0.200000',
erd_latest_tag_software_version: 'D1.3.50.0-hf01',
erd_max_gas_per_transaction: 600000000,
erd_meta_consensus_group_size: 58,
erd_min_gas_limit: 50000,
erd_min_gas_price: 1000000000,
erd_min_transaction_version: 1,
erd_num_metachain_nodes: 58,
erd_num_nodes_in_shard: 58,
erd_num_shards_without_meta: 3,
erd_rewards_top_up_gradient_point: '2000000000000000000000000',
erd_round_duration: 6000,
erd_rounds_per_epoch: 1200,
erd_shard_consensus_group_size: 21,
erd_start_time: 1648551600,
erd_top_up_factor: '0.500000'
}
},
code: 'successful'
};
7 changes: 7 additions & 0 deletions src/__mocks__/data/socketResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const socketResponse = {
sid: 'RlV7upxKjiJRyIhRAKb5',
upgrades: ['websocket'],
pingInterval: 25000,
pingTimeout: 20000,
maxPayload: 1000000
};
1 change: 1 addition & 0 deletions src/__mocks__/data/websocketConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const websocketConfig = { url: 'devnet-socket-api.multiversx.com' };
3 changes: 3 additions & 0 deletions src/__mocks__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './accountConfig';
export * from './server';
export * from './utils';
55 changes: 55 additions & 0 deletions src/__mocks__/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
DefaultBodyType,
PathParams,
ResponseComposition,
rest,
RestContext,
RestRequest
} from 'msw';
import { setupServer } from 'msw/node';
import { testAddress, testNetwork } from './accountConfig';
import {
account,
blocks,
dappConfig,
networkConfig,
socketResponse,
websocketConfig
} from './data';

export const mockResponse =
<T extends DefaultBodyType>(body: T) =>
(
_req: RestRequest<never, PathParams<string>>,
res: ResponseComposition<DefaultBodyType>,
ctx: RestContext
) => {
return res(ctx.status(200), ctx.json(body));
};

const handlers = [
rest.get(`${testNetwork.apiAddress}/dapp/config`, mockResponse(dappConfig)),

rest.get(
`${testNetwork.apiAddress}/network/config`,
mockResponse(networkConfig)
),
rest.get(
`https://${websocketConfig.url}/socket.io/`,
mockResponse(socketResponse)
),
rest.get(
`${testNetwork.apiAddress}/websocket/config`,
mockResponse(websocketConfig)
),
rest.get(
`${testNetwork.apiAddress}/accounts/${testAddress}`,
mockResponse(account)
),
rest.get(`${testNetwork.apiAddress}/blocks`, mockResponse(blocks))
];

// This configures a request mocking server with the given request handlers.
const server = setupServer(...handlers);

export { server, rest };
2 changes: 2 additions & 0 deletions src/__mocks__/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './mockWindowLocation';
export * from './mockWindowHistory';
24 changes: 24 additions & 0 deletions src/__mocks__/utils/mockWindowHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const mockWindowHistory = () => {
if (!window) {
return;
}

const history = window.history;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete window.history;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.history = Object.defineProperties(
{},
{
...Object.getOwnPropertyDescriptors(history),
pushState: {
configurable: true,
value: jest.fn()
}
}
);
};
24 changes: 24 additions & 0 deletions src/__mocks__/utils/mockWindowLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const mockWindowLocation = () => {
if (!window) {
return;
}

const location = window.location;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete window.location;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.location = Object.defineProperties(
{},
{
...Object.getOwnPropertyDescriptors(location),
assign: {
configurable: true,
value: jest.fn()
}
}
);
};
2 changes: 1 addition & 1 deletion src/apiCalls/configuration/getNetworkConfigFromApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { getCleanApiAddress } from 'store/slices/network/actions/getCleanApiAddress';
import { ApiNetworkConfigType } from '../../types/network.types';
import { ApiNetworkConfigType } from 'types/network.types';
import { NETWORK_CONFIG_ENDPOINT } from '../endpoints';

const urlIsValid = (url: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/apiCalls/configuration/getServerConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { NetworkType } from '../../types/network.types';
import { NetworkType } from 'types/network.types';
import { CONFIG_ENDPOINT } from '../endpoints';

export async function getServerConfiguration(apiAddress: string) {
Expand Down
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './network';
export * from './placeholders';
export * from './storage';
export * from './window';
4 changes: 2 additions & 2 deletions src/constants/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EnvironmentsEnum } from '../types/enums.types';
import { NetworkType } from '../types/network.types';
import { EnvironmentsEnum } from 'types/enums.types';
import { NetworkType } from 'types/network.types';

export const fallbackNetworkConfigurations: Record<
keyof typeof EnvironmentsEnum,
Expand Down
8 changes: 8 additions & 0 deletions src/constants/placeholders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Not Applicable
* @value N/A
*/
export const N_A = 'N/A';

export const ZERO = '0';
export const ELLIPSIS = '...';
2 changes: 1 addition & 1 deletion src/constants/window.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const safeWindow =
export const safeWindow: Window =
typeof window !== 'undefined' ? window : ({} as Window);
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './constants';
export * from './apiCalls';
export * from './store';
export * from './types';
export * from './services';
export * from './utils';
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './nativeAuth';
40 changes: 40 additions & 0 deletions src/services/nativeAuth/helpers/decodeLoginToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import isString from 'lodash/isString';
import { decodeBase64 } from 'utils/decoders/base64Utils';

export interface DecodedLoginTokenType {
blockHash: string;
extraInfo?: { timestamp: number };
origin: string;
ttl: number;
}

export const decodeLoginToken = (
loginToken: string
): DecodedLoginTokenType | null => {
if (!loginToken || !isString(loginToken)) {
return null;
}

const parts = loginToken.split('.');

if (parts.length !== 4) {
return null;
}

try {
const [origin, blockHash, ttl, extraInfo] = parts;
const parsedExtraInfo = JSON.parse(decodeBase64(extraInfo));
const parsedOrigin = decodeBase64(origin);

return {
ttl: Number(ttl),
extraInfo: parsedExtraInfo,
origin: parsedOrigin,
blockHash
};
} catch (e) {
console.error(`Error trying to decode ${loginToken}:`, e);

return null;
}
};
Loading

0 comments on commit a73883b

Please sign in to comment.