Draft quick README, use ENV var for passing configuration.

This commit is contained in:
KtorZ 2023-01-06 13:44:20 +01:00
parent 1b7be32d74
commit 193792b48f
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
3 changed files with 61 additions and 28 deletions

View File

@ -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
```

View File

@ -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<SpendingValidator> {
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<TxHash>{
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();
}
}

View File

@ -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<TxHash> {
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();
}