Skip to content

Commit

Permalink
fix: adds test coverage and fixes props
Browse files Browse the repository at this point in the history
  • Loading branch information
VanessaPC committed May 30, 2023
1 parent 7b04b13 commit aa5eaf7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 34 deletions.
9 changes: 0 additions & 9 deletions packages/core/src/Provider/HandleProvider/types.ts
Expand Up @@ -2,15 +2,6 @@ import { Cardano, Point, Provider } from '../..';

export type Handle = string;

/**
* Type of addresses that can resolve from a Handle
*/
export type CardanoAddress =
| Cardano.ByronAddress
| Cardano.PaymentAddress
| Cardano.EnterpriseAddress
| Cardano.PointerAddress;

/**
* @param policyId a hex encoded policyID
* @param handle a personalized string to identify a user
Expand Down
9 changes: 5 additions & 4 deletions packages/tx-construction/src/tx-builder/OutputBuilder.ts
Expand Up @@ -3,14 +3,15 @@ import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
import { Logger } from 'ts-log';

import {
HandleNotFoundError,
InvalidConfigurationError,
OutputBuilder,
OutputBuilderTxOut,
OutputValidationMinimumCoinError,
OutputValidationMissingRequiredError,
OutputValidationTokenBundleSizeError,
TxOutputFailure,
PartialTxOut
PartialTxOut,
TxOutputFailure
} from './types';
import { OutputValidation, OutputValidator } from '../output-validation';

Expand Down Expand Up @@ -151,9 +152,9 @@ export class TxOutputBuilder implements OutputBuilder {
txOut.handle = resolution[0];
txOut.address = resolution[0].resolvedAddresses.cardano;
} else {
// The an error because the handle resolved to null so we don't have
// Throw an error because the handle resolved to null so we don't have
// an address for the transaction.
throw new OutputValidationMissingRequiredError(this.#partialOutput);
throw new HandleNotFoundError(this.#partialOutput);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/tx-construction/src/tx-builder/TxBuilder.ts
Expand Up @@ -91,7 +91,7 @@ export class GenericTxBuilder implements TxBuilder {
#outputValidator: OutputBuilderValidator;
#delegateConfig: DelegateConfig;
#logger: Logger;
#handleProvider: HandleProvider;
#handleProvider: HandleProvider | undefined;
#handles: HandleResolution[];

constructor(dependencies: TxBuilderDependencies) {
Expand Down
11 changes: 9 additions & 2 deletions packages/tx-construction/src/tx-builder/types.ts
Expand Up @@ -21,7 +21,8 @@ export enum TxOutputFailure {
MinimumCoin = 'Minimum coin not met',
TokenBundleSizeExceedsLimit = 'Token Bundle Exceeds Limit',
MissingRequiredFields = 'Mandatory fields address or coin are missing',
MissingHandleProviderError = "Missing 'HandleProvider'"
MissingHandleProviderError = "Missing 'HandleProvider'",
HandleNotFound = 'Handle not found'
}

export class InvalidConfigurationError extends CustomError {
Expand All @@ -30,6 +31,12 @@ export class InvalidConfigurationError extends CustomError {
}
}

export class HandleNotFoundError extends CustomError {
public constructor(public txOut: PartialTxOut) {
super(TxOutputFailure.HandleNotFound);
}
}

export class OutputValidationMissingRequiredError extends CustomError {
public constructor(public txOut: PartialTxOut) {
super(TxOutputFailure.MissingRequiredFields);
Expand Down Expand Up @@ -224,7 +231,7 @@ export interface TxBuilderDependencies {
txBuilderProviders: TxBuilderProviders;
logger: Logger;
outputValidator?: OutputBuilderValidator;
handleProvider: HandleProvider;
handleProvider?: HandleProvider;
}

export type FinalizeTxDependencies = Pick<TxBuilderDependencies, 'inputResolver' | 'keyAgent'>;
47 changes: 29 additions & 18 deletions packages/tx-construction/test/tx-builder/TxBuilder.test.ts
Expand Up @@ -13,6 +13,8 @@ import { AssetId, mockProviders as mocks, somePartialStakePools } from '@cardano
import { CML, Cardano, Handle, ProviderError, ProviderFailure } from '@cardano-sdk/core';
import {
GenericTxBuilder,
HandleNotFoundError,
InvalidConfigurationError,
OutputBuilderValidator,
OutputValidation,
OutputValidationMinimumCoinError,
Expand Down Expand Up @@ -45,6 +47,7 @@ const resolvedHandle = {
describe('GenericTxBuilder', () => {
let outputValidator: jest.Mocked<OutputBuilderValidator>;
let txBuilder: GenericTxBuilder;
let txBuilderWithoutHandleProvider: GenericTxBuilder;
let txBuilderWithHandleErrors: GenericTxBuilder;
let txBuilderWithNullHandles: GenericTxBuilder;
let txBuilderProviders: jest.Mocked<TxBuilderProviders>;
Expand Down Expand Up @@ -91,27 +94,29 @@ describe('GenericTxBuilder', () => {
outputValidator = {
validateOutput: jest.fn().mockResolvedValue({ coinMissing: 0n } as OutputValidation)
};
txBuilder = new GenericTxBuilder({
handleProvider: {
healthCheck: jest.fn(),
resolveHandles: async () => [resolvedHandle]
},

const builderParams = {
inputResolver,
keyAgent: util.createAsyncKeyAgent(keyAgent),
logger: dummyLogger,
outputValidator,
txBuilderProviders
};

txBuilder = new GenericTxBuilder({
handleProvider: {
healthCheck: jest.fn(),
resolveHandles: async () => [resolvedHandle]
},
...builderParams
});
txBuilderWithoutHandleProvider = new GenericTxBuilder(builderParams);
txBuilderWithNullHandles = new GenericTxBuilder({
handleProvider: {
healthCheck: jest.fn(),
resolveHandles: async () => [null]
},
inputResolver,
keyAgent: util.createAsyncKeyAgent(keyAgent),
logger: dummyLogger,
outputValidator,
txBuilderProviders
...builderParams
});
txBuilderWithHandleErrors = new GenericTxBuilder({
handleProvider: {
Expand All @@ -126,11 +131,7 @@ describe('GenericTxBuilder', () => {
);
}
},
inputResolver,
keyAgent: util.createAsyncKeyAgent(keyAgent),
logger: dummyLogger,
outputValidator,
txBuilderProviders
...builderParams
});
});

Expand Down Expand Up @@ -358,6 +359,16 @@ describe('GenericTxBuilder', () => {
expect(outputBuilder.toTxOut().handle).toEqual(handle);
});

it('throws an error if attempting to set handle without a handleProvider', async () => {
try {
await txBuilderWithoutHandleProvider.buildOutput().handle(address).build();
} catch (error) {
expect(error instanceof InvalidConfigurationError).toBeTruthy();
}

expect.assertions(1);
});

it('can build a valid output', async () => {
const builtOutput = await txBuilder
.buildOutput()
Expand Down Expand Up @@ -413,13 +424,13 @@ describe('GenericTxBuilder', () => {
expect(txOut.address).toBe(resolvedHandle.resolvedAddresses.cardano);
});

it('rejects with an error when a handle is not found', async () => {
it('rejects with an error when a handle provider fails to resolve', async () => {
await expect(
txBuilderWithNullHandles.buildOutput().handle('alice').coin(output1Coin).build()
).rejects.toThrowError(OutputValidationMissingRequiredError);
).rejects.toThrowError(HandleNotFoundError);
});

it('rejects with an error when handle is not found', async () => {
it('rejects with an error when handle provider throws an error', async () => {
await expect(
txBuilderWithHandleErrors.buildOutput().handle('alice').coin(output1Coin).build()
).rejects.toThrowError(ProviderError);
Expand Down

0 comments on commit aa5eaf7

Please sign in to comment.