@nomicfoundation/hardhat-viem
Version:
Hardhat plugin for viem
136 lines • 6.77 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.innerGetTestClient = exports.getTestClient = exports.getWalletClient = exports.innerGetWalletClients = exports.getWalletClients = exports.innerGetPublicClient = exports.getPublicClient = void 0;
async function getParameters(chain, config) {
const { isDevelopmentNetwork } = await Promise.resolve().then(() => __importStar(require("./chains")));
const defaultParameters = isDevelopmentNetwork(chain.id)
? { pollingInterval: 50, cacheTime: 0 }
: {};
const transportParameters = isDevelopmentNetwork(chain.id)
? { retryCount: 0 }
: {};
return {
clientParameters: { ...defaultParameters, ...config },
transportParameters,
};
}
/**
* Get a PublicClient instance. This is a read-only client that can be used to
* query the blockchain.
*
* @param provider The Ethereum provider used to connect to the blockchain.
* @param publicClientConfig Optional configuration for the PublicClient instance. See the viem documentation for more information.
* @returns A PublicClient instance.
*/
async function getPublicClient(provider, publicClientConfig) {
const { getChain } = await Promise.resolve().then(() => __importStar(require("./chains")));
const chain = publicClientConfig?.chain ?? (await getChain(provider));
return innerGetPublicClient(provider, chain, publicClientConfig);
}
exports.getPublicClient = getPublicClient;
async function innerGetPublicClient(provider, chain, publicClientConfig) {
const viem = await Promise.resolve().then(() => __importStar(require("viem")));
const { clientParameters, transportParameters } = await getParameters(chain, publicClientConfig);
const publicClient = viem.createPublicClient({
chain,
transport: viem.custom(provider, transportParameters),
...clientParameters,
});
return publicClient;
}
exports.innerGetPublicClient = innerGetPublicClient;
/**
* Get a list of WalletClient instances. These are read-write clients that can
* be used to send transactions to the blockchain. Each client is associated
* with an account obtained from the provider using `eth_accounts`.
*
* @param provider The Ethereum provider used to connect to the blockchain.
* @param walletClientConfig Optional configuration for the WalletClient instances. See the viem documentation for more information.
* @returns A list of WalletClient instances.
*/
async function getWalletClients(provider, walletClientConfig) {
const { getAccounts } = await Promise.resolve().then(() => __importStar(require("./accounts")));
const { getChain } = await Promise.resolve().then(() => __importStar(require("./chains")));
const chain = walletClientConfig?.chain ?? (await getChain(provider));
const accounts = await getAccounts(provider);
return innerGetWalletClients(provider, chain, accounts, walletClientConfig);
}
exports.getWalletClients = getWalletClients;
async function innerGetWalletClients(provider, chain, accounts, walletClientConfig) {
const viem = await Promise.resolve().then(() => __importStar(require("viem")));
const { clientParameters, transportParameters } = await getParameters(chain, walletClientConfig);
const walletClients = accounts.map((account) => viem.createWalletClient({
chain,
account,
transport: viem.custom(provider, transportParameters),
...clientParameters,
}));
return walletClients;
}
exports.innerGetWalletClients = innerGetWalletClients;
/**
* Get a WalletClient instance for a specific address. This is a read-write
* client that can be used to send transactions to the blockchain.
*
* @param provider The Ethereum provider used to connect to the blockchain.
* @param address The public address of the account to use.
* @param walletClientConfig Optional configuration for the WalletClient instance. See the viem documentation for more information.
* @returns A WalletClient instance.
*/
async function getWalletClient(provider, address, walletClientConfig) {
const { getChain } = await Promise.resolve().then(() => __importStar(require("./chains")));
const chain = walletClientConfig?.chain ?? (await getChain(provider));
return (await innerGetWalletClients(provider, chain, [address], walletClientConfig))[0];
}
exports.getWalletClient = getWalletClient;
/**
* Get a TestClient instance. This is a read-write client that can be used to
* perform actions only available on test nodes such as hardhat or anvil.
*
* @param provider The Ethereum provider used to connect to the blockchain.
* @param testClientConfig Optional configuration for the TestClient instance. See the viem documentation for more information.
* @returns A TestClient instance.
*/
async function getTestClient(provider, testClientConfig) {
const { getChain, getMode } = await Promise.resolve().then(() => __importStar(require("./chains")));
const chain = testClientConfig?.chain ?? (await getChain(provider));
const mode = await getMode(provider);
return innerGetTestClient(provider, chain, mode, testClientConfig);
}
exports.getTestClient = getTestClient;
async function innerGetTestClient(provider, chain, mode, testClientConfig) {
const viem = await Promise.resolve().then(() => __importStar(require("viem")));
const { clientParameters, transportParameters } = await getParameters(chain, testClientConfig);
const testClient = viem.createTestClient({
mode,
chain,
transport: viem.custom(provider, transportParameters),
...clientParameters,
});
return testClient;
}
exports.innerGetTestClient = innerGetTestClient;
//# sourceMappingURL=clients.js.map