add missing types, update log descriptions, deno fmt

This commit is contained in:
Jack Fraser 2023-03-07 16:06:09 -06:00 committed by Lucas
parent 80afb5fc2d
commit 2cfa0aeda9
3 changed files with 48 additions and 34 deletions

View File

@ -1,9 +1,10 @@
import { Lucid } from "https://deno.land/x/lucid@0.8.3/mod.ts";
const lucid = await Lucid.new(undefined, "Preview");
const privateKey = lucid.utils.generatePrivateKey();
await Deno.writeTextFile("key.sk", privateKey);
const address = await lucid.selectWalletFromPrivateKey(privateKey).wallet.address();
const address = await lucid.selectWalletFromPrivateKey(privateKey).wallet
.address();
await Deno.writeTextFile("key.addr", address);

View File

@ -2,18 +2,18 @@ import {
Blockfrost,
Constr,
Data,
fromHex,
Lucid,
SpendingValidator,
toHex,
TxHash,
fromHex,
toHex
} 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";
const lucid = await Lucid.new(
new Blockfrost(
"https://cardano-preview.blockfrost.io/api/v0",
Deno.env.get('BLOCKFROST_API_KEY'),
Deno.env.get("BLOCKFROST_API_KEY"),
),
"Preview",
);
@ -25,7 +25,8 @@ const validator = await readValidator();
// --- Supporting functions
async function readValidator(): Promise<SpendingValidator> {
const validator = JSON.parse(await Deno.readTextFile("plutus.json")).validators[0];
const validator =
JSON.parse(await Deno.readTextFile("plutus.json")).validators[0];
return {
type: "PlutusV2",
script: toHex(cbor.encode(fromHex(validator.compiledCode))),
@ -35,22 +36,25 @@ async function readValidator(): Promise<SpendingValidator> {
const publicKeyHash = lucid.utils
.getAddressDetails(await lucid.wallet.address())
.paymentCredential
.hash;
?.hash;
const datum = Data.to(new Constr(0, [ publicKeyHash ]));
const datum = Data.to(new Constr(0, [publicKeyHash]));
const txLock = await lock(1000000, { into: validator, owner: datum });
const txHash = await lock(BigInt(1000000), { into: validator, owner: datum });
await lucid.awaitTx(txLock);
await lucid.awaitTx(txHash);
console.log(`1 ADA locked into the contract
Tx ID: ${txLock}
console.log(`1 ADA locked into the contract at:
Tx Hash: ${txHash}
Datum: ${datum}
`);
// --- Supporting functions
async function lock(lovelace, { into, owner }): Promise<TxHash>{
async function lock(
lovelace: bigint,
{ into, owner }: { into: SpendingValidator; owner: string },
): Promise<TxHash> {
const contractAddress = lucid.utils.validatorToAddress(into);
const tx = await lucid

View File

@ -1,20 +1,22 @@
import {
Blockfrost,
Constr,
Data,
Lucid,
SpendingValidator,
TxHash,
fromHex,
toHex,
utf8ToHex
} from "https://deno.land/x/lucid@0.8.3/mod.ts";
Blockfrost,
Constr,
Data,
fromHex,
Lucid,
OutRef,
Redeemer,
SpendingValidator,
toHex,
TxHash,
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";
const lucid = await Lucid.new(
new Blockfrost(
"https://cardano-preview.blockfrost.io/api/v0",
Deno.env.get('BLOCKFROST_API_KEY'),
Deno.env.get("BLOCKFROST_API_KEY"),
),
"Preview",
);
@ -23,22 +25,28 @@ lucid.selectWalletFromPrivateKey(await Deno.readTextFile("./key.sk"));
const validator = await readValidator();
const utxo = { txHash: Deno.args[0], outputIndex: 0 };
const lockOutRef: OutRef = { txHash: Deno.args[0], outputIndex: 0 };
const redeemer = Data.to(new Constr(0, [ utf8ToHex("Hello, World!")]) );
const redeemer = Data.to(new Constr(0, [utf8ToHex("Hello, World!")]));
const txUnlock = await unlock(utxo, { from: validator, using: redeemer });
const unlockTxHash = await unlock(lockOutRef, {
from: validator,
using: redeemer,
});
await lucid.awaitTx(txUnlock);
await lucid.awaitTx(unlockTxHash);
console.log(`1 ADA recovered from the contract
Tx ID: ${txUnlock}
console.log(`1 ADA unlocked from the contract
Tx Hash: ${unlockTxHash}
Redeemer: ${redeemer}
`);
// --- Supporting functions
async function unlock(ref, { from, using }): Promise<TxHash> {
async function unlock(
ref: OutRef,
{ from, using }: { from: SpendingValidator; using: Redeemer },
): Promise<TxHash> {
const [utxo] = await lucid.utxosByOutRef([ref]);
const tx = await lucid
@ -56,7 +64,8 @@ async function unlock(ref, { from, using }): Promise<TxHash> {
}
async function readValidator(): Promise<SpendingValidator> {
const validator = JSON.parse(await Deno.readTextFile("plutus.json")).validators[0];
const validator =
JSON.parse(await Deno.readTextFile("plutus.json")).validators[0];
return {
type: "PlutusV2",
script: toHex(cbor.encode(fromHex(validator.compiledCode))),