August 2019
Intermediate to advanced
486 pages
13h 52m
English
To overcome an attack on the approve function, there are different techniques.
For the previously discussed problem of the approve function, one solution is to set the allowance to 0 (zero) before setting it again with a new value:
function approve(address spender, uint tokens) public returns (bool success) { require((tokens == 0) || (allowed[msg.sender][spender] == 0)); allowed[msg.sender][spender] = tokens; Approval(msg.sender, spender, tokens); return true;}
The preceding code would prevent the front-running attack and would enforce that the approver would always set the allowance to 0 (zero) before setting it again with a new non-zero value. However, this technique requires two transactions ...