diff --git a/examples/hello_world/README.md b/examples/hello_world/README.md new file mode 100644 index 00000000..c043d8e6 --- /dev/null +++ b/examples/hello_world/README.md @@ -0,0 +1,33 @@ +# Hello, World! + +An example of an Hello, World! contract using Aiken and [Lucid](https://github.com/spacebudz/lucid). + +See the [full tutorial on aiken-lang.org](https://aiken-lang.org/getting-started/hello-world). + +## Building + +``` +aiken build +``` + +## Generating Credentials + +``` +deno run --allow-net --allow-write generate-credentials.ts +``` + +## Locking Funds + +> **Warning** Require `BLOCKFROST_API_KEY` environment variable to be set. + +``` +deno run --allow-net --allow-read hello_world-lock.ts +``` + +## Unlocking Funds + +> **Warning** Require `BLOCKFROST_API_KEY` environment variable to be set. + +``` +deno run --allow-net --allow-read hello_world-unlock.ts -- TRANSACTION_ID_FROM_LOCK +``` diff --git a/examples/hello_world/hello_world-lock.ts b/examples/hello_world/hello_world-lock.ts index d9eae792..8cc7efe3 100644 --- a/examples/hello_world/hello_world-lock.ts +++ b/examples/hello_world/hello_world-lock.ts @@ -13,7 +13,7 @@ import * as cbor from "https://deno.land/x/cbor@v1.4.1/index.js"; const lucid = await Lucid.new( new Blockfrost( "https://cardano-preview.blockfrost.io/api/v0", - /* BLOCKFROST API KEY */ + Deno.env.get('BLOCKFROST_API_KEY'), ), "Preview", ); @@ -21,9 +21,9 @@ const lucid = await Lucid.new( lucid.selectWalletFromPrivateKey(await Deno.readTextFile("./key.sk")); const validator = await readValidator("./assets/hello_world/spend/script.cbor"); - + // --- Supporting functions - + async function readValidator(filepath: String): Promise { return { type: "PlutusV2", @@ -39,27 +39,27 @@ const publicKeyHash = lucid.utils const datum = Data.to(new Constr(0, [ publicKeyHash ])); const txLock = await lock(1000000, { into: validator, owner: datum }); - + await lucid.awaitTx(txLock); - + console.log(`1 ADA locked into the contract Tx ID: ${txLock} Datum: ${datum} `); - + // --- Supporting functions async function lock(lovelace, { into, owner }): Promise{ const contractAddress = lucid.utils.validatorToAddress(into); - + const tx = await lucid .newTx() .payToContract(contractAddress, { inline: owner }, { lovelace }) .complete(); - + const signedTx = await tx .sign() .complete(); - + return signedTx.submit(); -} \ No newline at end of file +} diff --git a/examples/hello_world/hello_world-unlock.ts b/examples/hello_world/hello_world-unlock.ts index 8c903e8e..ab753762 100644 --- a/examples/hello_world/hello_world-unlock.ts +++ b/examples/hello_world/hello_world-unlock.ts @@ -9,49 +9,49 @@ import { toHex, utf8ToHex } from "https://deno.land/x/lucid@0.8.3/mod.ts"; - import * as cbor from "https://deno.land/x/cbor@v1.4.1/index.js"; - +import * as cbor from "https://deno.land/x/cbor@v1.4.1/index.js"; + const lucid = await Lucid.new( - new Blockfrost( - "https://cardano-preview.blockfrost.io/api/v0", - /* BLOCKFROST API KEY */ - ), - "Preview", - ); + new Blockfrost( + "https://cardano-preview.blockfrost.io/api/v0", + Deno.env.get('BLOCKFROST_API_KEY'), + ), + "Preview", +); lucid.selectWalletFromPrivateKey(await Deno.readTextFile("./key.sk")); const validator = await readValidator("./assets/hello_world/spend/script.cbor"); - -const utxo = { txHash: '/* Tx ID from hello_world_lock */', outputIndex: 0 }; - + +const utxo = { txHash: Deno.args[0], outputIndex: 0 }; + const redeemer = Data.to(new Constr(0, [ utf8ToHex("Hello, World!")]) ); - + const txUnlock = await unlock(utxo, { from: validator, using: redeemer }); await lucid.awaitTx(txUnlock); - + console.log(`1 ADA recovered from the contract Tx ID: ${txUnlock} Redeemer: ${redeemer} `); - + // --- Supporting functions - + async function unlock(ref, { from, using }): Promise { const [utxo] = await lucid.utxosByOutRef([ref]); - + const tx = await lucid .newTx() .collectFrom([utxo], using) .addSigner(await lucid.wallet.address()) .attachSpendingValidator(from) .complete(); - + const signedTx = await tx .sign() .complete(); - + return signedTx.submit(); }