授权额度
委托转账
授权额度 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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:
approvesets allowance[owner][spender]transferFromspends 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
常见问题解答
「授权额度」课时是免费的吗?
是的 — 「授权额度」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
「授权额度」这节课中我会学到什么?
委托转账 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「授权额度」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。