From dc4a44fb7f86b75da4e1d4abae940451a74efc14 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 28 Aug 2019 17:00:15 +0200 Subject: [PATCH] Protection to not lose money. --- docs/common-patterns.rst | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/docs/common-patterns.rst b/docs/common-patterns.rst index 5f15e3893..494f22dd4 100644 --- a/docs/common-patterns.rst +++ b/docs/common-patterns.rst @@ -41,15 +41,11 @@ become the new richest. 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 becomeRichest() public payable { + require(msg.value > mostSent, "Not enough money sent."); + pendingWithdrawals[richest] += msg.value; + richest = msg.sender; + mostSent = msg.value; } function withdraw() public { @@ -76,16 +72,12 @@ This is as opposed to the more intuitive sending pattern: mostSent = msg.value; } - function becomeRichest() public payable returns (bool) { - if (msg.value > mostSent) { - // This line can cause problems (explained below). - richest.transfer(msg.value); - richest = msg.sender; - mostSent = msg.value; - return true; - } else { - return false; - } + function becomeRichest() public payable { + require(msg.value > mostSent, "Not enough money sent."); + // This line can cause problems (explained below). + richest.transfer(msg.value); + richest = msg.sender; + mostSent = msg.value; } }