Since 2009, we have been utilizing our extensive expertise in blockchain technologies to help businesses, both large and small, maximize their efficiency.
Explore More
With more than 400+ experts, Oodles comprises a fantastic resource of business knowledge that spans multiple industries. Whatever the circumstances, we keep to our obligations.
Explore More
At Oodles, we help our clients work with a human understanding but at superhuman speed something that others can't. They thus advance and maintain their lead
4th December 2024
5 min read
Associate Consultant L2- Development
Automated Market Maker (AMM) model sits at the heart of crypto exchange transformation, a fundamental innovation powering many centralized crypto exchange platforms and decentralized exchanges (DEXs). It is a protocol, integrated using crypto exchange development, that allows cryptocurrency trading without using traditional order books. Instead of matching buy and sell orders, AMMs rely on liquidity pools that use mathematical formulas to determine prices. Users can trade directly with the pool, and prices are adjusted based on supply and demand within the pool.
Explore | An Exhaustive Introduction to Automated Market Makers (AMM)
Create a Hardhat Project: Initialize a new Hardhat project by running:
npx hardhat init
Inside the contracts directory, create a new file named Amm.sol. This Solidity file will hold our Amm logic.
Program :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;
contract TokenV2SwapAmm{
address private constant UNISWAPV2ROUTER =
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address private constant WETH_TOKEN = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address private constant DAI_TOKEN = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
address constant USDC_TOKEN = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
IUniswapV2Router private myrouter = IUniswapV2Router(UNISWAPV2ROUTER);
IERC20 private weth_token = IERC20(WETH_TOKEN);
IERC20 private dai_token = IERC20(DAI_TOKEN);
// Swap WETH_TOKEN to DAI_TOKEN
function swapSingleHopExactAmountIn(uint256 amount_In, uint256 amount_OutMin)
external
returns (uint256 amountOut)
{
weth_token.transferFrom(msg.sender, address(this), amount_In);
weth_token.approve(address(myrouter), amount_In);
address[] memory route;
route = new address[](2);
route[0] = WETH_TOKEN;
route[1] = DAI_TOKEN;
uint256[] memory amounts = myrouter.swapExactTokensForTokens(
amount_In, amount_OutMin, route, msg.sender, block.timestamp
);
// amounts[0] = WETH_TOKEN amount, amounts[1] = DAI_TOKEN amount
return amounts[1];
}
// Swap DAI_TOKEN -> WETH_TOKEN -> USDC_TOKEN
function swapMultiHopExactAmountIn(uint256 amount_In, uint256 amount_OutMin)
external
returns (uint256 amountOut)
{
dai_token.transferFrom(msg.sender, address(this), amount_In);
dai_token.approve(address(myrouter), amount_In);
address[] memory route;
route = new address[](3);
route[0] = DAI_TOKEN;
route[1] = WETH_TOKEN;
route[2] = USDC_TOKEN;
uint256[] memory amounts = myrouter.swapExactTokensForTokens(
amount_In, amount_OutMin, route, msg.sender, block.timestamp
);
// amounts[0] = DAI_TOKEN amount
// amounts[1] = WETH_TOKEN amount
// amounts[2] = USDC_TOKEN amount
return amounts[2];
}
// Swap WETH_TOKEN to DAI_TOKEN
function swapSingleHopExactAmountOut(
uint256 amountOutRequired,
uint256 amounttInMaxreq
) external returns (uint256 amountOut) {
weth_token.transferFrom(msg.sender, address(this), amounttInMaxreq);
weth_token.approve(address(myrouter), amounttInMaxreq);
address[] memory route;
route = new address[](2);
route[0] = WETH_TOKEN;
route[1] = DAI_TOKEN;
uint256[] memory amounts = myrouter.swapTokensForExactTokens(
amountOutRequired, amounttInMaxreq, route, msg.sender, block.timestamp
);
// Refund WETH_TOKEN to msg.sender
if (amounts[0] < amounttInMaxreq) {
weth_token.transfer(msg.sender, amounttInMaxreq - amounts[0]);
}
return amounts[1];
}
// Swap DAI_TOKEN -> WETH_TOKEN -> USDC_TOKEN
function swapMultiHopExactAmountOut(
uint256 amountOutRequired,
uint256 amounttInMaxreq
) external returns (uint256 amountOut) {
dai_token.transferFrom(msg.sender, address(this), amounttInMaxreq);
dai_token.approve(address(myrouter), amounttInMaxreq);
address[] memory route;
route = new address[](3);
route[0] = DAI_TOKEN;
route[1] = WETH_TOKEN;
route[2] = USDC_TOKEN;
uint256[] memory amounts = myrouter.swapTokensForExactTokens(
amountOutRequired, amounttInMaxreq, route, msg.sender, block.timestamp
);
// Refund DAI_TOKEN to msg.sender
if (amounts[0] < amounttInMaxreq) {
dai_token.transfer(msg.sender, amounttInMaxreq - amounts[0]);
}
return amounts[2];
}
}
interface IUniswapV2Router {
function swapExactTokensForTokens(
uint256 amount_In,
uint256 amount_OutMin,
address[] calldata route,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapTokensForExactTokens(
uint256 amountOut,
uint256 amounttInMaxreq,
address[] calldata route,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount)
external
returns (bool);
function allowance(address owner, address spender)
external
view
returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount)
external
returns (bool);
}
interface IWETH_TOKEN is IERC20 {
function deposit() external payable;
function withdraw(uint256 amount) external;
}
Inside the contracts directory, create a new file named Amm.sol. This Solidity file will contain the AMM logic for token swaps.
The following code snippet implements the AMM logic for token swaps using Uniswap's V2 router.
async function main() {
const Ammswap = await ethers.getContractFactory('TokenV2SwapAmm');
const swap = await Ammswap.deploy();
await swap.deployed();
console.log('Amm logic deployed to:', swap.address);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
- In the test directory, create a new file for the tests.
- Use Hardhat's testing framework to write tests for your contract.
npx hardhat test
Also, Explore | Identifying Smart Contract Orchestration Patterns in Solidity
Congratulations on implementing Automated Market Maker (AMM) Logic in Solidity and deploying your contract on Ethereum.
Ready to take your decentralized Amm swap system to the next level? Contact our expert blockchain developers at Oodles to transform your vision into a robust, feature-rich solution tailored to your specific needs. Let's innovate together and redefine the future of Amm swap.
Krishan Chand
Krishan, a highly skilled backend developer, excels in Node.js with a profound understanding of this technology. His expertise extends to various tools and frameworks, including MongoDB, HTML, CSS, Express, JavaScript, GIT/Github, and REST-API. Krishan's proficiency is further validated by multiple certifications, including Project Appreciation, JavaScript Programming, and HTML-5. Leveraging his extensive knowledge and development prowess, he consistently delivers outstanding results on projects within his domain.
Associate Consultant L2- Development
By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.
We would love to
hear from you!
Innovate with confidence!