-
Notifications
You must be signed in to change notification settings - Fork 0
/
DexBot.sol
66 lines (54 loc) · 2.67 KB
/
DexBot.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract SimpleTradingBot is Ownable {
IERC20 public token; // The token you want to trade
uint256 public buyPrice; // Price at which to buy
uint256 public sellPrice; // Price at which to sell
uint256 public balance;
//Set token address with erc20 token only
constructor(address _tokenAddress) {
token = IERC20(_tokenAddress);
}
// Set the buy and sell prices
function setPrices(uint256 _buyPrice, uint256 _sellPrice) external onlyOwner {
buyPrice = _buyPrice;
sellPrice = _sellPrice;
}
// Buy tokens when the price goes above the buyPrice
function buy() external {
require(buyPrice > 0, "Buy price is not set");
uint256 tokenBalance = token.balanceOf(address(this));
require(tokenBalance > 0, "Insufficient contract balance");
uint256 currentPrice = getCurrentTokenPrice(); // You need to implement this function
require(currentPrice > buyPrice, "Price is not above buy price");
// Calculate how many tokens to buy
uint256 tokensToBuy = balance / currentPrice;
require(tokensToBuy > 0, "No tokens to buy");
// Perform the buy
require(token.transfer(msg.sender, tokensToBuy), "Token transfer failed");
balance -= tokensToBuy;
}
// Sell tokens when the price goes below the sellPrice
function sell() external {
require(sellPrice > 0, "Sell price is not set");
uint256 tokenBalance = token.balanceOf(address(this));
require(tokenBalance > 0, "Insufficient contract balance");
uint256 currentPrice = getCurrentTokenPrice(); // You need to implement this function
require(currentPrice < sellPrice, "Price is not below sell price");
// Calculate how many tokens to sell
uint256 tokensToSell = tokenBalance;
require(tokensToSell > 0, "No tokens to sell");
// Perform the sell
require(token.transferFrom(msg.sender, address(this), tokensToSell), "Token transfer failed");
balance += tokensToSell;
}
// Corrected to 'pure' as it doesn't modify state or read from storage
function getCurrentTokenPrice() internal pure returns (uint256) {
// Implement this function to return the current token price
// You can use Chainlink oracles, Uniswap, or other sources to fetch the price
// For simplicity, we'll assume it returns a fixed value of 100 wei for demonstration purposes.
return 100 wei;
}
}