Skip to content

Commit

Permalink
refactor: consolidated InvalidAddressError into @taquito/core
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed May 3, 2023
1 parent 598da18 commit cacfc91
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"js"
],
"moduleNameMapper": {
"^@taquito/core$": "<rootDir>/packages/taquito-core/src/taquito-core.ts",
"^@taquito/rpc$": "<rootDir>/packages/taquito-rpc/src/taquito-rpc.ts",
"^@taquito/taquito$": "<rootDir>/packages/taquito/src/taquito.ts",
"^@taquito/michelson-encoder$": "<rootDir>/packages/taquito-michelson-encoder/src/taquito-michelson-encoder.ts",
Expand All @@ -48,7 +49,6 @@
"^@taquito/local-forging$": "<rootDir>/packages/taquito-local-forging/src/taquito-local-forging.ts",
"^@taquito/tzip16$": "<rootDir>/packages/taquito-tzip16/src/taquito-tzip16.ts",
"^@taquito/tzip12$": "<rootDir>/packages/taquito-tzip12/src/taquito-tzip12.ts",
"^@taquito/core$": "<rootDir>/packages/taquito-core/src/taquito-core.ts",
"^@taquito/contracts-library$": "<rootDir>/packages/taquito-contracts-library/src/contracts-library.ts"
},
"coveragePathIgnorePatterns": [
Expand Down
1 change: 1 addition & 0 deletions packages/taquito-contracts-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
]
},
"dependencies": {
"@taquito/core": "^16.1.2-beta-RC.0",
"@taquito/rpc": "^16.1.2-beta-RC.0",
"@taquito/taquito": "^16.1.2-beta-RC.0",
"@taquito/utils": "^16.1.2-beta-RC.0",
Expand Down
10 changes: 0 additions & 10 deletions packages/taquito-contracts-library/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/**
* @category Error
* @description Error that indicates an invalid address being used or passed
*/
export class InvalidAddressError extends Error {
constructor(message: string) {
super(message);
}
}

/**
* @category Error
* @description Error that indicates invalid script format being useed or passed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import { EntrypointsResponse, ScriptedContracts } from '@taquito/rpc';
import { Extension, Context } from '@taquito/taquito';
import { validateAddress, ValidationResult } from '@taquito/utils';
import { InvalidAddressError, InvalidScriptFormatError } from './errors';
import { InvalidScriptFormatError } from './errors';
import { ReadWrapperContractsLibrary } from './read-provider-wrapper';
import { InvalidAddressError } from '@taquito/core';

interface ContractsData {
[contractAddress: string]: { script: ScriptedContracts; entrypoints: EntrypointsResponse };
Expand Down Expand Up @@ -70,7 +71,7 @@ export class ContractsLibrary implements Extension {

private validateContractAddress(address: string) {
if (validateAddress(address) !== ValidationResult.VALID) {
throw new InvalidAddressError(`Address is invalid: ${address}`);
throw new InvalidAddressError(address);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { InvalidAddressError, InvalidScriptFormatError } from '../src/errors';
import { InvalidScriptFormatError } from '../src/errors';
import { ContractsLibrary } from '../src/taquito-contracts-library';
import { entrypoints, entrypoints2 } from './data/contract-entrypoints';
import { script, script2 } from './data/contract-script';
import { VERSION } from '../src/version';
import { InvalidAddressError } from '@taquito/core';

describe('ContractsLibrary tests', () => {
it('ContractsLibrary is instantiable', () => {
Expand Down Expand Up @@ -106,7 +107,7 @@ describe('ContractsLibrary tests', () => {
})
).toThrow(
expect.objectContaining({
message: expect.stringContaining('Address is invalid: KTinvalid'),
message: expect.stringContaining(`Address 'KTinvalid' is invalid.`),
})
);
});
Expand Down
20 changes: 20 additions & 0 deletions packages/taquito-core/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// ==========================================================================================
// parent error classes for Taquito
// ==========================================================================================
/**
* @category Error
* @description Parent error class all taquito errors to extend from
Expand Down Expand Up @@ -39,3 +42,20 @@ export class NetworkError extends TaquitoError {}
* @description Error indicates user attempts an action without necessary permissions
*/
export class PermissionDeniedError extends TaquitoError {}

// ==========================================================================================
// common error classes for Taquito
// ==========================================================================================
/**
* @category Error
* @description Error indicates an invalid originated or implicit address being passed or used
*/
export class InvalidAddressError extends ParameterValidationError {
public name = 'InvalidAddressError';
constructor(public address: string, errorDetail?: string) {
super();
this.name = 'InvalidAddressError';
this.message = `Address '${address}' is invalid.`;
errorDetail ? (this.message += ` ${errorDetail}`) : null;
}
}
26 changes: 25 additions & 1 deletion packages/taquito-core/test/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import {
UnsupportedAction,
NetworkError,
PermissionDeniedError,
InvalidAddressError,
} from '../src/taquito-core';

describe('parent-errors', () => {
describe('parent errors classes', () => {
it('should throw an ParameterValidationError', () => {
try {
throw new ParameterValidationError('tez');
Expand All @@ -30,6 +31,7 @@ describe('parent-errors', () => {
expect(error.message).toEqual('tez');
}
});

it('should throw an TezosToolkitConfigError', () => {
try {
throw new TezosToolkitConfigError('tez');
Expand All @@ -40,6 +42,7 @@ describe('parent-errors', () => {
expect(error.message).toEqual('tez');
}
});

it('should throw an UnsupportedAction', () => {
try {
throw new UnsupportedAction('tez');
Expand All @@ -50,6 +53,7 @@ describe('parent-errors', () => {
expect(error.message).toEqual('tez');
}
});

it('should throw an NetworkError', () => {
try {
throw new NetworkError('tez');
Expand All @@ -60,6 +64,7 @@ describe('parent-errors', () => {
expect(error.message).toEqual('tez');
}
});

it('should throw an PermissionDeniedError', () => {
try {
throw new PermissionDeniedError('tez');
Expand All @@ -71,3 +76,22 @@ describe('parent-errors', () => {
}
});
});

describe('common error classes', () => {
it('should throw an InvalidAddressError', () => {
try {
throw new InvalidAddressError('tz1');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidAddressError);
expect(error.message).toEqual("Address 'tz1' is invalid.");
}
try {
throw new InvalidAddressError('tz1', 'params source');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidAddressError);
expect(error.message).toEqual("Address 'tz1' is invalid. params source");
}
});
});
1 change: 1 addition & 0 deletions packages/taquito-local-forging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
]
},
"dependencies": {
"@taquito/core": "^16.1.2-beta-RC.0",
"@taquito/utils": "^16.1.2-beta-RC.0",
"bignumber.js": "^9.1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/taquito-local-forging/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
prefixLength,
InvalidKeyHashError,
InvalidPublicKeyError,
InvalidAddressError,
InvalidContractAddressError,
} from '@taquito/utils';
import {
Expand All @@ -30,6 +29,7 @@ import {
} from './michelson/codec';
import { Uint8ArrayConsumer } from './uint8array-consumer';
import { pad } from './utils';
import { InvalidAddressError } from '@taquito/core';

// https://tezos.gitlab.io/shell/p2p_api.html specifies data types and structure for forging

Expand Down
1 change: 1 addition & 0 deletions packages/taquito-rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
]
},
"dependencies": {
"@taquito/core": "^16.1.2-beta-RC.0",
"@taquito/http-utils": "^16.1.2-beta-RC.0",
"@taquito/utils": "^16.1.2-beta-RC.0",
"bignumber.js": "^9.1.0"
Expand Down
34 changes: 16 additions & 18 deletions packages/taquito-rpc/src/rpc-client-modules/rpc-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ import {
PendingOperations,
OriginationProofParams,
} from '../types';

import { InvalidAddressError } from '@taquito/core';
import {
InvalidAddressError,
InvalidContractAddressError,
validateContractAddress,
validateAddress,
Expand All @@ -77,7 +76,6 @@ type RpcMethodParam =
| EndorsingRightsQueryArguments
| OriginationProofParams;


const defaultTtl = 1000;

/***
Expand Down Expand Up @@ -1237,22 +1235,22 @@ export class RpcClientCache implements RpcClientInterface {
* @default args { version: '1', applied: true, refused: true, outdated, true, branchRefused: true, branchDelayed: true, validationPass: undefined }
* @see https://tezos.gitlab.io/CHANGES.html?highlight=pending_operations#id4
*/
async getPendingOperations(
args: PendingOperationsQueryArguments = {}
): Promise<PendingOperations> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_PENDING_OPERATIONS,
[args]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getPendingOperations(args);
this.put(key, response);
return response;
}
async getPendingOperations(
args: PendingOperationsQueryArguments = {}
): Promise<PendingOperations> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_PENDING_OPERATIONS,
[args]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getPendingOperations(args);
this.put(key, response);
return response;
}
}

/**
*
Expand Down
8 changes: 2 additions & 6 deletions packages/taquito-rpc/src/taquito-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ import {
OriginationProofParams,
} from './types';
import { castToBigNumber } from './utils/utils';
import {
InvalidAddressError,
validateAddress,
validateContractAddress,
ValidationResult,
} from '@taquito/utils';
import { validateAddress, validateContractAddress, ValidationResult } from '@taquito/utils';
import { InvalidAddressError } from '@taquito/core';

export { castToBigNumber } from './utils/utils';

Expand Down
1 change: 1 addition & 0 deletions packages/taquito-sapling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
]
},
"dependencies": {
"@taquito/core": "^16.1.2-beta-RC.0",
"@airgap/sapling-wasm": "0.0.9",
"@stablelib/nacl": "^1.0.3",
"@stablelib/random": "^1.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/taquito-sapling/src/taquito-sapling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { MichelCodecPacker, Packer, TzReadProvider } from '@taquito/taquito';
import {
b58cdecode,
format,
InvalidAddressError,
InvalidKeyError,
prefix,
Prefix,
Expand All @@ -31,6 +30,7 @@ import {
import { SaplingTransactionBuilder } from './sapling-tx-builder/sapling-transactions-builder';
import { DEFAULT_BOUND_DATA, DEFAULT_MEMO } from './constants';
import { InMemoryProvingKey } from './sapling-keys/in-memory-proving-key';
import { InvalidAddressError } from '@taquito/core';

export { SaplingTransactionViewer } from './sapling-tx-viewer/sapling-transaction-viewer';
export { InMemoryViewingKey } from './sapling-keys/in-memory-viewing-key';
Expand Down
6 changes: 3 additions & 3 deletions packages/taquito-sapling/test/taquito-sapling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe('SaplingToolkit', () => {
},
])
).rejects.toThrowError(
"The address 'tz1awHvfqEVsmNpXtLsLoHcjLk9HaXkzHf7Z' is invalid. The 'to' parameter must be a sapling address (zet1)."
"Address 'tz1awHvfqEVsmNpXtLsLoHcjLk9HaXkzHf7Z' is invalid. The 'to' parameter must be a sapling address (zet1)."
);

done();
Expand Down Expand Up @@ -517,7 +517,7 @@ describe('SaplingToolkit', () => {
amount: 8,
})
).rejects.toThrowError(
"The address 'zet12mVvzJ4QJhnNQetGHzdwTMcLgNrdC4SFact6BB5jpeqGAefWip3iGgEjvDA9z7b9Y' is invalid. The 'to' parameter must be a Tezos public key hash (tz1, tz2, tz3)."
"Address 'zet12mVvzJ4QJhnNQetGHzdwTMcLgNrdC4SFact6BB5jpeqGAefWip3iGgEjvDA9z7b9Y' is invalid. The 'to' parameter must be a Tezos public key hash (tz1, tz2, tz3)."
);

done();
Expand Down Expand Up @@ -654,7 +654,7 @@ describe('SaplingToolkit', () => {
},
])
).rejects.toThrowError(
"The address 'tz2V17qQHTuQ3GJLu5bmPQDJTLDVwiWCrYFh' is invalid. The 'to' parameter must be a sapling address (zet1)."
"Address 'tz2V17qQHTuQ3GJLu5bmPQDJTLDVwiWCrYFh' is invalid. The 'to' parameter must be a sapling address (zet1)."
);

done();
Expand Down
1 change: 1 addition & 0 deletions packages/taquito-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
]
},
"dependencies": {
"@taquito/core": "^16.1.2-beta-RC.0",
"@stablelib/blake2b": "^1.0.1",
"@stablelib/ed25519": "^1.0.3",
"@types/bs58check": "^2.1.0",
Expand Down
15 changes: 2 additions & 13 deletions packages/taquito-utils/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { InvalidAddressError } from '@taquito/core';

/**
* @category Error
* @description Error that indicates an invalid key being passed or used
Expand Down Expand Up @@ -61,19 +63,6 @@ export class InvalidContractAddressError extends Error {
}
}

/**
* @category Error
* @description Error that indicates an invalid address being passed or used (both contract and implicit)
*/
export class InvalidAddressError extends Error {
public name = 'InvalidAddressError';
constructor(public address: string, errorDetail?: string) {
super();
const baseMessage = `The address '${address}' is invalid.`;
this.message = errorDetail ? `${baseMessage} ${errorDetail}` : baseMessage;
}
}

/**
* @category Error
* @description Error that indicates an invalid chain id being passed or used
Expand Down
1 change: 1 addition & 0 deletions packages/taquito/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
]
},
"dependencies": {
"@taquito/core": "^16.1.2-beta-RC.0",
"@taquito/http-utils": "^16.1.2-beta-RC.0",
"@taquito/local-forging": "^16.1.2-beta-RC.0",
"@taquito/michel-codec": "^16.1.2-beta-RC.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/taquito/src/batch/rpc-batch-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ import { ContractMethodObject } from '../contract/contract-methods/contract-meth
import {
validateAddress,
validateKeyHash,
InvalidAddressError,
InvalidKeyHashError,
ValidationResult,
InvalidOperationKindError,
} from '@taquito/utils';
import { EstimationProvider } from '../estimate/estimate-provider-interface';
import { InvalidAddressError } from '@taquito/core';

export const BATCH_KINDS = [
OpKind.ACTIVATION,
Expand Down Expand Up @@ -89,7 +89,7 @@ export class OperationBatch extends OperationEmitter {
*/
withTransferTicket(params: TransferTicketParams) {
if (validateAddress(params.destination) !== ValidationResult.VALID) {
throw new InvalidAddressError(params.destination, 'param destination');
throw new InvalidAddressError(params.destination, 'params destination');
}
this.operations.push({ kind: OpKind.TRANSFER_TICKET, ...params });
return this;
Expand Down
Loading

0 comments on commit cacfc91

Please sign in to comment.