Original Blog Consider a decentralized exchange (DEX) contract, a prime example of where the address type comes into play. In a DEX, users deposit funds into the contract's addresses to initiate trades. The contract's logic governs the execution of trades and ensures that users' balances are updated accordingly.
contract DecentralizedExchange {
mapping(address => uint256) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function trade(address recipient, uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
balances[recipient] += amount;
}
}
Top comments (0)