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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
| pragma solidity ^0.8.0;
interface ICert { function totalSupply() external view returns (uint256); function balanceOf(address account) external view returns (uint256); function transfer(address to, uint256 amount) external returns (bool); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 amount) external returns (bool); function transferFrom(address from, address to, uint256 amount) external returns (bool); }//IERC20
interface IVault { function join(uint256 amount) external; function exit() external; function cert() external view returns(ICert); function transferToAccount(address account, uint256 amount) external returns(bool); function setPriveder(address flashLoanPriveder_) external; }
interface IFlashBorrower { function onFlashLoan( address initiator, address token, uint256 amount, bytes calldata data ) external returns (bool); }
interface IFlashLoanMain { function airdrop()external; function Complete()external returns(bool); }
interface IFlashLoanPriveder { function flashLoan( IFlashBorrower receiver, address token, uint256 amount, bytes memory signature, bytes calldata data ) external returns (bool); }
library StringsUpgradeable { bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
function toString(uint256 value) internal pure returns (string memory) {
if (value == 0) { return "0"; } uint256 temp = value; uint256 digits; while (temp != 0) { digits++; temp /= 10; } bytes memory buffer = new bytes(digits); while (value != 0) { digits -= 1; buffer[digits] = bytes1(uint8(48 + uint256(value % 10))); value /= 10; } return string(buffer); }
function toHexString(uint256 value) internal pure returns (string memory) { if (value == 0) { return "0x00"; } uint256 temp = value; uint256 length = 0; while (temp != 0) { length++; temp >>= 8; } return toHexString(value, length); }
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) { bytes memory buffer = new bytes(2 * length + 2); buffer[0] = "0"; buffer[1] = "x"; for (uint256 i = 2 * length + 1; i > 1; --i) { buffer[i] = _HEX_SYMBOLS[value & 0xf]; value >>= 4; } require(value == 0, "Strings: hex length insufficient"); return string(buffer); } }
library ECDSAUpgradeable { enum RecoverError { NoError, InvalidSignature, InvalidSignatureLength, InvalidSignatureS, InvalidSignatureV }
function _throwError(RecoverError error) private pure { if (error == RecoverError.NoError) { return; // no error: do nothing } else if (error == RecoverError.InvalidSignature) { revert("ECDSA: invalid signature"); } else if (error == RecoverError.InvalidSignatureLength) { revert("ECDSA: invalid signature length"); } else if (error == RecoverError.InvalidSignatureS) { revert("ECDSA: invalid signature 's' value"); } else if (error == RecoverError.InvalidSignatureV) { revert("ECDSA: invalid signature 'v' value"); } } function tryRecover( bytes32 hash, bytes32 r, bytes32 vs ) internal pure returns (address, RecoverError) { bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); uint8 v = uint8((uint256(vs) >> 255) + 27); return tryRecover(hash, v, r, s); }
function recover( bytes32 hash, bytes32 r, bytes32 vs ) internal pure returns (address) { (address recovered, RecoverError error) = tryRecover(hash, r, vs); _throwError(error); return recovered; }
function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) { if (signature.length == 65) { bytes32 r; bytes32 s; uint8 v; assembly { r := mload(add(signature, 0x20)) s := mload(add(signature, 0x40)) v := byte(0, mload(add(signature, 0x60))) } return tryRecover(hash, v, r, s); } else if (signature.length == 64) { bytes32 r; bytes32 vs; assembly { r := mload(add(signature, 0x20)) vs := mload(add(signature, 0x40)) } return tryRecover(hash, r, vs); } else { return (address(0), RecoverError.InvalidSignatureLength); } }
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) { (address recovered, RecoverError error) = tryRecover(hash, signature); _throwError(error); return recovered; }
function tryRecover( bytes32 hash, uint8 v, bytes32 r, bytes32 s ) internal pure returns (address, RecoverError) { if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) { return (address(0), RecoverError.InvalidSignatureS); } if (v != 27 && v != 28) { return (address(0), RecoverError.InvalidSignatureV); }
address signer = ecrecover(hash, v, r, s); if (signer == address(0)) { return (address(0), RecoverError.InvalidSignature); }
return (signer, RecoverError.NoError); }
function recover( bytes32 hash, uint8 v, bytes32 r, bytes32 s ) internal pure returns (address) { (address recovered, RecoverError error) = tryRecover(hash, v, r, s); _throwError(error); return recovered; }
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) { // 32 is the length in bytes of hash, // enforced by the type signature above return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash)); }
function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32) { return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n", StringsUpgradeable.toString(s.length), s)); }
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) { return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash)); } }
contract FlashLoanPriveder { IVault vault;
bytes32 public msgHash = 0x1a6092262d7dc33c2f4b9913ad9318a8c41a138bb42dfacd4c7b6b46b8656522; bytes32 public r = 0xb158f1759111cd99128505f450f608c97178e2b6b9b6f7c3b0d2949e3a21cd02; bytes32 public s = 0x3ade8887fce9b513d41eb36180d6f7d9e072c756991034de2c9a5da541fb8184; uint8 public v = 0x1b;
address public flashLoanMain;
constructor (address vault_) { flashLoanMain = msg.sender; vault = IVault(vault_); }
function flashLoan( IFlashBorrower receiver, address token, uint256 amount, bytes memory signature, bytes calldata data ) external returns (bool){ bytes32 message = keccak256(abi.encodePacked(address(this), amount, receiver, token)); require(ECDSAUpgradeable.recover(msgHash,v,r,s) == ECDSAUpgradeable.recover(message, signature),"Error signer!"); require( amount <= vault.cert().balanceOf(address(vault)), "AMOUNT_BIGGER_THAN_BALANCE" );//常规 : 借钱数量小于等于金库余额 //转钱 require(vault.transferToAccount(address(receiver), amount), "FLASH_LENDER_TRANSFER_FAILED"); require( //我们的合约receiver需要自己实现这个方法 //对我们借来的钱进行一系列操作 receiver.onFlashLoan(msg.sender, token, amount, data) == true, "FLASH_LENDER_CALLBACK_FAILED" ); require( //最后还钱,因为是transferFrom,因此在onFlashLoan的时候需要进行approve ICert(vault.cert()).transferFrom( address(receiver), address(vault), amount ), "FLASH_LENDER_REPAY_FAILED" ); return true; }
function getMsgHash(IFlashBorrower receiver,address token,uint256 amount) public view returns(bytes32){ bytes32 message = keccak256(abi.encodePacked(address(this), amount, receiver, token)); return message; }
}
contract Cert { mapping(address => uint256) private _balances; mapping(address => mapping(address => uint256)) private _allowances; address public flashLoanMain;
uint256 private _totalSupply; constructor()public { flashLoanMain = msg.sender; }
function totalSupply() public view returns (uint256) { return _totalSupply; }
function balanceOf(address account) public view returns (uint256) { return _balances[account]; }
function transfer(address to, uint256 amount) public returns (bool) { _transfer(msg.sender, to, amount); return true; }
function allowance(address owner, address spender) public view returns (uint256) { return _allowances[owner][spender]; }
function approve(address spender, uint256 amount) public returns (bool) { _approve(msg.sender, spender, amount); return true; }
function transferFrom( address from, address to, uint256 amount ) public returns (bool) { _spendAllowance(from, msg.sender, amount); _transfer(from, to, amount); return true; }
function mint(address to, uint256 amount) public { require(msg.sender==flashLoanMain,"Forbidden!"); _mint(to, amount); }
function _transfer( address from, address to, uint256 amount ) internal { require(from != address(0), "ERC20: transfer from the zero address"); require(to != address(0), "ERC20: transfer to the zero address"); uint256 fromBalance = _balances[from]; require(fromBalance >= amount, "ERC20: transfer amount exceeds balance"); _balances[from] = fromBalance - amount; _balances[to] += amount; }
function _mint(address account, uint256 amount) internal { require(account != address(0), "ERC20: mint to the zero address"); _totalSupply += amount; _balances[account] += amount; }
function _approve( address owner, address spender, uint256 amount ) internal { require(owner != address(0), "ERC20: approve from the zero address"); require(spender != address(0), "ERC20: approve to the zero address"); _allowances[owner][spender] = amount; }
function _spendAllowance( address owner, address spender, uint256 amount ) internal { uint256 currentAllowance = allowance(owner, spender); if (currentAllowance != type(uint256).max) { require(currentAllowance >= amount, "ERC20: insufficient allowance"); unchecked { _approve(owner, spender, currentAllowance - amount); } } } }//ERC20接口
contract Vault { mapping(address => uint256) public balanceOf; ICert public cert; address flashLoanPriveder; uint256 public totalSupply; address public flashLoanMain; uint256 internal constant RATIO_MULTIPLY_FACTOR = 10**6;
constructor (address cert_,uint amount) { flashLoanMain = msg.sender; cert = ICert(cert_); totalSupply += amount; uint256 receivedETokens = amount *RATIO_MULTIPLY_FACTOR / getRatio(); balanceOf[msg.sender] = receivedETokens; }
function setPriveder(address flashLoanPriveder_)public { require(msg.sender==flashLoanMain,"setPriveder Forbidden!"); flashLoanPriveder = flashLoanPriveder_; }
function join(uint256 amount) external{ require(amount > 0, "CANNOT_STAKE_ZERO_TOKENS"); uint256 receivedETokens = amount *RATIO_MULTIPLY_FACTOR / getRatio(); totalSupply += receivedETokens; balanceOf[msg.sender] += receivedETokens; require(cert.transferFrom(msg.sender, address(this), amount),"TRANSFER_STAKED_FAIL"); }
function exit() external { uint256 amount = balanceOf[msg.sender]; uint256 stakedTokensToTransfer = amount * getRatio() / RATIO_MULTIPLY_FACTOR; totalSupply -= amount; balanceOf[msg.sender] = 0; require(cert.transfer(msg.sender, stakedTokensToTransfer), 'TRANSFER_STAKED_FAIL'); }
function getRatio() public view returns(uint256){ if (totalSupply> 0 && cert.balanceOf(address(this)) > 0) { return cert.balanceOf(address(this)) *RATIO_MULTIPLY_FACTOR / totalSupply; } return 1; }
function transferToAccount(address account, uint256 amount) external returns(bool){ require(msg.sender==flashLoanPriveder,"transferToAccount Forbidden!"); return cert.transfer(account, amount); } }
contract FlashLoanMain { Vault public vault; Cert public cert; FlashLoanPriveder public flashLoanPriveder; bool public isAirdrop; bool public isComplete; event sendflag(address user);
constructor() { cert = new Cert();//ERC20 token vault = new Vault(address(cert),1000*10**18);//金库 cert.mint(address(vault),1000*10**18); flashLoanPriveder = new FlashLoanPriveder(address(vault)); vault.setPriveder(address(flashLoanPriveder)); }
function airdrop() public { require(!isAirdrop,"Already get airdrop!"); cert.mint(msg.sender,100*10**18); isAirdrop = true; }
function Complete()public returns(bool) { if (cert.balanceOf(msg.sender) > 100*10**18){ isComplete = true; emit sendflag(msg.sender); } return isComplete; }//完成要求:攻击账户的cert代币数量超过10^20 //因此,我们可以利用闪电贷来完成 }
|