엘 아타케 델 샌드위치
Eneigenphi puedes encontrar ejemplos de ataques del sandwitch en mainnet.
Antes de comenzar
Para este tutorial ocuparás Metamask u otra 지갑 호환 con fondos en Goerli que puedes obetener desde un faucet .
토큰 ERC20 con mecanismo anti-sandwitch
Asegúrate de usar un router que exista en el blockchain que estás utilizando.
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff
0x10ED43C718714eb63d5aA57B78B54704E256024E
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
interface IUniswapV2Factory {
function createPair(address tokenA, address tokenB) external returns (address pair);
}
interface IUniswapV2Router {
function factory() external pure returns (address);
function WETH() external pure returns (address);
}
contract MyToken is ERC20 {
mapping(address => uint256) public lastTxByAddress;
uint public blockCooldownAmount = 1;
address pair;
constructor() ERC20("My Token", "MTKN") {
_mint(msg.sender, 1_000_000 ether);
IUniswapV2Router uniswapRouter = IUniswapV2Router(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
pair = IUniswapV2Factory(uniswapRouter.factory()).createPair(address(this), uniswapRouter.WETH());
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal override {
amount;
if(from == pair)
{
ensureMaxTxFrequency(to);
lastTxByAddress[to] = block.number;
} else if(to == pair)
{
ensureMaxTxFrequency(from);
lastTxByAddress[from] = block.number;
}else
{
ensureMaxTxFrequency(from);
lastTxByAddress[from] = block.number;
ensureMaxTxFrequency(to);
lastTxByAddress[to] = block.number;
}
}
function ensureMaxTxFrequency(address addr) internal virtual {
bool isAllowed = lastTxByAddress[addr] == 0 ||
((lastTxByAddress[addr] + blockCooldownAmount) < (block.number + 1));
require(isAllowed, "Max tx frequency exceeded!");
}
}
Gracias por ver este 비디오!
Sígannos en dev.to y en para todo lo relacionado al desarrollo en Blockchain en Español.
Reference
이 문제에 관하여(엘 아타케 델 샌드위치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/turupawn/el-ataque-del-sandwich-429m텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)