Skip to content

Commit 05905ed

Browse files
fix: returning correct ledger id for localnode (#163)
Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas@arianelabs.com>
1 parent eed1e21 commit 05905ed

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

contracts/HtsSystemContractJson.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ contract HtsSystemContractJson is HtsSystemContract {
423423
if (block.chainid == 295) return "0x00"; // Mainnet
424424
if (block.chainid == 296) return "0x01"; // Testnet
425425
if (block.chainid == 297) return "0x02"; // Previewnet
426+
if (block.chainid == 298) return "0x03"; // Localhost
426427
return "0x00"; // Default to Mainnet
427428
}
428429

src/forwarder/mirror-node-client.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ class MirrorNodeClient {
4848
this.mirrorNodeUrl = mirrorNodeUrl;
4949
}
5050

51+
/**
52+
* Get ledger ID.
53+
*
54+
* @returns {string}
55+
*/
56+
getLedgerId() {
57+
const knownEnvironments = [
58+
{ name: 'mainnet', id: '0x00' },
59+
{ name: 'testnet', id: '0x01' },
60+
{ name: 'previewnet', id: '0x02' },
61+
{ name: 'localhost', id: '0x03' },
62+
{ name: '127.0.0.1', id: '0x03' },
63+
];
64+
for (let i = 0; i < knownEnvironments.length; i++) {
65+
if (this.mirrorNodeUrl.split(knownEnvironments[i].name).length === 2) {
66+
return knownEnvironments[i].id;
67+
}
68+
}
69+
return '0x00';
70+
}
71+
5172
/**
5273
* Fetches information about a token by its token ID.
5374
*

src/index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
*
2121
*/
2222
interface IMirrorNodeClient {
23+
/**
24+
* Get ledger ID.
25+
*
26+
* Returns the guessed ID of the ledger. Since there is only one environment supported at a time,
27+
* the ledger ID can be determined using the API URL as input:
28+
* - 0x00 for mainnet,
29+
* - 0x01 for testnet,
30+
* - 0x02 for previewnet.
31+
*/
32+
getLedgerId(): string;
33+
2334
/**
2435
* Get token by id.
2536
*

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ async function getHtsStorageAt(address, requestedSlot, blockNumber, mirrorNodeCl
250250
if (unresolvedValues === undefined) {
251251
const token = await mirrorNodeClient.getTokenById(tokenId, blockNumber);
252252
if (token === null) return ret(ZERO_HEX_32_BYTE, `Token \`${tokenId}\` not found`);
253-
unresolvedValues = slotMapOf(token).load(nrequestedSlot);
253+
unresolvedValues = slotMapOf(token, mirrorNodeClient.getLedgerId()).load(nrequestedSlot);
254254

255255
if (unresolvedValues === undefined)
256256
return ret(ZERO_HEX_32_BYTE, `Requested slot does not match any field slots`);

src/slotmap.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,10 @@ function visit(slot, baseSlot, obj, path, map) {
216216
* Secondly, it builds the actual `SlotMap` starting from the declared fields in the storage layout.
217217
*
218218
* @param {Record<string, unknown>} token
219+
* @param {string} ledgerId
219220
* @returns {SlotMap}
220221
*/
221-
function slotMapOf(token) {
222+
function slotMapOf(token, ledgerId) {
222223
token['token_type'] = token['type'];
223224
token['token_supply_type'] = token['supply_type'] === 'FINITE';
224225
token['default_kyc_status'] = false;
@@ -227,7 +228,7 @@ function slotMapOf(token) {
227228
// token['inherit_account_key'] = ZERO_HEX_32_BYTE;
228229
// token['delegatable_contract_id'] = zeroAddress();
229230
token['treasury'] = token['treasury_account_id'];
230-
token['ledger_id'] = '0x00';
231+
token['ledger_id'] = ledgerId;
231232
// Every inner `struct` will be flattened by `visit`,
232233
// so it uses the last part of the field path, _i.e._, `.second`.
233234
token['second'] = `${token['expiry_timestamp']}`;

0 commit comments

Comments
 (0)