@kadena/hardhat-chainweb
Version:
Hardhat plugin for Kadena's Chainweb network
178 lines • 8.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getKadenaExternalNetworks = exports.getKadenaNetworks = void 0;
const utils_1 = require("../server/utils");
// This function takes a default chains config and user chains config
// Copied from hardhat source code hardhat/src/internal/core/config/config-resolution.ts
const getChains = (defaultChains, userChains) => {
const chains = new Map(defaultChains);
if (userChains !== undefined) {
for (const [chainId, userChainConfig] of Object.entries(userChains)) {
const chainConfig = {
hardforkHistory: new Map(),
};
if (userChainConfig.hardforkHistory !== undefined) {
for (const [name, block] of Object.entries(userChainConfig.hardforkHistory)) {
chainConfig.hardforkHistory.set(name, block);
}
}
chains.set(parseInt(chainId, 10), chainConfig);
}
}
return chains;
};
function normalizeHexString(str) {
const normalized = str.trim().toLowerCase();
if (normalized.startsWith('0x')) {
return normalized;
}
return `0x${normalized}`;
}
// This function takes a default accounts config and user accounts config
// and returns a normalized accounts config
// Copied from hardhat source code hardhat/src/internal/core/config/config-resolution.ts
const getAccounts = (userAccounts, defaultAccounts) => {
const accounts = userAccounts === undefined
? defaultAccounts
: Array.isArray(userAccounts)
? userAccounts.map(({ privateKey, balance }) => ({
privateKey: normalizeHexString(privateKey),
balance,
}))
: {
...defaultAccounts,
...userAccounts,
};
return accounts;
};
const mapToEtherscan = ({ networkStem, chainId, chainwebChainId, apiURLTemplate, browserURLTemplate, }) => {
return {
chainId,
network: `${networkStem}${chainwebChainId}`,
urls: {
apiURL: apiURLTemplate.replace('{cid}', String(chainwebChainId)),
browserURL: browserURLTemplate.replace('{cid}', String(chainwebChainId)),
},
};
};
const getKadenaNetworks = ({ availableNetworks = {}, hardhatNetwork, networkStem = 'kadena_devnet_', chainIdOffset = 626000, numberOfChains = 2, accounts, loggingEnabled = false, forking, networkOptions, chainwebChainIdOffset = 0, etherscan, }) => {
const chainIds = new Array(numberOfChains)
.fill(0)
.map((_, i) => i + chainIdOffset);
const etherscanCustomChains = [];
const etherscanApiKeys = {};
const networks = chainIds.reduce((acc, chainId, chainwebChainIndex) => {
var _a, _b, _c;
const chainwebChainId = chainwebChainIndex + chainwebChainIdOffset;
const userNetworkConfig = availableNetworks[`${networkStem}${chainwebChainId}`];
const networkForking = forking
? {
enabled: true,
...forking,
}
: undefined;
const networkConfig = {
...hardhatNetwork,
...networkOptions,
chainId: chainId,
chainwebChainId,
type: 'chainweb:in-process',
loggingEnabled,
...userNetworkConfig,
forking: networkForking,
mining: {
...hardhatNetwork.mining,
...userNetworkConfig === null || userNetworkConfig === void 0 ? void 0 : userNetworkConfig.mining,
mempool: {
...hardhatNetwork.mining.mempool,
...(_a = userNetworkConfig === null || userNetworkConfig === void 0 ? void 0 : userNetworkConfig.mining) === null || _a === void 0 ? void 0 : _a.mempool,
},
},
minGasPrice: BigInt((_b = userNetworkConfig === null || userNetworkConfig === void 0 ? void 0 : userNetworkConfig.minGasPrice) !== null && _b !== void 0 ? _b : hardhatNetwork.minGasPrice),
accounts: getAccounts(accounts !== null && accounts !== void 0 ? accounts : userNetworkConfig === null || userNetworkConfig === void 0 ? void 0 : userNetworkConfig['accounts'], hardhatNetwork.accounts),
chains: getChains(hardhatNetwork.chains, (_c = networkOptions === null || networkOptions === void 0 ? void 0 : networkOptions.chains) !== null && _c !== void 0 ? _c : userNetworkConfig === null || userNetworkConfig === void 0 ? void 0 : userNetworkConfig.chains),
};
acc[`${networkStem}${chainwebChainId}`] = networkConfig;
if (etherscan) {
etherscanCustomChains.push(mapToEtherscan({
networkStem,
chainId,
chainwebChainId,
apiURLTemplate: etherscan.apiURLTemplate,
browserURLTemplate: etherscan.browserURLTemplate,
}));
etherscanApiKeys[`${networkStem}${chainwebChainId}`] =
etherscan === null || etherscan === void 0 ? void 0 : etherscan.apiKey;
}
return acc;
}, {});
return [networks, etherscanCustomChains, etherscanApiKeys];
};
exports.getKadenaNetworks = getKadenaNetworks;
const toHttpNetworkAccountsConfig = (accounts) => {
if (accounts === 'remote') {
return accounts;
}
if (Array.isArray(accounts)) {
return accounts;
}
return {
initialIndex: 0,
count: 20,
passphrase: '',
path: "m/44'/60'/0'/0",
...accounts,
};
};
const getKadenaExternalNetworks = ({ availableNetworks = {}, networkStem = 'kadena_devnet_', chainIdOffset = 626000, numberOfChains = 2, accounts = 'remote', baseUrl = 'http://localhost:8545', networkOptions = {}, chainwebChainIdOffset, etherscan, }) => {
const etherscanCustomChains = [];
const etherscanApiKeys = {};
const chainIds = new Array(numberOfChains)
.fill(0)
.map((_, i) => i + chainIdOffset);
const basePath = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
const networks = chainIds.reduce((acc, chainId, chainwebChainIndex) => {
var _a, _b, _c, _d, _e;
const chainwebChainId = chainwebChainIndex + chainwebChainIdOffset;
const userConfig = availableNetworks[`${networkStem}${chainwebChainId}`];
const networkConfig = {
...networkOptions,
chainId: chainId,
chainwebChainId,
type: 'chainweb:external',
gasPrice: 'auto',
gas: 'auto',
gasMultiplier: 1,
timeout: 20000,
httpHeaders: {},
url: `${basePath}${(0, utils_1.mapChainIdToRoute)(chainwebChainId)}`,
...userConfig,
accounts: (userConfig === null || userConfig === void 0 ? void 0 : userConfig.accounts)
? toHttpNetworkAccountsConfig(userConfig.accounts)
: accounts,
ignition: {
maxFeePerGasLimit: (_a = userConfig === null || userConfig === void 0 ? void 0 : userConfig.ignition) === null || _a === void 0 ? void 0 : _a.maxFeePerGasLimit,
maxPriorityFeePerGas: (_b = userConfig === null || userConfig === void 0 ? void 0 : userConfig.ignition) === null || _b === void 0 ? void 0 : _b.maxPriorityFeePerGas,
gasPrice: (_c = userConfig === null || userConfig === void 0 ? void 0 : userConfig.ignition) === null || _c === void 0 ? void 0 : _c.gasPrice,
disableFeeBumping: (_d = userConfig === null || userConfig === void 0 ? void 0 : userConfig.ignition) === null || _d === void 0 ? void 0 : _d.disableFeeBumping,
explorerUrl: (_e = userConfig === null || userConfig === void 0 ? void 0 : userConfig.ignition) === null || _e === void 0 ? void 0 : _e.explorerUrl,
},
};
acc[`${networkStem}${chainwebChainId}`] = networkConfig;
if (etherscan) {
etherscanCustomChains.push(mapToEtherscan({
networkStem,
chainId,
chainwebChainId,
apiURLTemplate: etherscan.apiURLTemplate,
browserURLTemplate: etherscan.browserURLTemplate,
}));
etherscanApiKeys[`${networkStem}${chainwebChainId}`] =
etherscan === null || etherscan === void 0 ? void 0 : etherscan.apiKey;
}
return acc;
}, {});
return [networks, etherscanCustomChains, etherscanApiKeys];
};
exports.getKadenaExternalNetworks = getKadenaExternalNetworks;
//# sourceMappingURL=configure.js.map