Skip to content

Commit

Permalink
fixup! chore: reusable tinybarsToWeibars method
Browse files Browse the repository at this point in the history
Signed-off-by: Nadezhda Popova <nadezhdapopova@Nadezhdas-Work-MacBook-Pro.local>
  • Loading branch information
Nadezhda Popova authored and Nadezhda Popova committed Oct 9, 2024
1 parent 3f63bf5 commit 8372d35
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/relay/src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,9 @@ const getFunctionSelector = (data?: string): string => {
return data.replace(/^0x/, '').substring(0, 8);
};

const tinybarsToWeibars = (value) => {
return value * constants.TINYBAR_TO_WEIBAR_COEF;
const tinybarsToWeibars = (value: number | null) => {
if (value && value < 0) throw new Error('Invalid value, cannot pass negative number.');
return value == null ? null : value * constants.TINYBAR_TO_WEIBAR_COEF;
};

export {
Expand Down
14 changes: 13 additions & 1 deletion packages/relay/tests/lib/formatters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,21 @@ describe('Formatters', () => {
});
});

describe('getFunctionSelector', () => {
describe('tinybarsToWeibars', () => {
it('should convert tinybars to weibars', () => {
expect(tinybarsToWeibars(10)).to.eql(100000000000);
});

it('should return null if null is passed', () => {
expect(tinybarsToWeibars(null)).to.eql(null);
});

it('should return 0 for 0 input', () => {
expect(tinybarsToWeibars(0)).to.eql(0);
});

it('should throw an error when value is smaller than 0', () => {
expect(() => (tinybarsToWeibars(-10))).to.throw(Error, 'Invalid value, cannot pass negative number.');
});
});
});

0 comments on commit 8372d35

Please sign in to comment.