Skip to content

Commit

Permalink
feat: implement isClarityType
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard authored and janniks committed Dec 14, 2023
1 parent 802487d commit 6eb1319
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
40 changes: 40 additions & 0 deletions packages/transactions/src/clarity/clarityValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
StringUtf8CV,
NoneCV,
SomeCV,
TrueCV,
FalseCV,
} from '.';

import { principalToString } from './types/principalCV';
Expand Down Expand Up @@ -169,3 +171,41 @@ export function getCVTypeString(val: ClarityValue): string {
return `(string-utf8 ${utf8ToBytes(val.data).length})`;
}
}

type ClarityTypetoValue = {
[ClarityType.OptionalNone]: NoneCV;
[ClarityType.OptionalSome]: SomeCV;
[ClarityType.ResponseOk]: ResponseOkCV;
[ClarityType.ResponseErr]: ResponseErrorCV;
[ClarityType.BoolTrue]: TrueCV;
[ClarityType.BoolFalse]: FalseCV;
[ClarityType.Int]: IntCV;
[ClarityType.UInt]: UIntCV;
[ClarityType.StringASCII]: StringAsciiCV;
[ClarityType.StringUTF8]: StringUtf8CV;
[ClarityType.PrincipalStandard]: StandardPrincipalCV;
[ClarityType.PrincipalContract]: ContractPrincipalCV;
[ClarityType.List]: ListCV;
[ClarityType.Tuple]: TupleCV;
[ClarityType.Buffer]: BufferCV;
};

/**
* @description narrow down the type of a generic ClarityValue
* @example
* ```ts
* // some functions can return a generic `ClarityValue` type
* let value = callReadOnlyFunction();
* // ^ ClarityValue
* // use `isClarityType` to narrow down the type
* assert(isClarityType(value, ClarityType.Int))
* console.log(value)
* // ^ IntCV
* ```
*/
export function isClarityType<T extends ClarityType>(
input: ClarityValue,
withType: T
): input is ClarityTypetoValue[T] {
return input.type === withType;
}
10 changes: 9 additions & 1 deletion packages/transactions/src/clarity/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ClarityValue, getCVTypeString, cvToString, cvToJSON, cvToValue } from './clarityValue';
import {
ClarityValue,
getCVTypeString,
cvToString,
cvToJSON,
cvToValue,
isClarityType,
} from './clarityValue';
import { ClarityType } from './constants';
import { BooleanCV, TrueCV, FalseCV, trueCV, falseCV, boolCV } from './types/booleanCV';
import { IntCV, UIntCV, intCV, uintCV } from './types/intCV';
Expand Down Expand Up @@ -91,6 +98,7 @@ export {
stringAsciiCV,
stringUtf8CV,
getCVTypeString,
isClarityType,
};

// Serialization
Expand Down
25 changes: 22 additions & 3 deletions packages/transactions/tests/clarity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BytesReader } from '../src/bytesReader';
import {
bufferCV,
BufferCV,
ClarityType,
ClarityValue,
contractPrincipalCV,
contractPrincipalCVFromStandard,
Expand Down Expand Up @@ -38,7 +39,14 @@ import {
uintCV,
UIntCV,
} from '../src/clarity';
import { cvToJSON, cvToString, cvToValue, getCVTypeString } from '../src/clarity/clarityValue';
import { Cl } from '../src';
import {
cvToJSON,
cvToString,
cvToValue,
getCVTypeString,
isClarityType,
} from '../src/clarity/clarityValue';
import { addressToString } from '../src/common';
import { deserializeAddress } from '../src/types';

Expand Down Expand Up @@ -423,9 +431,8 @@ describe('Clarity Types', () => {

// Test lexicographic ordering of tuple keys (to match Node Buffer compare)
const lexicographic = Object.keys(tuple.data).sort((a, b) => {
// eslint-disable-next-line node/prefer-global/buffer
const bufA = Buffer.from(a);
// eslint-disable-next-line node/prefer-global/buffer

const bufB = Buffer.from(b);
return bufA.compare(bufB);
});
Expand Down Expand Up @@ -678,4 +685,16 @@ describe('Clarity Types', () => {
);
});
});

describe('Clarity type narrowing', () => {
it('can narrow strings', () => {
const vUint = Cl.uint(1) as ClarityValue;
expect(isClarityType(vUint, ClarityType.UInt)).toBeTruthy();
expect(isClarityType(vUint, ClarityType.Int)).toBeFalsy();

const vInt = Cl.int(1) as ClarityValue;
expect(isClarityType(vInt, ClarityType.Int)).toBeTruthy();
expect(isClarityType(vInt, ClarityType.UInt)).toBeFalsy();
});
});
});

0 comments on commit 6eb1319

Please sign in to comment.