- Solidity 教程
- Solidity - 首頁
- Solidity - 概述
- Solidity - 環境搭建
- Solidity - 基本語法
- Solidity - 第一個應用
- Solidity - 註釋
- Solidity - 資料型別
- Solidity - 變數
- Solidity - 變數作用域
- Solidity - 運算子
- Solidity - 迴圈
- Solidity - 條件語句
- Solidity - 字串
- Solidity - 陣列
- Solidity - 列舉
- Solidity - 結構體
- Solidity - 對映
- Solidity - 型別轉換
- Solidity - 以太幣單位
- Solidity - 特殊變數
- Solidity - 樣式指南
- Solidity 函式
- Solidity - 函式
- Solidity - 函式修飾符
- Solidity - View 函式
- Solidity - Pure 函式
- Solidity - 回退函式
- 函式過載
- 數學函式
- 加密函式
- Solidity 常用模式
- Solidity - 提款模式
- Solidity - 受限訪問
- Solidity 高階特性
- Solidity - 合約
- Solidity - 繼承
- Solidity - 建構函式
- Solidity - 抽象合約
- Solidity - 介面
- Solidity - 庫
- Solidity - 彙編
- Solidity - 事件
- Solidity - 錯誤處理
- Solidity 有用資源
- Solidity - 快速指南
- Solidity - 有用資源
- Solidity - 討論
Solidity - 提款模式
提款模式確保不會進行直接轉賬呼叫,這會帶來安全風險。以下合約展示了不安全的轉賬呼叫方式來發送以太幣。
pragma solidity ^0.5.0;
contract Test {
address payable public richest;
uint public mostSent;
constructor() public payable {
richest = msg.sender;
mostSent = msg.value;
}
function becomeRichest() public payable returns (bool) {
if (msg.value > mostSent) {
// Insecure practice
richest.transfer(msg.value);
richest = msg.sender;
mostSent = msg.value;
return true;
} else {
return false;
}
}
}
上述合約可以透過使最富有者成為回退函式失敗的合約來使其處於不可用狀態。當回退函式失敗時,becomeRichest() 函式也會失敗,合約將永遠卡住。為了減輕這個問題,我們可以使用提款模式。
在提款模式中,我們將在每次轉賬前重置待處理金額。這將確保只有呼叫者合約失敗。
pragma solidity ^0.5.0;
contract Test {
address public richest;
uint public mostSent;
mapping (address => uint) pendingWithdrawals;
constructor() public payable {
richest = msg.sender;
mostSent = msg.value;
}
function becomeRichest() public payable returns (bool) {
if (msg.value > mostSent) {
pendingWithdrawals[richest] += msg.value;
richest = msg.sender;
mostSent = msg.value;
return true;
} else {
return false;
}
}
function withdraw() public {
uint amount = pendingWithdrawals[msg.sender];
pendingWithdrawals[msg.sender] = 0;
msg.sender.transfer(amount);
}
}
廣告