ERC-5827 · Stagnant(정체) · ERC · 2022-10-22
ERC-5827: Auto-renewable allowance extension
허용량(allowance)이 초당 일정 비율로 상한까지 스스로 다시 채워지는 ERC-20 확장으로, 무제한 승인 없이 구독과 지출 한도를 구현한다.
어떤 문제를 해결하나요?
ERC-20 allowance는 고정된 숫자입니다. 정기 결제(구독, 급여, 정기 적립식 매수)에는 불편합니다. 사용자가 매 기간마다 다시 승인하거나, dapp이 아주 큰 값이나 무제한 allowance를 요구하게 됩니다. 무제한 승인은 컨트랙트가 악의적이거나 공격당했을 때 사용자가 자금을 잃는 흔한 원인입니다.
ERC-5827은 갱신 가능한 allowance를 추가합니다. 사용자는 최댓값과 회복 속도를 승인합니다. spender가 일부를 사용하면 allowance는 그 속도로 최댓값을 향해 다시 올라갑니다. 신용카드나 체크카드 한도처럼 동작합니다. 예를 들어 "최대 100 USDC, 30일마다 100 USDC 회복"입니다.
어떻게 동작하나요?
approveRenewable(spender, value, recoveryRate)는 초기 allowance와 상한을 모두value로 설정합니다.recoveryRate는 초당 토큰 수입니다.recoveryRate > value이면 revert해야 하며(SHOULD),RenewableApproval(owner, spender, value, recoveryRate)를 반드시 발생시켜야 합니다(MUST).renewableAllowance(owner, spender)는 상한과 회복 속도를 반환합니다.allowance(owner, spender)는 지금 당장 쓸 수 있는 양을 반환하도록 오버라이드됩니다. 남은 양에 마지막 사용 이후 회복된 양을 더한 값이며 상한을 넘지 않습니다.transferFrom은 이렇게 회복된 양에서 차감하며, 너무 적으면InsufficientRenewableAllowance(available)로 revert해야 합니다.- 일반
approve도 여전히 동작하며 회복 속도를 0(일반적인, 갱신되지 않는 allowance)으로 설정합니다. supportsInterface(0x93cd7af6)는true를 반환해야 합니다.
선택적 추가 기능: IERC5827Proxy는 기존 ERC-20이 allowance 집행을 프록시 컨트랙트에 위임하게 해주고, IERC5827Expirable은 갱신 가능한 승인에 만료 타임스탬프를 추가합니다.
샘플 코드
핵심 회복 로직과 클라이언트 호출(ethers v6):
struct Renewable { uint256 maxAmount; uint256 recoveryRate; uint256 remaining; uint64 lastUpdated; }
mapping(address => mapping(address => Renewable)) internal _rAllowance;
function allowance(address owner, address spender) public view returns (uint256) {
Renewable memory a = _rAllowance[owner][spender];
uint256 recovered = a.recoveryRate * (block.timestamp - a.lastUpdated);
uint256 total = a.remaining + recovered;
return total > a.maxAmount ? a.maxAmount : total;
}
function _spendAllowance(address owner, address spender, uint256 amount) internal {
uint256 available = allowance(owner, spender);
if (available < amount) revert InsufficientRenewableAllowance(available);
Renewable storage a = _rAllowance[owner][spender];
a.remaining = available - amount;
a.lastUpdated = uint64(block.timestamp);
}
import { parseUnits } from "ethers";
// Allow a subscription contract to pull up to 100 tokens, refilling over 30 days
const cap = parseUnits("100", 18);
const perSecond = cap / BigInt(30 * 24 * 60 * 60);
await token.approveRenewable(subscriptionContract, cap, perSecond);
알아둘 점
- 상태: Stagnant. 네이티브로 구현한 토큰은 드뭅니다. 기존 토큰에는 프록시 변형이 현실적인 방법입니다.
- 회복은 이산적인 주기가 아니라 연속적입니다. 초당 속도가 높으면 spender는 여러 트랜잭션에 걸쳐 여전히 많은 양을 빼갈 수 있습니다.
- ERC-5827을 모르는 도구는
allowance()나Approval이벤트를 spender가 가져갈 수 있는 최대량으로 해석할 수 있습니다. 갱신 가능한 allowance에서는 spender가 시간이 지나면 더 가져갈 수 있습니다. - 관련 아이디어: ERC-2612(서명 승인)와 ERC-7674(임시적인, 단일 트랜잭션 승인).