Last active
June 27, 2025 18:39
-
-
Save lucasfernandes/a1cf5e756e8c1b7f5ad6ef9751411afe to your computer and use it in GitHub Desktop.
Bitgert Finance Blockchain Lead Developer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ANSWER 3: | |
* BEP-20 tokens follow the same interface as ERC-20 (EVM standard). | |
* To interact with a BEP-20 token, I use the standard ERC-20 interface, | |
* or preferably the `SafeERC20` wrapper from OpenZeppelin to improve safety. | |
* | |
* Key functions used to interact with the token contract: | |
* | |
* - `transferFrom(address from, address to, uint256 amount)` — for staking | |
* - `transfer(address to, uint256 amount)` — for withdrawals | |
* - `balanceOf(address account)` — to display user balances | |
* | |
* When using `SafeERC20`, these become: | |
* | |
* - `safeTransferFrom(from, to, amount);` | |
* - `safeTransfer(to, amount);` | |
* | |
* Example: | |
*/ | |
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity 0.8.28; | |
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | |
contract Staking { | |
using SafeERC20 for IERC20; | |
IERC20 public token; | |
constructor(address tokenAddress) { | |
token = IERC20(tokenAddress); | |
} | |
function stake(uint256 amount) external { | |
require(amount > 0, "Amount must be greater than zero"); | |
token.safeTransferFrom(msg.sender, address(this), amount); | |
// update staking logic here... | |
} | |
function withdraw(uint256 amount) external { | |
// check if user can withdraw... | |
token.safeTransfer(msg.sender, amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment