2019-02-11 10:54:27 +00:00
|
|
|
.. index:: contract;modular, modular contract
|
|
|
|
|
|
|
|
*****************
|
|
|
|
Modular Contracts
|
|
|
|
*****************
|
|
|
|
|
2019-05-20 13:04:00 +00:00
|
|
|
A modular approach to building your contracts helps you reduce the complexity
|
|
|
|
and improve the readability which will help to identify bugs and vulnerabilities
|
|
|
|
during development and code review.
|
2022-07-13 12:02:29 +00:00
|
|
|
If you specify and control the behaviour of each module in isolation, the
|
2019-05-20 13:04:00 +00:00
|
|
|
interactions you have to consider are only those between the module specifications
|
|
|
|
and not every other moving part of the contract.
|
|
|
|
In the example below, the contract uses the ``move`` method
|
2019-02-11 10:54:27 +00:00
|
|
|
of the ``Balances`` :ref:`library <libraries>` to check that balances sent between
|
2019-05-20 13:04:00 +00:00
|
|
|
addresses match what you expect. In this way, the ``Balances`` library
|
|
|
|
provides an isolated component that properly tracks balances of accounts.
|
|
|
|
It is easy to verify that the ``Balances`` library never produces negative balances or overflows
|
|
|
|
and the sum of all balances is an invariant across the lifetime of the contract.
|
2019-02-11 10:54:27 +00:00
|
|
|
|
2021-06-25 10:25:29 +00:00
|
|
|
.. code-block:: solidity
|
2019-02-11 10:54:27 +00:00
|
|
|
|
2020-05-13 15:45:58 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-09-08 08:48:04 +00:00
|
|
|
pragma solidity >=0.5.0 <0.9.0;
|
2019-02-11 10:54:27 +00:00
|
|
|
|
|
|
|
library Balances {
|
|
|
|
function move(mapping(address => uint256) storage balances, address from, address to, uint amount) internal {
|
|
|
|
require(balances[from] >= amount);
|
|
|
|
require(balances[to] + amount >= balances[to]);
|
|
|
|
balances[from] -= amount;
|
|
|
|
balances[to] += amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
contract Token {
|
|
|
|
mapping(address => uint256) balances;
|
|
|
|
using Balances for *;
|
2023-02-01 18:53:33 +00:00
|
|
|
mapping(address => mapping(address => uint256)) allowed;
|
2019-02-11 10:54:27 +00:00
|
|
|
|
|
|
|
event Transfer(address from, address to, uint amount);
|
|
|
|
event Approval(address owner, address spender, uint amount);
|
|
|
|
|
2021-08-10 10:39:41 +00:00
|
|
|
function transfer(address to, uint amount) external returns (bool success) {
|
2019-02-11 10:54:27 +00:00
|
|
|
balances.move(msg.sender, to, amount);
|
|
|
|
emit Transfer(msg.sender, to, amount);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:39:41 +00:00
|
|
|
function transferFrom(address from, address to, uint amount) external returns (bool success) {
|
2019-02-11 10:54:27 +00:00
|
|
|
require(allowed[from][msg.sender] >= amount);
|
|
|
|
allowed[from][msg.sender] -= amount;
|
|
|
|
balances.move(from, to, amount);
|
|
|
|
emit Transfer(from, to, amount);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:39:41 +00:00
|
|
|
function approve(address spender, uint tokens) external returns (bool success) {
|
2019-02-11 10:54:27 +00:00
|
|
|
require(allowed[msg.sender][spender] == 0, "");
|
|
|
|
allowed[msg.sender][spender] = tokens;
|
|
|
|
emit Approval(msg.sender, spender, tokens);
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-13 15:19:49 +00:00
|
|
|
|
2021-08-10 10:39:41 +00:00
|
|
|
function balanceOf(address tokenOwner) external view returns (uint balance) {
|
2019-12-13 15:19:49 +00:00
|
|
|
return balances[tokenOwner];
|
|
|
|
}
|
2019-02-11 10:54:27 +00:00
|
|
|
}
|