Skip to content

Commit

Permalink
Change burned field format in consolidator to more flexible string[] …
Browse files Browse the repository at this point in the history
…| boolean (#58)

* Invalidate consume without a reason

* New burned format is string[] | boolean
  • Loading branch information
Yuripetusko authored May 2, 2021
1 parent edab37e commit 407c9fa
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 117 deletions.
4 changes: 2 additions & 2 deletions src/rmrk1.0.0/classes/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class NFT {
changes: Change[] = [];
owner: string;
loadedMetadata?: NFTMetadata;
burned: string;
burned: string[] | boolean;
constructor(
block: number,
collection: string,
Expand All @@ -42,7 +42,7 @@ export class NFT {
this.owner = "";
this.reactions = {};
this.forsale = BigInt(0);
this.burned = "";
this.burned = false;
this.updatedAtBlock = updatedAtBlock || block;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tools/consolidator/consolidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface NFTConsolidated {
changes: Change[];
owner: string;
loadedMetadata?: NFTMetadata;
burned: string;
burned: string[] | boolean;
updatedAtBlock?: number;
}

Expand Down
7 changes: 5 additions & 2 deletions src/tools/consolidator/interactions/buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Change } from "../../../rmrk1.0.0/changelog";
import { Remark } from "../remark";
import { NFT as N100 } from "../../..";
import { encodeAddress } from "@polkadot/keyring";
import { isBurnedNFT } from "../../utils";

export const buyInteraction = (
remark: Remark, // Current remark
Expand Down Expand Up @@ -49,7 +50,9 @@ const isTransferValid = (remark: Remark, nft: N100, ss58Format?: number) => {
remark.extra_ex?.forEach((el: BlockCall) => {
if (el.call === "balances.transfer") {
const [owner, forsale] = el.value.split(",");
const ownerEncoded = ss58Format ? encodeAddress(owner, ss58Format) : owner;
const ownerEncoded = ss58Format
? encodeAddress(owner, ss58Format)
: owner;
transferValue = [ownerEncoded, forsale].join(",");
if (transferValue === `${nft.owner},${nft.forsale}`) {
transferValid = true;
Expand All @@ -72,7 +75,7 @@ const validate = (
);

switch (true) {
case nft.burned != "":
case isBurnedNFT(nft):
throw new Error(
`[${OP_TYPES.BUY}] Attempting to buy burned NFT ${buyEntity.id}`
);
Expand Down
7 changes: 4 additions & 3 deletions src/tools/consolidator/interactions/consume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Change } from "../../../rmrk1.0.0/changelog";
import { Remark } from "../remark";
import { Consume } from "../../../rmrk1.0.0/classes/consume";
import { NFT } from "../../..";
import { isBurnedNFT } from "../../utils";

export const consumeInteraction = (
remark: Remark,
Expand All @@ -16,7 +17,7 @@ export const consumeInteraction = (
);
}

if (nft.burned != "") {
if (isBurnedNFT(nft)) {
throw new Error(
`[${OP_TYPES.CONSUME}] Attempting to burn already burned NFT ${consumeEntity.id}`
);
Expand All @@ -36,12 +37,12 @@ export const consumeInteraction = (
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>`);
burnReasons.push(el.value);
});
}

nft.updatedAtBlock = remark.block;
const burnReason = burnReasons.join(",");
const burnReason = burnReasons.length < 1 ? true : burnReasons;
nft.addChange({
field: "burned",
old: "",
Expand Down
3 changes: 2 additions & 1 deletion src/tools/consolidator/interactions/emote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Remark } from "../remark";
import { Emote } from "../../../rmrk1.0.0/classes/emote";
import { NFT } from "../../..";
import { Change } from "../../../rmrk1.0.0/changelog";
import { isBurnedNFT } from "../../utils";

const addEmoteChange = (
remark: Remark,
Expand Down Expand Up @@ -32,7 +33,7 @@ export const emoteInteraction = (
);
}

if (nft.burned != "") {
if (isBurnedNFT(nft)) {
throw new Error(
`[${OP_TYPES.EMOTE}] Cannot emote to a burned NFT ${emoteEntity.id}`
);
Expand Down
3 changes: 2 additions & 1 deletion src/tools/consolidator/interactions/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { List } from "../../../rmrk1.0.0/classes/list";
import { NFT } from "../../..";
import { OP_TYPES } from "../../constants";
import { Change } from "../../../rmrk1.0.0/changelog";
import { isBurnedNFT } from "../../utils";

export const listForSaleInteraction = (
remark: Remark,
Expand All @@ -15,7 +16,7 @@ export const listForSaleInteraction = (
);
}

if (nft.burned != "") {
if (isBurnedNFT(nft)) {
throw new Error(
`[${OP_TYPES.LIST}] Attempting to list burned NFT ${listEntity.id}`
);
Expand Down
3 changes: 2 additions & 1 deletion src/tools/consolidator/interactions/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Change } from "../../../rmrk1.0.0/changelog";
import { Remark } from "../remark";
import { Send } from "../../../rmrk1.0.0/classes/send";
import { NFT } from "../../..";
import { isBurnedNFT } from "../../utils";

export const sendInteraction = (
remark: Remark,
Expand All @@ -15,7 +16,7 @@ export const sendInteraction = (
);
}

if (nft.burned != "") {
if (isBurnedNFT(nft)) {
throw new Error(
`[${OP_TYPES.SEND}] Attempting to send burned NFT ${sendEntity.id}`
);
Expand Down
8 changes: 8 additions & 0 deletions src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BlockCall, BlockCalls } from "./types";
import { Call as TCall } from "@polkadot/types/interfaces";
import { BlockHash } from "@polkadot/types/interfaces/chain";
import { encodeAddress } from "@polkadot/util-crypto";
import { NFT } from "../rmrk1.0.0/classes/nft";

export const getApi = async (wsEndpoint: string): Promise<ApiPromise> => {
const wsProvider = new WsProvider(wsEndpoint);
Expand Down Expand Up @@ -259,3 +260,10 @@ export const filterBlocksByCollection = (
rmrk.remark.includes(collectionFilter)
)
);

/**
* Burned NFT has burned field set as true or has atleast one burned reason string in array
* @param nft
*/
export const isBurnedNFT = (nft: NFT) =>
nft.burned || (Array.isArray(nft.burned) && nft.burned.length > 0);
Loading

0 comments on commit 407c9fa

Please sign in to comment.