概要
FiatTokenV2コントラクトは、Circle社が発行する法定通貨担保型ERC20トークン(例:USDC)のV2実装です。
このコントラクトは、ERC3009やERC2612といった署名ベースの転送や承認機能に対応しており、柔軟かつガスレスなトークン操作が可能です。
FiatTokenV2 は、以下の複数の機能を統合しています。
- V1.1の基本機能(ERC20、ブラックリスト、Pausable、Ownershipなど)
- EIP3009
- 署名によるトークン転送(
transferWithAuthorization等)
- 署名によるトークン転送(
- EIP2612
permitによる署名付き承認
- 増減式の
allowance操作(increaseAllowance/decreaseAllowance)
また、トークンの初期化処理(バージョン識別とEIP712ドメインの設定)も含まれています。
コード
/**
* Copyright 2023 Circle Internet Group, Inc. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
pragma solidity 0.6.12;
import { FiatTokenV1_1 } from "../v1.1/FiatTokenV1_1.sol";
import { EIP712 } from "../util/EIP712.sol";
import { EIP3009 } from "./EIP3009.sol";
import { EIP2612 } from "./EIP2612.sol";
/**
* @title FiatToken V2
* @notice ERC20 Token backed by fiat reserves, version 2
*/
contract FiatTokenV2 is FiatTokenV1_1, EIP3009, EIP2612 {
uint8 internal _initializedVersion;
/**
* @notice Initialize v2
* @param newName New token name
*/
function initializeV2(string calldata newName) external {
// solhint-disable-next-line reason-string
require(initialized && _initializedVersion == 0);
name = newName;
_DEPRECATED_CACHED_DOMAIN_SEPARATOR = EIP712.makeDomainSeparator(
newName,
"2"
);
_initializedVersion = 1;
}
/**
* @notice Increase the allowance by a given increment
* @param spender Spender's address
* @param increment Amount of increase in allowance
* @return True if successful
*/
function increaseAllowance(address spender, uint256 increment)
external
virtual
whenNotPaused
notBlacklisted(msg.sender)
notBlacklisted(spender)
returns (bool)
{
_increaseAllowance(msg.sender, spender, increment);
return true;
}
/**
* @notice Decrease the allowance by a given decrement
* @param spender Spender's address
* @param decrement Amount of decrease in allowance
* @return True if successful
*/
function decreaseAllowance(address spender, uint256 decrement)
external
virtual
whenNotPaused
notBlacklisted(msg.sender)
notBlacklisted(spender)
returns (bool)
{
_decreaseAllowance(msg.sender, spender, decrement);
return true;
}
/**
* @notice Execute a transfer with a signed authorization
* @param from Payer's address (Authorizer)
* @param to Payee's address
* @param value Amount to be transferred
* @param validAfter The time after which this is valid (unix time)
* @param validBefore The time before which this is valid (unix time)
* @param nonce Unique nonce
* @param v v of the signature
* @param r r of the signature
* @param s s of the signature
*/
function transferWithAuthorization(
address from,
address to,
uint256 value,
uint256 validAfter,
uint256 validBefore,
bytes32 nonce,
uint8 v,
bytes32 r,
bytes32 s
) external whenNotPaused notBlacklisted(from) notBlacklisted(to) {
_transferWithAuthorization(
from,
to,
value,
validAfter,
validBefore,
nonce,
v,
r,
s
);
}
/**
* @notice Receive a transfer with a signed authorization from the payer
* @dev This has an additional check to ensure that the payee's address
* matches the caller of this function to prevent front-running attacks.
* @param from Payer's address (Authorizer)
* @param to Payee's address
* @param value Amount to be transferred
* @param validAfter The time after which this is valid (unix time)
* @param validBefore The time before which this is valid (unix time)
* @param nonce Unique nonce
* @param v v of the signature
* @param r r of the signature
* @param s s of the signature
*/
function receiveWithAuthorization(
address from,
address to,
uint256 value,
uint256 validAfter,
uint256 validBefore,
bytes32 nonce,
uint8 v,
bytes32 r,
bytes32 s
) external whenNotPaused notBlacklisted(from) notBlacklisted(to) {
_receiveWithAuthorization(
from,
to,
value,
validAfter,
validBefore,
nonce,
v,
r,
s
);
}
/**
* @notice Attempt to cancel an authorization
* @dev Works only if the authorization is not yet used.
* @param authorizer Authorizer's address
* @param nonce Nonce of the authorization
* @param v v of the signature
* @param r r of the signature
* @param s s of the signature
*/
function cancelAuthorization(
address authorizer,
bytes32 nonce,
uint8 v,
bytes32 r,
bytes32 s
) external whenNotPaused {
_cancelAuthorization(authorizer, nonce, v, r, s);
}
/**
* @notice Update allowance with a signed permit
* @param owner Token owner's address (Authorizer)
* @param spender Spender's address
* @param value Amount of allowance
* @param deadline The time at which the signature expires (unix time), or max uint256 value to signal no expiration
* @param v v of the signature
* @param r r of the signature
* @param s s of the signature
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
)
external
virtual
whenNotPaused
notBlacklisted(owner)
notBlacklisted(spender)
{
_permit(owner, spender, value, deadline, v, r, s);
}
/**
* @dev Internal function to increase the allowance by a given increment
* @param owner Token owner's address
* @param spender Spender's address
* @param increment Amount of increase
*/
function _increaseAllowance(
address owner,
address spender,
uint256 increment
) internal override {
_approve(owner, spender, allowed[owner][spender].add(increment));
}
/**
* @dev Internal function to decrease the allowance by a given decrement
* @param owner Token owner's address
* @param spender Spender's address
* @param decrement Amount of decrease
*/
function _decreaseAllowance(
address owner,
address spender,
uint256 decrement
) internal override {
_approve(
owner,
spender,
allowed[owner][spender].sub(
decrement,
"ERC20: decreased allowance below zero"
)
);
}
}変数
uint8 internal _initializedVersion;V2の初期化バージョン。
initializeV2 実行時に 1 に設定され、二重初期化を防止します。
関数
initializeV2
function initializeV2(string calldata newName) external {
require(initialized && _initializedVersion == 0);
name = newName;
_DEPRECATED_CACHED_DOMAIN_SEPARATOR = EIP712.makeDomainSeparator(
newName,
"2"
);
_initializedVersion = 1;
}V2の初期化処理を行う関数。
- トークンの名前を更新し、EIP712ドメインのバージョンを "2" に設定します。
_initializedVersionにより二重初期化を防ぎます。
引数
newName- 新しいトークン名。
increaseAllowance
function increaseAllowance(address spender, uint256 increment)
external
virtual
whenNotPaused
notBlacklisted(msg.sender)
notBlacklisted(spender)
returns (bool)
{
_increaseAllowance(msg.sender, spender, increment);
return true;
}spender に対する allowance を増やす関数。
_increaseAllowance 関数を呼び出し、既存の許可額に increment を加算します。
whenNotPaused- pause中でないことを保証。
notBlacklisted(msg.sender)- 呼び出し元がブラックリストに含まれていない。
notBlacklisted(spender)- 承認対象がブラックリストに含まれていない。
引数
spender- トークンを使用させたい相手。
increment増やす許可額。
戻り値
true- 成功時。
decreaseAllowance
function decreaseAllowance(address spender, uint256 decrement)
external
virtual
whenNotPaused
notBlacklisted(msg.sender)
notBlacklisted(spender)
returns (bool)
{
_decreaseAllowance(msg.sender, spender, decrement);
return true;
}spender に対する allowance を減らす関数。
_decreaseAllowance 関数を呼び出し、指定量減らします。
残高が不足しているときはrevert します。
whenNotPaused- pause中でないことを保証。
notBlacklisted(msg.sender)- 呼び出し元がブラックリストに含まれていない。
notBlacklisted(spender)- 対象がブラックリストに含まれていない。
引数
spender- トークン使用を許している相手。
decrement- 減らす許可額。
戻り値
true- 成功時。
transferWithAuthorization
function transferWithAuthorization(
address from,
address to,
uint256 value,
uint256 validAfter,
uint256 validBefore,
bytes32 nonce,
uint8 v,
bytes32 r,
bytes32 s
) external whenNotPaused notBlacklisted(from) notBlacklisted(to) {
_transferWithAuthorization(
from,
to,
value,
validAfter,
validBefore,
nonce,
v,
r,
s
);
}EIP3009形式の署名付きトークンを送金する関数。
署名を検証し、未使用・有効なものであれば from → to に value を送金します。
whenNotPaused- コントラクトが停止中でないこと。
notBlacklisted(from/to)- 送信者・受信者ともにブロックされていないこと。
引数
from- 送信者(署名者)。
to- 受信者。
value- 送金額。
validAfter- 有効開始時刻(UNIX)。
validBefore失効時刻。nonce- 一意な識別子。
v, r, s- ECDSA署名要素。
receiveWithAuthorization
function receiveWithAuthorization(
address from,
address to,
uint256 value,
uint256 validAfter,
uint256 validBefore,
bytes32 nonce,
uint8 v,
bytes32 r,
bytes32 s
) external whenNotPaused notBlacklisted(from) notBlacklisted(to) {
_receiveWithAuthorization(
from,
to,
value,
validAfter,
validBefore,
nonce,
v,
r,
s
);
}署名者から msg.sender へのトークン受取(EIP3009)を実行する関数。
to == msg.sender を保証することで、フロントラン攻撃を防ぎます。
他人の署名を悪用して先にトークンを取得することを防止します。
whenNotPaused- コントラクトが停止中でないこと。
notBlacklisted(from/to)- 送信者・受信者ともにブロックされていないこと。
引数
from- 送信者(署名者)。
to- 受信者。
value- 送金額。
validAfter- 有効開始時刻(UNIX)。
validBefore失効時刻。nonce- 一意な識別子。
v, r, s- ECDSA署名要素。
cancelAuthorization
function cancelAuthorization(
address authorizer,
bytes32 nonce,
uint8 v,
bytes32 r,
bytes32 s
) external whenNotPaused {
_cancelAuthorization(authorizer, nonce, v, r, s);
}未使用の署名を無効化する(EIP3009の取り消し処理)。
キャンセル署名が正しければ、nonce をマークして二重使用を防ぎます。
whenNotPaused- 停止状態では実行できない。
引数
authorizer- 署名者アドレス。
nonce- ユニークな識別子。
v, r, s- 署名。
permit
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external virtual whenNotPaused notBlacklisted(owner) notBlacklisted(spender) {
_permit(owner, spender, value, deadline, v, r, s);
}EIP2612による署名付き approve を実行する関数。
オフチェーン署名によって、承認操作(allowance設定)をガスレスで実行可能。DEXや支払いAPIとの組み合わせで使用されます。
whenNotPaused- 停止状態では実行できない。
notBlacklistedownerがブラックリストに含まれていない。
引数
owner- 承認者。
spender- 承認対象。
value- 承認額。
deadline- 署名の有効期限。
v, r, s- 署名パラメータ。