import { PublicDataWrite, RevertCode } from '@aztec/circuits.js';
import { schemas } from '@aztec/foundation/schemas';
import { z } from 'zod';
import { L2BlockHash } from './block_hash.js';
import { TxHash } from './tx_hash.js';
/**
 * Possible status of a transaction.
 */
export var TxStatus;
(function (TxStatus) {
    TxStatus["DROPPED"] = "dropped";
    TxStatus["PENDING"] = "pending";
    TxStatus["SUCCESS"] = "success";
    TxStatus["APP_LOGIC_REVERTED"] = "app_logic_reverted";
    TxStatus["TEARDOWN_REVERTED"] = "teardown_reverted";
    TxStatus["BOTH_REVERTED"] = "both_reverted";
})(TxStatus || (TxStatus = {}));
/**
 * Represents a transaction receipt in the Aztec network.
 * Contains essential information about the transaction including its status, origin, and associated addresses.
 * REFACTOR: TxReceipt should be returned only once the tx is mined, and all its fields should be required.
 * We should not be using a TxReceipt to answer a query for a pending or dropped tx.
 */
export class TxReceipt {
    constructor(
    /** A unique identifier for a transaction. */
    txHash, 
    /** The transaction's status. */
    status, 
    /** Description of transaction error, if any. */
    error, 
    /** The transaction fee paid for the transaction. */
    transactionFee, 
    /** The hash of the block containing the transaction. */
    blockHash, 
    /** The block number in which the transaction was included. */
    blockNumber, 
    /** Information useful for testing/debugging, set when test flag is set to true in `waitOpts`. */
    debugInfo) {
        this.txHash = txHash;
        this.status = status;
        this.error = error;
        this.transactionFee = transactionFee;
        this.blockHash = blockHash;
        this.blockNumber = blockNumber;
        this.debugInfo = debugInfo;
    }
    static empty() {
        return new TxReceipt(TxHash.zero(), TxStatus.DROPPED, '');
    }
    static get schema() {
        return z
            .object({
            txHash: TxHash.schema,
            status: z.nativeEnum(TxStatus),
            error: z.string(),
            blockHash: L2BlockHash.schema.optional(),
            blockNumber: z.number().optional(),
            transactionFee: schemas.BigInt.optional(),
            debugInfo: DebugInfoSchema.optional(),
        })
            .transform(TxReceipt.from);
    }
    static from(fields) {
        return new TxReceipt(fields.txHash, fields.status, fields.error, fields.transactionFee, fields.blockHash, fields.blockNumber, fields.debugInfo);
    }
    static statusFromRevertCode(revertCode) {
        if (revertCode.equals(RevertCode.OK)) {
            return TxStatus.SUCCESS;
        }
        else if (revertCode.equals(RevertCode.APP_LOGIC_REVERTED)) {
            return TxStatus.APP_LOGIC_REVERTED;
        }
        else if (revertCode.equals(RevertCode.TEARDOWN_REVERTED)) {
            return TxStatus.TEARDOWN_REVERTED;
        }
        else if (revertCode.equals(RevertCode.BOTH_REVERTED)) {
            return TxStatus.BOTH_REVERTED;
        }
        else {
            throw new Error(`Unknown revert code: ${revertCode}`);
        }
    }
}
const DebugInfoSchema = z.object({
    noteHashes: z.array(schemas.Fr),
    nullifiers: z.array(schemas.Fr),
    publicDataWrites: z.array(PublicDataWrite.schema),
    l2ToL1Msgs: z.array(schemas.Fr),
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHhfcmVjZWlwdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy90eC90eF9yZWNlaXB0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxlQUFlLEVBQUUsVUFBVSxFQUFFLE1BQU0sb0JBQW9CLENBQUM7QUFFakUsT0FBTyxFQUFFLE9BQU8sRUFBRSxNQUFNLDJCQUEyQixDQUFDO0FBR3BELE9BQU8sRUFBRSxDQUFDLEVBQUUsTUFBTSxLQUFLLENBQUM7QUFFeEIsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLGlCQUFpQixDQUFDO0FBQzlDLE9BQU8sRUFBRSxNQUFNLEVBQUUsTUFBTSxjQUFjLENBQUM7QUFFdEM7O0dBRUc7QUFDSCxNQUFNLENBQU4sSUFBWSxRQU9YO0FBUEQsV0FBWSxRQUFRO0lBQ2xCLCtCQUFtQixDQUFBO0lBQ25CLCtCQUFtQixDQUFBO0lBQ25CLCtCQUFtQixDQUFBO0lBQ25CLHFEQUF5QyxDQUFBO0lBQ3pDLG1EQUF1QyxDQUFBO0l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