Skip to content

Commit

Permalink
chore: reusable tinybarsToWeibars method and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nadezhda Popova <[email protected]>
  • Loading branch information
Nadezhda Popova authored and nadezhdapopovaa committed Oct 9, 2024
1 parent 2f9c0e8 commit 84c4102
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 4 deletions.
11 changes: 10 additions & 1 deletion packages/relay/src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const formatContractResult = (cr: any) => {
transactionIndex: nullableNumberTo0x(cr.transaction_index),
type: cr.type === null ? '0x0' : nanOrNumberTo0x(cr.type),
v: cr.v === null ? '0x0' : nanOrNumberTo0x(cr.v),
value: nanOrNumberTo0x(cr.amount * constants.TINYBAR_TO_WEIBAR_COEF),
value: nanOrNumberTo0x(tinybarsToWeibars(cr.amount)),
// for legacy EIP155 with tx.chainId=0x0, mirror-node will return a '0x' (EMPTY_HEX) value for contract result's chain_id
// which is incompatibile with certain tools (i.e. foundry). By setting this field, chainId, to undefined, the end jsonrpc
// object will leave out this field, which is the proper behavior for other tools to be compatible with.
Expand Down Expand Up @@ -301,6 +301,14 @@ const getFunctionSelector = (data?: string): string => {
return data.replace(/^0x/, '').substring(0, 8);
};

const tinybarsToWeibars = (value: number | null) => {
if (value && value < 0) throw new Error('Invalid value - cannot pass negative number');
if (value && value > constants.TOTAL_SUPPLY_TINYBARS)
throw new Error('Value cannot be more than the total supply of tinybars in the blockchain');

return value == null ? null : value * constants.TINYBAR_TO_WEIBAR_COEF;
};

export {
hashNumber,
formatRequestIdMessage,
Expand Down Expand Up @@ -328,4 +336,5 @@ export {
ASCIIToHex,
getFunctionSelector,
mapKeysAndValues,
tinybarsToWeibars,
};
1 change: 1 addition & 0 deletions packages/relay/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export enum CallType {
export default {
HBAR_TO_TINYBAR_COEF: 100_000_000,
TINYBAR_TO_WEIBAR_COEF: 10_000_000_000,
TOTAL_SUPPLY_TINYBARS: 5_000_000_000_000_000_000,
// 131072 bytes are 128kbytes
SEND_RAW_TRANSACTION_SIZE_LIMIT: process.env.SEND_RAW_TRANSACTION_SIZE_LIMIT
? parseInt(process.env.SEND_RAW_TRANSACTION_SIZE_LIMIT)
Expand Down
26 changes: 26 additions & 0 deletions packages/relay/tests/lib/formatters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
toNullIfEmptyHex,
trimPrecedingZeros,
weibarHexToTinyBarInt,
tinybarsToWeibars,
} from '../../src/formatters';
import constants from '../../src/lib/constants';
import { BigNumber as BN } from 'bignumber.js';
Expand Down Expand Up @@ -737,4 +738,29 @@ describe('Formatters', () => {
expect(result).to.deep.equal({ A: '1', B: '2', C: '3' });
});
});

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');
});

it('should throw an error when value is larger than the total supply of tinybars', () => {
expect(() => tinybarsToWeibars(constants.TOTAL_SUPPLY_TINYBARS * 10)).to.throw(
Error,
'Value cannot be more than the total supply of tinybars in the blockchain',
);
});
});
});
7 changes: 4 additions & 3 deletions packages/server/tests/helpers/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ export default class Assertions {
relayResponse.transactionIndex,
"Assert transaction: 'transactionIndex' should equal mirrorNode response",
).to.eq(ethers.toQuantity(mirrorNodeResponse.transaction_index));
expect(relayResponse.value, "Assert transaction: 'value' should equal mirrorNode response").to.eq(
ethers.toQuantity(mirrorNodeResponse.amount),
);
expect(
relayResponse.value,
"Assert transaction: 'value' should equal mirrorNode response converted in weibar",
).to.eq(ethers.toQuantity(BigInt(mirrorNodeResponse.amount * constants.TINYBAR_TO_WEIBAR_COEF)));
}

static transactionReceipt = (transactionReceipt, mirrorResult, effectiveGas) => {
Expand Down
125 changes: 125 additions & 0 deletions scripts/signTransaction.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 84c4102

Please sign in to comment.