/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/
import * as z from "zod";
import { remap as remap$ } from "../lib/primitives.js";
import { safeParse } from "../lib/schemas.js";
import { Result as SafeParseResult } from "../types/fp.js";
import {
AccountIdentifier,
AccountIdentifier$inboundSchema,
AccountIdentifier$Outbound,
AccountIdentifier$outboundSchema,
} from "./accountidentifier.js";
import {
Amount,
Amount$inboundSchema,
Amount$Outbound,
Amount$outboundSchema,
} from "./amount.js";
import {
CoinChange,
CoinChange$inboundSchema,
CoinChange$Outbound,
CoinChange$outboundSchema,
} from "./coinchange.js";
import { SDKValidationError } from "./errors/sdkvalidationerror.js";
import {
OperationIdentifier,
OperationIdentifier$inboundSchema,
OperationIdentifier$Outbound,
OperationIdentifier$outboundSchema,
} from "./operationidentifier.js";
export type OperationMetadata = {};
/**
* Operations contain all balance-changing information within a transaction. They are always one-sided (only affect 1 AccountIdentifier) and can succeed or fail independently from a Transaction. Operations are used both to represent on-chain data (Data API) and to construct new transactions (Construction API), creating a standard interface for reading and writing to blockchains.
*/
export type Operation = {
/**
* The operation_identifier uniquely identifies an operation within a transaction.
*/
operationIdentifier: OperationIdentifier;
/**
* Restrict referenced related_operations to identifier indices < the current operation_identifier.index. This ensures there exists a clear DAG-structure of relations. Since operations are one-sided, one could imagine relating operations in a single transfer or linking operations in a call tree.
*/
relatedOperations?: Array<OperationIdentifier> | undefined;
/**
* Type is the network-specific type of the operation. Ensure that any type that can be returned here is also specified in the NetworkOptionsResponse. This can be very useful to downstream consumers that parse all block data.
*/
type: string;
/**
* Status is the network-specific status of the operation. Status is not defined on the transaction object because blockchains with smart contracts may have transactions that partially apply (some operations are successful and some are not). Blockchains with atomic transactions (all operations succeed or all operations fail) will have the same status for each operation. On-chain operations (operations retrieved in the `/block` and `/block/transaction` endpoints) MUST have a populated status field (anything on-chain must have succeeded or failed). However, operations provided during transaction construction (often times called "intent" in the documentation) MUST NOT have a populated status field (operations yet to be included on-chain have not yet succeeded or failed).
*/
status?: string | undefined;
/**
* The account_identifier uniquely identifies an account within a network. All fields in the account_identifier are utilized to determine this uniqueness (including the metadata field, if populated).
*/
account?: AccountIdentifier | undefined;
/**
* Amount is some Value of a Currency. It is considered invalid to specify a Value without a Currency.
*/
amount?: Amount | undefined;
/**
* CoinChange is used to represent a change in state of a some coin identified by a coin_identifier. This object is part of the Operation model and must be populated for UTXO-based blockchains. Coincidentally, this abstraction of UTXOs allows for supporting both account-based transfers and UTXO-based transfers on the same blockchain (when a transfer is account-based, don't populate this model).
*/
coinChange?: CoinChange | undefined;
metadata?: OperationMetadata | undefined;
};
/** @internal */
export const OperationMetadata$inboundSchema: z.ZodType<
OperationMetadata,
z.ZodTypeDef,
unknown
> = z.object({});
/** @internal */
export type OperationMetadata$Outbound = {};
/** @internal */
export const OperationMetadata$outboundSchema: z.ZodType<
OperationMetadata$Outbound,
z.ZodTypeDef,
OperationMetadata
> = z.object({});
/**
* @internal
* @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
*/
export namespace OperationMetadata$ {
/** @deprecated use `OperationMetadata$inboundSchema` instead. */
export const inboundSchema = OperationMetadata$inboundSchema;
/** @deprecated use `OperationMetadata$outboundSchema` instead. */
export const outboundSchema = OperationMetadata$outboundSchema;
/** @deprecated use `OperationMetadata$Outbound` instead. */
export type Outbound = OperationMetadata$Outbound;
}
export function operationMetadataToJSON(
operationMetadata: OperationMetadata,
): string {
return JSON.stringify(
OperationMetadata$outboundSchema.parse(operationMetadata),
);
}
export function operationMetadataFromJSON(
jsonString: string,
): SafeParseResult<OperationMetadata, SDKValidationError> {
return safeParse(
jsonString,
(x) => OperationMetadata$inboundSchema.parse(JSON.parse(x)),
`Failed to parse 'OperationMetadata' from JSON`,
);
}
/** @internal */
export const Operation$inboundSchema: z.ZodType<
Operation,
z.ZodTypeDef,
unknown
> = z.object({
operation_identifier: OperationIdentifier$inboundSchema,
related_operations: z.array(OperationIdentifier$inboundSchema).optional(),
type: z.string(),
status: z.string().optional(),
account: AccountIdentifier$inboundSchema.optional(),
amount: Amount$inboundSchema.optional(),
coin_change: CoinChange$inboundSchema.optional(),
metadata: z.lazy(() => OperationMetadata$inboundSchema).optional(),
}).transform((v) => {
return remap$(v, {
"operation_identifier": "operationIdentifier",
"related_operations": "relatedOperations",
"coin_change": "coinChange",
});
});
/** @internal */
export type Operation$Outbound = {
operation_identifier: OperationIdentifier$Outbound;
related_operations?: Array<OperationIdentifier$Outbound> | undefined;
type: string;
status?: string | undefined;
account?: AccountIdentifier$Outbound | undefined;
amount?: Amount$Outbound | undefined;
coin_change?: CoinChange$Outbound | undefined;
metadata?: OperationMetadata$Outbound | undefined;
};
/** @internal */
export const Operation$outboundSchema: z.ZodType<
Operation$Outbound,
z.ZodTypeDef,
Operation
> = z.object({
operationIdentifier: OperationIdentifier$outboundSchema,
relatedOperations: z.array(OperationIdentifier$outboundSchema).optional(),
type: z.string(),
status: z.string().optional(),
account: AccountIdentifier$outboundSchema.optional(),
amount: Amount$outboundSchema.optional(),
coinChange: CoinChange$outboundSchema.optional(),
metadata: z.lazy(() => OperationMetadata$outboundSchema).optional(),
}).transform((v) => {
return remap$(v, {
operationIdentifier: "operation_identifier",
relatedOperations: "related_operations",
coinChange: "coin_change",
});
});
/**
* @internal
* @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
*/
export namespace Operation$ {
/** @deprecated use `Operation$inboundSchema` instead. */
export const inboundSchema = Operation$inboundSchema;
/** @deprecated use `Operation$outboundSchema` instead. */
export const outboundSchema = Operation$outboundSchema;
/** @deprecated use `Operation$Outbound` instead. */
export type Outbound = Operation$Outbound;
}
export function operationToJSON(operation: Operation): string {
return JSON.stringify(Operation$outboundSchema.parse(operation));
}
export function operationFromJSON(
jsonString: string,
): SafeParseResult<Operation, SDKValidationError> {
return safeParse(
jsonString,
(x) => Operation$inboundSchema.parse(JSON.parse(x)),
`Failed to parse 'Operation' from JSON`,
);
}