|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 5 | +import "@openzeppelin/contracts/access/Ownable.sol"; |
| 6 | + |
| 7 | +contract BSCBridgeDAO is Ownable { |
| 8 | + IERC20 public token; // The ERC20 token used for unlocking and locking |
| 9 | + |
| 10 | + enum LockStatus { Pending, Released } |
| 11 | + |
| 12 | + struct LockedToken { |
| 13 | + address user; |
| 14 | + uint256 amount; |
| 15 | + string sourceChain; |
| 16 | + LockStatus status; |
| 17 | + } |
| 18 | + |
| 19 | + // Mapping to track locked tokens |
| 20 | + mapping(uint256 => LockedToken) public lockedTokens; |
| 21 | + uint256 public lockedTokenCount; |
| 22 | + |
| 23 | + event TokensUnlocked(address indexed user, uint256 amount); |
| 24 | + event TokensLocked(address indexed user, uint256 amount, string sourceChain, uint256 indexed lockId); |
| 25 | + event LockStatusUpdated(uint256 indexed lockId, LockStatus status); |
| 26 | + |
| 27 | + constructor(address _token) { |
| 28 | + require(_token != address(0), "Token address cannot be zero"); |
| 29 | + token = IERC20(_token); |
| 30 | + } |
| 31 | + |
| 32 | + // Function to unlock tokens on BSC (to be called by the bridge) |
| 33 | + function unlockTokens(address user, uint256 amount) external onlyOwner { |
| 34 | + require(amount > 0, "Amount must be greater than zero"); |
| 35 | + token.transfer(user, amount); |
| 36 | + emit TokensUnlocked(user, amount); |
| 37 | + } |
| 38 | + |
| 39 | + // Function to lock tokens (to be called by the Ethereum bridge) |
| 40 | + function lockTokens(uint256 amount, string memory sourceChain) external { |
| 41 | + require(amount > 0, "Amount must be greater than zero"); |
| 42 | + require(token.balanceOf(msg.sender) >= amount, "Insufficient balance"); |
| 43 | + |
| 44 | + // Transfer tokens to the contract |
| 45 | + token.transferFrom(msg.sender, address(this), amount); |
| 46 | + |
| 47 | + // Record the locked token |
| 48 | + lockedTokens[lockedTokenCount] = LockedToken({ |
| 49 | + user: msg.sender, |
| 50 | + amount: amount, |
| 51 | + sourceChain: sourceChain, |
| 52 | + status: LockStatus.Pending |
| 53 | + }); |
| 54 | + |
| 55 | + emit TokensLocked(msg.sender, amount, sourceChain, lockedTokenCount); |
| 56 | + lockedTokenCount++; |
| 57 | + } |
| 58 | + |
| 59 | + // Function to update the status of a locked token (to be called by the bridge) |
| 60 | + function updateLockStatus(uint256 lockId, LockStatus status) external onlyOwner { |
| 61 | + require(lockId < lockedTokenCount, "Invalid lock ID"); |
| 62 | + lockedTokens[lockId].status = status; |
| 63 | + emit LockStatusUpdated(lockId, status); |
| 64 | + } |
| 65 | + |
| 66 | + // Function to get the details of a locked token |
| 67 | + function getLockedToken(uint256 lockId) external view returns (address user, uint256 amount, string memory sourceChain, LockStatus status) { |
| 68 | + require(lockId < lockedTokenCount, "Invalid lock ID"); |
| 69 | + LockedToken storage lockedToken = lockedTokens[lockId]; |
| 70 | + return (lockedToken.user, lockedToken.amount, lockedToken.sourceChain, lockedToken.status); |
| 71 | + } |
| 72 | + |
| 73 | + // Function to get the balance of the contract |
| 74 | + function getBalance() external view returns (uint256) { |
| 75 | + return token.balanceOf(address(this)); |
| 76 | + } |
| 77 | +} |
0 commit comments