Skip to content

Commit

Permalink
Merge pull request #315 from multiversx/tx-watcher-is-completed-fix
Browse files Browse the repository at this point in the history
Check if isCompleted is undefined
  • Loading branch information
popenta authored Aug 22, 2023
2 parents 2ffd086 + 11ea6ec commit 5b5a69e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
28 changes: 28 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"devDependencies": {
"@multiversx/sdk-network-providers": "1.2.1",
"@multiversx/sdk-network-providers-next": "npm:@multiversx/[email protected]",
"@multiversx/sdk-wallet": "3.0.0",
"@multiversx/sdk-wallet-next": "npm:@multiversx/[email protected]",
"@types/assert": "1.4.6",
Expand Down
6 changes: 6 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,9 @@ export class ErrGasLimitShouldBe0ForInnerTransaction extends Err {
super("gas limit must be 0 for the inner transaction for relayed v2");
}
}

export class ErrIsCompletedFieldIsMissingOnTransaction extends Err {
public constructor() {
super("The transaction watcher requires the `isCompleted` property to be defined on the transaction object. Perhaps you've used the sdk-network-provider's `getTransaction()` and in that case you should also pass `withProcessStatus=true`.")
}
}
4 changes: 2 additions & 2 deletions src/interfaceOfNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export interface INetworkConfig {
}

export interface ITransactionOnNetwork {
isCompleted: boolean;
isCompleted?: boolean;

hash: string;
type: string;
value: string;
Expand Down
39 changes: 37 additions & 2 deletions src/transaction.local.net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TokenTransfer } from "./tokenTransfer";
import { Transaction } from "./transaction";
import { TransactionPayload } from "./transactionPayload";
import { TransactionWatcher } from "./transactionWatcher";
import { ProxyNetworkProvider } from "@multiversx/sdk-network-providers-next";

describe("test transaction", function () {
let alice: TestWallet, bob: TestWallet;
Expand All @@ -15,8 +16,8 @@ describe("test transaction", function () {
({ alice, bob } = await loadTestWallets());
});

it("should send transactions", async function () {
this.timeout(30000);
it("should send transactions and wait for completion", async function () {
this.timeout(70000);

let provider = createLocalnetProvider();
let watcher = new TransactionWatcher(provider);
Expand Down Expand Up @@ -62,6 +63,40 @@ describe("test transaction", function () {
assert.deepEqual(TokenTransfer.egldFromAmount(85).valueOf(), newBalanceOfBob.minus(initialBalanceOfBob));
});

it("should send transaction and wait for completion using the new proxy provider", async function () {
this.timeout(70000);

let provider = createLocalnetProvider();
let newProvider = new ProxyNetworkProvider("http://localhost:7950", { timeout: 5000 });
let watcher = new TransactionWatcher({
getTransaction: async (hash: string) => { return await newProvider.getTransaction(hash, true) }
});

let network = await provider.getNetworkConfig();

await alice.sync(provider);
await bob.sync(provider);
let initialBalanceOfBob = new BigNumber(bob.account.balance.toString());

let transactionOne = new Transaction({
sender: alice.address,
receiver: bob.address,
value: TokenTransfer.egldFromAmount(42),
gasLimit: network.MinGasLimit,
chainID: network.ChainID
});

transactionOne.setNonce(alice.account.nonce);
await signTransaction({ transaction: transactionOne, wallet: alice });
await provider.sendTransaction(transactionOne);
await watcher.awaitCompleted(transactionOne);

await bob.sync(provider);
let newBalanceOfBob = new BigNumber(bob.account.balance.toString());

assert.deepEqual(TokenTransfer.egldFromAmount(42).valueOf(), newBalanceOfBob.minus(initialBalanceOfBob));
});

it("should simulate transactions", async function () {
this.timeout(20000);

Expand Down
14 changes: 12 additions & 2 deletions src/transactionWatcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AsyncTimer } from "./asyncTimer";
import { Err, ErrExpectedTransactionEventsNotFound, ErrExpectedTransactionStatusNotReached } from "./errors";
import { Err, ErrExpectedTransactionEventsNotFound, ErrExpectedTransactionStatusNotReached, ErrIsCompletedFieldIsMissingOnTransaction } from "./errors";
import { ITransactionFetcher } from "./interface";
import { ITransactionEvent, ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork";
import { Logger } from "./logger";
Expand Down Expand Up @@ -69,7 +69,13 @@ export class TransactionWatcher {
* Waits until the transaction is completely processed.
*/
public async awaitCompleted(transaction: ITransaction): Promise<ITransactionOnNetwork> {
const isCompleted = (transactionOnNetwork: ITransactionOnNetwork) => transactionOnNetwork.isCompleted;
const isCompleted = (transactionOnNetwork: ITransactionOnNetwork) => {
if (transactionOnNetwork.isCompleted === undefined) {
throw new ErrIsCompletedFieldIsMissingOnTransaction();
}
return transactionOnNetwork.isCompleted
};

const doFetch = async () => await this.fetcher.getTransaction(transaction.getHash().hex());
const errorProvider = () => new ErrExpectedTransactionStatusNotReached();

Expand Down Expand Up @@ -155,6 +161,10 @@ export class TransactionWatcher {
} catch (error) {
Logger.debug("TransactionWatcher.awaitConditionally(): cannot (yet) fetch data.");

if (error instanceof ErrIsCompletedFieldIsMissingOnTransaction) {
throw error;
}

if (!(error instanceof Err)) {
throw error;
}
Expand Down

0 comments on commit 5b5a69e

Please sign in to comment.