Skip to content

Commit

Permalink
Merge pull request #61 from rmrk-team/optional-interaction-changes
Browse files Browse the repository at this point in the history
Consolidator: Add option to return interaction changes
  • Loading branch information
Yuripetusko authored May 22, 2021
2 parents 9158b4e + 6d0b6cb commit b85a7f8
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 4 deletions.
45 changes: 41 additions & 4 deletions src/tools/consolidator/consolidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ import {
consolidatedNFTtoInstance,
} from "./utils";

type InteractionChanges = Partial<Record<OP_TYPES, string>>[];

export type ConsolidatorReturnType = {
nfts: NFTConsolidated[];
collections: CollectionConsolidated[];
invalid: InvalidCall[];
changes?: InteractionChanges;
lastBlock?: number;
};

Expand Down Expand Up @@ -77,22 +80,26 @@ export class Consolidator {
readonly dbAdapter: IConsolidatorAdapter;
readonly ss58Format?: number;
readonly emitEmoteChanges?: boolean;
readonly emitInteractionChanges?: boolean;
private interactionChanges: InteractionChanges = [];

/**
* If true emitEmoteChanges records full log of EMOTE events in nft 'changes' prop
* @param ss58Format
* @param dbAdapter
* @param emitEmoteChanges
* @param emitEmoteChanges log EMOTE events in nft 'changes' prop
* @param emitInteractionChanges return interactions changes ( OP_TYPE: id )
*/
constructor(
ss58Format?: number,
dbAdapter?: IConsolidatorAdapter,
emitEmoteChanges?: boolean
emitEmoteChanges?: boolean,
emitInteractionChanges?: boolean
) {
if (ss58Format) {
this.ss58Format = ss58Format;
}
this.emitEmoteChanges = emitEmoteChanges || false;
this.emitInteractionChanges = emitInteractionChanges || false;

this.dbAdapter = dbAdapter || new InMemoryAdapter();

Expand Down Expand Up @@ -152,6 +159,9 @@ export class Consolidator {
validateMintIds(collection, remark);
await this.dbAdapter.updateCollectionMint(collection);
this.collections.push(collection);
if (this.emitInteractionChanges) {
this.interactionChanges.push({ [OP_TYPES.MINT]: collection.id });
}
} catch (e) {
invalidate(collection.id, e.message);
return true;
Expand Down Expand Up @@ -201,6 +211,9 @@ export class Consolidator {
await this.dbAdapter.updateNFTMint(nft, remark.block);

this.nfts.push(nft);
if (this.emitInteractionChanges) {
this.interactionChanges.push({ [OP_TYPES.MINTNFT]: nft.getId() });
}
} catch (e) {
invalidate(nft.getId(), e.message);
return true;
Expand Down Expand Up @@ -238,6 +251,9 @@ export class Consolidator {
sendInteraction(remark, sendEntity, nft);
if (nft && consolidatedNFT) {
await this.dbAdapter.updateNFTSend(nft, consolidatedNFT, remark.block);
if (this.emitInteractionChanges) {
this.interactionChanges.push({ [OP_TYPES.SEND]: nft.getId() });
}
}
} catch (e) {
invalidate(sendEntity.id, e.message);
Expand Down Expand Up @@ -276,6 +292,9 @@ export class Consolidator {
listForSaleInteraction(remark, listEntity, nft);
if (nft && consolidatedNFT) {
await this.dbAdapter.updateNFTList(nft, consolidatedNFT, remark.block);
if (this.emitInteractionChanges) {
this.interactionChanges.push({ [OP_TYPES.LIST]: nft.getId() });
}
}
} catch (e) {
invalidate(listEntity.id, e.message);
Expand Down Expand Up @@ -319,6 +338,9 @@ export class Consolidator {
consolidatedNFT,
remark.block
);
if (this.emitInteractionChanges) {
this.interactionChanges.push({ [OP_TYPES.CONSUME]: nft.getId() });
}
}
} catch (e) {
invalidate(consumeEntity.id, e.message);
Expand Down Expand Up @@ -353,6 +375,9 @@ export class Consolidator {
buyInteraction(remark, buyEntity, nft, this.ss58Format);
if (nft && consolidatedNFT) {
await this.dbAdapter.updateNFTBuy(nft, consolidatedNFT, remark.block);
if (this.emitInteractionChanges) {
this.interactionChanges.push({ [OP_TYPES.BUY]: nft.getId() });
}
}
} catch (e) {
invalidate(buyEntity.id, e.message);
Expand Down Expand Up @@ -390,6 +415,9 @@ export class Consolidator {
remark.block !== consolidatedNFT.updatedAtBlock
) {
await this.dbAdapter.updateNFTEmote(nft, consolidatedNFT, remark.block);
if (this.emitInteractionChanges) {
this.interactionChanges.push({ [OP_TYPES.EMOTE]: nft.getId() });
}
}
} catch (e) {
invalidate(emoteEntity.id, e.message);
Expand Down Expand Up @@ -434,6 +462,11 @@ export class Consolidator {
consolidatedCollection,
remark.block
);
if (this.emitInteractionChanges) {
this.interactionChanges.push({
[OP_TYPES.CHANGEISSUER]: collection.id,
});
}
}
} catch (e) {
invalidate(changeIssuerEntity.id, e.message);
Expand Down Expand Up @@ -516,13 +549,17 @@ export class Consolidator {
// `${this.nfts.length} NFTs across ${this.collections.length} collections.`
// );
// console.log(`${this.invalidCalls.length} invalid calls.`);
return {
const result: ConsolidatorReturnType = {
nfts: this.dbAdapter.getAllNFTs ? await this.dbAdapter.getAllNFTs() : [],
collections: this.dbAdapter.getAllCollections
? await this.dbAdapter.getAllCollections()
: [],
invalid: this.invalidCalls,
};
if (this.emitInteractionChanges) {
result.changes = this.interactionChanges;
}
return result;
}
}

Expand Down
155 changes: 155 additions & 0 deletions test/__snapshots__/consolidator.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/consolidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,17 @@ describe("tools: Consolidator", () => {
"Attempting to change issuer of collection 705BED5A790A0D0072-BICHITOS when not issuer!"
);
});

it("should run consolidation from dump with interaction changes", async () => {
const remarks = getRemarksFromBlocks(blocksDumpAll, [
"0x726d726b",
"0x524d524b",
]);
const consolidator = new Consolidator(undefined, undefined, false, true);
const consolidated = await consolidator.consolidate(remarks);
const last50Changes = consolidated.changes!.slice(
consolidated.changes!.length - 50
);
expect(last50Changes).toMatchSnapshot();
});
});

0 comments on commit b85a7f8

Please sign in to comment.