Skip to content

Commit

Permalink
feat: add effectful system
Browse files Browse the repository at this point in the history
  • Loading branch information
solidsnakedev committed Jan 28, 2024
1 parent 93df385 commit 0d22a32
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 231 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
"@types/node": "^20.10.8",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
"effect": "^2.2.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"fast-check": "^3.15.0",
"ts-node": "^10.9.2",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
Expand Down
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

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

16 changes: 16 additions & 0 deletions src/core/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Data} from "effect"
export class NoUTXOsInScriptError {
readonly _tag = "NoUTXOsInScriptError";
}
export class MissingDatumError {
readonly _tag = "NoUTXOsInScriptError";
}

export class InvalidDatumError {
readonly _tag = "InvalidDatumError";
}

export class TransactionError extends Data.TaggedError("TransactionError")<{
message: string
}> {}

21 changes: 20 additions & 1 deletion src/core/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {
getAddressDetails,
Lucid,
SpendingValidator,
} from "@anastasia-labs/lucid-cardano-fork"
} from "@anastasia-labs/lucid-cardano-fork";
import { AddressD } from "../contract.types.js";
import { Either, ReadableUTxO, Result } from "../types.js";
import { Either as E, pipe } from "effect";
import { InvalidDatumError, MissingDatumError } from "../errors.js";

export const utxosAtScript = async (
lucid: Lucid,
Expand All @@ -32,6 +34,23 @@ export const utxosAtScript = async (
return lucid.utxosAt(scriptValidatorAddr);
};

export const safeParseDatum = <T>(
datum: string | null | undefined,
datumType: T
) =>
pipe(
datum,
E.fromNullable(() => new MissingDatumError()),
E.flatMap((d) =>
E.try({
try: () => {
return Data.from(d, datumType);
},
catch: (_e) => new InvalidDatumError()
})
)
);

export const parseSafeDatum = <T>(
lucid: Lucid,
datum: string | null | undefined,
Expand Down
29 changes: 22 additions & 7 deletions src/endpoints/Contract.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
import {
ContractConfig,
LockTokensConfig,
Lucid,
Result,
TxComplete,
VestingDatum,
collectVestingTokens,
getVestingByAddress,
lockTokens,
parseUTxOsAtScript,
} from "../index.js";
import linearVesting from "../../test/linearVesting.json" assert { type: "json" };

export const withMesh = (_scripts: ContractConfig) => (mesh: string) => {
return {
lockTokens: () => {
console.log("Implement lockTokens" )
console.log("Implement lockTokens");
},
collectVestingTokens:
() =>{
collectVestingTokens: () => {
console.log("Implement collectVestingTokens");
},
getVestedTokens: () =>{
getVestedTokens: () => {
console.log("Implement getVestedTokens");
},
mesh: mesh,
};
};
// //NOTE: add generic function signatures
// type ContractActions = {
// lock: (config: LockTokensConfig) => {
// build: () => Promise<Result<TxComplete>>;
// };
// collect: (config: LockTokensConfig) => {
// build: () => Promise<Result<TxComplete>>;
// };
// getVestedTokens: () =>
// };
export const withLucid = (scripts: ContractConfig) => async (lucid: Lucid) => {
const userAddress = await lucid.wallet.address();
//TODO: add object freeze or a better immutable function
return {
lockTokens: lockTokens(scripts)(lucid),
collectVestingTokens: collectVestingTokens(scripts)(lucid),
getVestedTokens: () =>
getVestingByAddress(lucid, userAddress, scripts.vesting),
lucid: lucid
lucid: lucid,
getScriptUTxOs: () =>
parseUTxOsAtScript(lucid, scripts.vesting, VestingDatum),
};
};

Expand All @@ -41,8 +58,6 @@ export const createContract = (config: ContractConfig) => {
};
};


export const Contract = createContract({
vesting: linearVesting.cborHex,
});

Loading

0 comments on commit 0d22a32

Please sign in to comment.