Set Up Your Local Environment
Last modified:
This guide sets up a Hardhat project with the CenturionDEX v3 contracts installed, ready for building the examples in the Guides section or your own integrations.
Quick Start
Clone the boilerplate repo, which has the required imports pre-configured:
git clone https://github.com/CenturionDEX/centurion-dex-smcs/tree/main/contracts
cd first-contract-example
npm installThen skip to Local Node with a Mainnet Fork.
Start from Scratch
Install dependencies
Install Node.js and NPM if you haven't already, then initialize a project:
$ npm initAdd Hardhat:
$ npm add --save-dev hardhatInitialize Hardhat and choose a templated project (JavaScript or TypeScript):
$ npx hardhat initThe scaffolded structure uses ./contracts for Solidity files, ./test for tests, and ./scripts for deployment scripts.
Install the CenturionDEX v3 contract packages:
$ npm add @centurion-dex/v3-periphery @centurion-dex/v3-coreSet the Solidity compiler version to 0.7.6 in hardhat.config.js to match the v3 contracts:
module.exports = {
solidity: "0.7.6",
};Compile a Basic Contract
Verify the setup by compiling a minimal swap contract. Create ./contracts/Swap.sol:
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity =0.7.6;
pragma abicoder v2;
import '@centurion-dex/v3-periphery/contracts/interfaces/ISwapRouter.sol';
import '@centurion-dex/v3-periphery/contracts/libraries/TransferHelper.sol';
contract SimpleSwap {
ISwapRouter public immutable swapRouter;
address public constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
address public constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
uint24 public constant feeTier = 3000;
constructor(ISwapRouter _swapRouter) {
swapRouter = _swapRouter;
}
function swapWETHForDAI(uint256 amountIn) external returns (uint256 amountOut) {
TransferHelper.safeTransferFrom(WETH9, msg.sender, address(this), amountIn);
TransferHelper.safeApprove(WETH9, address(swapRouter), amountIn);
// Note: To use this example, you should explicitly set slippage limits, omitting for simplicity
uint256 minOut = /* Calculate min output */ 0;
uint160 priceLimit = /* Calculate price limit */ 0;
ISwapRouter.ExactInputSingleParams memory params =
ISwapRouter.ExactInputSingleParams({
tokenIn: WETH9,
tokenOut: DAI,
fee: feeTier,
recipient: msg.sender,
deadline: block.timestamp,
amountIn: amountIn,
amountOutMinimum: minOut,
sqrtPriceLimitX96: priceLimit
});
amountOut = swapRouter.exactInputSingle(params);
}
}Compile:
$ npx hardhat compileYou should see Compiled { x } Solidity files successfully.
Local Node with a Mainnet Fork
Testing against live liquidity is critical for integration correctness, but trading on mainnet is expensive. A local fork gives you real pool state at zero cost.
See the SDK getting started guide for a full example of running a fork.
Run tests against your local fork:
$ npx hardhat test --network localhostNext Steps
With your environment ready, jump to the guides to start building CenturionDEX v3 integrations. Place contracts in ./contracts and tests in ./test, then run them against your local fork.