Trade cryptocurrencies directly on-chain with automated market making. Full custody. Zero intermediaries. 24/7 permissionless access on Binance Smart Chain.
A Decentralized Exchange (DEX) is a blockchain-based platform that allows users to trade cryptocurrencies directly with each other — no centralized intermediary required.
Unlike traditional exchanges, a DEX operates through smart contracts and automated protocols, giving users full control over their funds and eliminating counterparty risk.
"DEXs represent the core of DeFi — trustless, transparent, and permissionless alternatives to centralized exchanges."
DEXs are the backbone of DeFi — offering a trustless, transparent, and permissionless alternative to centralized exchanges. The DefiAX DEX empowers individuals to trade securely with full asset custody and zero counterparty risk.
Open source • Auditable • AMM DEX on BSC / ETH
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
interface IERC20 {
function transfer(address to, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
function balanceOf(address user) external view returns (uint256);
}
contract SimpleDEX {
IERC20 public tokenA; IERC20 public tokenB;
uint256 public reserveA; uint256 public reserveB;
uint256 public totalLiquidity;
mapping(address => uint256) public liquidityOf;
uint256 public constant FEE_BPS = 30; uint256 public constant BPS = 10_000;
event Swap(address indexed trader, address tokenIn, uint256 amountIn, address tokenOut, uint256 amountOut);
constructor(IERC20 _tokenA, IERC20 _tokenB) { tokenA = _tokenA; tokenB = _tokenB; }
function addLiquidity(uint256 amountA, uint256 amountB) external {
require(amountA > 0 && amountB > 0, "Zero");
tokenA.transferFrom(msg.sender, address(this), amountA);
tokenB.transferFrom(msg.sender, address(this), amountB);
uint256 liq = totalLiquidity == 0 ? _sqrt(amountA * amountB)
: _min((amountA * totalLiquidity) / reserveA, (amountB * totalLiquidity) / reserveB);
liquidityOf[msg.sender] += liq; totalLiquidity += liq;
reserveA += amountA; reserveB += amountB;
}
function swapAForB(uint256 amountIn) external {
require(amountIn > 0, "Zero");
tokenA.transferFrom(msg.sender, address(this), amountIn);
uint256 fee = (amountIn * (BPS - FEE_BPS)) / BPS;
uint256 amountOut = (reserveB * fee) / (reserveA + fee);
require(amountOut > 0, "Zero out");
tokenB.transfer(msg.sender, amountOut);
reserveA += amountIn; reserveB -= amountOut;
emit Swap(msg.sender, address(tokenA), amountIn, address(tokenB), amountOut);
}
function swapBForA(uint256 amountIn) external {
require(amountIn > 0, "Zero");
tokenB.transferFrom(msg.sender, address(this), amountIn);
uint256 fee = (amountIn * (BPS - FEE_BPS)) / BPS;
uint256 amountOut = (reserveA * fee) / (reserveB + fee);
require(amountOut > 0, "Zero out");
tokenA.transfer(msg.sender, amountOut);
reserveB += amountIn; reserveA -= amountOut;
emit Swap(msg.sender, address(tokenB), amountIn, address(tokenA), amountOut);
}
function getPriceAinB() external view returns (uint256) { return (reserveB * 1e18) / reserveA; }
function getReserves() external view returns (uint256, uint256) { return (reserveA, reserveB); }
function _min(uint256 x, uint256 y) internal pure returns (uint256) { return x < y ? x : y; }
function _sqrt(uint256 y) internal pure returns (uint256 z) {
if (y > 3) { z = y; uint256 x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } }
else if (y != 0) { z = 1; }
}
}