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

@ -5,5 +5,6 @@ const lucid = await Lucid.new(undefined, "Preview");
const privateKey = lucid.utils.generatePrivateKey(); const privateKey = lucid.utils.generatePrivateKey();
await Deno.writeTextFile("key.sk", privateKey); 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); await Deno.writeTextFile("key.addr", address);

View File

@ -2,18 +2,18 @@ import {
Blockfrost, Blockfrost,
Constr, Constr,
Data, Data,
fromHex,
Lucid, Lucid,
SpendingValidator, SpendingValidator,
toHex,
TxHash, TxHash,
fromHex,
toHex
} from "https://deno.land/x/lucid@0.8.3/mod.ts"; } 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( const lucid = await Lucid.new(
new Blockfrost( new Blockfrost(
"https://cardano-preview.blockfrost.io/api/v0", "https://cardano-preview.blockfrost.io/api/v0",
Deno.env.get('BLOCKFROST_API_KEY'), Deno.env.get("BLOCKFROST_API_KEY"),
), ),
"Preview", "Preview",
); );
@ -25,7 +25,8 @@ const validator = await readValidator();
// --- Supporting functions // --- Supporting functions
async function readValidator(): Promise<SpendingValidator> { 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 { return {
type: "PlutusV2", type: "PlutusV2",
script: toHex(cbor.encode(fromHex(validator.compiledCode))), script: toHex(cbor.encode(fromHex(validator.compiledCode))),
@ -35,22 +36,25 @@ async function readValidator(): Promise<SpendingValidator> {
const publicKeyHash = lucid.utils const publicKeyHash = lucid.utils
.getAddressDetails(await lucid.wallet.address()) .getAddressDetails(await lucid.wallet.address())
.paymentCredential .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 console.log(`1 ADA locked into the contract at:
Tx ID: ${txLock} Tx Hash: ${txHash}
Datum: ${datum} Datum: ${datum}
`); `);
// --- Supporting functions // --- 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 contractAddress = lucid.utils.validatorToAddress(into);
const tx = await lucid const tx = await lucid

View File

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