All files / tools/consolidator/interactions buy.ts

84.62% Statements 22/26
61.54% Branches 8/13
100% Functions 4/4
84.62% Lines 22/26

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82              1x           14x 14x 9x         5x   1x             1x   1x             1x     1x 5x 5x 5x 5x 5x 5x 1x       5x     1x 5x   5x           4x                                  
import { Buy } from "../../../rmrk1.0.0/classes/buy";
import { OP_TYPES } from "../../constants";
import { BlockCall } from "../../types";
import { Change } from "../../../rmrk1.0.0/changelog";
import { Remark } from "../remark";
import { NFT as N100 } from "../../..";
 
export const interactionBuy = (
  remark: Remark, // Current remark
  buyEntity: Buy,
  nft?: N100 // NFT in current state
): void => {
  // An NFT was bought after having been LISTed for sale
  console.log("Instantiating buy");
  if (!nft) {
    throw new Error(
      `[${OP_TYPES.BUY}] Attempting to buy non-existant NFT ${buyEntity.id}`
    );
  }
 
  validate(remark, buyEntity, nft);
 
  nft.addChange({
    field: "owner",
    old: nft.owner,
    new: remark.caller,
    caller: remark.caller,
    block: remark.block,
  } as Change);
  nft.owner = remark.caller;
 
  nft.addChange({
    field: "forsale",
    old: nft.forsale,
    new: BigInt(0),
    caller: remark.caller,
    block: remark.block,
  } as Change);
  nft.forsale = BigInt(0);
};
 
const isTransferValid = (remark: Remark, nft: N100) => {
  let transferValid = false;
  let transferValue = "";
  remark.extra_ex?.forEach((el: BlockCall) => {
    Eif (el.call === "balances.transfer") {
      transferValue = el.value;
      if (el.value === `${nft.owner},${nft.forsale}`) {
        transferValid = true;
      }
    }
  });
  return { transferValid, transferValue };
};
 
const validate = (remark: Remark, buyEntity: Buy, nft: N100) => {
  const { transferValid, transferValue } = isTransferValid(remark, nft);
 
  switch (true) {
    case nft.burned != "":
      throw new Error(
        `[${OP_TYPES.BUY}] Attempting to buy burned NFT ${buyEntity.id}`
      );
    case nft.forsale <= BigInt(0):
      throw new Error(
        `[${OP_TYPES.BUY}] Attempting to buy not-for-sale NFT ${buyEntity.id}`
      );
    case nft.transferable === 0 || nft.transferable >= remark.block:
      throw new Error(
        `[${OP_TYPES.BUY}] Attempting to buy non-transferable NFT ${buyEntity.id}.`
      );
    case remark.extra_ex?.length === 0:
      throw new Error(
        `[${OP_TYPES.BUY}] No accompanying transfer found for purchase of NFT with ID ${buyEntity.id}.`
      );
    case !transferValid:
      throw new Error(
        `[${OP_TYPES.BUY}] Transfer for the purchase of NFT ID ${buyEntity.id} not valid. Recipient, amount should be ${nft.owner},${nft.forsale}, is ${transferValue}.`
      );
  }
};