Skip to content

Commit

Permalink
Unit tests for json functions
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenbf committed Oct 6, 2023
1 parent 40de574 commit 853c576
Show file tree
Hide file tree
Showing 24 changed files with 225 additions and 104 deletions.
1 change: 1 addition & 0 deletions packages/sdk/src/pub/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export {
TypedJson,
ToTypedJson,
} from '../types/util.js';
export { jsonParse, jsonStringify } from '../types/json.js';

// These cannot be exported directly as modules because of a bug in an eslint plugin.
// https://github.com/import-js/eslint-plugin-import/issues/2289.
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/types/AccountAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class AccountAddress extends TypeBase<Serializable> {
* - Is a valid base58 string with version byte of 1.
* - The base58 string is a length of 50 (encoding exactly 32 bytes).
*/
export { AccountAddress as Type };
export type Type = AccountAddress;
export const instanceOf = (value: unknown): value is AccountAddress =>
value instanceof AccountAddress;

/**
* Type guard for AccountAddress
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/types/BlockHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class BlockHash extends TypeBase<Serializable> {
/**
* Represents a hash of a block in the chain.
*/
export { BlockHash as Type };
export type Type = BlockHash;
export const instanceOf = (value: unknown): value is BlockHash =>
value instanceof BlockHash;

/**
* Create a BlockHash from a buffer of 32 bytes.
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/types/CcdAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class CcdAmount extends TypeBase<Serializable> {
* The base unit of CCD is micro CCD, which is the representation
* used on chain.
*/
export { CcdAmount as Type };
export type Type = CcdAmount;
export const instanceOf = (value: unknown): value is CcdAmount =>
value instanceof CcdAmount;

/**
* Constructs a CcdAmount and checks that it is valid. It accepts a number, string, big, or bigint as parameter.
Expand Down
10 changes: 7 additions & 3 deletions packages/sdk/src/types/ContractAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class ContractAddress extends TypeBase<Serializable> {
}

/** Address of a smart contract instance. */
export { ContractAddress as Type };
export type Type = ContractAddress;
export const instanceOf = (value: unknown): value is ContractAddress =>
value instanceof ContractAddress;

/**
* Type guard for ContractAddress
Expand Down Expand Up @@ -122,6 +124,9 @@ export function equals(left: ContractAddress, right: ContractAddress): boolean {
return left.index === right.index && left.subindex === right.subindex;
}

const fromSerializable = (v: Serializable) =>
new ContractAddress(BigInt(v.index), BigInt(v.subindex));

/**
* Takes a JSON string and converts it to instance of type {@linkcode Type}.
*
Expand All @@ -131,6 +136,5 @@ export function equals(left: ContractAddress, right: ContractAddress): boolean {
*/
export const fromTypedJSON = makeFromTypedJson(
JSON_DISCRIMINATOR,
(v: Serializable) =>
new ContractAddress(BigInt(v.index), BigInt(v.subindex))
fromSerializable
);
9 changes: 4 additions & 5 deletions packages/sdk/src/types/ContractName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class ContractName extends TypeBase<Serializable> {
}

/** The name of a smart contract. Note: This does _not_ including the 'init_' prefix. */
export { ContractName as Type };
export type Type = ContractName;
export const instanceOf = (value: unknown): value is ContractName =>
value instanceof ContractName;

/**
* Create a contract name from a string, ensuring it follows the format of a contract name.
Expand Down Expand Up @@ -109,7 +111,4 @@ export function equals(left: ContractName, right: ContractName): boolean {
* @throws {TypedJsonParseError} - If unexpected JSON string is passed.
* @returns {Type} The parsed instance.
*/
export const fromTypedJSON = makeFromTypedJson(
JSON_DISCRIMINATOR,
ContractName
);
export const fromTypedJSON = makeFromTypedJson(JSON_DISCRIMINATOR, fromString);
6 changes: 4 additions & 2 deletions packages/sdk/src/types/CredentialRegistrationId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class CredentialRegistrationId extends TypeBase<Serializable> {
* - Has length exactly 96, because a credId is 48 bytes.
* - Checks the first bit is 1, which indicates that the value represents a compressed BLS12-381 curve point.
*/
export { CredentialRegistrationId as Type };
export type Type = CredentialRegistrationId;
export const instanceOf = (value: unknown): value is CredentialRegistrationId =>
value instanceof CredentialRegistrationId;

/**
* Construct a CredentialRegistrationId from a hex string.
Expand Down Expand Up @@ -120,5 +122,5 @@ export function toBuffer(cred: CredentialRegistrationId): Uint8Array {
*/
export const fromTypedJSON = makeFromTypedJson(
JSON_DISCRIMINATOR,
CredentialRegistrationId
fromHexString
);
2 changes: 1 addition & 1 deletion packages/sdk/src/types/DataBlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class DataBlob extends TypeBase<Serializable> {
this.data = Buffer.from(data);
}

toJSON(): string {
public toJSON(): string {
return packBufferWithWord16Length(this.data).toString('hex');
}

Expand Down
8 changes: 6 additions & 2 deletions packages/sdk/src/types/Duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class Duration extends TypeBase<Serializable> {
/**
* Type representing a duration of time.
*/
export { Duration as Type };
export type Type = Duration;
export const instanceOf = (value: unknown): value is Duration =>
value instanceof Duration;

/**
* Construct a Duration from a given number of milliseconds.
Expand Down Expand Up @@ -76,6 +78,8 @@ export function toProto(duration: Duration): Proto.Duration {
};
}

const fromSerializable = (v: Serializable) => fromMillis(BigInt(v));

/**
* Takes a JSON string and converts it to instance of type {@linkcode Type}.
*
Expand All @@ -85,5 +89,5 @@ export function toProto(duration: Duration): Proto.Duration {
*/
export const fromTypedJSON = makeFromTypedJson(
JSON_DISCRIMINATOR,
(v: string) => fromMillis(BigInt(v))
fromSerializable
);
11 changes: 9 additions & 2 deletions packages/sdk/src/types/Energy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class Energy extends TypeBase<Serializable> {
}

/** Energy measure. Used as part of cost calculations for transactions. */
export { Energy as Type };
export type Type = Energy;
export const instanceOf = (value: unknown): value is Energy =>
value instanceof Energy;

/**
* Construct an Energy type.
Expand Down Expand Up @@ -60,11 +62,16 @@ export function toProto(energy: Energy): Proto.Energy {
};
}

const fromSerializable = (v: Serializable) => create(BigInt(v));

/**
* Takes a JSON string and converts it to instance of type {@linkcode Type}.
*
* @param {TypedJson} json - The typed JSON to convert.
* @throws {TypedJsonParseError} - If unexpected JSON string is passed.
* @returns {Type} The parsed instance.
*/
export const fromTypedJSON = makeFromTypedJson(JSON_DISCRIMINATOR, Energy);
export const fromTypedJSON = makeFromTypedJson(
JSON_DISCRIMINATOR,
fromSerializable
);
9 changes: 4 additions & 5 deletions packages/sdk/src/types/EntrypointName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class EntrypointName<S extends string = string> extends TypeBase<Serializable> {
/**
* Type representing an entrypoint of a smart contract.
*/
export { EntrypointName as Type };
export type Type<S extends string = string> = EntrypointName<S>;
export const instanceOf = (value: unknown): value is EntrypointName =>
value instanceof EntrypointName;

/**
* Create a smart contract entrypoint name from a string, ensuring it follows the required format.
Expand Down Expand Up @@ -80,7 +82,4 @@ export function toString<S extends string>(
* @throws {TypedJsonParseError} - If unexpected JSON string is passed.
* @returns {Type} The parsed instance.
*/
export const fromTypedJSON = makeFromTypedJson(
JSON_DISCRIMINATOR,
EntrypointName
);
export const fromTypedJSON = makeFromTypedJson(JSON_DISCRIMINATOR, fromString);
10 changes: 6 additions & 4 deletions packages/sdk/src/types/InitName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class InitName extends TypeBase<Serializable> {
}

/** The name of an init-function for a smart contract. Note: This is of the form 'init_<contractName>'. */
export { InitName as Type };
export type Type = InitName;
export const instanceOf = (value: unknown): value is InitName =>
value instanceof InitName;

/**
* Create an InitName directly from a string, ensuring it follows the format of an init-function name.
Expand All @@ -34,10 +36,10 @@ export { InitName as Type };
* @returns {InitName}
*/
export function fromString(value: string): InitName {
if (value.length <= 100) {
if (value.length > 100) {
throw new Error('Invalid InitName: Can be atmost 100 characters long.');
}
if (value.startsWith('init_')) {
if (!value.startsWith('init_')) {
throw new Error("Invalid InitName: Must be prefixed with 'init_'.");
}
if (value.includes('.')) {
Expand Down Expand Up @@ -106,4 +108,4 @@ export function toProto(initName: InitName): Proto.InitName {
* @throws {TypedJsonParseError} - If unexpected JSON string is passed.
* @returns {Type} The parsed instance.
*/
export const fromTypedJSON = makeFromTypedJson(JSON_DISCRIMINATOR, InitName);
export const fromTypedJSON = makeFromTypedJson(JSON_DISCRIMINATOR, fromString);
4 changes: 3 additions & 1 deletion packages/sdk/src/types/ModuleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class ModuleClient {
/**
* Type representing a smart contract module deployed on chain.
*/
export { ModuleClient as Type };
export type Type = ModuleClient;
export const instanceOf = (value: unknown): value is ModuleClient =>
value instanceof ModuleClient;

/**
* Create a new `GenericModule` instance for interacting with a smart contract module on chain.
Expand Down
16 changes: 10 additions & 6 deletions packages/sdk/src/types/ModuleReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ModuleReference extends TypeBase<Serializable> {
super();
}

toJSON(): string {
public toJSON(): string {
return packBufferWithWord32Length(this.decodedModuleRef).toString(
'hex'
);
Expand All @@ -42,7 +42,9 @@ class ModuleReference extends TypeBase<Serializable> {
/**
* Reference to a smart contract module.
*/
export { ModuleReference as Type };
export type Type = ModuleReference;
export const instanceOf = (value: unknown): value is ModuleReference =>
value instanceof ModuleReference;

/**
* Create a ModuleReference from a buffer of 32 bytes.
Expand Down Expand Up @@ -112,6 +114,11 @@ export function equals(left: ModuleReference, right: ModuleReference): boolean {
return left.moduleRef === right.moduleRef;
}

const fromSerializable = (v: Serializable) => {
const data = Buffer.from(v, 'hex');
return fromBuffer(data);
};

/**
* Takes a JSON string and converts it to instance of type {@linkcode Type}.
*
Expand All @@ -121,8 +128,5 @@ export function equals(left: ModuleReference, right: ModuleReference): boolean {
*/
export const fromTypedJSON = makeFromTypedJson(
JSON_DISCRIMINATOR,
(v: Serializable) => {
const data = Buffer.from(v, 'hex');
return fromBuffer(data);
}
fromSerializable
);
4 changes: 3 additions & 1 deletion packages/sdk/src/types/Parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class Parameter extends TypeBase<Serializable> {
}

/** Parameter for a smart contract entrypoint. */
export { Parameter as Type };
export type Type = Parameter;
export const instanceOf = (value: unknown): value is Parameter =>
value instanceof Parameter;

/**
* Create an empty parameter.
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/types/ReceiveName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class ReceiveName extends TypeBase<Serializable> {
* - It is at most 100 characters.
* - It contains at least one '.' character.
*/
export { ReceiveName as Type };
export type Type = ReceiveName;
export const instanceOf = (value: unknown): value is ReceiveName =>
value instanceof ReceiveName;

/**
* Create a ReceiveName.
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/types/ReturnValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class ReturnValue extends TypeBase<Serializable> {
}

/** Return value from invoking a smart contract entrypoint. */
export { ReturnValue as Type };
export type Type = ReturnValue;
export const instanceOf = (value: unknown): value is ReturnValue =>
value instanceof ReturnValue;

/**
* Create an empty return value.
Expand Down
8 changes: 6 additions & 2 deletions packages/sdk/src/types/SequenceNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class SequenceNumber extends TypeBase<Serializable> {
}

/** A transaction sequence number. (Formerly refered as Nonce) */
export { SequenceNumber as Type };
export type Type = SequenceNumber;
export const instanceOf = (value: unknown): value is SequenceNumber =>
value instanceof SequenceNumber;

/**
* Construct an SequenceNumber type.
Expand Down Expand Up @@ -62,6 +64,8 @@ export function toProto(sequenceNumber: SequenceNumber): Proto.SequenceNumber {
};
}

const fromSerializable = (v: Serializable) => create(BigInt(v));

/**
* Takes a JSON string and converts it to instance of type {@linkcode Type}.
*
Expand All @@ -71,5 +75,5 @@ export function toProto(sequenceNumber: SequenceNumber): Proto.SequenceNumber {
*/
export const fromTypedJSON = makeFromTypedJson(
JSON_DISCRIMINATOR,
(v: string) => create(BigInt(v))
fromSerializable
);
8 changes: 6 additions & 2 deletions packages/sdk/src/types/Timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class Timestamp extends TypeBase<Serializable> {
}

/** Represents a timestamp. */
export { Timestamp as Type };
export type Type = Timestamp;
export const instanceOf = (value: unknown): value is Timestamp =>
value instanceof Timestamp;

/**
* Create a Timestamp from milliseconds since Unix epoch.
Expand Down Expand Up @@ -95,6 +97,8 @@ export function toProto(timestamp: Timestamp): Proto.Timestamp {
};
}

const fromSerializable = (v: Serializable) => fromMillis(BigInt(v));

/**
* Takes a JSON string and converts it to instance of type {@linkcode Type}.
*
Expand All @@ -104,5 +108,5 @@ export function toProto(timestamp: Timestamp): Proto.Timestamp {
*/
export const fromTypedJSON = makeFromTypedJson(
JSON_DISCRIMINATOR,
(v: string) => fromMillis(BigInt(v))
fromSerializable
);
10 changes: 7 additions & 3 deletions packages/sdk/src/types/TransactionExpiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ class TransactionExpiry extends TypeBase<Serializable> {
super();
}

toJSON(): number {
public toJSON(): number {
return Number(this.expiryEpochSeconds);
}
}

/**
* Representation of a transaction expiry date.
*/
export { TransactionExpiry as Type };
export type Type = TransactionExpiry;
export const instanceOf = (value: unknown): value is TransactionExpiry =>
value instanceof TransactionExpiry;

/**
* Construct a TransactionExpiry from a number of seconds since unix epoch.
Expand Down Expand Up @@ -98,6 +100,8 @@ export function toProto(expiry: TransactionExpiry): Proto.TransactionTime {
};
}

const fromSerializable = (v: Serializable) => fromEpochSeconds(BigInt(v));

/**
* Takes a JSON string and converts it to instance of type {@linkcode Type}.
*
Expand All @@ -107,5 +111,5 @@ export function toProto(expiry: TransactionExpiry): Proto.TransactionTime {
*/
export const fromTypedJSON = makeFromTypedJson(
JSON_DISCRIMINATOR,
(v: string) => fromEpochSeconds(BigInt(v))
fromSerializable
);
Loading

0 comments on commit 853c576

Please sign in to comment.