Skip to content

Commit

Permalink
nonce increment before signing (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliancwirko committed Jun 4, 2023
1 parent d7bddd8 commit 08ff5fb
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 54 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### [0.6.0](https://github.com/useElven/core/releases/tag/v0.6.0) (2023-06-04)
- improvements for nonce incrementation to let triggering multiple transactions at the same time. The logic is slightly different, but it shouldn't break anything

### [0.5.0](https://github.com/useElven/core/releases/tag/v0.5.0) (2023-05-28)
- Breaking: switch to using sdk-native-auth-client instead passing string-based login tokens. There is no fallback or other option, so it is a breaking change. Native Auth is recommended. The old way of doing that will be deprecated. Please freeze the previous version if you are not ready to switch yet
- update dependencies
Expand Down
101 changes: 61 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useelven/core",
"version": "0.5.0",
"version": "0.6.0",
"description": "Core hooks for MultiversX React DApps",
"license": "MIT",
"author": "Julian Ćwirko <julian.io>",
Expand Down
15 changes: 6 additions & 9 deletions src/hooks/common-helpers/sendTxOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ export interface TransactionCallbackParams {
}

export const postSendTxOperations = async (
tx: Transaction,
signedTx: Transaction,
setTransaction: Dispatch<SetStateAction<Transaction | null>>,
setTxResult: Dispatch<SetStateAction<ITransactionOnNetwork | null>>,
apiNetworkProvider: NetworkState['apiNetworkProvider'],
cb?: (params: TransactionCallbackParams) => void
) => {
setTransaction(signedTx);
if (apiNetworkProvider) {
const transactionWatcher = new TransactionWatcher(apiNetworkProvider);
const txResult = await transactionWatcher.awaitCompleted(tx);
setTransaction(tx);
const txResult = await transactionWatcher.awaitCompleted(signedTx);
setTransaction(signedTx);
setTxResult(txResult);
cb?.({ transaction: tx, pending: false, txResult });
const sender = tx.getSender();
cb?.({ transaction: signedTx, pending: false, txResult });
const sender = signedTx.getSender();
const senderAccount = new Account(sender);
const userAccountOnNetwork = await apiNetworkProvider.getAccount(sender);
senderAccount.update(userAccountOnNetwork);
setAccountState('address', senderAccount.address.bech32());
setAccountState('nonce', senderAccount.getNonceThenIncrement());
setAccountState('balance', senderAccount.balance.toString());
}
};
Expand All @@ -66,8 +66,6 @@ export const sendTxOperations = async (
await dappProvider.signTransaction(tx, {
callbackUrl: webWalletRedirectUrl || currentUrl,
});
// web wallet provider doesn't return signed transaction
signedTx = tx;
}
if (dappProvider instanceof ExtensionProvider) {
signedTx = await dappProvider.signTransaction(tx);
Expand All @@ -80,7 +78,6 @@ export const sendTxOperations = async (
}
if (loginInfoSnap.loginMethod !== LoginMethodsEnum.wallet) {
await apiNetworkProvider.sendTransaction(signedTx);
setTransaction(signedTx);
await postSendTxOperations(
signedTx,
setTransaction,
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/common-helpers/useWebWalletTxSend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { errorParse } from '../../utils/errorParse';
import { ApiNetworkProvider } from '@multiversx/sdk-network-providers/out';
import { useAccount } from '../useAccount';
import { setAccountState } from '../../store/auth';
import { useNetwork } from '../useNetwork';

interface UseWebWalletTxSendProps {
Expand Down Expand Up @@ -56,7 +57,7 @@ export const useWebWalletTxSend = ({
setPending(true);
cb?.({ pending: true });
const transaction = Transaction.fromPlainObject(transactionObj);
transaction.setNonce(currentNonce);

try {
await networkStateSnap.apiNetworkProvider.sendTransaction(
transaction
Expand All @@ -76,6 +77,7 @@ export const useWebWalletTxSend = ({
} finally {
setPending(false);
cb?.({ pending: false });
setAccountState('nonce', currentNonce + 1);
}
}
};
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useExtensionLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const useExtensionLogin = (params?: Login) => {
userAccountInstance.update(userAccountOnNetwork);

setAccountState('address', userAccountInstance.address.bech32());

setAccountState('nonce', userAccountInstance.nonce.valueOf());
setAccountState('balance', userAccountInstance.balance.toString());
} catch (e) {
Expand Down
10 changes: 8 additions & 2 deletions src/hooks/useTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import {
IGasLimit,
ITransactionValue,
ITransactionOnNetwork,
Account,
} from '@multiversx/sdk-core';
import { useWebWalletTxSend } from './common-helpers/useWebWalletTxSend';
import {
TransactionCallbackParams,
sendTxOperations,
} from './common-helpers/sendTxOperations';
import { DappProvider } from '../types/network';
import { setAccountState } from '../store/auth';
import { ApiNetworkProvider } from '@multiversx/sdk-network-providers/out';
import { useConfig } from './useConfig';
import { useLoginInfo } from './useLoginInfo';
Expand Down Expand Up @@ -71,17 +73,21 @@ export function useTransaction(
setPending(true);
cb?.({ pending: true });

const sender = new Address(accountSnap.address);

const tx = new Transaction({
nonce: currentNonce,
receiver: new Address(address),
gasLimit,
chainID: configStateSnap.shortId || 'D',
data,
value: value || 0,
sender: new Address(accountSnap.address),
sender,
});

tx.setNonce(currentNonce);
const senderAccount = new Account(sender);
senderAccount.incrementNonce();
setAccountState('nonce', currentNonce + 1);

sendTxOperations(
networkStateSnap.dappProvider as DappProvider,
Expand Down

0 comments on commit 08ff5fb

Please sign in to comment.