Token Checker
Background
The Token Checker contract is designed to make it simple to check for what standard various tokens belong too.
The Token Checker contract is also used by Sediments Token Gate contract
Installation
npm add @dirtroad/sediment
yarn add @dirtroad/sediment
pnpm add @dirtroad/sediment
Using Token Checker
// SPDX-License-Identifier: MITpragma solidity ^0.8.24;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";import "@openzeppelin/contracts/utils/Context.sol";import "@dirtroad/sediment/contracts/tokenChecker/TokenChecker.sol";
contract PiggyBank is TokenChecker, Context {
using SafeERC20 for IERC20;
mapping(address => mapping(address => uint256)) private balances;
function deposit(address token, uint256 amount) external { require(isERC20(token), "Only ERC-20s accepted"); IERC20(token).safeTransferFrom(_msgSender(), address(this), amount); balances[_msgSender()][token] = balances[_msgSender()][token] + amount; }
function withdraw(address token) external { require(isERC20(token), "Only ERC-20s accepted"); IERC20(token).safeTransferFrom(address(this), _msgSender(), amount); balances[_msgSender()][token] = 0; }}
API
Solidity API
IERC165
supportsInterface
function supportsInterface(bytes4 interfaceId) external view returns (bool)
TokenChecker
A contract to check if a given contract address implements the ERC-20, ERC-721, or ERC-1155 token standard
isERC20
function isERC20(address tokenAddress) public pure returns (bool)
Check if the given contract address is an ERC-20 token contract
Parameters
Name | Type | Description |
---|---|---|
tokenAddress | address | The address of the token contract to check |
Return Values
Name | Type | Description |
---|---|---|
[0] | bool | bool True if the contract is an ERC-20 token, false otherwise |
isERC721
function isERC721(address tokenAddress) public view returns (bool)
Check if the given contract address is an ERC-721 token contract
Parameters
Name | Type | Description |
---|---|---|
tokenAddress | address | The address of the token contract to check |
Return Values
Name | Type | Description |
---|---|---|
[0] | bool | bool True if the contract is an ERC-721 token, false otherwise |
isERC1155
function isERC1155(address tokenAddress) public view returns (bool)
Check if the given contract address is an ERC-1155 token contract
Parameters
Name | Type | Description |
---|---|---|
tokenAddress | address | The address of the token contract to check |
Return Values
Name | Type | Description |
---|---|---|
[0] | bool | bool True if the contract is an ERC-1155 token, false otherwise |