Skip to content

Commit

Permalink
Fixed bug regarding inline datum; updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrokonrad committed Dec 28, 2022
1 parent d73536f commit f3d26c9
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/examples/always_succeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const alwaysSucceedAddress: Address = lucid.utils.validatorToAddress(
alwaysSucceedScript,
);

const Datum = () => Data.empty();
const Redeemer = () => Data.empty();
const Datum = () => Data.void();
const Redeemer = () => Data.void();

export async function lockUtxo(
lovelace: Lovelace,
Expand Down
6 changes: 3 additions & 3 deletions src/examples/mint_simple_NFT.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
Blockfrost,
fromText,
Lucid,
MintingPolicy,
PolicyId,
TxHash,
Unit,
utf8ToHex,
} from "../mod.ts";

/*
Expand Down Expand Up @@ -46,7 +46,7 @@ const policyId: PolicyId = lucid.utils.mintingPolicyToId(
export async function mintNFT(
name: string,
): Promise<TxHash> {
const unit: Unit = policyId + utf8ToHex(name);
const unit: Unit = policyId + fromText(name);

const tx = await lucid
.newTx()
Expand All @@ -65,7 +65,7 @@ export async function mintNFT(
export async function burnNFT(
name: string,
): Promise<TxHash> {
const unit: Unit = policyId + utf8ToHex(name);
const unit: Unit = policyId + fromText(name);

const tx = await lucid
.newTx()
Expand Down
2 changes: 1 addition & 1 deletion src/examples/parameterized_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function mint(

const tx = await lucid
.newTx()
.mintAssets({ [unit]: 1n }, Data.empty())
.mintAssets({ [unit]: 1n }, Data.void())
.attachMintingPolicy(mintingPolicy)
.complete();

Expand Down
4 changes: 2 additions & 2 deletions src/examples/sign_message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Lucid, utf8ToHex } from "../../mod.ts";
import { fromText, Lucid } from "../../mod.ts";

// Sign a message and verify it.

Expand All @@ -7,7 +7,7 @@ const lucid = await Lucid.new();
lucid.selectWalletFromSeed("car rare ...");

const address = await lucid.wallet.address();
const payload = utf8ToHex("Hello from Lucid!");
const payload = fromText("Hello from Lucid!");

const signedMessage = await lucid.newMessage(address, payload).sign();

Expand Down
2 changes: 1 addition & 1 deletion src/plutus/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const Data = {
* Note: Constructor cannot be used here, also only bytes/integers as Json keys.
*/
toJson,
empty: function (): Datum | Redeemer {
void: function (): Datum | Redeemer {
return "d87980";
},
castFrom,
Expand Down
48 changes: 11 additions & 37 deletions src/provider/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,13 @@ export class Simulator implements Provider {

const redeemers = (() => {
const tagMap: Record<number, string> = {
0: "Mint",
1: "Spend",
0: "Spend",
1: "Mint",
2: "Cert",
3: "Reward",
};
const collected = [];
for (let i = 0; i < (witnesses.redeemers()?.len || 0); i++) {
for (let i = 0; i < (witnesses.redeemers()?.len() || 0); i++) {
const redeemer = witnesses.redeemers()!.get(i);
collected.push({
tag: tagMap[redeemer.tag().kind()],
Expand All @@ -351,9 +351,10 @@ export class Simulator implements Provider {

function checkAndConsumeHash(
credential: Credential,
tag: string,
index: number,
) {
switch (credential?.type) {
switch (credential.type) {
case "Key": {
if (!keyHashes.includes(credential.hash)) {
throw new Error(
Expand All @@ -370,7 +371,7 @@ export class Simulator implements Provider {
} else if (plutusHashes.includes(credential.hash)) {
if (
redeemers.find((redeemer) =>
redeemer.tag === "Reward" && redeemer.index === index
redeemer.tag === tag && redeemer.index === index
)
) {
consumedHashes.add(credential.hash);
Expand All @@ -387,7 +388,7 @@ export class Simulator implements Provider {

for (let index = 0; index < (body.mint()?.keys().len() || 0); index++) {
const policyId = body.mint()!.keys().get(index).to_hex();
checkAndConsumeHash({ type: "Script", hash: policyId }, index);
checkAndConsumeHash({ type: "Script", hash: policyId }, "Mint", index);
}

// Check withdrawal witnesses
Expand All @@ -407,7 +408,7 @@ export class Simulator implements Provider {
const { stakeCredential } = getAddressDetails(
rewardAddress,
);
checkAndConsumeHash(stakeCredential!, index);
checkAndConsumeHash(stakeCredential!, "Reward", index);
if (this.chain[rewardAddress]?.delegation.rewards !== withdrawal) {
throw new Error(
"Withdrawal amount doesn't match actual reward balance.",
Expand Down Expand Up @@ -457,7 +458,7 @@ export class Simulator implements Provider {
).to_address().to_bech32(undefined);

const { stakeCredential } = getAddressDetails(rewardAddress);
checkAndConsumeHash(stakeCredential!, index);
checkAndConsumeHash(stakeCredential!, "Cert", index);

if (!this.chain[rewardAddress]?.registeredStake) {
throw new Error(
Expand All @@ -476,7 +477,7 @@ export class Simulator implements Provider {
const poolId = delegation.pool_keyhash().to_bech32("pool");

const { stakeCredential } = getAddressDetails(rewardAddress);
checkAndConsumeHash(stakeCredential!, index);
checkAndConsumeHash(stakeCredential!, "Cert", index);

if (
!this.chain[rewardAddress]?.registeredStake &&
Expand Down Expand Up @@ -529,34 +530,7 @@ export class Simulator implements Provider {

resolvedInputs.forEach(({ entry: { utxo } }, index) => {
const { paymentCredential } = getAddressDetails(utxo.address);
switch (paymentCredential?.type) {
case "Key": {
if (!keyHashes.includes(paymentCredential.hash)) {
throw new Error(
`Missing vkey witness. Key hash: ${paymentCredential.hash}`,
);
}
consumedHashes.add(paymentCredential.hash);
break;
}
case "Script":
if (nativeHashes.includes(paymentCredential.hash)) {
consumedHashes.add(paymentCredential.hash);
break;
} else if (plutusHashes.includes(paymentCredential.hash)) {
if (
redeemers.find((redeemer) =>
redeemer.tag === "Spend" && redeemer.index === index
)
) {
consumedHashes.add(paymentCredential.hash);
break;
}
}
throw new Error(
`Missing script witness. Script hash: ${paymentCredential.hash}`,
);
}
checkAndConsumeHash(paymentCredential!, "Spend", index);
});

if (!keyHashes.every((keyHash) => consumedHashes.has(keyHash))) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ export function coreToUtxo(coreUtxo: Core.TransactionUnspentOutput): UTxO {
: coreUtxo.output().address().to_bech32(undefined),
datumHash: coreUtxo.output()?.datum()?.as_data_hash()?.to_hex(),
datum: coreUtxo.output()?.datum()?.as_data() &&
toHex(coreUtxo.output().datum()!.as_data()!.to_bytes()),
toHex(coreUtxo.output().datum()!.as_data()!.get().to_bytes()),
scriptRef: coreUtxo.output()?.script_ref() &&
fromScriptRef(coreUtxo.output().script_ref()!),
};
Expand Down

0 comments on commit f3d26c9

Please sign in to comment.