Skip to content

Commit

Permalink
fixes and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Aug 11, 2023
1 parent 59c9e41 commit 68f55e4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 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`.")
}
}
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
8 changes: 6 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 @@ -71,7 +71,7 @@ export class TransactionWatcher {
public async awaitCompleted(transaction: ITransaction): Promise<ITransactionOnNetwork> {
const isCompleted = (transactionOnNetwork: ITransactionOnNetwork) => {
if (transactionOnNetwork.isCompleted === undefined) {
throw new Err("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`.");
throw new ErrIsCompletedFieldIsMissingOnTransaction();
}
return transactionOnNetwork.isCompleted
};
Expand Down Expand Up @@ -161,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 68f55e4

Please sign in to comment.