Skip to content

Commit 6ef6446

Browse files
ebadierenadezhdapopovaaNadezhda Popova
authored
feat: change units of the value in eth gettransactionbyhash result (#… (#3081)
feat: change units of the value in eth gettransactionbyhash result (#3080) * fix: change unit of transaction value result * chore: reusable tinybarsToWeibars method and tests --------- Signed-off-by: Nadezhda Popova <[email protected]> Signed-off-by: ebadiere <[email protected]> Co-authored-by: Nadezhda Popova <[email protected]> Co-authored-by: Nadezhda Popova <[email protected]>
1 parent 78cd1dd commit 6ef6446

File tree

6 files changed

+172
-6
lines changed

6 files changed

+172
-6
lines changed

packages/relay/src/formatters.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const formatContractResult = (cr: any) => {
178178
transactionIndex: nullableNumberTo0x(cr.transaction_index),
179179
type: cr.type === null ? '0x0' : nanOrNumberTo0x(cr.type),
180180
v: cr.v === null ? '0x0' : nanOrNumberTo0x(cr.v),
181-
value: nanOrNumberTo0x(cr.amount),
181+
value: nanOrNumberTo0x(tinybarsToWeibars(cr.amount)),
182182
// for legacy EIP155 with tx.chainId=0x0, mirror-node will return a '0x' (EMPTY_HEX) value for contract result's chain_id
183183
// which is incompatibile with certain tools (i.e. foundry). By setting this field, chainId, to undefined, the end jsonrpc
184184
// object will leave out this field, which is the proper behavior for other tools to be compatible with.
@@ -303,6 +303,14 @@ const getFunctionSelector = (data?: string): string => {
303303
return data.replace(/^0x/, '').substring(0, 8);
304304
};
305305

306+
const tinybarsToWeibars = (value: number | null) => {
307+
if (value && value < 0) throw new Error('Invalid value - cannot pass negative number');
308+
if (value && value > constants.TOTAL_SUPPLY_TINYBARS)
309+
throw new Error('Value cannot be more than the total supply of tinybars in the blockchain');
310+
311+
return value == null ? null : value * constants.TINYBAR_TO_WEIBAR_COEF;
312+
};
313+
306314
export {
307315
hashNumber,
308316
formatRequestIdMessage,
@@ -330,4 +338,5 @@ export {
330338
ASCIIToHex,
331339
getFunctionSelector,
332340
mapKeysAndValues,
341+
tinybarsToWeibars,
333342
};

packages/relay/src/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export enum CallType {
6969
export default {
7070
HBAR_TO_TINYBAR_COEF: 100_000_000,
7171
TINYBAR_TO_WEIBAR_COEF: 10_000_000_000,
72+
TOTAL_SUPPLY_TINYBARS: 5_000_000_000_000_000_000,
7273
// 131072 bytes are 128kbytes
7374
SEND_RAW_TRANSACTION_SIZE_LIMIT: process.env.SEND_RAW_TRANSACTION_SIZE_LIMIT
7475
? parseInt(process.env.SEND_RAW_TRANSACTION_SIZE_LIMIT)

packages/relay/tests/lib/eth/eth-config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*
1919
*/
2020
import {
21+
defaultDetailedContractResultByHash,
2122
defaultEvmAddress,
2223
defaultLogs1,
2324
defaultLogs2,
@@ -26,7 +27,7 @@ import {
2627
mockData,
2728
toHex,
2829
} from '../../helpers';
29-
import { numberTo0x } from '../../../dist/formatters';
30+
import { numberTo0x, nanOrNumberTo0x } from '../../../dist/formatters';
3031
import constants from '../../../src/lib/constants';
3132

3233
export const BLOCK_TRANSACTION_COUNT = 77;
@@ -601,6 +602,9 @@ export const BLOCK_BY_HASH_FROM_RELAY = {
601602
};
602603
export const CONTRACT_EVM_ADDRESS = '0xd8db0b1dbf8ba6721ef5256ad5fe07d72d1d04b9';
603604
export const DEFAULT_TX_HASH = '0x4a563af33c4871b51a8b108aa2fe1dd5280a30dfb7236170ae5e5e7957eb6392';
605+
const DEFAULT_TRANSACTION_VALUE = nanOrNumberTo0x(
606+
defaultDetailedContractResultByHash.amount * constants.TINYBAR_TO_WEIBAR_COEF,
607+
);
604608
export const DEFAULT_TRANSACTION = {
605609
accessList: [],
606610
blockHash: '0xd693b532a80fed6392b428604171fb32fdbf953728a3a7ecc7d4062b1652c042',
@@ -620,7 +624,7 @@ export const DEFAULT_TRANSACTION = {
620624
transactionIndex: '0x1',
621625
type: 2,
622626
v: 1,
623-
value: '0x77359400',
627+
value: DEFAULT_TRANSACTION_VALUE,
624628
};
625629
export const DEFAULT_DETAILED_CONTRACT_RESULT_BY_HASH = {
626630
address: '0xd8db0b1dbf8ba6721ef5256ad5fe07d72d1d04b9',

packages/relay/tests/lib/formatters.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
getFunctionSelector,
4444
toHexString,
4545
weibarHexToTinyBarInt,
46+
tinybarsToWeibars,
4647
} from '../../src/formatters';
4748
import constants from '../../src/lib/constants';
4849
import { BigNumber as BN } from 'bignumber.js';
@@ -737,4 +738,29 @@ describe('Formatters', () => {
737738
expect(result).to.deep.equal({ A: '1', B: '2', C: '3' });
738739
});
739740
});
741+
742+
describe('tinybarsToWeibars', () => {
743+
it('should convert tinybars to weibars', () => {
744+
expect(tinybarsToWeibars(10)).to.eql(100000000000);
745+
});
746+
747+
it('should return null if null is passed', () => {
748+
expect(tinybarsToWeibars(null)).to.eql(null);
749+
});
750+
751+
it('should return 0 for 0 input', () => {
752+
expect(tinybarsToWeibars(0)).to.eql(0);
753+
});
754+
755+
it('should throw an error when value is smaller than 0', () => {
756+
expect(() => tinybarsToWeibars(-10)).to.throw(Error, 'Invalid value - cannot pass negative number');
757+
});
758+
759+
it('should throw an error when value is larger than the total supply of tinybars', () => {
760+
expect(() => tinybarsToWeibars(constants.TOTAL_SUPPLY_TINYBARS * 10)).to.throw(
761+
Error,
762+
'Value cannot be more than the total supply of tinybars in the blockchain',
763+
);
764+
});
765+
});
740766
});

packages/server/tests/helpers/assertions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ export default class Assertions {
190190
relayResponse.transactionIndex,
191191
"Assert transaction: 'transactionIndex' should equal mirrorNode response",
192192
).to.eq(ethers.toQuantity(mirrorNodeResponse.transaction_index));
193-
expect(relayResponse.value, "Assert transaction: 'value' should equal mirrorNode response").to.eq(
194-
ethers.toQuantity(mirrorNodeResponse.amount),
195-
);
193+
expect(
194+
relayResponse.value,
195+
"Assert transaction: 'value' should equal mirrorNode response converted in weibar",
196+
).to.eq(ethers.toQuantity(BigInt(mirrorNodeResponse.amount * constants.TINYBAR_TO_WEIBAR_COEF)));
196197
}
197198

198199
static transactionReceipt = (transactionReceipt, mirrorResult, effectiveGas) => {

0 commit comments

Comments
 (0)