From d619836e3b6f35b6afb4702e6116a2f9a31589fb Mon Sep 17 00:00:00 2001 From: "Jason M. Hasperhoven" Date: Thu, 19 Dec 2024 17:18:54 +0400 Subject: [PATCH] Add toValueView() to pnum (#1951) * Add toValueView() * Add changeset * Add unknown case and test for value view --- .changeset/beige-forks-tell.md | 5 ++++ packages/types/src/pnum.test.ts | 49 +++++++++++++++++++++++++++++++++ packages/types/src/pnum.ts | 25 ++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 .changeset/beige-forks-tell.md diff --git a/.changeset/beige-forks-tell.md b/.changeset/beige-forks-tell.md new file mode 100644 index 000000000..44d3ee69e --- /dev/null +++ b/.changeset/beige-forks-tell.md @@ -0,0 +1,5 @@ +--- +'@penumbra-zone/types': minor +--- + +Add toValueView() to pnum diff --git a/packages/types/src/pnum.test.ts b/packages/types/src/pnum.test.ts index 71a901bef..5356fa78b 100644 --- a/packages/types/src/pnum.test.ts +++ b/packages/types/src/pnum.test.ts @@ -2,6 +2,11 @@ import { describe, expect, it } from 'vitest'; import { pnum } from './pnum.js'; import { BigNumber } from 'bignumber.js'; import { Amount } from '@penumbra-zone/protobuf/penumbra/core/num/v1/num_pb'; +import { + DenomUnit, + Metadata, + ValueView, +} from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb'; describe('pnum', () => { it('should correctly parse and convert a number with decimals', () => { @@ -136,4 +141,48 @@ describe('pnum', () => { expect(result2.toFormattedString()).toBe(result3.toFormattedString()); expect(result3.toString()).toBe('12345.5678'); }); + + it('should correctly convert to ValueView', () => { + const unknown = pnum(12345.5678, { exponent: 4 }).toValueView(); + const metadata = new Metadata({ + base: 'UM', + display: 'penumbra', + denomUnits: [ + new DenomUnit({ + exponent: 0, + denom: 'UM', + }), + ], + }); + const known = pnum(12345.5678, { exponent: 4 }).toValueView(metadata); + + expect(unknown).toStrictEqual( + new ValueView({ + valueView: { + case: 'unknownAssetId', + value: { + amount: new Amount({ + lo: 123455678n, + hi: 0n, + }), + }, + }, + }), + ); + + expect(known).toStrictEqual( + new ValueView({ + valueView: { + case: 'knownAssetId', + value: { + amount: new Amount({ + lo: 123455678n, + hi: 0n, + }), + metadata, + }, + }, + }), + ); + }); }); diff --git a/packages/types/src/pnum.ts b/packages/types/src/pnum.ts index 39dddd172..527907bfa 100644 --- a/packages/types/src/pnum.ts +++ b/packages/types/src/pnum.ts @@ -2,7 +2,7 @@ import { BigNumber } from 'bignumber.js'; import { round } from '@penumbra-zone/types/round'; import { LoHi, joinLoHi, splitLoHi } from '@penumbra-zone/types/lo-hi'; import { Amount } from '@penumbra-zone/protobuf/penumbra/core/num/v1/num_pb'; -import { ValueView } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb'; +import { Metadata, ValueView } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb'; import { getAmount, getDisplayDenomExponentFromValueView } from '@penumbra-zone/getters/value-view'; import { removeTrailingZeros } from '@penumbra-zone/types/shortify'; @@ -120,6 +120,29 @@ function pnum( toAmount(): Amount { return new Amount(splitLoHi(BigInt(value.toFixed(0)))); }, + + toValueView(metadata?: Metadata): ValueView { + if (metadata) { + return new ValueView({ + valueView: { + case: 'knownAssetId', + value: { + amount: new Amount(splitLoHi(BigInt(value.toFixed(0)))), + metadata, + }, + }, + }); + } + + return new ValueView({ + valueView: { + case: 'unknownAssetId', + value: { + amount: new Amount(splitLoHi(BigInt(value.toFixed(0)))), + }, + }, + }); + }, }; }