Skip to content

Commit

Permalink
Fix pnum parsing ValueView (#1953)
Browse files Browse the repository at this point in the history
* Fix pnum parsing valueviews

* Add test for parsing ValueView

* Add changeset

* Improve linking scripts

* Update link comment

* Remove console log
  • Loading branch information
JasonMHasperhoven authored Dec 23, 2024
1 parent 032b43d commit ebc58d2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-suns-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@penumbra-zone/types': minor
---

Fix pnum parsing ValueView
28 changes: 28 additions & 0 deletions packages/types/src/pnum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,34 @@ describe('pnum', () => {
expect(result3.toString()).toBe('12345.5678');
});

it('should correctly parse ViewView', () => {
const result = pnum(
new ValueView({
valueView: {
case: 'knownAssetId',
value: {
amount: new Amount({
lo: 123455678n,
hi: 0n,
}),
metadata: new Metadata({
base: 'UM',
display: 'penumbra',
denomUnits: [
new DenomUnit({
exponent: 4,
denom: 'penumbra',
}),
],
}),
},
},
}),
);

expect(result.toString()).toBe('12345.5678');
});

it('should correctly convert to ValueView', () => {
const unknown = pnum(12345.5678, { exponent: 4 }).toValueView();
const metadata = new Metadata({
Expand Down
6 changes: 5 additions & 1 deletion packages/types/src/pnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ function pnum(
value = new BigNumber(input).shiftedBy(exponent);
} else if (typeof input === 'bigint') {
value = new BigNumber(input.toString());
} else if (input instanceof ValueView) {
} else if (
typeof input === 'object' &&
'valueView' in input &&
typeof input.valueView === 'object'
) {
const amount = getAmount(input);
value = new BigNumber(joinLoHi(amount.lo, amount.hi).toString());
exponent =
Expand Down
17 changes: 13 additions & 4 deletions scripts/link-externally.js → scripts/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ import { fileURLToPath } from 'url';
* This script is used to link dependencies from this monorepo to other repos.
*
* @usage:
* node scripts/link-externally.js enable
* node scripts/link-externally.js disable
* node scripts/link 1|true|enable|link|undefined
* node scripts/link 0|false|disable|unlink
*
* or
*
* node scripts/link
* node scripts/unlink
*
* // when enabled, you can link the package to another repo like this:
* cd other/repo && pnpm link ../penumbra-zone/web/packages/ui
*
* // watch inside packages
* cd packages/ui && pnpm dev:pack
* cd packages/types && pnpm dev:pack
*/

// Base directory for packages in the monorepo
Expand All @@ -19,8 +28,8 @@ const packagesDir = path.join(path.dirname(__filename), '..', 'packages');

// Get the command-line argument to determine action
const action = process.argv[2];
const enableLinking = action === 'enable';
const disableLinking = action === 'disable';
const enableLinking = ['enable', 'link', '1', 'true', undefined].includes(action);
const disableLinking = ['disable', 'unlink', '0', 'false'].includes(action);

// Helper function to enable linked exports
function enableLinkedExports(packageJsonPath) {
Expand Down
9 changes: 9 additions & 0 deletions scripts/unlink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable no-console -- disable console.log */
import { spawn } from 'child_process';

const child = spawn('node', ['scripts/link', 'unlink']);

child.stdout.on('data', data => {
// eslint-disable-next-line no-undef -- disable no-undef
console.log(data.toString());
});

0 comments on commit ebc58d2

Please sign in to comment.