0Pricing
Web3 & DApp Development Fundamentals · Lekcja

Limity wydatków

Delegowane transfery

Limity wydatków to bezpłatna lekcja Web3 & DApp Development Fundamentals na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Web3 & DApp Development Fundamentals, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Web3 & DApp Development Fundamentals zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Delegated Transfers

Allowances let a token owner authorize another account - the spender - to move tokens on their behalf. This is what enables decentralized exchanges and DeFi protocols to pull your tokens with permission.

The flow is: approve sets a limit, then transferFrom spends within it.

The Allowance Storage

Allowances live in a nested mapping: owner to spender to amount. It records how much each spender may move for each owner.

<code>mapping(address => mapping(address => uint256)) public allowance;</code>

Setting an Allowance

The owner calls approve to authorize a spender. This overwrites any previous allowance for that spender.

<code>function approve(address spender, uint256 amount) public returns (bool) {
    allowance[msg.sender][spender] = amount;
    emit Approval(msg.sender, spender, amount);
    return true;
}</code>

Reading an Allowance

Anyone can query the remaining allowance with the public mapping or the standard allowance(owner, spender) view function.

<code>// remaining = allowance[owner][spender];
function allowanceOf(address owner, address spender) public view returns (uint256) {
    return allowance[owner][spender];
}</code>

Implementing transferFrom

transferFrom lets an approved spender move tokens from the owner to a recipient. It must check both the owner's balance and the spender's allowance.

<code>function transferFrom(address from, address to, uint256 amount) public returns (bool) {
    require(balanceOf[from] >= amount, 'insufficient balance');
    require(allowance[from][msg.sender] >= amount, 'allowance exceeded');
    allowance[from][msg.sender] -= amount;
    balanceOf[from] -= amount;
    balanceOf[to] += amount;
    emit Transfer(from, to, amount);
    return true;
}</code>

Decrementing the Allowance

Notice that transferFrom reduces the allowance by the amount spent. This prevents a spender from reusing the same approval indefinitely.

<code>allowance[from][msg.sender] -= amount; // consumed</code>

Infinite Approval

Some protocols request the maximum uint256 allowance so users only approve once. Many implementations skip decrementing when the allowance is set to this max value, saving gas.

<code>uint256 constant MAX = type(uint256).max;
// if (allowance == MAX) skip decrement</code>

The Approve Race Condition

There is a known hazard: changing an allowance from one non-zero value to another can be front-run, letting a spender use both. The classic mitigation is to set the allowance to 0 first, then to the new value.

<code>// recommended pattern
token.approve(spender, 0);
token.approve(spender, newAmount);</code>

increaseAllowance and decreaseAllowance

To avoid the race condition, some tokens add helper functions that adjust the allowance relative to its current value rather than overwriting it.

<code>function increaseAllowance(address spender, uint256 added) public returns (bool) {
    allowance[msg.sender][spender] += added;
    emit Approval(msg.sender, spender, allowance[msg.sender][spender]);
    return true;
}</code>

Real-World Use: DEX

When you trade on a DEX you first approve the router contract. The router then calls transferFrom to pull your tokens into the swap. Without the allowance the trade cannot happen.

Security Reminder

An allowance is a standing permission. Granting an infinite allowance to a malicious or buggy contract can drain your tokens. Review and revoke unused approvals regularly.

<code>// revoke by approving zero
token.approve(spender, 0);</code>

Quick Check

Test your understanding of allowances.

Recap

You learned the allowance mechanism:

  • approve sets allowance[owner][spender]
  • transferFrom spends within the allowance and decrements it
  • The approve race condition is mitigated by setting to 0 first or using increase/decreaseAllowance
  • Allowances power DEXs and DeFi - revoke unused ones for safety

Często zadawane pytania

Czy lekcja „Limity wydatków” jest bezpłatna?

Tak — pełny tekst „Limity wydatków” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Web3 & DApp Development Fundamentals, przejdź na CoddyKit PRO. Kurs Web3 & DApp Development Fundamentals zawiera 4 lekcji w sumie.

Co nauczysz się w „Limity wydatków”?

Delegowane transfery Ćwiczysz Web3 & DApp Development Fundamentals z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Web3 & DApp Development Fundamentals?

Nie wymagamy żadnego doświadczenia. Web3 & DApp Development Fundamentals w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Limity wydatków”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Web3 & DApp Development Fundamentals?

Tak. Każda lekcja Web3 & DApp Development Fundamentals zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Standard ERC-20
  2. Implementowanie tokena
  3. Limity wydatków
  4. Tworzenie i spalanie
← Powrót do Web3 & DApp Development Fundamentals