Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free unused memory in Data.from #232

Closed
wants to merge 54 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
9d0d87e
feat: free references in Data.from
yHSJ Nov 1, 2023
ff1f03b
fix: formatting
yHSJ Nov 1, 2023
d9cc706
chore: update freeable types
yHSJ Nov 2, 2023
b947eea
chore: add context to the freeables types
yHSJ Nov 2, 2023
3e89765
feat: free references in Lucid.new
yHSJ Nov 1, 2023
d74544e
feat: free references in Lucid.switchProvider
yHSJ Nov 1, 2023
f3442e4
feat: free references in Lucid.selectWalletFromPrivateKey
yHSJ Nov 1, 2023
fda53f1
feat: free memory in Lucid.selectWallet
yHSJ Nov 1, 2023
953e714
feat: free memory in Lucid.selectWalletFrom
yHSJ Nov 1, 2023
b295d46
feat: free memory in Lucid.selectWalletFromSeed
yHSJ Nov 1, 2023
41c2b09
chore: format
yHSJ Nov 1, 2023
67f6d45
fix: failing tests
yHSJ Nov 1, 2023
58f61d2
remove CML classes from lucid state, add protocol parameters to const…
joacohoyos Nov 1, 2023
5448c26
feat: manage memory in tx
yHSJ Nov 4, 2023
37d4953
fix: don't free null pointers
yHSJ Nov 4, 2023
d5508b7
feat: manage memory in utils.validatorToAddress
yHSJ Nov 4, 2023
c2b4db9
feat: manage memory in utils.credentialToAddress
yHSJ Nov 4, 2023
c811418
feat: manage memory in utils.validatorToRewardAddress
yHSJ Nov 4, 2023
70e4f0a
feat: manage memory in utils.credentialToRewardAddress
yHSJ Nov 4, 2023
716aa07
feat: manage memory in utils.validatorToScriptHash
yHSJ Nov 4, 2023
0134e1e
feat: manage memory in utils.datumToHash
yHSJ Nov 4, 2023
dbd7cc7
feat: manage memory in utils.getAddressDetails
yHSJ Nov 4, 2023
9be2d6a
feat manage memory in utils.generatePrivateKey
yHSJ Nov 4, 2023
b663556
feat: manage memory in utils.valueToAssets
yHSJ Nov 4, 2023
17bfece
feat: manage memory in utils.assetsToValue
yHSJ Nov 4, 2023
3b9e59f
feat: manage memory in utils.fromScriptRef
yHSJ Nov 4, 2023
109ba8b
feat: manage memory in utils.toScriptRef
yHSJ Nov 4, 2023
e47205f
feat: manage memory in utils.coreToUtxo
yHSJ Nov 4, 2023
cf18ff1
feat: manage memory in utils.toPublicKey
yHSJ Nov 4, 2023
3a13927
feat: manage memory in utils.nativeScriptFromJson
yHSJ Nov 4, 2023
c8e93d6
feat: manage memory in utils.applyParamsToScript
yHSJ Nov 4, 2023
51b58ab
feat: manage memory in utils.applyDoubleCborEncoding
yHSJ Nov 4, 2023
fe30d93
feat: fix failing tests
yHSJ Nov 4, 2023
7aef11b
feat: manage memory in data.to
yHSJ Nov 4, 2023
c412b82
feat: manage memory in emulator.submitTx
yHSJ Nov 4, 2023
7746b77
feat: manage memory in maestro.getUtxosInternal
yHSJ Nov 4, 2023
46551df
feat: manage memory in blockfrost.getUtxos
yHSJ Nov 4, 2023
2e7f15d
feat: manage memory in blockfrost.getUtxosWithUnit
yHSJ Nov 4, 2023
78ab324
feat: manage memory in blockfrost.datumJsonToCbor
yHSJ Nov 4, 2023
70197eb
feat: mange memory in tx_signed.toHash
yHSJ Nov 4, 2023
8a212b7
feat: manage memory in message.signWithPrivateKey
yHSJ Nov 4, 2023
9fb26d3
feat: manage memory in TxComplete.constructor
yHSJ Nov 4, 2023
7546a41
feat: manage memory in TxComplete.sign
yHSJ Nov 4, 2023
4b9dd7f
feat: manage memory in TxComplete.signWithPrivateKey
yHSJ Nov 4, 2023
5325911
feat: manage memory in TxComplete.partialSign
yHSJ Nov 4, 2023
8c07f53
feat: manage memory in TxComplete.partialSignWithPrivateKey
yHSJ Nov 4, 2023
362aadb
feat: manage memory in TxComplete.assemble
yHSJ Nov 4, 2023
6013703
feat: manage memory in TxComplete.complete
yHSJ Nov 4, 2023
5941c9a
feat: manage memory in TxComplete.toHash
yHSJ Nov 4, 2023
a2a139e
feat: add free method to TxComplete
yHSJ Nov 4, 2023
1fe8557
feat manage memory in kupmios.kupmiosUtxosToUtxos
yHSJ Nov 4, 2023
0bc5858
feat: add memory management to pool_registration example
yHSJ Nov 4, 2023
62e6b2d
chore: prepare for npm release
yHSJ Nov 4, 2023
8c76fcd
chore: update readme
yHSJ Nov 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 28 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
<p align="center">
<img width="100px" src="./logo/lucid.svg" align="center"/>
<h1 align="center">Lucid</h1>
<p align="center">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.</p>
<h1 align="center">Jucid</h1>
<p align="center">Jucid a fork of Lucid, which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript, Deno and Node.js without worrying about memory leaks.</p>

<p align="center">
<img src="https://img.shields.io/github/commit-activity/m/berry-pool/lucid?style=for-the-badge" />
<a href="https://www.npmjs.com/package/lucid-cardano">
<img src="https://img.shields.io/npm/v/lucid-cardano?style=for-the-badge" />
<img src="https://img.shields.io/github/commit-activity/m/yHSJ/jucid?style=for-the-badge" />
<a href="https://www.npmjs.com/package/jucid-cardano">
<img src="https://img.shields.io/npm/v/jucid-cardano?style=for-the-badge" />
</a>
<a href="https://doc.deno.land/https://deno.land/x/lucid/mod.ts">
<img src="https://img.shields.io/readthedocs/cardano-lucid?style=for-the-badge" />
</a>
<a href="https://www.npmjs.com/package/lucid-cardano">
<img src="https://img.shields.io/npm/dw/lucid-cardano?style=for-the-badge" />
<a href="https://www.npmjs.com/package/jucid-cardano">
<img src="https://img.shields.io/npm/dw/jucid-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" />
<img src="https://img.shields.io/npm/l/jucid-cardano?style=for-the-badge" />
<a href="https://twitter.com/JSHyCS">
<img src="https://img.shields.io/twitter/follow/JSHyCS?style=for-the-badge&logo=twitter" />
</a>
</p>

</p>

### Lucid Fork

Jucid is JSHy's fork of Lucid (Jshy + Lucid = Jucid). Lucid accidentally introduces memory leaks into applications due to incorrect memory management of WASM objects. Jucid handles internal memory correctly, exposes new interfaces to allow you to management objects you use correclty, and is a drop-in replacement for Lucid. Once this fork is merged into Lucid, it is unlikely it will be maintained.

### Get started

#### NPM

```
npm install lucid-cardano
npm install jucid-cardano
```

#### Deno 🦕

For JavaScript and TypeScript

```js
import { Lucid } from "https://deno.land/x/[email protected]/mod.ts";
import { jucid } from "https://deno.land/x/[email protected]/mod.ts";
```

#### Web

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

###
###

### Build from source

Expand All @@ -61,31 +62,29 @@ Outputs a `dist` folder

### Examples

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

### 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/[email protected]/mod.ts"; Deno
import { Blockfrost, Lucid } from "jucid-cardano"; // NPM

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

// Assumes you are in a browser environment
const api = await window.cardano.nami.enable();
lucid.selectWallet(api);

const tx = await lucid.newTx()
.payToAddress("addr...", { lovelace: 5000000n })
.complete();

const signedTx = await tx.sign().complete();
const tx = lucid.newTx().payToAddress("addr...", { lovelace: 5000000n });
const completeTx = await tx.complete();
const signedTx = await completeTx.sign();

const txHash = await signedTx.submit();
Freeables.free(tx, completeTx, signedTx);

console.log(txHash);
```
Expand Down Expand Up @@ -114,7 +113,7 @@ deno task test:core

### Docs

[View docs](https://doc.deno.land/https://deno.land/x/lucid/mod.ts) 📖
[View docs](https://doc.deno.land/https://deno.land/x/jucid/mod.ts) 📖

You can generate documentation with:

Expand Down Expand Up @@ -143,15 +142,3 @@ project's `package.json`. Otherwise you will get import issues.

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.
137 changes: 75 additions & 62 deletions blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,31 @@ type Blueprint = {
compiledCode: string;
hash: string;
}[];
definitions: Record<string, {
title: string;
schema: {
$ref: string;
};
}>;
definitions: Record<
string,
{
title: string;
schema: {
$ref: string;
};
}
>;
};

const plutusJson: Blueprint = JSON.parse(
await Deno.readTextFile("plutus.json"),
await Deno.readTextFile("plutus.json")
);

const plutusVersion = "Plutus" +
plutusJson.preamble.plutusVersion.toUpperCase();
const plutusVersion =
"Plutus" + plutusJson.preamble.plutusVersion.toUpperCase();

const definitions = plutusJson.definitions;

const imports = `// deno-lint-ignore-file
import { applyParamsToScript, Data, Validator } from "${
flags.npm
? "lucid-cardano"
: `https://deno.land/x/lucid@${packageJson.version}/mod.ts`
? "jucid-cardano"
: `https://deno.land/x/jucid@${packageJson.version}/mod.ts`
}"`;

const validators = plutusJson.validators.map((validator) => {
Expand All @@ -79,10 +82,10 @@ const validators = plutusJson.validators.map((validator) => {
dataType: "list",
items: params.map((param) => resolveSchema(param.schema, definitions)),
};
const paramsArgs = params.map((
param,
index,
) => [snakeToCamel(param.title), schemaToType(paramsSchema.items[index])]);
const paramsArgs = params.map((param, index) => [
snakeToCamel(param.title),
schemaToType(paramsSchema.items[index]),
]);

const script = validator.compiledCode;

Expand All @@ -96,9 +99,9 @@ const validators = plutusJson.validators.map((validator) => {
export const ${name} = Object.assign(
function (${paramsArgs.map((param) => param.join(":")).join(",")}) {${
paramsArgs.length > 0
? `return { type: "${plutusVersion}", script: applyParamsToScript("${script}", [${
paramsArgs.map((param) => param[0]).join(",")
}], ${JSON.stringify(paramsSchema)} as any) };`
? `return { type: "${plutusVersion}", script: applyParamsToScript("${script}", [${paramsArgs
.map((param) => param[0])
.join(",")}], ${JSON.stringify(paramsSchema)} as any) };`
: `return {type: "${plutusVersion}", script: "${script}"};`
}},
${datum ? `{${datumTitle}: ${JSON.stringify(datumSchema)}},` : ""}
Expand All @@ -116,7 +119,7 @@ await new Deno.Command(Deno.execPath(), {
console.log(
"%cGenerated %cplutus.ts",
"color: green; font-weight: bold",
"font-weight: bold",
"font-weight: bold"
);

function resolveSchema(schema: any, definitions: any): any {
Expand Down Expand Up @@ -153,8 +156,9 @@ function resolveSchema(schema: any, definitions: any): any {
};
} else {
if (schema["$ref"]) {
const refKey =
schema["$ref"].replaceAll("~1", "/").split("#/definitions/")[1];
const refKey = schema["$ref"]
.replaceAll("~1", "/")
.split("#/definitions/")[1];
return resolveSchema(definitions[refKey], definitions);
} else {
return schema;
Expand All @@ -177,11 +181,11 @@ function schemaToType(schema: any): string {
if (isVoid(schema)) {
return "undefined";
} else {
return `{${
schema.fields.map((field: any) =>
`${field.title || "wrapper"}:${schemaToType(field)}`
).join(";")
}}`;
return `{${schema.fields
.map(
(field: any) => `${field.title || "wrapper"}:${schemaToType(field)}`
)
.join(";")}}`;
}
}
case "enum": {
Expand All @@ -195,35 +199,37 @@ function schemaToType(schema: any): string {
if (isNullable(schema)) {
return `${schemaToType(schema.anyOf[0].fields[0])} | null`;
}
return schema.anyOf.map((entry: any) =>
entry.fields.length === 0
? `"${entry.title}"`
: `{${entry.title}: ${
entry.fields[0].title
? `{${
entry.fields.map((field: any) =>
[field.title, schemaToType(field)].join(":")
).join(",")
}}}`
: `[${
entry.fields.map((field: any) => schemaToType(field)).join(",")
}]}`
}`
).join(" | ");
return schema.anyOf
.map((entry: any) =>
entry.fields.length === 0
? `"${entry.title}"`
: `{${entry.title}: ${
entry.fields[0].title
? `{${entry.fields
.map((field: any) =>
[field.title, schemaToType(field)].join(":")
)
.join(",")}}}`
: `[${entry.fields
.map((field: any) => schemaToType(field))
.join(",")}]}`
}`
)
.join(" | ");
}
case "list": {
if (schema.items instanceof Array) {
return `[${
schema.items.map((item: any) => schemaToType(item)).join(",")
}]`;
return `[${schema.items
.map((item: any) => schemaToType(item))
.join(",")}]`;
} else {
return `Array<${schemaToType(schema.items)}>`;
}
}
case "map": {
return `Map<${schemaToType(schema.keys)}, ${
schemaToType(schema.values)
}>`;
return `Map<${schemaToType(schema.keys)}, ${schemaToType(
schema.values
)}>`;
}
case undefined: {
return "Data";
Expand All @@ -233,35 +239,42 @@ function schemaToType(schema: any): string {
}

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

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

function isNullable(shape: any): boolean {
return shape.anyOf && shape.anyOf[0]?.title === "Some" &&
shape.anyOf[1]?.title === "None";
return (
shape.anyOf &&
shape.anyOf[0]?.title === "Some" &&
shape.anyOf[1]?.title === "None"
);
}

function snakeToCamel(s: string): string {
const withUnderscore = s.charAt(0) === "_" ? s.charAt(0) : "";
return withUnderscore +
(withUnderscore ? s.slice(1) : s).toLowerCase().replace(
/([-_][a-z])/g,
(group) =>
group
.toUpperCase()
.replace("-", "")
.replace("_", ""),
);
return (
withUnderscore +
(withUnderscore ? s.slice(1) : s)
.toLowerCase()
.replace(/([-_][a-z])/g, (group) =>
group.toUpperCase().replace("-", "").replace("_", "")
)
);
}

function upperFirst(s: string): string {
const withUnderscore = s.charAt(0) === "_" ? s.charAt(0) : "";
return withUnderscore +
return (
withUnderscore +
s.charAt(withUnderscore ? 1 : 0).toUpperCase() +
s.slice((withUnderscore ? 1 : 0) + 1);
s.slice((withUnderscore ? 1 : 0) + 1)
);
}
6 changes: 3 additions & 3 deletions docs/docs/getting-started/choose-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ import { Lucid, Maestro } from "https://deno.land/x/lucid/mod.ts";

const lucid = await Lucid.new(
new Maestro({
network: "Preprod", // For MAINNET: "Mainnet".
apiKey: "<Your-API-Key>", // Get yours by visiting https://docs.gomaestro.org/docs/Getting-started/Sign-up-login.
turboSubmit: false // Read about paid turbo transaction submission feature at https://docs.gomaestro.org/docs/Dapp%20Platform/Turbo%20Transaction.
network: "Preprod", // For MAINNET: "Mainnet".
apiKey: "<Your-API-Key>", // Get yours by visiting https://docs.gomaestro.org/docs/Getting-started/Sign-up-login.
turboSubmit: false, // Read about paid turbo transaction submission feature at https://docs.gomaestro.org/docs/Dapp%20Platform/Turbo%20Transaction.
}),
"Preprod", // For MAINNET: "Mainnet".
);
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "lucid-cardano",
"version": "0.10.7",
"name": "jucid-cardano",
"version": "1.0.0-alpha.1",
"license": "MIT",
"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"
"author": "Josh Marchand",
"description": "Jucid is a fork of Lucid which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript, Deno and Node.js without introducing memory leaks.",
"repository": "https://github.com/yHSJ/jucid"
}
Loading