Getting Started with Hardhat
ISwitchboard.sol is a solidity interface representing the Switchboard contract. You may initialize this interface in your program if you pass a valid Switchboard address. You may find a list of supported networks in the Networks Page - or use some of the utility functions below to load the addresses dynamically based on a chainId.
Install Contractsâ
Add @switchboard-xyz/evm.js to your Hardhat project:
- npm
- Yarn
- pnpm
npm i -D @switchboard-xyz/evm.js
yarn add --dev @switchboard-xyz/evm.js
pnpm add -D @switchboard-xyz/evm.js
Import the Contractâ
Import the ISwitchboard interface from @switchboard-xyz/evm.js/contracts/
.
You will need to pass the Switchboard address into your constructor or initialization function. This ensures you always call the correct Switchboard contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import {ISwitchboard} from "@switchboard-xyz/evm.js/contracts/ISwitchboard.sol";
contract MyContract {
ISwitchboard switchboard;
constructor(address switchboardAddress) {
switchboard = ISwitchboard(switchboardAddress);
}
}
Deploying with Hardhatâ
If you're using a hardhat deployment script, you can use the following snippet to get the address of the Switchboard contract from a provided chainId:
import { ethers } from "hardhat";
import { getEvmConfig, IEvmNetworkConfig } from "@switchboard-xyz/evm.js";
async function main() {
const { chainId } = await ethers.provider.getNetwork();
const sbConfig: IEvmNetworkConfig = getEvmConfig(chainId);
const sbAddress: string = sbConfig.address;
// ... hardhat sample deploy script
const myContract = await ethers.deployContract("MyContract", [sbAddress]);
await myContract.waitForDeployment();
console.log(
`MyContract deployed to ${myContract.target} with Switchboard address ${sbAddress}`
);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});