Lợi Ích | Mô Tả |
---|---|
Tiết kiệm gas khi lưu trữ | Ít slot cần thiết hơn → rẻ hơn |
Tăng hiệu quả giao dịch | Giảm chi phí thực thi trên blockchain |
Chế Độ Đóng Gói | Slot 0 |
---|---|
Tốt (1 slot) | 0x0000...BBBB...AAAA (uint128 b nằm ở 16 bytes cao, a ở thấp) |
Kém (2 slot) | Slot 0: 0x000...AAAA Slot 1: 0x000...BBBB |
payable
:receive() external payable { // Xử lý khi nhận ETH hợp lệ}
fallback() external payable { // Xử lý các cuộc gọi không xác định}
receive()
được gọi khi giao dịch gửi ETH không kèm dữ liệu. fallback()
được gọi trong trường hợp không tìm thấy hàm được gọi hoặc khi gửi dữ liệu.transfer
function basicTransferETH(address payable recipient, uint256 amount) external { require(address(this).balance >= amount, "Insufficient balance"); recipient.transfer(amount);}
transfer
gửi 2300 gas, đủ để phát sự kiện.send
function basicSendETH(address payable recipient, uint256 amount) external { require(address(this).balance >= amount, "Insufficient balance"); bool success = recipient.send(amount); require(success, "Failed to send Ether");}
bool
để ta kiểm tra thủ công.call
An Toànfunction safeTransferETH(address payable recipient, uint256 amount) external payable { require(address(this).balance >= amount, "Insufficient balance"); (bool sent, ) = recipient.call{value: amount}(""); require(sent, "Failed to send Ether");}
call
là cách an toàn và linh hoạt nhất để gửi ETH vì nó không giới hạn gas và cho phép kiểm tra kỹ lưỡng kết quả.transfer
trả về giá trị boolean. Luôn kiểm tra giá trị này để đảm bảo giao dịch thành công:bool success = IERC20(tokenAddress).transfer(to, amount);require(success, "Token transfer failed");
Thuộc tính | Ý Nghĩa |
---|---|
msg.sender | Địa chỉ gọi hàm (EOA hoặc contract khác) |
msg.value | Lượng ETH gửi kèm theo cuộc gọi (đơn vị wei) |
msg.data | Dữ liệu đầy đủ truyền vào hàm |
msg.sig | Selector 4 bytes của hàm được gọi |
function pay() public payable { require(msg.value > 0, "No ETH sent"); address payer = msg.sender;}
msg.sender
sẽ là A, không phải người ngoài khởi tạo giao dịch ban đầu. Tránh dùng tx.origin
do nguy cơ bị tấn công phishing.// SPDX-License-Identifier: MITpragma solidity ^0.8.20;
interface IERC20 { function transfer(address to, uint256 amount) external returns (bool);}
contract DeepDiveContract { // ❌ Mặt hạn chế về storage packing uint128 bigNumber; uint256 veryBigNumber;
// ✅ Tối ưu lưu trữ với storage packing uint128 smallA; uint128 smallB;
// Sự kiện event Received(address sender, uint256 amount, bytes data, bytes4 sig); event PaymentSent(address recipient, uint256 amount); event TokenSent(address recipient, uint256 amount, address tokenAddress);
// Nhận ETH receive() external payable { emit Received(msg.sender, msg.value, msg.data, msg.sig); }
fallback() external payable { emit Received(msg.sender, msg.value, msg.data, msg.sig); }
// Gửi ETH sử dụng call function sendEther(address payable _to) external payable { require(msg.value > 0, "Send some ETH to forward"); (bool sent, ) = _to.call{value: msg.value}(""); require(sent, "Failed to send Ether"); emit PaymentSent(_to, msg.value); }
// Gửi token ERC20 function sendToken(address tokenAddress, address to, uint256 amount) external { bool success = IERC20(tokenAddress).transfer(to, amount); require(success, "Token transfer failed"); emit TokenSent(to, amount, tokenAddress); }
// Ghi dữ liệu vào storage không tối ưu function writeBadStorage(uint128 _bigNumber, uint256 _veryBigNumber) external { bigNumber = _bigNumber; veryBigNumber = _veryBigNumber; }
// Ghi dữ liệu vào storage tối ưu function writeGoodStorage(uint128 _smallA, uint128 _smallB) external { smallA = _smallA; smallB = _smallB; }
// Đọc storage không tối ưu function viewBadStorage() external view returns (uint128, uint256) { return (bigNumber, veryBigNumber); }
// Đọc storage tối ưu function viewGoodStorage() external view returns (uint128, uint128) { return (smallA, smallB); }}
msg
là cơ sở vững chắc để viết các smart contract bảo mật và chuyên nghiệp. Nắm chắc những kiến thức nền tảng này sẽ giúp bạn chuẩn bị tốt hơn cho việc phát triển các hợp đồng thông minh phức tạp, an toàn và hiệu quả.