Skip to content

Commit

Permalink
feat(types): real imports
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Apr 16, 2024
1 parent 66d2b59 commit 4c4d232
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 257 deletions.
83 changes: 40 additions & 43 deletions packages/web-components/src/display/display.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { assert, details } from '@agoric/assert';
import { AssetKind } from '@agoric/ertp';
import '@agoric/ertp/exported.js';
import { stringifyCopyBag } from './copyBagValue/stringifyCopyBag.js';
import { parseAsNat } from './natValue/parseAsNat.js';
import { stringifyNat } from './natValue/stringifyNat.js';
import { stringifySet } from './setValue/stringifySet.js';
import { stringifyCopyBag } from './copyBagValue/stringifyCopyBag.js';

/** @typedef {import('@agoric/ertp').AssetKind} AssetKind */
/** @typedef {import('@agoric/ertp/src/types.js').AmountValue} AmountValue */
/** @typedef {import('@agoric/ertp/src/types.js').Brand} Brand */
/** @typedef {import('@agoric/ertp/src/types.js').Amount} Amount */
import type {
Amount,
AmountValue,
AssetKind as AssetKindUnion, // not a proper enum so can't reuse name for value and type
Brand,
NatValue,
} from '@agoric/ertp/src/types.js';

/**
* @param {string} str - string to parse as a value
* @param {AssetKind} [assetKind] - assetKind of the value
* @param {number} [decimalPlaces] - places to move the decimal to the left
* @returns {AmountValue}
* @param str - string to parse as a value
* @param [assetKind] - assetKind of the value
* @param [decimalPlaces] - places to move the decimal to the left
*/
export const parseAsValue = (
str,
assetKind = AssetKind.NAT,
str: string,
assetKind: AssetKindUnion = AssetKind.NAT,
decimalPlaces = 0,
) => {
): AmountValue => {
if (assetKind === AssetKind.NAT) {
return parseAsNat(str, decimalPlaces);
}
Expand All @@ -30,37 +31,35 @@ export const parseAsValue = (
};

/**
* @param {string} str - string to parse as a value
* @param {Brand} brand - brand to use in the amount
* @param {AssetKind} [assetKind] - assetKind of the value
* @param {number} [decimalPlaces] - places to move the decimal to the left
* @returns {Amount}
* @param str - string to parse as a value
* @param brand - brand to use in the amount
* @param [assetKind] - assetKind of the value
* @param [decimalPlaces] - places to move the decimal to the left
*/
export const parseAsAmount = (
str,
brand,
str: string,
brand: Brand,
assetKind = AssetKind.NAT,
decimalPlaces = 0,
) => {
): Amount => {
return { brand, value: parseAsValue(str, assetKind, decimalPlaces) };
};

/**
* @param {AmountValue} value - value to stringify
* @param {AssetKind} [assetKind] - assetKind of the value
* @param {number} [decimalPlaces] - places to move the decimal to the
* @param value - value to stringify
* @param [assetKind] - assetKind of the value
* @param [decimalPlaces] - places to move the decimal to the
* right in the string
* @param {number} [placesToShow] - places after the decimal to show
* @returns {string}
* @param [placesToShow] - places after the decimal to show
*/
export const stringifyValue = (
value,
assetKind = AssetKind.NAT,
value: AmountValue,
assetKind: AssetKindUnion = AssetKind.NAT,
decimalPlaces = 0,
placesToShow,
) => {
placesToShow?: number,
): string => {
if (assetKind === AssetKind.NAT) {
return stringifyNat(value, decimalPlaces, placesToShow);
return stringifyNat(value as NatValue, decimalPlaces, placesToShow);
}
if (assetKind === AssetKind.SET) {
return stringifySet(value);
Expand All @@ -75,10 +74,9 @@ export const stringifyValue = (

/**
* Stringify the value of a purse
* @param {any} purse
* @returns {string}
* @param purse
*/
export const stringifyPurseValue = purse => {
export const stringifyPurseValue = (purse): string => {
if (!purse) {
return '0';
}
Expand All @@ -91,18 +89,17 @@ export const stringifyPurseValue = purse => {

/**
* Stringify the value in an amount
* @param {Amount} amount
* @param {AssetKind} [assetKind] - assetKind of the value
* @param {number} [decimalPlaces] - places to move the decimal to the
* @param amount
* @param [assetKind] - assetKind of the value
* @param [decimalPlaces] - places to move the decimal to the
* right in the string
* @param {number} [placesToShow] - places after the decimal to show
* @returns {string}
* @param [placesToShow] - places after the decimal to show
*/
export function stringifyAmountValue(
amount,
assetKind,
decimalPlaces,
placesToShow,
amount: Amount,
assetKind: AssetKindUnion,
decimalPlaces?: number,
placesToShow?: number,
) {
if (!amount) {
return '0';
Expand Down
7 changes: 3 additions & 4 deletions packages/web-components/src/display/natValue/parseAsNat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import { roundToDecimalPlaces } from './helpers/roundToDecimalPlaces.js';
* still 300n cents with decimalPlaces =2 because the thousandths place is dropped.
*
* In the future, we may add a parameter to change the rounding rules.
* @param {string} str
* @param {number} decimalPlaces
* @returns {bigint}
* @param str
* @param decimalPlaces
*/
export function parseAsNat(str, decimalPlaces = 0) {
export function parseAsNat(str: string, decimalPlaces = 0): bigint {
assert.typeof(str, 'string', details`value to parse ${str} must be a string`);
const capturedNum = captureNum(str);
const roundedRight = roundToDecimalPlaces(capturedNum.right, decimalPlaces);
Expand Down
29 changes: 13 additions & 16 deletions packages/web-components/src/display/natValue/ratio.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
/** @file Copied from zoe/contractSupport to avoid taking that dependency */
import { Fail, q } from '@agoric/assert';
import { AmountMath } from '@agoric/ertp';
import type { Amount, Brand } from '@agoric/ertp/exported';

const PERCENT = 100n;

/** @typedef {import('@agoric/ertp/src/types.js').Brand} Brand */
export interface Ratio {
numerator: Amount<'nat'>;
denominator: Amount<'nat'>;
}

/**
* @typedef {object} Ratio
* @property {import('@agoric/ertp/src/types.js').Amount<'nat'>} numerator
* @property {import('@agoric/ertp/src/types.js').Amount<'nat'>} denominator
*/

// Copied from zoe/contractSupport to avoid taking that dependency
/**
* @param {bigint} numerator
* @param {Brand} numeratorBrand
* @param {bigint} [denominator] The default denominator is 100
* @param {Brand} [denominatorBrand] The default is to reuse the numeratorBrand
* @returns {Ratio}
* @param numerator
* @param numeratorBrand
* @param [denominator] The default denominator is 100
* @param [denominatorBrand] The default is to reuse the numeratorBrand
*/
export const makeRatio = (
numerator,
numeratorBrand,
numerator: bigint,
numeratorBrand: Brand<'nat'>,
denominator = PERCENT,
denominatorBrand = numeratorBrand,
) => {
): Ratio => {
denominator > 0n ||
Fail`No infinite ratios! Denominator was 0 ${q(denominatorBrand)}`;

Expand Down
21 changes: 5 additions & 16 deletions packages/web-components/src/display/natValue/stringifyNat.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { assert } from '@agoric/assert';
import { roundToDecimalPlaces } from './helpers/roundToDecimalPlaces.js';

/** @typedef {import('@agoric/ertp/src/types.js').NatValue} NatValue */
import type { NatValue } from '@agoric/ertp/exported.js';

const CONVENTIONAL_DECIMAL_PLACES = 2;
const MAX_DECIMAL_PLACES = 100;

/**
* @param {NatValue} value
* @returns {number}
*/
const calcTrailingZeros = value => {
const calcTrailingZeros = (value: NatValue): number => {
let zeroes = 0;
while (value > 0n && value % 10n === 0n) {
zeroes += 1;
Expand All @@ -19,17 +14,11 @@ const calcTrailingZeros = value => {
return zeroes;
};

/**
* @param {NatValue?} [natValue]
* @param {number} [decimalPlaces]
* @param {number} [placesToShow]
* @returns {string}
*/
export const stringifyNat = (
natValue = null,
natValue: NatValue | null = null,
decimalPlaces = 0,
placesToShow,
) => {
placesToShow?: number,
): string => {
if (natValue === null) {
return '';
}
Expand Down
17 changes: 5 additions & 12 deletions packages/web-components/src/display/natValue/stringifyRatio.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import { assert, details } from '@agoric/assert';
import { captureNum } from './helpers/captureNum.js';
import { roundToDecimalPlaces } from './helpers/roundToDecimalPlaces.js';

/** @typedef {import('@agoric/ertp/src/types.js').Brand} Brand */
/** @typedef {import('./ratio.js').Ratio} Ratio */
import type { Brand } from '@agoric/ertp/exported.js';
import type { Ratio } from './ratio.js';

const PLACES_TO_SHOW = 2;

/**
* @param {Ratio} ratio
* @param {(brand: Brand) => number | undefined} getDecimalPlaces
* @param {number} [placesToShow]
* @returns {string}
*/
export const stringifyRatio = (
ratio,
getDecimalPlaces,
ratio: Ratio,
getDecimalPlaces: (brand: Brand) => number | undefined,
placesToShow = PLACES_TO_SHOW,
) => {
): string => {
if (ratio === null || ratio === undefined) {
return '0';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@ import { assert } from '@agoric/assert';
import { isNatValue } from '@agoric/ertp';

import { stringifyNat } from './stringifyNat.js';
import type { Ratio } from './ratio.js';
import type { Brand } from '@agoric/ertp/exported.js';

const PLACES_TO_SHOW = 2;

/** @typedef {import('@agoric/ertp/src/types.js').Brand} Brand */

/**
* @param {import('./ratio.js').Ratio} ratio
* @param {(brand: Brand) => number | undefined } getDecimalPlaces
* @param {number} [numPlacesToShow]
* @param {number} [denomPlacesToShow]
* @returns {string}
*/
export const stringifyRatioAsFraction = (
ratio,
getDecimalPlaces,
ratio: Ratio,
getDecimalPlaces: (brand: Brand) => number | undefined,
numPlacesToShow = PLACES_TO_SHOW,
denomPlacesToShow = PLACES_TO_SHOW,
) => {
): string => {
assert(isNatValue(ratio.numerator.value));
assert(isNatValue(ratio.denominator.value));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@ import { assert, details } from '@agoric/assert';

import { captureNum } from './helpers/captureNum.js';
import { roundToDecimalPlaces } from './helpers/roundToDecimalPlaces.js';
import type { Ratio } from './ratio.js';
import type { Brand } from '@agoric/ertp/exported.js';

const PERCENT_BASE = 100n;
const PLACES_TO_SHOW = 0;

/** @typedef {import('@agoric/ertp/src/types.js').Brand} Brand */

/**
* @param {import('./ratio.js').Ratio} ratio
* @param {(brand: Brand) => number | undefined } getDecimalPlaces
* @param {number} [placesToShow]
* @returns {string}
*/
export const stringifyRatioAsPercent = (
ratio,
getDecimalPlaces,
ratio: Ratio,
getDecimalPlaces: (brand: Brand) => number | undefined,
placesToShow = PLACES_TO_SHOW,
) => {
): string => {
if (ratio === null || ratio === undefined) {
return '0';
}
Expand Down
12 changes: 4 additions & 8 deletions packages/web-components/src/wallet-connection/chainInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/** @typedef {import('@keplr-wallet/types').Bech32Config} Bech32Config */
/** @typedef {import('@keplr-wallet/types').FeeCurrency} FeeCurrency */
import type { Bech32Config, FeeCurrency } from '@keplr-wallet/types';

/** @type {FeeCurrency} */
export const stakeCurrency = {
export const stakeCurrency: FeeCurrency = {
coinDenom: 'BLD',
coinMinimalDenom: 'ubld',
coinDecimals: 6,
Expand All @@ -14,8 +12,7 @@ export const stakeCurrency = {
},
};

/** @type {FeeCurrency} */
export const stableCurrency = {
export const stableCurrency: FeeCurrency = {
coinDenom: 'IST',
coinMinimalDenom: 'uist',
coinDecimals: 6,
Expand All @@ -27,8 +24,7 @@ export const stableCurrency = {
},
};

/** @type {Bech32Config} */
export const bech32Config = {
export const bech32Config: Bech32Config = {
bech32PrefixAccAddr: 'agoric',
bech32PrefixAccPub: 'agoricpub',
bech32PrefixValAddr: 'agoricvaloper',
Expand Down
13 changes: 3 additions & 10 deletions packages/web-components/src/wallet-connection/connectKeplr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@ import {
import { Errors } from './errors.js';
import { agoricConverters, agoricRegistryTypes } from './signerOptions.js';

/** @typedef {import('@keplr-wallet/types').Keplr} Keplr */
import type { Keplr } from '@keplr-wallet/types';

/**
*
* @param {string} chainId
* @param {string} rpc
*/
export const connectKeplr = async (chainId, rpc) => {
export const connectKeplr = async (chainId: string, rpc: string) => {
if (!('keplr' in window)) {
throw Error(Errors.noKeplr);
}
/** @type {import('@keplr-wallet/types').Keplr} */
// @ts-expect-error cast (checked above)
const keplr = window.keplr;
const keplr = window.keplr as Keplr;

await null;
try {
Expand Down
Loading

0 comments on commit 4c4d232

Please sign in to comment.