Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pnum parsing ValueView #1953

Merged
merged 7 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
});