Skip to content

Commit

Permalink
fix: returning correct ledger id for localnode (#163)
Browse files Browse the repository at this point in the history
Signed-off-by: Mariusz Jasuwienas <[email protected]>
  • Loading branch information
arianejasuwienas committed Jan 28, 2025
1 parent eed1e21 commit 05905ed
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions contracts/HtsSystemContractJson.sol
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ contract HtsSystemContractJson is HtsSystemContract {
if (block.chainid == 295) return "0x00"; // Mainnet
if (block.chainid == 296) return "0x01"; // Testnet
if (block.chainid == 297) return "0x02"; // Previewnet
if (block.chainid == 298) return "0x03"; // Localhost
return "0x00"; // Default to Mainnet
}

Expand Down
21 changes: 21 additions & 0 deletions src/forwarder/mirror-node-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ class MirrorNodeClient {
this.mirrorNodeUrl = mirrorNodeUrl;
}

/**
* Get ledger ID.
*
* @returns {string}
*/
getLedgerId() {
const knownEnvironments = [
{ name: 'mainnet', id: '0x00' },
{ name: 'testnet', id: '0x01' },
{ name: 'previewnet', id: '0x02' },
{ name: 'localhost', id: '0x03' },
{ name: '127.0.0.1', id: '0x03' },
];
for (let i = 0; i < knownEnvironments.length; i++) {
if (this.mirrorNodeUrl.split(knownEnvironments[i].name).length === 2) {
return knownEnvironments[i].id;
}
}
return '0x00';
}

/**
* Fetches information about a token by its token ID.
*
Expand Down
11 changes: 11 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
*
*/
interface IMirrorNodeClient {
/**
* Get ledger ID.
*
* Returns the guessed ID of the ledger. Since there is only one environment supported at a time,
* the ledger ID can be determined using the API URL as input:
* - 0x00 for mainnet,
* - 0x01 for testnet,
* - 0x02 for previewnet.
*/
getLedgerId(): string;

/**
* Get token by id.
*
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ async function getHtsStorageAt(address, requestedSlot, blockNumber, mirrorNodeCl
if (unresolvedValues === undefined) {
const token = await mirrorNodeClient.getTokenById(tokenId, blockNumber);
if (token === null) return ret(ZERO_HEX_32_BYTE, `Token \`${tokenId}\` not found`);
unresolvedValues = slotMapOf(token).load(nrequestedSlot);
unresolvedValues = slotMapOf(token, mirrorNodeClient.getLedgerId()).load(nrequestedSlot);

if (unresolvedValues === undefined)
return ret(ZERO_HEX_32_BYTE, `Requested slot does not match any field slots`);
Expand Down
5 changes: 3 additions & 2 deletions src/slotmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,10 @@ function visit(slot, baseSlot, obj, path, map) {
* Secondly, it builds the actual `SlotMap` starting from the declared fields in the storage layout.
*
* @param {Record<string, unknown>} token
* @param {string} ledgerId
* @returns {SlotMap}
*/
function slotMapOf(token) {
function slotMapOf(token, ledgerId) {
token['token_type'] = token['type'];
token['token_supply_type'] = token['supply_type'] === 'FINITE';
token['default_kyc_status'] = false;
Expand All @@ -227,7 +228,7 @@ function slotMapOf(token) {
// token['inherit_account_key'] = ZERO_HEX_32_BYTE;
// token['delegatable_contract_id'] = zeroAddress();
token['treasury'] = token['treasury_account_id'];
token['ledger_id'] = '0x00';
token['ledger_id'] = ledgerId;
// Every inner `struct` will be flattened by `visit`,
// so it uses the last part of the field path, _i.e._, `.second`.
token['second'] = `${token['expiry_timestamp']}`;
Expand Down

0 comments on commit 05905ed

Please sign in to comment.