forked from roberts/contract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrection of reflectio
210 lines (179 loc) · 5.79 KB
/
correction of reflectio
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
pragma solidity ^0.8.0;
interface IUniswapV2Router {
function addLiquidityETH(
address token,
uint256 amountTokenDesired,
uint256 amountTokenMin,
uint256 amountETHMin,
address to,
uint256 deadline
)
external
payable
returns (
uint256 amountToken,
uint256 amountETH,
uint256 liquidity
);
}
contract MyToken {
string public name = "MyToken";
string public symbol = "MTK";
uint256 public totalSupply = 1000000000000000000000000; // 1,000,000 tokens
uint8 public decimals = 18;
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
address private _owner;
address private _uniswapV2RouterAddress;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
modifier onlyOwner() {
require(msg.sender == _owner, "Only the owner can call this function");
_;
}
constructor(address uniswapV2RouterAddress) {
_owner = msg.sender;
_uniswapV2RouterAddress = uniswapV2RouterAddress;
_balances[msg.sender] = totalSupply;
emit Transfer(address(0), msg.sender, totalSupply);
}
function balanceOf(address account) public view returns (uint256) {
return _balances[account];
}
function transfer(address recipient, uint256 amount)
public
returns (bool)
{
_transfer(msg.sender, recipient, amount);
return true;
}
function approve(address spender, uint256 amount) public returns (bool) {
_approve(msg.sender, spender, amount);
return true;
}
function transferFrom(
address sender,
address recipient,
uint256 amount
) public returns (bool) {
_transfer(sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][msg.sender];
require(
amount <= currentAllowance,
"Transfer amount exceeds allowance"
);
_approve(sender, msg.sender, currentAllowance - amount);
return true;
}
function increaseAllowance(address spender, uint256 addedValue)
public
returns (bool)
{
_approve(
msg.sender,
spender,
_allowances[msg.sender][spender] + addedValue
);
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue)
public
returns (bool)
{
uint256 currentAllowance = _allowances[msg.sender][spender];
require(
subtractedValue <= currentAllowance,
"Decreased allowance below zero"
);
_approve(msg.sender, spender, currentAllowance - subtractedValue);
return true;
}
function _transfer(
address sender,
address recipient,
uint256 amount
) private {
require(sender != address(0), "Transfer from the zero address");
require(recipient != address(0), "Transfer to the zero address");
require(amount > 0, "Transfer amount must be greater than zero");
require(
_balances[sender] >= amount,
"Insufficient balance for transfer"
);
_balances[sender] -= amount;
_balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
}
function _approve(
address owner,
address spender,
uint256 amount
) private {
require(owner != address(0), "Approve from the zero address");
require(spender != address(0), "Approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
IUniswapV2Router uniswapV2Router = IUniswapV2Router(
_uniswapV2RouterAddress
);
_approve(address(this), address(uniswapV2Router), tokenAmount);
uniswapV2Router.addLiquidityETH{value: ethAmount}(
address(this),
tokenAmount,
0,
0,
address(this),
block.timestamp
);
}
function swapTokensForEth(uint256 tokenAmount) private {
// Swap tokens for ETH using Uniswap V2 Router
}
function swapAndLiquify(uint256 tokenAmount) private {
// Swap tokens for ETH and add liquidity
swapTokensForEth(tokenAmount);
addLiquidity(tokenAmount, address(this).balance);
}
function swapTokensForEth(uint256 tokenAmount) private {
IUniswapV2Router uniswapV2Router = IUniswapV2Router(
_uniswapV2RouterAddress
);
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = uniswapV2Router.WETH();
_approve(address(this), address(uniswapV2Router), tokenAmount);
uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmount,
0,
path,
address(this),
block.timestamp
);
}
function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
IUniswapV2Router uniswapV2Router = IUniswapV2Router(
_uniswapV2RouterAddress
);
_approve(address(this), address(uniswapV2Router), tokenAmount);
uniswapV2Router.addLiquidityETH{value: ethAmount}(
address(this),
tokenAmount,
0,
0,
address(this),
block.timestamp
);
}
function setUniswapV2Router(address uniswapV2RouterAddress)
external
onlyOwner
{
_uniswapV2RouterAddress = uniswapV2RouterAddress;
}
}