Skip to content

Commit

Permalink
Add tests for updating nft
Browse files Browse the repository at this point in the history
Minor updates
  • Loading branch information
nikhils9 committed Sep 18, 2023
1 parent c890d9d commit 5d0dc94
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 8 deletions.
1 change: 1 addition & 0 deletions off-chain/mint-nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ if (!userUtxos || !userUtxos.length) {
// TODO Idx of utxo < 256

// Selecting a utxo containing atleast 5 ADA to cover tx fees and min ADA
// Note: To avoid tx balancing errors, the utxo should only contain lovelaces
const selectedUtxo = getUtxoWithAssets(userUtxos, { ["lovelace"]: 5000000n });

// Calculating asset name from the utxo which will be spent in the minting tx
Expand Down
1 change: 1 addition & 0 deletions off-chain/tests/mint-nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ if (!userUtxos || !userUtxos.length) {
// TODO Idx of utxo < 256

// Selecting a utxo containing atleast 5 ADA to cover tx fees and min ADA
// Note: To avoid tx balancing errors, the utxo should only contain lovelaces
const selectedUtxo = getUtxoWithAssets(userUtxos, { ["lovelace"]: 5000000n });

// Calculating asset name from the utxo which will be spent in the minting tx
Expand Down
90 changes: 90 additions & 0 deletions off-chain/tests/update-nft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { Constr, Data, fromText, toUnit, UTxO } from "lucid";
import {
createLucidInstance,
getAppliedValidator,
getCredential,
getUtxoWithAssets,
} from "../utils/lucid/utils.ts";
import { REFERENCE_TOKEN_LABEL } from "../common/constants.ts";
import { AppliedValidator, MetaDatum } from "../common/types.ts";

const lucid = await createLucidInstance();
lucid.selectWalletFromPrivateKey(await getCredential("issuer.sk"));

const storeValidator = await getAppliedValidator("reference_store.store.json");
const storeUtxos = await lucid.utxosAt(storeValidator.lockAddress);
const mintValidator = await getAppliedValidator("mint.mint.json");

const assetNameSuffix =
"004e80f65697e1332fbb75f5a4a0927f5856ade4df76b64bd762a2f6";
const refToken = toUnit(
mintValidator.policyId,
assetNameSuffix,
REFERENCE_TOKEN_LABEL,
);
const refUtxo = getUtxoWithAssets(storeUtxos, { [refToken]: 1n });

let metaDatum: MetaDatum;
try {
metaDatum = Data.from<MetaDatum>(refUtxo.datum!, MetaDatum);
const metadata = metaDatum?.metadata;
metadata?.set(fromText("name"), fromText("BlockOwls Telly Updated"));
metadata?.set(fromText("power"), 1000n);
} catch (error) {
console.error(
`Error occured while deserializing UTxO datum at refToken(${refToken} to MetaDatum.
Datum: ${refUtxo.datum}` + error.toString(),
);
}

let serializeMetadatum = Data.to<MetaDatum>(metaDatum!, MetaDatum);
const rdmr = Data.to(new Constr(0, []));

// Run tests
await testEmptyDatum();
await testRefTokenRemoved();

async function testEmptyDatum() {
console.log("testEmptyDatum");

await txBuilder(refUtxo, refToken, Data.void(), storeValidator);
}

async function testRefTokenRemoved() {
console.log("testRefTokenRemoved");

const validator = Object.create(storeValidator) as AppliedValidator;
validator.lockAddress = await getCredential("issuer.addr");
await txBuilder(refUtxo, refToken, serializeMetadatum, validator);
}

async function txBuilder(
refUtxo: UTxO,
refToken: string,
metaDatum: string,
storeValidator: AppliedValidator,
) {
try {
const tx = await lucid
.newTx()
.collectFrom([refUtxo], rdmr)
.payToContract(storeValidator.lockAddress, {
inline: metaDatum,
}, { [refToken]: 1n })
.attachSpendingValidator(storeValidator.validator)
.addSigner(await lucid.wallet.address())
.complete();

const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();

console.log(
`Successfully updated CIP-68 Reference NFT: ${refToken},
storeAddress: ${storeValidator.lockAddress},
token: ${mintValidator.policyId},
txHash: ${txHash}`,
);
} catch (error) {
console.log("Test passed. " + error);
}
}
16 changes: 8 additions & 8 deletions off-chain/utils/tx/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ const lucid = await createLucidInstance();

lucid.selectWalletFromPrivateKey(await getCredential("user.sk"));

//const asset = { lovelace: 5000000n };
const token = {
["781b0f6aee558273d74cf7ffad4454a08dc5ea5d7bde73a29fdd990d000de14001cd485e2906baadd152a75b51835ccf876df02b25470c26151ba02b"]:
1n,
const asset = {
lovelace: 10000000n,
// "unit": 1n
};
// const receiver = await getCredential("issuer.addr");
const receiver =
"addr_test1qrmdrjfxyzzda9rcq459lljmmke0jffnttpvu2za5zpr7p4za8cfdkfad86l0yyn0vdmrqu66dt6zafasg85laeqe6uqd248et";

const receiver = await getCredential("issuer.addr");
// const receiver =
// "addr_test1qrmdrjfxyzzda9rcq459lljmmke0jffnttpvu2za5zpr7p4za8cfdkfad86l0yyn0vdmrqu66dt6zafasg85laeqe6uqd248et";

const tx = await lucid
.newTx()
.payToAddress(receiver, token)
.payToAddress(receiver, asset)
.complete();

const signedTx = await tx.sign().complete();
Expand Down

0 comments on commit 5d0dc94

Please sign in to comment.