Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: returning correct ledger id in tokeninfo (#163) #222

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions src/forwarder/json-rpc-forwarder.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const { getHtsCode, getHtsStorageAt, HTSAddress, LONG_ZERO_PREFIX, getHIP719Code
/** @type {Partial<import('hardhat/types').HardhatNetworkForkingConfig>} */
const { url: forkUrl, mirrorNodeUrl, workerPort, localAddresses = [] } = workerData;

require('../slotmap').setLedgerId(workerData.chainId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import setLedgerId in the require('..') that is above.

Suggested change
require('../slotmap').setLedgerId(workerData.chainId);
setLedgerId(workerData.chainId);

We should treat exported modules in declated package.json as independent. All external used functions/classes/consts should be imported from an index.js declared in package.json. This way is easier to consumers to also use the package.


assert(mirrorNodeUrl !== undefined, 'json-rpc-forwarder: Missing Mirror Node URL');

debug(
Expand Down
17 changes: 15 additions & 2 deletions src/slotmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ const {
storageLayout: { storage, types },
} = require('../out/HtsSystemContract.sol/HtsSystemContract.json');

let ledgerId = '0x00';
acuarica marked this conversation as resolved.
Show resolved Hide resolved
/**
acuarica marked this conversation as resolved.
Show resolved Hide resolved
* Sets the `ledgerId` used when retrieving fungible and non-fungible tokens.
* The `ledgerId` depends on the `chainId` of the remote network it is forking from.
*
* @param {number} chainId
*/
const setLedgerId = chainId => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's export this one from index.d.ts. Also we can move the doc comments there, so consumers can take advantage of the js docs.

nit: convert to function.

const chainIdToLedgerIdMap = { 295: '0x00', 296: '0x01', 297: '0x02', 298: '0x03' };
ledgerId =
chainIdToLedgerIdMap[/**@type{keyof typeof chainIdToLedgerIdMap}*/ (chainId)] || '0x00';
};

/**
* Represents the value in the `SlotMap`.
* This can be either a `string`, or a function that returns a `string`.
Expand Down Expand Up @@ -227,7 +240,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 Expand Up @@ -339,4 +352,4 @@ class PersistentStorageMap {
}
}

module.exports = { packValues, slotMapOf, PersistentStorageMap };
module.exports = { packValues, slotMapOf, PersistentStorageMap, setLedgerId };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also export setLedgerId through index.js.