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

Transactions interpretation utils #38

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
798c0be
Merge pull request #17 from multiversx/development
CiprianDraghici Aug 29, 2024
9f66cbb
Added transactions utils
razvantomegea Dec 2, 2024
4e491dd
Added store and tests
razvantomegea Dec 2, 2024
2989894
added services and apiCalls
razvantomegea Dec 3, 2024
2f46a5c
Added tests and refactoring
razvantomegea Dec 3, 2024
01caef8
Merge branch 'refs/heads/development' into rt/feature/transactions
razvantomegea Dec 4, 2024
f744464
Removed unused code
razvantomegea Dec 4, 2024
f2cb8f9
fix
razvantomegea Dec 4, 2024
51bdb7b
Updated
razvantomegea Dec 4, 2024
26c3c9f
Removed endpoints
razvantomegea Dec 4, 2024
590c727
Added getTransactionByHash.ts
razvantomegea Dec 4, 2024
9d77761
Added getTransactionsByHashes.ts
razvantomegea Dec 4, 2024
ed513b3
Lint, test, and build fixes
razvantomegea Dec 4, 2024
293ca88
refactor
razvantomegea Dec 4, 2024
7d7723d
refactor
razvantomegea Dec 4, 2024
d5d96f2
CHANGELOG.md
razvantomegea Dec 4, 2024
025f6e5
pre-merge-unit-tests.yml
razvantomegea Dec 4, 2024
ef82ddf
Fix config 1
arhtudormorar Dec 5, 2024
da7adaa
Fix config 2
arhtudormorar Dec 5, 2024
4d53f81
Fix config 3
arhtudormorar Dec 5, 2024
13eaebd
Work on custom providers
arhtudormorar Dec 5, 2024
bcfa723
Code compiling
arhtudormorar Dec 5, 2024
d1b1d93
Improve custom providers
arhtudormorar Dec 5, 2024
1966b1b
Call with config
arhtudormorar Dec 5, 2024
ed1ea42
Project building
arhtudormorar Dec 5, 2024
533b3f3
Merge remote-tracking branch 'refs/remotes/origin/ag/feature/create-p…
razvantomegea Dec 5, 2024
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
40 changes: 40 additions & 0 deletions .github/workflows/pre-merge-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: "Jest Unit Tests"
on:
pull_request:
branches: [main]
paths:
- 'src/**'
- '**.js'
- '**.ts'
- '**.json'
repository_dispatch:
types: run-unit-tests
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
run-unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.ref }}
if: ${{ !github.event.pull_request.draft }}
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 18
- name: Setup yarn
run: npm install -g yarn
- name: Clean up
run: |
rm -rf node_modules build
- name: Install Dependencies
run: yarn install
- name: Build
run: yarn build
- name: Run unit tests
run: yarn test --silent
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- [Added transaction interpretation utils](https://github.com/multiversx/mx-sdk-dapp-core/pull/38)
- [Added custom web socket url support](https://github.com/multiversx/mx-sdk-dapp-core/pull/35)
- [Metamask integration](https://github.com/multiversx/mx-sdk-dapp-core/pull/27)
- [Extension integration](https://github.com/multiversx/mx-sdk-dapp-core/pull/26)
Expand Down
4 changes: 2 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default [
},
settings: {
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx']
'@typescript-eslint/parser': ['.ts']
},
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
extensions: ['.js', '.ts'],
moduleDirectory: ['node_modules', 'src/']
},
typescript: {
Expand Down
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ module.exports = {
'web.tsx',
'json',
'node'
]
],
workerIdleMemoryLimit: '512MB', // Memory used per worker. Required to prevent memory leaks
maxWorkers: '50%' // Maximum tests ran in parallel. Required to prevent CPU usage at 100%
};
1 change: 1 addition & 0 deletions src/apiCalls/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './configuration';
export * from './endpoints';
export * from './transactions';
2 changes: 2 additions & 0 deletions src/apiCalls/transactions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './getTransactionByHash';
export * from './getTransactionsByHashes';
9 changes: 5 additions & 4 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export * from './network.constants';
export * from './placeholders.constants';
export * from './storage.constants';
export * from './window.constants';
export * from './browser.constants';
export * from './errorMessages.constants';
export * from './mvx.constants';
export * from './ledger.constants';
export * from './network.constants';
export * from './placeholders.constants';
export * from './storage.constants';
export * from './transactions.constants';
export * from './window.constants';
13 changes: 3 additions & 10 deletions src/constants/mvx.constants.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
export const GAS_PRICE_MODIFIER = 0.01;
export const GAS_PER_DATA_BYTE = 1500;
export const GAS_LIMIT = 50000;
/**
* Extra gas limit for guarded transactions
*/
export const EXTRA_GAS_LIMIT_GUARDED_TX = 50000;
export const GAS_PRICE = 1000000000;
export const ALL_SHARDS_SHARD_ID = 4294967280;
export const CROSS_SHARD_ROUNDS = 5;
export const DECIMALS = 18;
export const DIGITS = 4;
export const VERSION = 1;
export const LEDGER_CONTRACT_DATA_ENABLED_VALUE = 1;
export const METACHAIN_SHARD_ID = 4294967295;
export const ALL_SHARDS_SHARD_ID = 4294967280;
export const VERSION = 1;
14 changes: 10 additions & 4 deletions src/constants/transactions.constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
export const CANCEL_TRANSACTION_TOAST_ID = 'cancel-transaction-toast';
export const AVERAGE_TX_DURATION_MS = 6000;
export const CROSS_SHARD_ROUNDS = 5;
export const TRANSACTIONS_STATUS_POLLING_INTERVAL_MS = 90 * 1000; // 90sec
export const TRANSACTIONS_STATUS_DROP_INTERVAL_MS = 10 * 60 * 1000; // 10min
export const CANCEL_TRANSACTION_TOAST_DEFAULT_DURATION = 20000;
export const CANCEL_TRANSACTION_TOAST_ID = 'cancel-transaction-toast';
export const EXTRA_GAS_LIMIT_GUARDED_TX = 50_000;
export const GAS_LIMIT = 50_000;
export const GAS_PER_DATA_BYTE = 1_500;
export const GAS_PRICE = 1_000_000_000;
export const GAS_PRICE_MODIFIER = 0.01;
export const REFUNDED_GAS = 'refundedGas';
export const TRANSACTIONS_STATUS_DROP_INTERVAL_MS = 10 * 60 * 1000; // 10min
export const TRANSACTIONS_STATUS_POLLING_INTERVAL_MS = 90 * 1000; // 90sec
export const WALLET_SIGN_SESSION = 'signSession';
12 changes: 11 additions & 1 deletion src/core/methods/initApp/initApp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { safeWindow } from 'constants/index';
import { restoreProvider } from 'core/providers/helpers/restoreProvider';
import { ProviderFactory } from 'core/providers/ProviderFactory';
import { getDefaultNativeAuthConfig } from 'services/nativeAuth/methods/getDefaultNativeAuthConfig';
import { NativeAuthConfigType } from 'services/nativeAuth/nativeAuth.types';
import { initializeNetwork } from 'store/actions';
Expand Down Expand Up @@ -30,7 +32,8 @@ const defaultInitAppProps = {
* */
export async function initApp({
storage = defaultInitAppProps.storage,
dAppConfig
dAppConfig,
customProviders
}: InitAppType) {
initStore(storage.getStorageCallback);

Expand All @@ -57,6 +60,13 @@ export async function initApp({

const isLoggedIn = getIsLoggedIn();

const usedProviders = [
...((safeWindow as any)?.multiversx?.providers || []),
...(customProviders || [])
];

ProviderFactory.customProviders(usedProviders || []);

if (isLoggedIn) {
await restoreProvider();
await registerWebsocketListener();
Expand Down
2 changes: 2 additions & 0 deletions src/core/methods/initApp/initApp.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ICustomProvider } from 'core/providers/types/providerFactory.types';
import { NativeAuthConfigType } from 'services/nativeAuth/nativeAuth.types';
import { StorageCallback } from 'store/storage';
import { EnvironmentsEnum } from 'types/enums.types';
Expand Down Expand Up @@ -49,4 +50,5 @@ export type InitAppType = {
getStorageCallback: StorageCallback;
};
dAppConfig: DappConfigType;
customProviders?: ICustomProvider[];
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'store/actions/account/accountActions';
import { networkSelector } from 'store/selectors';
import { getStore } from 'store/store';
import { retryMultipleTimes } from 'utils/retryMultipleTimes';
import { retryMultipleTimes } from 'utils/misc/retryMultipleTimes';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this?

import {
BatchTransactionsWSResponseType,
websocketConnection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getPendingTransactions } from './getPendingTransactions';
import { manageFailedTransactions } from './manageFailedTransactions';
import { TransactionsTrackerType } from '../../trackTransactions.types';

export interface TransactionStatusTrackerPropsType
export interface TransactionStatusTrackerParamsType
extends TransactionsTrackerType {
sessionId: string;
transactionBatch: SignedTransactionType[];
Expand Down Expand Up @@ -126,7 +126,7 @@ export async function checkBatch({
isSequential,
onSuccess,
onFail
}: TransactionStatusTrackerPropsType) {
}: TransactionStatusTrackerParamsType) {
try {
if (transactions == null) {
return;
Expand Down
8 changes: 3 additions & 5 deletions src/core/providers/DappProvider/helpers/logout/logout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getAddress } from 'core/methods/account/getAddress';
import { getProviderType } from 'core/providers/helpers/utils';
import {
IProvider,
ProviderTypeEnum
Expand All @@ -26,7 +25,7 @@ const broadcastLogoutAcrossTabs = (address: string) => {
storage.local.removeItem(localStorageKeys.logoutEvent);
};

export type LogoutPropsType = {
export type LogoutParamsType = {
shouldAttemptReLogin?: boolean;
shouldBroadcastLogoutAcrossTabs?: boolean;
/*
Expand All @@ -37,7 +36,7 @@ export type LogoutPropsType = {

interface IProviderLogout {
provider: IProvider;
options?: LogoutPropsType;
options?: LogoutParamsType;
}

export async function logout({
Expand All @@ -48,7 +47,6 @@ export async function logout({
}
}: IProviderLogout) {
let address = getAddress();
const providerType = getProviderType(provider);

if (options.shouldBroadcastLogoutAcrossTabs) {
broadcastLogoutAcrossTabs(address);
Expand All @@ -59,7 +57,7 @@ export async function logout({

if (
options.hasConsentPopup &&
providerType === ProviderTypeEnum.crossWindow
provider.getType() === ProviderTypeEnum.crossWindow
) {
(provider as unknown as CrossWindowProvider).setShouldShowConsentPopup(
true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Message, Address } from '@multiversx/sdk-core';
import { getAddress } from 'core/methods/account/getAddress';
import { getProviderType } from 'core/providers/helpers/utils';
import {
IProvider,
ProviderTypeEnum
Expand All @@ -22,7 +21,6 @@ export async function signMessage({
options
}: SignMessageType): Promise<Nullable<Message>> {
const address = getAddress();
const providerType = getProviderType(provider);

const messageToSign = new Message({
address: new Address(address),
Expand All @@ -31,7 +29,7 @@ export async function signMessage({

if (
options?.hasConsentPopup &&
providerType === ProviderTypeEnum.crossWindow
provider.getType() === ProviderTypeEnum.crossWindow
) {
(provider as unknown as CrossWindowProvider).setShouldShowConsentPopup(
true
Expand Down
37 changes: 20 additions & 17 deletions src/core/providers/ProviderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ import { getConfig } from './helpers/getConfig';
import { createIframeProvider } from './helpers/iframe/createIframeProvider';
import { createLedgerProvider } from './helpers/ledger/createLedgerProvider';
import {
ICustomProvider,
IProvider,
IProviderFactory,
ProviderTypeEnum
} from './types/providerFactory.types';

export class ProviderFactory {
public async create({
private static _customProviders: ICustomProvider[] = [];

public static customProviders(providers: ICustomProvider[]) {
this._customProviders = providers;
}

public static async create({
type,
config: userConfig,
customProvider
config: userConfig
}: IProviderFactory): Promise<DappProvider> {
let createdProvider: IProvider | null = null;
const { account, ui } = await getConfig(userConfig);
const config = await getConfig(userConfig);
const { account, UI } = config;

switch (type) {
case ProviderTypeEnum.extension: {
Expand All @@ -47,10 +54,7 @@ export class ProviderFactory {
}

case ProviderTypeEnum.ledger: {
const ledgerProvider = await createLedgerProvider(
ui.ledger.eventBus,
ui.ledger.mount
);
const ledgerProvider = await createLedgerProvider(UI.ledger.mount);

if (!ledgerProvider) {
throw new Error('Unable to create ledger provider');
Expand Down Expand Up @@ -106,16 +110,15 @@ export class ProviderFactory {
break;
}

case ProviderTypeEnum.custom: {
if (!customProvider) {
throw new Error('Unable to create custom provider provider');
}
createdProvider = customProvider;
default: {
this._customProviders.forEach(async (customProvider) => {
if (customProvider.type === type) {
createdProvider = await customProvider.constructor(config);
createdProvider.getType = () => type;
}
});
break;
}

default:
break;
}

if (!createdProvider) {
Expand All @@ -125,7 +128,7 @@ export class ProviderFactory {
const dappProvider = new DappProvider(createdProvider);

setAccountProvider(dappProvider);
setProviderType(type);
setProviderType(type as ProviderTypeEnum);

return dappProvider;
}
Expand Down
Loading
Loading