89 lines
3.0 KiB
Solidity
89 lines
3.0 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
pragma solidity ^0.8.0;
|
|
|
|
contract Trading {
|
|
// Mapping of token addresses to their balances
|
|
mapping(address => uint256) public tokenBalances;
|
|
|
|
// Mapping of user addresses to their trading balances
|
|
mapping(address => uint256) public userBalances;
|
|
|
|
// Mapping of token addresses to their prices
|
|
mapping(address => uint256) public tokenPrices;
|
|
|
|
// Event emitted when a user deposits tokens
|
|
event Deposited(address indexed user, address indexed token, uint256 amount);
|
|
|
|
// Event emitted when a user withdraws tokens
|
|
event Withdrawn(address indexed user, address indexed token, uint256 amount);
|
|
|
|
// Event emitted when a user places an order
|
|
event OrderPlaced(address indexed user, address indexed token, uint256 amount, uint256 price);
|
|
|
|
// Event emitted when an order is filled
|
|
event OrderFilled(address indexed user, address indexed token, uint256 amount, uint256 price);
|
|
|
|
/**
|
|
* @dev Initializes the contract
|
|
*/
|
|
constructor() {
|
|
// Initialize token prices
|
|
tokenPrices[address(0x1)] = 100; // Token A price (example address)
|
|
tokenPrices[address(0x2)] = 200; // Token B price (example address)
|
|
}
|
|
|
|
/**
|
|
* @dev Deposits tokens for a user
|
|
* @param _token Address of the token being deposited
|
|
* @param _amount Amount of tokens to deposit
|
|
*/
|
|
function deposit(address _token, uint256 _amount) public {
|
|
require(_amount > 0, "Cannot deposit 0 tokens");
|
|
|
|
tokenBalances[_token] += _amount;
|
|
userBalances[msg.sender] += _amount;
|
|
|
|
emit Deposited(msg.sender, _token, _amount);
|
|
}
|
|
|
|
/**
|
|
* @dev Withdraws tokens for a user
|
|
* @param _token Address of the token being withdrawn
|
|
* @param _amount Amount of tokens to withdraw
|
|
*/
|
|
function withdraw(address _token, uint256 _amount) public {
|
|
require(_amount > 0, "Cannot withdraw 0 tokens");
|
|
require(userBalances[msg.sender] >= _amount, "Insufficient balance");
|
|
|
|
tokenBalances[_token] -= _amount;
|
|
userBalances[msg.sender] -= _amount;
|
|
|
|
emit Withdrawn(msg.sender, _token, _amount);
|
|
}
|
|
|
|
/**
|
|
* @dev Places an order for a user
|
|
* @param _token Address of the token being traded
|
|
* @param _amount Amount of tokens to trade
|
|
* @param _price Price of the token
|
|
*/
|
|
function placeOrder(address _token, uint256 _amount, uint256 _price) public {
|
|
require(_amount > 0, "Cannot place order for 0 tokens");
|
|
require(_price > 0, "Cannot place order with 0 price");
|
|
|
|
// Check if the order can be filled
|
|
if (tokenBalances[_token] >= _amount) {
|
|
// Fill the order
|
|
tokenBalances[_token] -= _amount;
|
|
userBalances[msg.sender] += _amount;
|
|
|
|
emit OrderFilled(msg.sender, _token, _amount, _price);
|
|
} else {
|
|
// Place the order in the order book or take appropriate action
|
|
// ...
|
|
|
|
emit OrderPlaced(msg.sender, _token, _amount, _price);
|
|
}
|
|
}
|
|
}
|