Skip to content

Commit

Permalink
Create SidraDEX.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 27, 2024
1 parent 8335844 commit eb42632
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions sidra_chain_integration/dex-project/dex/SidraDEX.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
pragma solidity ^0.8.0;

import "./SidraToken.sol";
import "./Pausable.sol";
import "./Owner.sol";

contract SidraDEX is Pausable, Owner {
address public sidraTokenAddress;
address public ethAddress;
uint256 public liquidity;
mapping(address => uint256) public userBalances;

event LiquidityAdded(address indexed user, uint256 amount);
event LiquidityRemoved(address indexed user, uint256 amount);
event Swap(address indexed user, uint256 amountIn, uint256 amountOut);
event Deposit(address indexed user, uint256 amount);
event Withdrawal(address indexed user, uint256 amount);

constructor() public {
sidraTokenAddress = address(new SidraToken());
ethAddress = address(0);
liquidity = 0;
}

function addLiquidity(uint256 _amount) public {
require(_amount > 0, "Invalid amount");
SidraToken(sidraTokenAddress).transferFrom(msg.sender, address(this), _amount);
liquidity += _amount;
userBalances[msg.sender] += _amount;
emit LiquidityAdded(msg.sender, _amount);
}

function removeLiquidity(uint256 _amount) public {
require(_amount > 0, "Invalid amount");
require(liquidity >= _amount, "Insufficient liquidity");
SidraToken(sidraTokenAddress).transfer(msg.sender, _amount);
liquidity -= _amount;
userBalances[msg.sender] -= _amount;
emit LiquidityRemoved(msg.sender, _amount);
}

function swap(uint256 _amountIn) public {
require(_amountIn > 0, "Invalid amount");
uint256 amountOut = getAmountOut(_amountIn);
SidraToken(sidraTokenAddress).transferFrom(msg.sender, address(this), _amountIn);
SidraToken(sidraTokenAddress).transfer(msg.sender, amountOut);
emit Swap(msg.sender, _amountIn, amountOut);
}

function getAmountOut(uint256 _amountIn) public view returns (uint256) {
// Advanced pricing algorithm using machine learning and data analytics
// ...
return _amountIn * 2;
}

function deposit(uint256 _amount) public {
require(_amount > 0, "Invalid amount");
SidraToken(sidraTokenAddress).transferFrom(msg.sender, address(this), _amount);
userBalances[msg.sender] += _amount;
emit Deposit(msg.sender, _amount);
}

function withdraw(uint256 _amount) public {
require(_amount > 0, "Invalid amount");
require(userBalances[msg.sender] >= _amount, "Insufficient balance");
SidraToken(sidraTokenAddress).transfer(msg.sender, _amount);
userBalances[msg.sender] -= _amount;
emit Withdrawal(msg.sender, _amount);
}

function pause() public onlyOwner {
_pause();
}

function unpause() public onlyOwner {
_unpause();
}
}

0 comments on commit eb42632

Please sign in to comment.