Deploy A Smart contract on Sonic
This guide covers adding the network to a browser wallet, then deploying a simple contract using Foundry and Hardhat — pick whichever you prefer.
Last updated
Was this helpful?
Was this helpful?
curl -L https://foundry.paradigm.xyz | bash
foundryupmkdir sonic-deploy && cd sonic-deploy
forge init// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract HelloSonic {
function hello() external pure returns (string memory) {
return "Hello from Sonic";
}
}export PRIVATE_KEY=<your-deployer-private-key>
export SONIC_RPC_URL=https://go.getblock.io/<ACCESS-TOKEN>/
forge create src/HelloSonic.sol:HelloSonic \
--rpc-url $SONIC_RPC_URL \
--private-key $PRIVATE_KEY \
--broadcastexport ETHERSCAN_API_KEY=<your-etherscan-api-key>
forge verify-contract <contract_address> \
src/HelloSonic.sol:HelloSonic \
--chain-id 146 \
--etherscan-api-key $ETHERSCAN_API_KEYmkdir sonic-deploy && cd sonic-deploy
npm init -y
npm install --save-dev hardhat
npx hardhat initrequire('@nomicfoundation/hardhat-toolbox');
const PRIVATE_KEY = process.env.PRIVATE_KEY;
module.exports = {
solidity: '0.8.26',
networks: {
sonic: {
url: 'https://go.getblock.io/<ACCESS-TOKEN>/',
chainId: 146,
accounts: [PRIVATE_KEY]
},
},
etherscan: {
apiKey: process.env.ETHERSCAN_API_KEY
}
};// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract HelloSonic {
function hello() external pure returns (string memory) {
return "Hello from Sonic";
}
}const hre = require('hardhat');
async function main() {
const contract = await hre.ethers.deployContract('HelloSonic');
await contract.waitForDeployment();
console.log('HelloSonic deployed to:', await contract.getAddress());
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});npx hardhat compile
npx hardhat run scripts/deploy.js --network sonicnpx hardhat verify --network sonic <contract_address>