|
| 1 | +import type * as Assets from "@evolution-sdk/evolution/sdk/Assets" |
| 2 | +import type * as Datum from "@evolution-sdk/evolution/sdk/Datum" |
| 3 | +import type * as Script from "@evolution-sdk/evolution/sdk/Script" |
| 4 | +import type * as UTxO from "@evolution-sdk/evolution/sdk/UTxO" |
| 5 | + |
| 6 | +/** |
| 7 | + * Options for creating a test UTxO. |
| 8 | + */ |
| 9 | +export type CreateTestUtxoOptions = { |
| 10 | + /** |
| 11 | + * The address of the UTxO. Defaults to a test address. |
| 12 | + * @default "addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgs68faae" |
| 13 | + */ |
| 14 | + address?: string |
| 15 | + /** |
| 16 | + * Optional datum to attach to the UTxO. |
| 17 | + */ |
| 18 | + datumOption?: Datum.Datum |
| 19 | + /** |
| 20 | + * The amount of lovelace in the UTxO. |
| 21 | + */ |
| 22 | + lovelace: bigint |
| 23 | + /** |
| 24 | + * Optional native assets to include in the UTxO. |
| 25 | + * Map of policyId+assetName (hex encoded) to quantity. |
| 26 | + */ |
| 27 | + nativeAssets?: Record<string, bigint> |
| 28 | + /** |
| 29 | + * The output index. Defaults to 0. |
| 30 | + * @default 0 |
| 31 | + */ |
| 32 | + outputIndex?: number |
| 33 | + /** |
| 34 | + * Optional reference script to attach to the UTxO. |
| 35 | + */ |
| 36 | + scriptRef?: Script.Script |
| 37 | + /** |
| 38 | + * The transaction hash. Defaults to 64 zeros. |
| 39 | + * @default "0".repeat(64) |
| 40 | + */ |
| 41 | + txHash?: string |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Default test address used when no address is provided. |
| 46 | + */ |
| 47 | +const DEFAULT_TEST_ADDRESS = |
| 48 | + "addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgs68faae" |
| 49 | + |
| 50 | +/** |
| 51 | + * Creates a test UTxO with the specified parameters. |
| 52 | + */ |
| 53 | +export const createTestUtxo = (options: CreateTestUtxoOptions): UTxO.UTxO => { |
| 54 | + const { |
| 55 | + address = DEFAULT_TEST_ADDRESS, |
| 56 | + datumOption, |
| 57 | + lovelace, |
| 58 | + nativeAssets, |
| 59 | + outputIndex = 0, |
| 60 | + scriptRef, |
| 61 | + txHash = "0".repeat(64) |
| 62 | + } = options |
| 63 | + |
| 64 | + // Ensure txHash is 64 hex characters (convert short IDs to valid hex) |
| 65 | + const paddedTxHash = txHash.length === 64 && /^[0-9a-fA-F]+$/.test(txHash) |
| 66 | + ? txHash |
| 67 | + : Array.from(txHash) |
| 68 | + .map(c => c.charCodeAt(0).toString(16).padStart(2, '0')) |
| 69 | + .join('') |
| 70 | + .padEnd(64, '0') |
| 71 | + |
| 72 | + const assets: Assets.Assets = nativeAssets |
| 73 | + ? { lovelace, ...nativeAssets } |
| 74 | + : { lovelace } |
| 75 | + |
| 76 | + return { |
| 77 | + address, |
| 78 | + assets, |
| 79 | + datumOption, |
| 80 | + outputIndex, |
| 81 | + scriptRef, |
| 82 | + txHash: paddedTxHash |
| 83 | + } |
| 84 | +} |
0 commit comments