The UTxO management system provides functions for handling unspent transaction outputs.
// src/utils/wallet-stuff.ts
// Gets the most recent UTxOs for a given wallet UTxO ID
// Recursively traverses the UTxO map to find the latest UTxOs
const getMostRecentUtxos = (
walletUtxoMap: WalletUtxoMap,
walletUtxoId: string
): Server.Utxo[] => {
const outputUtxosRef =
walletUtxoMap.outputUtxosRefByWalletUtxoId[walletUtxoId];
if (outputUtxosRef === undefined) {
return [];
} else {
const outputUtxos =
walletUtxoMap.outputUtxosByOutputUtxosRef[outputUtxosRef];
let finalMostRecentUtxos: Server.Utxo[] = [];
for (const outputUtxo of outputUtxos) {
const outputWalletUtxoId = utxoToWalletUtxoId(outputUtxo);
const mostRecentUtxos = getMostRecentUtxos(
walletUtxoMap,
outputWalletUtxoId
);
if (mostRecentUtxos.length === 0) {
finalMostRecentUtxos.push(outputUtxo);
} else {
finalMostRecentUtxos = finalMostRecentUtxos.concat(mostRecentUtxos);
}
}
return Array.from(new Set(finalMostRecentUtxos));
}
};
// src/utils/wallet-stuff.ts
// Removes UTxOs that are already on-chain from the wallet UTxO map
export const pruneWalletUtxoMap = (
walletUtxoMap: WalletUtxoMap,
lucidWalletUtxos: Lucid.UTxO[]
) => {
const onchainTxHashes = new Set(lucidWalletUtxos.map((u) => u.txHash));
pruneTxHashes(walletUtxoMap, onchainTxHashes);
};
// src/utils/wallet-stuff.ts
// Gets the most recent UTxOs from Lucid UTxOs
// Returns a tuple of [remaining Lucid UTxOs, most recent server UTxOs]
export const getMostRecentUtxosFromLucidUtxos = (
walletUtxoMap: WalletUtxoMap,
lucidWalletUtxos: Lucid.UTxO[]
): [Lucid.UTxO[], Server.Utxo[]] => {
pruneWalletUtxoMap(walletUtxoMap, lucidWalletUtxos);
const remainingLucidUtxos: Lucid.UTxO[] = [];
let finalServerUtxos: Server.Utxo[] = [];
for (const lucidUtxo of lucidWalletUtxos) {
const serverUtxos = getMostRecentUtxos(
walletUtxoMap,
lucidUtxoToWalletUtxoId(lucidUtxo)
);
if (serverUtxos.length === 0) {
remainingLucidUtxos.push(lucidUtxo);
} else {
finalServerUtxos = finalServerUtxos.concat(serverUtxos);
}
}
return [remainingLucidUtxos, Array.from(new Set(finalServerUtxos))];
};