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

Update sendAndWait and waitForConfirmation methods #811

Merged
merged 5 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ from `algosdk` and sends it to the network.

#### @algo-builder/web

- `executeTx` promise now returns [TxnReceipt](https://github.com/scale-it/algo-builder/blob/master/packages/web/src/types.ts#L458) instead of `algosdk.modelsv2.PendingTransactionResponse`.
- `executeTx`, `sendAndWait`, `waitForConfirmation`, `sendTxAndWait` promise now returns [TxnReceipt](https://github.com/scale-it/algo-builder/blob/master/packages/web/src/types.ts#L458) instead of `algosdk.modelsv2.PendingTransactionResponse`.

#### @algo-builder/algob
- `sendAndWait`, `waitForConfirmation`, `executeSignedTxnFromFile`, `sendTxAndWait` promise now returns `TxnReceipt` instead of `ConfirmedTxInfo`.
- `logTx` second argument of `txConfirmation` now expects type of `TxnReceipt` instead of `ConfirmedTxInfo`.

### Examples

Expand Down
14 changes: 7 additions & 7 deletions packages/algob/src/internal/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import type {
ASCCache,
CheckpointFunctions,
CheckpointRepo,
ConfirmedTxInfo,
Deployer,
FundASCFlags,
LogicSig,
Expand Down Expand Up @@ -127,7 +126,7 @@ class DeployerBasicMode {
return this.algoOp.algodClient;
}

async waitForConfirmation(txId: string): Promise<ConfirmedTxInfo> {
async waitForConfirmation(txId: string): Promise<TxnReceipt> {
return await this.algoOp.waitForConfirmation(txId);
}

Expand Down Expand Up @@ -270,11 +269,12 @@ class DeployerBasicMode {
* Send signed transaction to network and wait for confirmation
* @param rawTxns Signed Transaction(s)
* @param waitRounds number of rounds to wait for transaction to be confirmed - default is 10
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
sendAndWait(
rawTxns: Uint8Array | Uint8Array[],
waitRounds = wtypes.WAIT_ROUNDS
): Promise<ConfirmedTxInfo> {
): Promise<TxnReceipt> {
return this.algoOp.sendAndWait(rawTxns, waitRounds);
}

Expand Down Expand Up @@ -560,9 +560,9 @@ class DeployerBasicMode {
* Sends signedTransaction and waits for the response
* @param transactions array of signedTransaction objects.
* @param rounds number of rounds to wait for response
* @returns ConfirmedTxInfo
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
sendTxAndWait(transactions: SignedTransaction[], rounds?: number): Promise<ConfirmedTxInfo> {
sendTxAndWait(transactions: SignedTransaction[], rounds?: number): Promise<TxnReceipt> {
if (transactions.length < 1) {
throw Error("No transactions to process");
} else {
Expand Down Expand Up @@ -656,7 +656,7 @@ export class DeployerDeployMode extends DeployerBasicMode implements Deployer {
/**
* Log transaction with message using txwriter
*/
logTx(message: string, txConfirmation: ConfirmedTxInfo): void {
logTx(message: string, txConfirmation: TxnReceipt): void {
this.txWriter.push(message, txConfirmation);
}

Expand Down Expand Up @@ -1013,7 +1013,7 @@ export class DeployerRunMode extends DeployerBasicMode implements Deployer {
});
}

logTx(message: string, txConfirmation: ConfirmedTxInfo): void {
logTx(message: string, txConfirmation: TxnReceipt): void {
throw new BuilderError(ERRORS.BUILTIN_TASKS.DEPLOYER_EDIT_OUTSIDE_DEPLOY, {
methodName: "logTx",
});
Expand Down
21 changes: 13 additions & 8 deletions packages/algob/src/lib/algo-operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface AlgoOperator {
txWriter: txWriter,
scTmplParams?: SCParams
) => Promise<rtypes.AppInfo>;
waitForConfirmation: (txId: string) => Promise<ConfirmedTxInfo>;
waitForConfirmation: (txId: string) => Promise<TxnReceipt>;
getAssetByID: (assetIndex: number | bigint) => Promise<modelsv2.Asset>;
optInAccountToASA: (
asaName: string,
Expand Down Expand Up @@ -120,7 +120,7 @@ export interface AlgoOperator {
sendAndWait: (
rawTxns: Uint8Array | Uint8Array[],
waitRounds: number
) => Promise<ConfirmedTxInfo>;
) => Promise<TxnReceipt>;
getReceiptTxns: (txns: Transaction[]) => Promise<TxnReceipt[]>;
}

Expand All @@ -136,28 +136,33 @@ export class AlgoOperatorImpl implements AlgoOperator {
* Send signed transaction to network and wait for confirmation
* @param rawTxns Signed Transaction(s)
* @param waitRounds number of rounds to wait for transaction to be confirmed - default is 10
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async sendAndWait(
rawTxns: Uint8Array | Uint8Array[],
waitRounds = wtypes.WAIT_ROUNDS
): Promise<ConfirmedTxInfo> {
): Promise<TxnReceipt> {
const txInfo = await this.algodClient.sendRawTransaction(rawTxns).do();
return await this.waitForConfirmation(txInfo.txId, waitRounds);
}

// Source:
// https://github.com/algorand/docs/blob/master/examples/assets/v2/javascript/AssetExample.js#L21
// Function used to wait for a tx confirmation
/**
* Source: https://github.com/algorand/docs/blob/master/examples/assets/v2/javascript/AssetExample.js#L21
* Function used to wait for a tx confirmation
* @param txId txn ID for which confirmation is required
* @param waitRounds number of rounds to wait for transaction to be confirmed - default is 10
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async waitForConfirmation(
txId: string,
waitRounds = wtypes.WAIT_ROUNDS
): Promise<ConfirmedTxInfo> {
): Promise<TxnReceipt> {
const pendingInfo = await algosdk.waitForConfirmation(this.algodClient, txId, waitRounds);
if (pendingInfo["pool-error"]) {
throw new Error(`Transaction Pool Error: ${pendingInfo["pool-error"] as string}`);
}
if (pendingInfo[confirmedRound] !== null && pendingInfo[confirmedRound] > 0) {
return pendingInfo as ConfirmedTxInfo;
return { txID: txId, ...pendingInfo as ConfirmedTxInfo } as TxnReceipt
}
throw new Error("timeout");
}
Expand Down
5 changes: 3 additions & 2 deletions packages/algob/src/lib/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@algo-builder/web";
import algosdk, { decodeSignedTransaction, SuggestedParams, Transaction } from "algosdk";

import { ConfirmedTxInfo, Deployer, TxnReceipt } from "../types";
import { Deployer, TxnReceipt } from "../types";
import { loadEncodedTxFromFile } from "./files";
import { registerCheckpoints } from "./script-checkpoints";

Expand Down Expand Up @@ -331,11 +331,12 @@ export async function executeTx(
* current network's blockchain block height.
* @param deployer Deployer
* @param fileName raw(encoded) signed txn file
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
export async function executeSignedTxnFromFile(
deployer: Deployer,
fileName: string
): Promise<ConfirmedTxInfo> {
): Promise<TxnReceipt> {
const signedTxn = loadEncodedTxFromFile(fileName);
if (signedTxn === undefined) {
throw new Error(`File ${fileName} does not exist`);
Expand Down
7 changes: 4 additions & 3 deletions packages/algob/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,14 @@ export interface Deployer {

registerSSCInfo: (name: string, sscInfo: rtypes.AppInfo) => void;

logTx: (message: string, txConfirmation: ConfirmedTxInfo) => void;
logTx: (message: string, txConfirmation: TxnReceipt) => void;

/**
* Send signed transaction to network and wait for confirmation
* @param rawTxns Signed Transaction(s)
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
sendAndWait: (rawTxns: Uint8Array | Uint8Array[]) => Promise<ConfirmedTxInfo>;
sendAndWait: (rawTxns: Uint8Array | Uint8Array[]) => Promise<TxnReceipt>;

/**
* Return receipts for each transaction in group txn
Expand Down Expand Up @@ -605,7 +606,7 @@ export interface Deployer {

/**
* Queries blockchain for a given transaction and waits until it will be processed. */
waitForConfirmation: (txId: string) => Promise<ConfirmedTxInfo>;
waitForConfirmation: (txId: string) => Promise<TxnReceipt>;

/**
* Queries blockchain using algodv2 for asset information by index */
Expand Down
4 changes: 2 additions & 2 deletions packages/algob/test/lib/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { TextEncoder } from "util";

import { DeployerDeployMode, DeployerRunMode } from "../../src/internal/deployer";
import { DeployerConfig } from "../../src/internal/deployer_cfg";
import { ConfirmedTxInfo, Deployer, TxnReceipt } from "../../src/types";
import { Deployer, TxnReceipt } from "../../src/types";
import { expectBuilderError, expectBuilderErrorAsync } from "../helpers/errors";
import { mkEnv } from "../helpers/params";
import { useFixtureProject, useFixtureProjectCopy } from "../helpers/project";
Expand Down Expand Up @@ -178,7 +178,7 @@ describe("ASA modify fields", function () {
* Verifies correct asset fields are sent to network
* @param rawTxns rawTxns Signed transactions in Uint8Array
*/
function checkTx(rawTxns: Uint8Array | Uint8Array[]): Promise<ConfirmedTxInfo> {
function checkTx(rawTxns: Uint8Array | Uint8Array[]): Promise<TxnReceipt> {
if (Array.isArray(rawTxns)) {
// verify here if group tx
} else {
Expand Down
9 changes: 4 additions & 5 deletions packages/algob/test/mocks/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import algosdk, { Account, LogicSigAccount, modelsv2 } from "algosdk";

import type {
ASCCache,
ConfirmedTxInfo,
Deployer,
FundASCFlags,
LogicSig,
Expand Down Expand Up @@ -63,11 +62,11 @@ export class FakeDeployer implements Deployer {
throw new Error("Not implemented");
}

logTx(message: string, txConfirmation: ConfirmedTxInfo): void {
logTx(message: string, txConfirmation: TxnReceipt): void {
throw new Error("Not implemented");
}

sendAndWait(rawTxns: Uint8Array | Uint8Array[]): Promise<ConfirmedTxInfo> {
sendAndWait(rawTxns: Uint8Array | Uint8Array[]): Promise<TxnReceipt> {
throw new Error("Not implemented");
}

Expand Down Expand Up @@ -119,7 +118,7 @@ export class FakeDeployer implements Deployer {
throw new Error("Not implemented");
}

addCheckpointKV(key: string, value: string): void {} // eslint-disable-line @typescript-eslint/no-empty-function
addCheckpointKV(key: string, value: string): void { } // eslint-disable-line @typescript-eslint/no-empty-function

getCheckpointKV(key: string): string | undefined {
return "metadata";
Expand Down Expand Up @@ -237,7 +236,7 @@ export class FakeDeployer implements Deployer {
throw new Error("Not implemented");
}

waitForConfirmation(txId: string): Promise<ConfirmedTxInfo> {
waitForConfirmation(txId: string): Promise<TxnReceipt> {
throw new Error("Not implemented");
}

Expand Down
5 changes: 3 additions & 2 deletions packages/algob/test/mocks/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import algosdk, {
SuggestedParams,
} from "algosdk";

import { ConfirmedTxInfo } from "../../src/types";
import { TxnReceipt } from "../../src/types";
import { bobAcc } from "./account";

export const mockAlgod = new Algodv2("dummyToken", "https://dummyNetwork", 8080);
Expand Down Expand Up @@ -104,7 +104,8 @@ export const TXN_OBJ: algosdk.EncodedTransaction = {
nonpart: true,
};

export const mockConfirmedTx: ConfirmedTxInfo = {
export const mockTxnReceipt: TxnReceipt = {
txID: "1",
"confirmed-round": 1,
"asset-index": 1,
"application-index": 1,
Expand Down
9 changes: 4 additions & 5 deletions packages/algob/test/stubs/algo-operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { txWriter } from "../../src/internal/tx-log-writer";
import { AlgoOperator } from "../../src/lib/algo-operator";
import {
ASCCache,
ConfirmedTxInfo,
FundASCFlags,
LsigInfo,
SCParams,
Expand All @@ -16,8 +15,8 @@ import {
MOCK_APPLICATION_ADDRESS,
mockAlgod,
mockAssetInfo,
mockConfirmedTx,
mockPendingTransactionInformation,
mockTxnReceipt,
} from "../mocks/tx";

export class AlgoOperatorDryRunImpl implements AlgoOperator {
Expand All @@ -31,13 +30,13 @@ export class AlgoOperatorDryRunImpl implements AlgoOperator {
});
}

sendAndWait(rawTxns: Uint8Array | Uint8Array[]): Promise<ConfirmedTxInfo> {
sendAndWait(rawTxns: Uint8Array | Uint8Array[]): Promise<TxnReceipt> {
return new Promise((resolve, _reject) => {
resolve(mockConfirmedTx);
resolve(mockTxnReceipt);
});
}

waitForConfirmation(_txID: string): Promise<ConfirmedTxInfo> {
waitForConfirmation(_txID: string): Promise<TxnReceipt> {
return this.sendAndWait([]);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ export class Runtime {
/**
* Sends signedTransaction and waits for the response
* @param transactions array of signedTransaction objects.
* @returns TxnReceipt
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
sendTxAndWait(transactions: SignedTransaction[]): TxnReceipt[] {
return this.executeTx(transactions);
Expand Down
12 changes: 9 additions & 3 deletions packages/web/src/lib/myalgowallet-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class MyAlgoWalletSession {
* Send signed transaction to network and wait for confirmation
* @param rawTxns Signed Transaction(s)
* @param waitRounds number of rounds to wait for transaction to be confirmed - default is 10
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async sendAndWait(
rawTxns: Uint8Array | Uint8Array[],
Expand All @@ -152,8 +153,13 @@ export class MyAlgoWalletSession {
return await this.waitForConfirmation(txInfo.txId, waitRounds);
}

// Function used to wait for a tx confirmation
private async waitForConfirmation(
/**
* Function used to wait for a tx confirmation
* @param txId txn ID for which confirmation is required
* @param waitRounds number of rounds to wait for transaction to be confirmed - default is 10
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async waitForConfirmation(
txId: string,
waitRounds = WAIT_ROUNDS
): Promise<TxnReceipt> {
Expand Down Expand Up @@ -262,7 +268,7 @@ export class MyAlgoWalletSession {
* Sends signedTransaction and waits for the response
* @param transactions array of signedTransaction objects.
* @param rounds number of rounds to wait for response
* @returns TxnReceipt
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async sendTxAndWait(transactions: SignedTransaction[], rounds?: number): Promise<TxnReceipt> {
if (transactions.length < 1) {
Expand Down
12 changes: 9 additions & 3 deletions packages/web/src/lib/wallectconnect-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export class WallectConnectSession {
* Send signed transaction to network and wait for confirmation
* @param rawTxns Signed Transaction(s)
* @param waitRounds number of rounds to wait for transaction to be confirmed - default is 10
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async sendAndWait(
rawTxns: Uint8Array | Uint8Array[],
Expand All @@ -196,8 +197,13 @@ export class WallectConnectSession {
return await this.waitForConfirmation(txInfo.txId, waitRounds);
}

// Function used to wait for a tx confirmation
private async waitForConfirmation(
/**
* Function used to wait for a tx confirmation
* @param txId txn ID for which confirmation is required
* @param waitRounds number of rounds to wait for transaction to be confirmed - default is 10
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async waitForConfirmation(
txId: string,
waitRounds = WAIT_ROUNDS
): Promise<TxnReceipt> {
Expand Down Expand Up @@ -322,7 +328,7 @@ export class WallectConnectSession {
* Sends signedTransaction and waits for the response
* @param transactions array of signedTransaction objects.
* @param rounds number of rounds to wait for response
* @returns TxnReceipt
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async sendTxAndWait(transactions: SignedTransaction[], rounds?: number): Promise<TxnReceipt> {
if (transactions.length < 1) {
Expand Down
4 changes: 3 additions & 1 deletion packages/web/src/lib/web-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class WebMode {
* wait for confirmation for transaction using transaction id
* @param txId Transaction id
* @param waitRounds number of rounds to wait for transaction to be confirmed - default is 10
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async waitForConfirmation(
txId: string,
Expand Down Expand Up @@ -77,6 +78,7 @@ export class WebMode {
* Send signed transaction to network and wait for confirmation
* @param signedTxn Signed Transaction blob encoded in base64
* @param waitRounds number of rounds to wait for transaction to be confirmed - default is 10
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async sendAndWait(signedTxn: string, waitRounds: number = WAIT_ROUNDS): Promise<TxnReceipt> {
const txInfo = await this.algoSigner.send({
Expand Down Expand Up @@ -347,7 +349,7 @@ export class WebMode {
* Sends signedTransaction and waits for the response
* @param transactions array of signedTransaction objects.
* @param rounds number of rounds to wait for response
* @returns TxnReceipt
* @returns TxnReceipt which includes confirmed txn response along with txID
*/
async sendTxAndWait(transactions: SignedTransaction[], rounds?: number): Promise<TxnReceipt> {
if (transactions.length < 1) {
Expand Down