Skip to content

Commit

Permalink
perf: addressed pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed Jul 10, 2023
1 parent 20f3fa0 commit 82cb0ac
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 156 deletions.
70 changes: 35 additions & 35 deletions integration-tests/tzip16-non-compliant-contract.spec.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import { CONFIGS } from "./config";
import { tzip16, Tzip16Module, BigMapMetadataNotFound } from '@taquito/tzip16';
import { tzip16, Tzip16Module, BigMapContractMetadataNotFoundError } from '@taquito/tzip16';

CONFIGS().forEach(({ lib, rpc, setup }) => {
const Tezos = lib;
Tezos.addExtension(new Tzip16Module());
const Tezos = lib;
Tezos.addExtension(new Tzip16Module());

describe(`Test contract origination of a Tzip16 non-complaint contract through contract api using: ${rpc}`, () => {
describe(`Test contract origination of a Tzip16 non-complaint contract through contract api using: ${rpc}`, () => {

beforeEach(async (done) => {
await setup()
done()
})
it('Verify contract.originate for a simple contract having no metadata and then try to fetch metadata', async (done) => {
beforeEach(async (done) => {
await setup()
done()
})
it('Verify contract.originate for a simple contract having no metadata and then try to fetch metadata', async (done) => {

const value = '1234';
const code =
[{ "prim": "parameter", "args": [{ "prim": "bytes" }] },
{ "prim": "storage", "args": [{ "prim": "bytes" }] },
{
"prim": "code",
"args":
[[{ "prim": "DUP" }, { "prim": "CAR" }, { "prim": "SWAP" },
{ "prim": "CDR" }, { "prim": "CONCAT" },
{ "prim": "NIL", "args": [{ "prim": "operation" }] },
{ "prim": "PAIR" }]]
}];
const value = '1234';
const code =
[{ "prim": "parameter", "args": [{ "prim": "bytes" }] },
{ "prim": "storage", "args": [{ "prim": "bytes" }] },
{
"prim": "code",
"args":
[[{ "prim": "DUP" }, { "prim": "CAR" }, { "prim": "SWAP" },
{ "prim": "CDR" }, { "prim": "CONCAT" },
{ "prim": "NIL", "args": [{ "prim": "operation" }] },
{ "prim": "PAIR" }]]
}];

const op = await Tezos.contract.originate({
code: code,
storage: value
});
await op.confirmation();
const contractAddress = (await op.contract()).address;
const op = await Tezos.contract.originate({
code: code,
storage: value
});
await op.confirmation();
const contractAddress = (await op.contract()).address;

const contract = await Tezos.contract.at(contractAddress, tzip16);
try {
await contract.tzip16().getMetadata();
} catch (ex) {
expect(ex).toBeInstanceOf(BigMapMetadataNotFound);
}
done();
});
const contract = await Tezos.contract.at(contractAddress, tzip16);
try {
await contract.tzip16().getMetadata();
} catch (ex) {
expect(ex).toBeInstanceOf(BigMapContractMetadataNotFoundError);
}
done();
});
});
})
70 changes: 35 additions & 35 deletions integration-tests/tzip16-non-compliant-wallet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import { CONFIGS } from "./config";
import { tzip16, Tzip16Module, BigMapMetadataNotFound } from '@taquito/tzip16';
import { tzip16, Tzip16Module, BigMapContractMetadataNotFoundError } from '@taquito/tzip16';

CONFIGS().forEach(({ lib, rpc, setup }) => {
const Tezos = lib;
Tezos.addExtension(new Tzip16Module());
const Tezos = lib;
Tezos.addExtension(new Tzip16Module());

describe(`Test contract origination of a Tzip16 non-complaint contract through wallet api using: ${rpc}`, () => {
describe(`Test contract origination of a Tzip16 non-complaint contract through wallet api using: ${rpc}`, () => {

beforeEach(async (done) => {
await setup()
done()
})
it('Verify contract.originate for a simple contract having no metadata and then try to fetch metadata', async (done) => {
beforeEach(async (done) => {
await setup()
done()
})
it('Verify contract.originate for a simple contract having no metadata and then try to fetch metadata', async (done) => {

const value = '1234';
const code =
[{ "prim": "parameter", "args": [{ "prim": "bytes" }] },
{ "prim": "storage", "args": [{ "prim": "bytes" }] },
{
"prim": "code",
"args":
[[{ "prim": "DUP" }, { "prim": "CAR" }, { "prim": "SWAP" },
{ "prim": "CDR" }, { "prim": "CONCAT" },
{ "prim": "NIL", "args": [{ "prim": "operation" }] },
{ "prim": "PAIR" }]]
}];
const value = '1234';
const code =
[{ "prim": "parameter", "args": [{ "prim": "bytes" }] },
{ "prim": "storage", "args": [{ "prim": "bytes" }] },
{
"prim": "code",
"args":
[[{ "prim": "DUP" }, { "prim": "CAR" }, { "prim": "SWAP" },
{ "prim": "CDR" }, { "prim": "CONCAT" },
{ "prim": "NIL", "args": [{ "prim": "operation" }] },
{ "prim": "PAIR" }]]
}];

const op = await Tezos.wallet.originate({
code,
storage: value
}).send();
await op.confirmation();
const contractAddress = (await op.contract()).address;
const op = await Tezos.wallet.originate({
code,
storage: value
}).send();
await op.confirmation();
const contractAddress = (await op.contract()).address;

const contract = await Tezos.wallet.at(contractAddress, tzip16);
try {
await contract.tzip16().getMetadata();
} catch (ex) {
expect(ex).toBeInstanceOf(BigMapMetadataNotFound);
}
done();
});
const contract = await Tezos.wallet.at(contractAddress, tzip16);
try {
await contract.tzip16().getMetadata();
} catch (ex) {
expect(ex).toBeInstanceOf(BigMapContractMetadataNotFoundError);
}
done();
});
});
})
2 changes: 1 addition & 1 deletion packages/taquito-tzip12/src/tzip12-contract-abstraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class Tzip12ContractAbstraction {
);
return metadataFromUri.metadata;
} catch (e: any) {
if (e.name === 'InvalidUri') {
if (e.name === 'InvalidUriError') {
console.warn(
`The URI ${bytes2Char(uri)} is present in the token metadata, but is invalid.`
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MichelsonMap, ViewSimulationError } from '@taquito/taquito';
import { char2Bytes, InvalidUri } from '@taquito/tzip16';
import { char2Bytes, InvalidUriError } from '@taquito/tzip16';
import { Tzip12ContractAbstraction } from '../src/tzip12-contract-abstraction';
import { InvalidTokenMetadata, TokenIdNotFound, TokenMetadataNotFound } from '../src/errors';

Expand Down Expand Up @@ -223,11 +223,11 @@ describe('Tzip12 contract abstraction test', () => {
it('Test 1 for fetchTokenMetadataFromUri(): Should warn that the URI is invalid and return undefined', async (done) => {
const tokenMap = new MichelsonMap();
tokenMap.set('test', char2Bytes('test'));
tokenMap.set('', char2Bytes('invalidURI'));
tokenMap.set('', char2Bytes('InvalidUriError'));
tokenMap.set('testtest', char2Bytes('testtest'));

mockMetadataProvider.provideMetadata.mockImplementation(() => {
throw new InvalidUri(char2Bytes('invalidURI'));
throw new InvalidUriError(char2Bytes('InvalidUriError'));
});

const tokenMetadata = await tzip12Abs['fetchTokenMetadataFromUri'](
Expand Down
38 changes: 19 additions & 19 deletions packages/taquito-tzip16/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export { InvalidViewParameterError } from '@taquito/core';
* @category Error
* @description Error indicates missing big map metadata (non compliance to the TZIP-16 standard)
*/
export class BigMapMetadataNotFound extends TaquitoError {
constructor() {
export class BigMapContractMetadataNotFoundError extends TaquitoError {
constructor(public readonly invalidBigMapId: any) {
super();
this.name = 'BigMapMetadataNotFound';
this.name = 'BigMapContractMetadataNotFoundError';
this.message =
'Non-compliance with the TZIP-016 standard. No big map named metadata was found in the contract storage.';
}
Expand All @@ -19,10 +19,10 @@ export class BigMapMetadataNotFound extends TaquitoError {
* @category Error
* @description Error indicates missing metadata in storage
*/
export class ContractMetadataNotFound extends TaquitoError {
export class ContractMetadataNotFoundError extends TaquitoError {
constructor(public readonly info: string) {
super();
this.name = 'ContractMetadataNotFound';
this.name = 'ContractMetadataNotFoundError';
this.message = `No metadata was found in the contract storage. ${info}`;
}
}
Expand All @@ -31,10 +31,10 @@ export class ContractMetadataNotFound extends TaquitoError {
* @category Error
* @description Error indicates missing URI (non compliance to the TZIP-16 standard)
*/
export class UriNotFound extends TaquitoError {
export class UriNotFoundError extends TaquitoError {
constructor() {
super();
this.name = 'UriNotFound';
this.name = 'UriNotFoundError';
this.message =
'Non-compliance with the TZIP-016 standard. No URI found in the contract storage.';
}
Expand All @@ -44,10 +44,10 @@ export class UriNotFound extends TaquitoError {
* @category Error
* @description Error indicates an invalid URI (non compliance to the TZIP-16 standard)
*/
export class InvalidUri extends TaquitoError {
export class InvalidUriError extends TaquitoError {
constructor(public readonly uri: string) {
super();
this.name = 'InvalidUri';
this.name = 'InvalidUriError';
this.message = `Non-compliance with the TZIP-016 standard. The URI is invalid: ${uri}.`;
}
}
Expand All @@ -56,10 +56,10 @@ export class InvalidUri extends TaquitoError {
* @category Error
* @description Error indicates invalid metadata (non compliance to the TZIP-16 standard)
*/
export class InvalidContractMetadata extends TaquitoError {
export class InvalidContractMetadataError extends TaquitoError {
constructor(public readonly invalidMetadata: string) {
super();
this.name = 'InvalidContractMetadata';
this.name = 'InvalidContractMetadataError';
this.message = `The metadata found at the pointed ressource are not compliant with tzip16 standard: ${invalidMetadata}.`;
}
}
Expand All @@ -68,10 +68,10 @@ export class InvalidContractMetadata extends TaquitoError {
* @category Error
* @description Error indicates the uri protocol being passed or used is not supported
*/
export class ProtocolNotSupported extends ParameterValidationError {
export class ProtocolNotSupportedError extends ParameterValidationError {
constructor(public readonly protocol: string) {
super();
this.name = 'ProtocolNotSupported';
this.name = 'ProtocolNotSupportedError';
this.message = `The protocol found in the URI is not supported: ${protocol}.`;
}
}
Expand All @@ -80,10 +80,10 @@ export class ProtocolNotSupported extends ParameterValidationError {
* @category Error
* @description Error indicates the metadata type is invalid (non compliance to the TZIP-16 standard)
*/
export class InvalidContractMetadataType extends TaquitoError {
export class InvalidContractMetadataTypeError extends TaquitoError {
constructor() {
super();
this.name = 'InvalidContractMetadataType';
this.name = 'InvalidContractMetadataTypeError';
this.message =
'Non-compliance with the TZIP-016 standard. The type of metadata should be bytes.';
}
Expand All @@ -93,10 +93,10 @@ export class InvalidContractMetadataType extends TaquitoError {
* @category Error
* @description Error indicates metadata provider being unconfigured in the TezosToolkit instance
*/
export class UnconfiguredMetadataProviderError extends TezosToolkitConfigError {
export class UnconfiguredContractMetadataProviderError extends TezosToolkitConfigError {
constructor() {
super();
this.name = 'UnconfiguredMetadataProviderError';
this.name = 'UnconfiguredContractMetadataProviderError';
this.message =
'No metadata provider has been configured. The default one can be configured by calling addExtension(new Tzip16Module()) on your TezosToolkit instance.';
}
Expand All @@ -106,10 +106,10 @@ export class UnconfiguredMetadataProviderError extends TezosToolkitConfigError {
* @category Error
* @description Error indicates a forbidden instruction being found inside the View code
*/
export class ForbiddenInstructionInViewCode extends TaquitoError {
export class ForbiddenInstructionInViewCodeError extends TaquitoError {
constructor(public readonly instruction: string) {
super();
this.name = 'ForbiddenInstructionInViewCode';
this.name = 'ForbiddenInstructionInViewCodeError';
this.message = `Error found in the code of the view. It contains a forbidden instruction: ${instruction}.`;
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/taquito-tzip16/src/handlers/tezos-storage-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Context, ContractAbstraction, ContractProvider, Wallet } from '@taquito
import { Handler, Tzip16Uri } from '../metadata-provider';
import { bytes2Char } from '@taquito/utils';
import {
InvalidContractMetadataType,
BigMapMetadataNotFound,
InvalidUri,
ContractMetadataNotFound,
InvalidContractMetadataTypeError,
BigMapContractMetadataNotFoundError,
InvalidUriError,
ContractMetadataNotFoundError,
} from '../errors';

const typeOfValueToFind = {
Expand All @@ -27,7 +27,7 @@ export class TezosStorageHandler implements Handler {
) {
const parsedTezosStorageUri = this.parseTezosStorageUri(location);
if (!parsedTezosStorageUri) {
throw new InvalidUri(`tezos-storage:${location}`);
throw new InvalidUriError(`tezos-storage:${location}`);
}
const script = await context.readProvider.getScript(
parsedTezosStorageUri.contractAddress || contractAbstraction.address,
Expand All @@ -39,7 +39,7 @@ export class TezosStorageHandler implements Handler {
);

if (!bigMapId || !bigMapId.int) {
throw new BigMapMetadataNotFound();
throw new BigMapContractMetadataNotFoundError(bigMapId);
}
const bytes = await context.contract.getBigMapKeyByID<string>(
bigMapId.int.toString(),
Expand All @@ -48,15 +48,15 @@ export class TezosStorageHandler implements Handler {
);

if (!bytes) {
throw new ContractMetadataNotFound(
throw new ContractMetadataNotFoundError(
`No '${parsedTezosStorageUri.path}' key found in the big map %metadata of the contract ${
parsedTezosStorageUri.contractAddress || contractAbstraction.address
}`
);
}

if (!/^[0-9a-fA-F]*$/.test(bytes)) {
throw new InvalidContractMetadataType();
throw new InvalidContractMetadataTypeError();
}
return bytes2Char(bytes);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/taquito-tzip16/src/metadata-provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ContractAbstraction, ContractProvider, Wallet, Context } from '@taquito/taquito';
import { MetadataInterface } from './metadata-interface';
import { MetadataContext } from './tzip16-contract-abstraction';
import { InvalidContractMetadata, InvalidUri, ProtocolNotSupported } from './errors';
import { InvalidContractMetadataError, InvalidUriError, ProtocolNotSupportedError } from './errors';
import { calculateSHA256Hash } from './tzip16-utils';

export interface MetadataProviderInterface {
Expand Down Expand Up @@ -54,12 +54,12 @@ export class MetadataProvider implements MetadataProviderInterface {
): Promise<MetadataEnvelope> {
const uriInfo = this.extractProtocolInfo(uri);
if (!uriInfo || !uriInfo.location) {
throw new InvalidUri(uri);
throw new InvalidUriError(uri);
}

const handler = this.handlers.get(uriInfo.protocol);
if (!handler) {
throw new ProtocolNotSupported(uriInfo.protocol);
throw new ProtocolNotSupportedError(uriInfo.protocol);
}

const metadata = await handler.getMetadata(contractAbstraction, uriInfo, context);
Expand All @@ -68,7 +68,7 @@ export class MetadataProvider implements MetadataProviderInterface {
try {
metadataJSON = JSON.parse(metadata);
} catch (ex) {
throw new InvalidContractMetadata(metadata);
throw new InvalidContractMetadataError(metadata);
}

return {
Expand Down
Loading

0 comments on commit 82cb0ac

Please sign in to comment.