Skip to content

Commit

Permalink
feat: migration started
Browse files Browse the repository at this point in the history
  • Loading branch information
keyan-m committed Oct 16, 2024
1 parent a2367fa commit ed235cb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 71 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"test": "test"
},
"dependencies": {
"@lucid-evolution/lucid": "^0.3.18"
"@lucid-evolution/lucid": "^0.3.18",
"effect": "^3.7.3"
},
"packageManager": "pnpm@9.7.1+sha512.faf344af2d6ca65c4c5c8c2224ea77a81a5e8859cbc4e06b1511ddce2f0151512431dd19e6aff31f2c6a8f5f2aced9bd2273e1fed7dd4de1868984059d2c4247"
}
37 changes: 20 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 68 additions & 53 deletions src/core/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
} from "@lucid-evolution/lucid";
import { AddressD, Value } from "../contract.types.js";
import { Either, ReadableUTxO, Result } from "../types.js";
import { Effect, pipe } from "effect";
import { UnknownException } from "effect/Cause";

export function ok<T>(x: T): Result<T> {
return {
Expand All @@ -28,79 +30,92 @@ export function ok<T>(x: T): Result<T> {
};
}

export const utxosAtScript = async (
export const utxosAtScript = (
lucid: LucidEvolution,
script: string,
stakeCredentialHash?: string
) => {
const network = lucid.config().network;
): Effect.Effect<UTxO[], UnknownException, never> =>
Effect.gen(function* () {
const network = lucid.config().network;

const scriptValidator: SpendingValidator = {
type: "PlutusV2",
script: script,
};
const scriptValidator: SpendingValidator = {
type: "PlutusV2",
script: script,
};

const scriptValidatorAddr = stakeCredentialHash
? validatorToAddress(
network,
scriptValidator,
keyHashToCredential(stakeCredentialHash)
)
: validatorToAddress(network, scriptValidator);
const scriptValidatorAddr = stakeCredentialHash
? validatorToAddress(
network,
scriptValidator,
keyHashToCredential(stakeCredentialHash)
)
: validatorToAddress(network, scriptValidator);

return lucid.utxosAt(scriptValidatorAddr);
};
return yield* Effect.tryPromise(() => lucid.utxosAt(scriptValidatorAddr));
});

export const parseSafeDatum = <T>(
datum: string | null | undefined,
datumType: T
): Either<string, T> => {
if (datum) {
try {
const parsedDatum = Data.from(datum, datumType);
return {
type: "right",
value: parsedDatum,
};
} catch (error) {
return { type: "left", value: `invalid datum : ${error}` };
}
} else {
return { type: "left", value: "missing datum" };
}
};
): Effect.Effect<T, UnknownException, never> =>
Effect.gen(function* () {
if (!datum)
yield* Effect.fail<UnknownException>(
new UnknownException(new Error("missing datum"))
);
const parsedDatum = yield* Effect.try<T, UnknownException>({
try: () => Data.from(datum!, datumType),
catch: (error) => new UnknownException(error, "invalid datum"),
});
return parsedDatum;
});

export const parseUTxOsAtScript = async <T>(
export const parseUTxOsAtScript = <T>(
lucid: LucidEvolution,
script: string,
datumType: T,
stakeCredentialHash?: string
): Promise<ReadableUTxO<T>[]> => {
try {
const utxos = await utxosAtScript(
lucid,
script,
stakeCredentialHash
);
return utxos.flatMap((utxo) => {
const result = parseSafeDatum<T>(utxo.datum, datumType);
if (result.type == "right") {
return {
) =>
pipe(
utxosAtScript(lucid, script, stakeCredentialHash),
Effect.flatMap((utxos): Effect.Effect<ReadableUTxO<T>, UnknownException> =>
pipe(
Effect.all(
utxos.map((utxo) => parseSafeDatum<T>(utxo.datum, datumType))
),
Effect.map((parsed) => ({
outRef: {
txHash: utxo.txHash,
outputIndex: utxo.outputIndex,
},
datum: result.value,
datum: parsed,
assets: utxo.assets,
};
} else {
return [];
}
});
} catch (e) {
return [];
}
};
}))
)
)
);
// pipe(
// utxosAtScript(lucid, script, stakeCredentialHash),
// Effect.map((utxos: UTxO[]) => pipe(
// utxos,
// Effect.flatMap(
// (utxo: UTxO): Effect.Effect<ReadableUTxO<T>, UnknownException, never> =>
// parseSafeDatum<T>(utxo.datum, datumType).pipe(
// Effect.map(
// (parsed) =>
// ({
// outRef: {
// txHash: utxo.txHash,
// outputIndex: utxo.outputIndex,
// },
// datum: parsed,
// assets: utxo.assets,
// } as ReadableUTxO<T>)
// )
// )
// ))
// )
// );

export const toCBORHex = (rawHex: string) => {
return applyDoubleCborEncoding(rawHex);
Expand Down

0 comments on commit ed235cb

Please sign in to comment.