Skip to content

Commit

Permalink
Merge pull request #381 from ERC725Alliance/feat/schemas-types
Browse files Browse the repository at this point in the history
feat: allow to import TS typed schemas from `@erc725/erc725.js/schemas`
  • Loading branch information
richtera authored Jan 22, 2024
2 parents 6039fae + c8d3c70 commit e46dfd8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 27 deletions.
26 changes: 23 additions & 3 deletions docs/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,29 @@ _A quick reference for keys used in schema definitions can be seen below_

## Standard LSP Schemas

The most common schemas of [LUKSO Standard Proposals](https://github.com/lukso-network/LIPs/tree/main/LSPs) are available under the [`schemas/`](https://github.com/ERC725Alliance/erc725.js/tree/develop/schemas) folder.
The most common schemas of [LUKSO Standard Proposals](https://github.com/lukso-network/LIPs/tree/main/LSPs) are available to import. These are typed automatically with the Typescript type `ERC725JSONSchema[]` for when instantiating `new ERC725(...)` from Typescript projects.

Current provided LSPs are:
```ts
import {
LSP1Schema,
LSP3Schema,
LSP4Schema,
LSP4LegacySchema,
LSP5Schema,
LSP6Schema,
LSP8Schema,
LSP9Schema,
LSP10Schema,
LSP12Schema,
LSP17Schema,
} from '@erc725/erc725.js/schemas';

const erc725js = new ERC725(LSP12Schema);
```

The raw JSON schemas are also available for import from the [`schemas/`](https://github.com/ERC725Alliance/erc725.js/tree/develop/schemas) folder.

Current provided LSPs JSON schemas are:

```
LSP1UniversalReceiverDelegate.json
Expand All @@ -38,7 +58,7 @@ LSP12IssuedAssets.json
LSP17ContractExtension.json
```

You can import them from:
You can import the raw JSON as follow:

```js
import LSP3 from '@erc725/erc725.js/schemas/LSP3ProfileMetadata.json';
Expand Down
15 changes: 15 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import Web3 from 'web3';
import * as sinon from 'sinon';
import { hexToNumber, leftPad, numberToHex } from 'web3-utils';

// examples of schemas to load (for testing)
import { LSP1Schema, LSP12Schema, LSP3Schema, LSP6Schema } from './schemas';

import ERC725 from '.';
import {
decodeKeyValue,
Expand Down Expand Up @@ -68,6 +71,18 @@ describe('Running @erc725/erc725.js tests...', () => {
}, /Incorrect or unsupported provider/);
});

it('should allow importing the schemas and instantiating with them', async () => {
const schemasToLoad = [
...LSP1Schema,
...LSP12Schema,
...LSP3Schema,
...LSP6Schema,
];
const erc725 = new ERC725(schemasToLoad);

assert.deepStrictEqual(erc725.options.schemas, schemasToLoad);
});

it('should throw when calling getData without address & provider options set', async () => {
const erc725 = new ERC725(mockSchema);
try {
Expand Down
62 changes: 39 additions & 23 deletions src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import { ERC725JSONSchema } from '../types/ERC725JSONSchema';

import LSP1UniversalReceiverDelegate from '../../schemas/LSP1UniversalReceiverDelegate.json';
import LSP3Profile from '../../schemas/LSP3ProfileMetadata.json';
import LSP4DigitalAssetLegacy from '../../schemas/LSP4DigitalAssetLegacy.json';
import LSP4DigitalAsset from '../../schemas/LSP4DigitalAsset.json';
import LSP5ReceivedAssets from '../../schemas/LSP5ReceivedAssets.json';
import LSP6KeyManager from '../../schemas/LSP6KeyManager.json';
import LSP8IdentifiableDigitalAsset from '../../schemas/LSP8IdentifiableDigitalAsset.json';
import LSP9Vault from '../../schemas/LSP9Vault.json';
import LSP10ReceivedVaults from '../../schemas/LSP10ReceivedVaults.json';
import LSP12IssuedAssets from '../../schemas/LSP12IssuedAssets.json';
import LSP17ContractExtension from '../../schemas/LSP17ContractExtension.json';
import LSP1JSONSchema from '../../schemas/LSP1UniversalReceiverDelegate.json';
import LSP3JSONSchema from '../../schemas/LSP3ProfileMetadata.json';
import LSP4JSONSchema from '../../schemas/LSP4DigitalAsset.json';
import LSP4LegacyJSONSchema from '../../schemas/LSP4DigitalAssetLegacy.json';
import LSP5JSONSchema from '../../schemas/LSP5ReceivedAssets.json';
import LSP6JSONSchema from '../../schemas/LSP6KeyManager.json';
import LSP8JSONSchema from '../../schemas/LSP8IdentifiableDigitalAsset.json';
import LSP9JSONSchema from '../../schemas/LSP9Vault.json';
import LSP10JSONSchema from '../../schemas/LSP10ReceivedVaults.json';
import LSP12JSONSchema from '../../schemas/LSP12IssuedAssets.json';
import LSP17JSONSchema from '../../schemas/LSP17ContractExtension.json';

export default LSP1UniversalReceiverDelegate.concat(
LSP3Profile,
LSP4DigitalAssetLegacy,
LSP4DigitalAsset,
LSP5ReceivedAssets,
LSP6KeyManager,
LSP8IdentifiableDigitalAsset,
LSP9Vault,
LSP10ReceivedVaults,
LSP12IssuedAssets,
LSP17ContractExtension,
) as ERC725JSONSchema[];
type schemaType = ERC725JSONSchema[];

export const LSP1Schema: schemaType = LSP1JSONSchema as schemaType;
export const LSP3Schema: schemaType = LSP3JSONSchema as schemaType;
export const LSP4Schema: schemaType = LSP4JSONSchema as schemaType;
export const LSP4LegacySchema: schemaType = LSP4LegacyJSONSchema as schemaType;
export const LSP5Schema: schemaType = LSP5JSONSchema as schemaType;
export const LSP6Schema: schemaType = LSP6JSONSchema as schemaType;
export const LSP8Schema: schemaType = LSP8JSONSchema as schemaType;
export const LSP9Schema: schemaType = LSP9JSONSchema as schemaType;
export const LSP10Schema: schemaType = LSP10JSONSchema as schemaType;
export const LSP12Schema: schemaType = LSP12JSONSchema as schemaType;
export const LSP17Schema: schemaType = LSP17JSONSchema as schemaType;

const AllSchemas = LSP1Schema.concat(
LSP3Schema,
LSP4Schema,
LSP4LegacySchema,
LSP5Schema,
LSP6Schema,
LSP8Schema,
LSP9Schema,
LSP10Schema,
LSP12Schema,
LSP17Schema,
);

export default AllSchemas;
2 changes: 1 addition & 1 deletion src/types/ERC725JSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export function isValidValueType(
export interface ERC725JSONSchema {
name: string; // Describes the name of the key, SHOULD compromise of the Standards name + sub type. e.g: LSP2Name
key: string; // The keccak256 hash of the name. This is the actual key that MUST be retrievable via ERC725Y.getData(bytes32 key)
keyType: ERC725JSONSchemaKeyType; // Types that determine how the values should be interpreted.
keyType: ERC725JSONSchemaKeyType | string; // Types that determine how the values should be interpreted.
valueContent: ERC725JSONSchemaValueContent | string; // string holds '0x1345ABCD...' If the value content are specific bytes, than the returned value is expected to equal those bytes.
valueType: ERC725JSONSchemaValueType | string; // The type of the value. This is used to determine how the value should be encoded / decode (`string` for tuples and CompactBytesArray).
}

0 comments on commit e46dfd8

Please sign in to comment.