Skip to content

Commit

Permalink
refactor, lint fix, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrokonrad committed Jan 24, 2025
1 parent 4de1d12 commit 27f738e
Show file tree
Hide file tree
Showing 29 changed files with 202 additions and 165 deletions.
2 changes: 0 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

- #### Avoid external dependencies. The less dependencies the easier Lucid is interoperable between Browser, Node.js and Deno. But Deno dependencies are fine most of the time because they are mostly written in a browser compatible way.

- #### Avoid exposing functions and arguments directly from the `cardano-multiplatform-lib` or `message-signing` library. Rather wrap them and expose types from JavaScript like `string`, `number` or `object`.

- #### Add tests to the `tests` folder.

- #### Always run and test the code with the latest Deno version. Simply do `deno upgrade` to get the latest version.
Expand Down
76 changes: 33 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,36 @@
<img src="https://img.shields.io/npm/dw/lucid-cardano?style=for-the-badge" />
</a>
<img src="https://img.shields.io/npm/l/lucid-cardano?style=for-the-badge" />
<a href="https://twitter.com/spacebudzNFT">
<img src="https://img.shields.io/twitter/follow/spacebudzNFT?style=for-the-badge&logo=twitter" />
<a href="https://twitter.com/spacebudznft">
<img src="https://img.shields.io/twitter/follow/spacebudznft?style=for-the-badge&logo=twitter" />
</a>
</p>

</p>

### Get started

#### NPM

```
npm install lucid-cardano
```

`--experimental-wasm-modules` flag needs to be set in Node.js
Lucid is a Deno first TypeScript framework. Node.js and NPM are still supported, but may be deprecated in the future.

#### Deno 🦕

For JavaScript and TypeScript

```js
import { Lucid } from "https://deno.land/x/lucid@0.10.11/mod.ts";
import { Lucid } from "https://deno.land/x/lucid/mod.ts";
```

#### Web
#### NPM

```html
<script type="module">
import { Lucid } from "https://unpkg.com/[email protected]/web/mod.js"
// ...
</script>
```
npm install lucid-cardano
```

###
`--experimental-wasm-modules` flag needs to be set in Node.js as well as `{ "type": "module" }` in package.json

### Build from source

Build NPM and Web target
Build for NPM

```
deno task build
Expand All @@ -63,19 +54,19 @@ Outputs a `dist` folder

### Examples

- [Basic examples](./src/examples/)
- [Next.js Blockfrost Proxy API Example](https://github.com/GGAlanSmithee/cardano-lucid-blockfrost-proxy-example)
- [Basic examples](./examples/)

### Basic usage

```js
// import { Blockfrost, Lucid } from "https://deno.land/x/[email protected]/mod.ts"; Deno
import { Blockfrost, Lucid } from "lucid-cardano"; // NPM
import { Blockfrost, Lucid } from "https://deno.land/x/lucid/mod.ts";

const lucid = await Lucid.new(
new Blockfrost("https://cardano-preview.blockfrost.io/api/v0", "<projectId>"),
"Preview",
);
const lucid = new Lucid({
provider: new Blockfrost(
"https://cardano-preview.blockfrost.io/api/v0",
"<projectId>",
),
});

// Assumes you are in a browser environment
const api = await window.cardano.nami.enable();
Expand All @@ -92,6 +83,20 @@ const txHash = await signedTx.submit();
console.log(txHash);
```

### Blueprint

Lucid supports [CIP-0057](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0057) blueprints.

```
deno run -A https://deno.land/x/lucid/blueprint.ts
```

```js

```

See [more examples](./tests/data.test.ts)

### Test

```
Expand All @@ -100,9 +105,7 @@ deno task test

### Build Core

This library is built on top of a customized version of the serialization-lib
(cardano-multiplatform-lib) and on top of the message-signing library, which are
written in Rust.
The core library (instruction builder, crypto, hashing etc.) is written in Rust and compiled to WASM.

```
deno task build:core
Expand Down Expand Up @@ -138,22 +141,9 @@ experiments: {
}
```

To run the library in Node.js you need to set `{"type" : "module"}` in your
project's `package.json`. Otherwise you will get import issues.

### Contributing

Contributions and PRs are welcome!\
Contributions and PRs are welcome\
The [contribution instructions](./CONTRIBUTING.md).

Join us on [Discord](https://discord.gg/82MWs63Tdm)!

### Use Lucid with React

[use-cardano](https://use-cardano.alangaming.com/) a React context, hook and set
of components built on top of Lucid.

### Use Lucid with Next.js

[Cardano Starter Kit](https://cardano-starter-kit.alangaming.com/) a Next.js
starter kit for building Cardano dApps.
28 changes: 15 additions & 13 deletions blueprint.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Json } from "./mod.ts";

type Blueprint = {
preamble: {
title: string;
Expand Down Expand Up @@ -126,12 +128,12 @@ function definitionsToTypes(definitions: Blueprint["definitions"]): string {
).join("\n");
}

function resolveSchema(schema: any): any {
function resolveSchema(schema: Json): Json {
if (schema.items) {
if (schema.items instanceof Array) {
return {
...schema,
items: schema.items.map((item: any) => resolveSchema(item)),
items: schema.items.map((item: Json) => resolveSchema(item)),
};
} else {
return {
Expand All @@ -142,9 +144,9 @@ function resolveSchema(schema: any): any {
} else if (schema.anyOf) {
return {
...schema,
anyOf: schema.anyOf.map((a: any) => ({
anyOf: schema.anyOf.map((a: Json) => ({
...a,
fields: a.fields.map((field: any) => ({
fields: a.fields.map((field: Json) => ({
...resolveSchema(field),
title: field.title ? snakeToCamel(field.title) : undefined,
})),
Expand All @@ -171,7 +173,7 @@ function resolveDefinitions(
);
}

function schemaToType(schema: any): string {
function schemaToType(schema: Json): string {
if (!schema) throw new Error("Could not generate type.");
const shapeType = (schema.anyOf ? "enum" : schema["$ref"] ? "$ref" : "") ||
schema.dataType;
Expand All @@ -188,7 +190,7 @@ function schemaToType(schema: any): string {
return "undefined";
} else {
return `{${
schema.fields.map((field: any) =>
schema.fields.map((field: Json) =>
`${snakeToCamel(field.title || "wrapper")}:${schemaToType(field)}`
).join(";")
}}`;
Expand All @@ -205,26 +207,26 @@ function schemaToType(schema: any): string {
if (isNullable(schema)) {
return `${schemaToType(schema.anyOf[0].fields[0])} | null`;
}
return schema.anyOf.map((entry: any) =>
return schema.anyOf.map((entry: Json) =>
entry.fields.length === 0
? `"${snakeToCamel(entry.title)}"`
: `{${snakeToCamel(entry.title)}: ${
entry.fields[0].title
? `{${
entry.fields.map((field: any) =>
entry.fields.map((field: Json) =>
[snakeToCamel(field.title), schemaToType(field)].join(":")
).join(",")
}}}`
: `[${
entry.fields.map((field: any) => schemaToType(field)).join(",")
entry.fields.map((field: Json) => schemaToType(field)).join(",")
}]}`
}`
).join(" | ");
}
case "list": {
if (schema.items instanceof Array) {
return `[${
schema.items.map((item: any) => schemaToType(item)).join(",")
schema.items.map((item: Json) => schemaToType(item)).join(",")
}]`;
} else {
return `Array<${schemaToType(schema.items)}>`;
Expand All @@ -246,16 +248,16 @@ function schemaToType(schema: any): string {
throw new Error("Could not type cast data.");
}

function isBoolean(shape: any): boolean {
function isBoolean(shape: Json): boolean {
return shape.anyOf && shape.anyOf[0]?.title === "False" &&
shape.anyOf[1]?.title === "True";
}

function isVoid(shape: any): boolean {
function isVoid(shape: Json): boolean {
return shape.index === 0 && shape.fields.length === 0;
}

function isNullable(shape: any): boolean {
function isNullable(shape: Json): boolean {
return shape.anyOf && shape.anyOf[0]?.title === "Some" &&
shape.anyOf[1]?.title === "None";
}
Expand Down
9 changes: 7 additions & 2 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { copySync, moveSync } from "https://deno.land/[email protected]/fs/mod.ts";
import partialPackage from "./package.json" with { type: "json" };
import denoJson from "./deno.json" with { type: "json" };
import * as dnt from "jsr:@deno/dnt";

await dnt.emptyDir("./dist");
Expand Down Expand Up @@ -33,7 +33,12 @@ try {
skipSourceOutput: true,
shims: {},
package: {
...partialPackage,
name: "lucid-cardano",
version: denoJson.version,
license: denoJson.license,
author: denoJson.author,
description: denoJson.description,
repository: denoJson.repository,
type: "module",
main: "./esm/mod.js",
engines: {
Expand Down
14 changes: 11 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
"test:core": "cd src/core/libs/lucid_core && cargo test; cd ../message_signing && RUSTFLAGS=-Awarnings cargo test"
},
"lint": {
"include": ["src/"],
"exclude": ["src/core/"]
"include": ["src/", "tests/", "blueprint.ts"],
"exclude": ["src/core/libs/"]
},
"nodeModulesDir": "none"
"nodeModulesDir": "none",

"name": "@spacebudz/lucid",
"version": "0.10.11",
"license": "MIT",
"exports": "./mod.ts",
"author": "Alessandro Konrad",
"description": "Lucid is a library, which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript, Deno and Node.js.",
"repository": "https://github.com/spacebudz/lucid"
}
8 changes: 0 additions & 8 deletions package.json

This file was deleted.

1 change: 1 addition & 0 deletions src/core/libs/lucid_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fraction = "0.15.3"
rand = "0.8.4"
num-integer = "0.1.46"
thiserror = "2.0.4"
console_error_panic_hook = "0.1.2"

[patch.crates-io]
serde = { git = "https://github.com/serde-rs/serde.git", rev = "8d5f1d103f80726c9b865a724e4cf58cb5a6885b"}
Expand Down
1 change: 1 addition & 0 deletions src/core/libs/lucid_core/pkg/lucid_core.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* tslint:disable */
/* eslint-disable */
export function main(): void;
export type Network = "Mainnet" | "Preprod" | "Preview" | { Emulator: number };

export type Credential = { type: "Key"; hash: string } | { type: "Script"; hash: string };
Expand Down
29 changes: 29 additions & 0 deletions src/core/libs/lucid_core/pkg/lucid_core_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ function debugString(val) {
return className;
}

export function main() {
wasm.main();
}

function takeFromExternrefTable0(idx) {
const value = wasm.__wbindgen_export_4.get(idx);
wasm.__externref_table_dealloc(idx);
Expand Down Expand Up @@ -1239,6 +1243,18 @@ export function __wbg_entries_3265d4158b33e5dc(arg0) {
return ret;
};

export function __wbg_error_7534b8e9a36f1ab4(arg0, arg1) {
let deferred0_0;
let deferred0_1;
try {
deferred0_0 = arg0;
deferred0_1 = arg1;
console.error(getStringFromWasm0(arg0, arg1));
} finally {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
};

export function __wbg_getRandomValues_bcb4912f16000dc4() { return handleError(function (arg0, arg1) {
arg0.getRandomValues(arg1);
}, arguments) };
Expand Down Expand Up @@ -1325,6 +1341,11 @@ export function __wbg_new_78feb108b6472713() {
return ret;
};

export function __wbg_new_8a6f238a6ece86ea() {
const ret = new Error();
return ret;
};

export function __wbg_new_a12002a7f91c75be(arg0) {
const ret = new Uint8Array(arg0);
return ret;
Expand Down Expand Up @@ -1391,6 +1412,14 @@ export function __wbg_set_8fc6bf8a5b1071d1(arg0, arg1, arg2) {
return ret;
};

export function __wbg_stack_0ed75d68575b0f3c(arg0, arg1) {
const ret = arg1.stack;
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len1 = WASM_VECTOR_LEN;
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
};

export function __wbg_static_accessor_GLOBAL_88a902d13a557d07() {
const ret = typeof global === 'undefined' ? null : global;
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
Expand Down
Binary file modified src/core/libs/lucid_core/pkg/lucid_core_bg.wasm
Binary file not shown.
7 changes: 4 additions & 3 deletions src/core/libs/lucid_core/pkg/lucid_core_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export const main: () => void;
export const __wbg_addresses_free: (a: number, b: number) => void;
export const addresses_keyHashToCredential: (a: number, b: number) => any;
export const addresses_scriptHashToCredential: (a: number, b: number) => any;
Expand Down Expand Up @@ -56,15 +57,15 @@ export const utils_applyParamsToScript: (a: number, b: number, c: number, d: num
export const utils_encodeBech32: (a: number, b: number, c: number, d: number) => [number, number, number, number];
export const utils_applySingleCborEncoding: (a: number, b: number) => [number, number, number, number];
export const utils_applyDoubleCborEncoding: (a: number, b: number) => [number, number, number, number];
export const __wbg_crypto_free: (a: number, b: number) => void;
export const __wbg_codec_free: (a: number, b: number) => void;
export const __wbg_utils_free: (a: number, b: number) => void;
export const __wbg_crypto_free: (a: number, b: number) => void;
export const __wbg_hasher_free: (a: number, b: number) => void;
export const __wbg_codec_free: (a: number, b: number) => void;
export const __wbindgen_malloc: (a: number, b: number) => number;
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
export const __wbindgen_exn_store: (a: number) => void;
export const __externref_table_alloc: () => number;
export const __wbindgen_export_4: WebAssembly.Table;
export const __externref_table_dealloc: (a: number) => void;
export const __wbindgen_free: (a: number, b: number, c: number) => void;
export const __externref_table_dealloc: (a: number) => void;
export const __wbindgen_start: () => void;
Loading

0 comments on commit 27f738e

Please sign in to comment.