Skip to content

Commit

Permalink
Merge branch 'main' into changeset-release/main
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMHasperhoven authored Dec 19, 2024
2 parents 3cd6340 + d619836 commit 31d7352
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/beige-forks-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@penumbra-zone/types': minor
---

Add toValueView() to pnum
49 changes: 49 additions & 0 deletions packages/types/src/pnum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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,
},
},
}),
);
});
});
25 changes: 24 additions & 1 deletion packages/types/src/pnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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)))),
},
},
});
},
};
}

Expand Down

0 comments on commit 31d7352

Please sign in to comment.