All files / tools/consolidator/interactions consume.ts

18.75% Statements 3/16
12.5% Branches 1/8
50% Functions 1/2
18.75% Lines 3/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63              1x         9x 9x                                                                                                  
import { OP_TYPES } from "../../constants";
import { BlockCall } from "../../types";
import { Change } from "../../../rmrk1.0.0/changelog";
import { Remark } from "../remark";
import { Consume } from "../../../rmrk1.0.0/classes/consume";
import { NFT } from "../../..";
 
export const consumeInteraction = (
  remark: Remark,
  consumeEntity: Consume,
  nft?: NFT
): void => {
  Eif (!nft) {
    throw new Error(
      `[${OP_TYPES.CONSUME}] Attempting to CONSUME non-existant NFT ${consumeEntity.id}`
    );
  }
 
  if (nft.burned != "") {
    throw new Error(
      `[${OP_TYPES.CONSUME}] Attempting to burn already burned NFT ${consumeEntity.id}`
    );
  }
 
  // Check if burner is owner of NFT
  if (nft.owner != remark.caller) {
    throw new Error(
      `[${OP_TYPES.CONSUME}] Attempting to CONSUME non-owned NFT ${consumeEntity.id}`
    );
  }
 
  // Burn and note reason
 
  const burnReasons: string[] = [];
  // Check if we have extra calls in the batch
  if (remark.extra_ex?.length) {
    // Check if the transfer is valid, i.e. matches target recipient and value.
    remark.extra_ex?.forEach((el: BlockCall) => {
      burnReasons.push(`<consume>${el.value}</consume>`);
    });
  }
 
  const burnReason = burnReasons.join(",");
  nft.addChange({
    field: "burned",
    old: "",
    new: burnReason,
    caller: remark.caller,
    block: remark.block,
  } as Change);
  nft.burned = burnReason;
 
  // De list if listed for sale
  nft.addChange({
    field: "forsale",
    old: nft.forsale,
    new: BigInt(0),
    caller: remark.caller,
    block: remark.block,
  } as Change);
  nft.forsale = BigInt(0);
};