Skip to content

Commit

Permalink
Merge pull request #113 from polkadot-cloud/rb-init-scraper-tests
Browse files Browse the repository at this point in the history
feat: Init scraper tests, metadata types to classes
  • Loading branch information
Ross Bulat authored Jun 13, 2024
2 parents b9397c2 + 8f50c43 commit eda5825
Show file tree
Hide file tree
Showing 42 changed files with 60,854 additions and 85 deletions.
3 changes: 2 additions & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://json.schemastore.org/mocharc.json",
"require": "tsx",
"spec": "test/**/*.spec.ts"
"spec": "test/**/*.spec.ts",
"recursive": true
}
2 changes: 1 addition & 1 deletion src/canvas/RuntimeSnapshot/PalletItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { motion } from 'framer-motion';
import { faChevronRight } from '@fortawesome/free-solid-svg-icons';
import { CanvasSubheading, RuntimeItemWrapper } from 'canvas/Wrappers';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import type { PalletItemScraped } from 'model/Metadata/Scraper/types';
import type { PalletItemScraped } from 'model/Scraper/types';
import { Fragment, useRef, useState } from 'react';
import { faChevronDown } from '@fortawesome/pro-solid-svg-icons';
import type { Sync } from '@w3ux/types';
Expand Down
2 changes: 1 addition & 1 deletion src/canvas/RuntimeSnapshot/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { CanvasCardWrapper, CanvasFullScreenWrapper } from 'canvas/Wrappers';
import { ButtonText } from 'library/Buttons/ButtonText';
import { useOverlay } from 'library/Overlay/Provider';
import { PalletScraper } from 'model/Metadata/Scraper/Pallet';
import { PalletScraper } from 'model/Scraper/Pallet';
import { PalletItem } from './PalletItem';

export const RuntimeSnapshot = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/canvas/RuntimeSnapshot/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { PalletScraper } from 'model/Metadata/Scraper/Pallet';
import type { PalletListItem } from 'model/Metadata/Scraper/types';
import type { PalletScraper } from 'model/Scraper/Pallet';
import type { PalletListItem } from 'model/Scraper/types';

export interface PalletItemProps {
scraper: PalletScraper;
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/ChainSpaceEnv/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { ownerIdToTabId, tabIdToOwnerId } from 'contexts/Tabs/Utils';
import type { ApiIndexLabel } from 'contexts/ApiIndexer/types';
import type { MetadataVersion } from 'model/Metadata/types';
import type { ApiPromise } from '@polkadot/api';
import { PalletScraper } from 'model/Metadata/Scraper/Pallet';
import { PalletScraper } from 'model/Scraper/Pallet';
import { xxhashAsHex } from '@polkadot/util-crypto';
import { u16 } from 'scale-ts';
import type { AnyJson } from '@w3ux/types';
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useSelectFirst/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const useSelectFirst = ({
newFilteredPallets.find((item: unknown) => item === activeItem) ===
undefined
) {
console.log('check failing');
onSelect(newFilteredPallets[0]);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/library/Header/Wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export const HeaderWrapper = styled.div`
> button {
opacity: 0.4;
transition: opacity 0.2s;
margin-left: 1rem;
margin-left: 0.9rem;
> .svg {
margin-top: 0.15rem;
width: 1rem;
height: 1rem;
width: 0.9rem;
height: 0.9rem;
}
&:hover {
Expand Down
4 changes: 2 additions & 2 deletions src/library/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export const Header = () => {
<CloudSvg className="svg" />
</button>
<button onClick={() => window.open(DocsUrl)}>
<FontAwesomeIcon icon={faBooks} transform="grow-1" />
<FontAwesomeIcon icon={faBooks} transform="shrink-2" />
</button>

<button onClick={() => window.open(GithubRepoUrl)}>
<FontAwesomeIcon icon={faGithub} transform="grow-3" />
<FontAwesomeIcon icon={faGithub} transform="grow-0" />
</button>
</div>
</HeaderWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only

import type { AnyJson } from '@w3ux/types';
import type { PalletItemScraped } from '../Scraper/types';
import type { PalletItemScraped } from '../types';

export class FormatCallSignature {
// The raw input config to format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only

import type { AnyJson } from '@w3ux/types';
import type { PalletItemScraped } from '../Scraper/types';
import type { PalletItemScraped } from '../types';

export class FormatInputFields {
// The raw input config to format.
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions src/model/Scraper/Lookup/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { MetadataLookup } from './types';

// Class to hold metadata lookup, with type accessors.
export class Lookup {
lookup: MetadataLookup;

constructor(lookup: MetadataLookup) {
this.lookup = lookup;
}

// Get types array from lookup.
types() {
return this.lookup.types;
}

// Get a type record from lookup types, or undefined otherwise.
getType(typeId: number) {
return this.types().find(({ id }: { id: number }) => id === typeId);
}
}
22 changes: 22 additions & 0 deletions src/model/Scraper/Lookup/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { AnyJson } from '@w3ux/types';

export interface MetadataLookup {
types: LookupTypes;
}

export type LookupTypes = LookupItem[];

export interface LookupItem {
id: number;
type: LookupType;
}

export interface LookupType {
path: string[];
params: AnyJson[];
def: AnyJson;
docs: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,7 @@ export class PalletScraper extends MetadataScraper {
if (!callType) {
return [];
}
const lookup = this.lookup.types.find(
({ id }: { id: number }) => id === callType
);

const lookup = this.lookup.getType(callType);
return lookup?.type?.def?.variant?.variants || [];
}

Expand Down
26 changes: 26 additions & 0 deletions src/model/Scraper/Types/Array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { IArrayType, MetadataType } from './types';
import type { TrailParam } from '../types';

// Class to hold an array type.
export class ArrayType implements MetadataType {
// The raw lookup data of this type.
lookup: LookupItem;

// The array type and length.
array: IArrayType;

constructor(array: IArrayType, lookup: LookupItem) {
this.lookup = lookup;
this.array = array;
}

// Scrape array type. Overwrites `type` with scraped type.
scrape(scraper: MetadataScraper, trailParam: TrailParam) {
return scraper.getType(this.array.type, trailParam);
}
}
29 changes: 29 additions & 0 deletions src/model/Scraper/Types/BitSequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { TrailParam } from '../types';
import type { BitSequenceType, MetadataType } from './types';

// Class to hold a bit sequence type.
export class BitSequence implements MetadataType {
// The raw lookup data of this type.
lookup: LookupItem;

// The type of this bit sequence.
bitSequence: BitSequenceType;

constructor(bitSequence: BitSequenceType, lookup: LookupItem) {
this.lookup = lookup;
this.bitSequence = bitSequence;
}

// Scrape bitSequence type. Overwrites `bitStoreType` and `bitOrderType` with scraped types.
scrape(scraper: MetadataScraper, { trailId }: TrailParam) {
return {
bitOrderType: scraper.start(this.bitSequence.bitOrderType, trailId),
bitStoreType: scraper.start(this.bitSequence.bitStoreType, trailId),
};
}
}
26 changes: 26 additions & 0 deletions src/model/Scraper/Types/Compact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { TrailParam } from '../types';
import type { CompactType, MetadataType } from './types';

// Class to hold a compact type.
export class Compact implements MetadataType {
// The raw lookup data of this type.
lookup: LookupItem;

// The inner type of this compact type.
type: number;

constructor(compact: CompactType, lookup: LookupItem) {
this.lookup = lookup;
this.type = compact.type;
}

// Scrape compact type. Overwrites `type` with scraped type.
scrape(scraper: MetadataScraper, trailParam: TrailParam) {
return scraper.getType(this.type, trailParam);
}
}
29 changes: 29 additions & 0 deletions src/model/Scraper/Types/Composite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { TrailParam } from '../types';
import type { CompositeField, CompositeType, MetadataType } from './types';

// Class to hold a composite type.
export class Composite implements MetadataType {
// The raw lookup data of this type.
lookup: LookupItem;

// The fields of this composite.
fields: CompositeField[];

constructor(composite: CompositeType, lookup: LookupItem) {
this.fields = composite.fields;
this.lookup = lookup;
}

// Scrape composite fields. Overwrites `fields` with scraped fields.
scrape(scraper: MetadataScraper, { trailId }: TrailParam) {
return [...this.fields].map((field) => ({
...field,
type: scraper.start(field.type, trailId),
}));
}
}
24 changes: 24 additions & 0 deletions src/model/Scraper/Types/Primitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { LookupItem } from '../Lookup/types';
import type { MetadataType } from './types';

// Class to hold a primitive type.
export class Primitive implements MetadataType {
// The raw lookup data of this type.
lookup: LookupItem;

// The type of this primitive.
primitive: string;

constructor(primitive: string, lookup: LookupItem) {
this.lookup = lookup;
this.primitive = primitive;
}

// Scrape primitive type. Simply returns the type.
scrape() {
return this.primitive;
}
}
26 changes: 26 additions & 0 deletions src/model/Scraper/Types/Sequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { MetadataType, SequenceType } from './types';
import type { TrailParam } from '../types';

// Class to hold a sequence type.
export class Sequence implements MetadataType {
// The raw lookup data of this type.
lookup: LookupItem;

// The type of this sequence.
type: number;

constructor(sequence: SequenceType, lookup: LookupItem) {
this.lookup = lookup;
this.type = sequence.type;
}

// Scrape sequence type. Overwrites `type` with scraped type.
scrape(scraper: MetadataScraper, trailParam: TrailParam) {
return scraper.getType(this.type, trailParam);
}
}
26 changes: 26 additions & 0 deletions src/model/Scraper/Types/Tuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { MetadataType, TupleType } from './types';
import type { TrailParam } from '../types';

// Class to hold a tuple type.
export class Tuple implements MetadataType {
// The raw lookup data of this type.
lookup: LookupItem;

// The types of this tuple.
tuple: TupleType;

constructor(tuple: TupleType, lookup: LookupItem) {
this.lookup = lookup;
this.tuple = tuple;
}

// Scrape tuple types. Overwrites the type with scraped type at each index.
scrape(scraper: MetadataScraper, { trailId }: TrailParam) {
return this.tuple.map((id: number) => scraper.start(id, trailId));
}
}
32 changes: 32 additions & 0 deletions src/model/Scraper/Types/Variant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { MetadataType, VariantItem } from './types';
import type { TrailParam } from '../types';

// Class to hold a variant type.
export class Variant implements MetadataType {
// The raw lookup data of this type.
lookup: LookupItem;

// The variants of this variant type.
items: VariantItem[];

constructor(variants: VariantItem[], lookup: LookupItem) {
this.items = variants;
this.lookup = lookup;
}

// Scrape variant fields. Overwrites `fields` with scraped fields.
scrape(scraper: MetadataScraper, { trailId }: TrailParam) {
return [...this.items].map((item) => ({
...item,
fields: item.fields.map((field) => ({
...field,
type: scraper.start(field.type, trailId),
})),
}));
}
}
Loading

0 comments on commit eda5825

Please sign in to comment.