Skip to content

TW89SCm6oXHUtqjL9XPKePbxTrjQjif71W #449

@arashebadi38

Description

@arashebadi38

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.10;

interface ITRC20 {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function transfer(address recipient, uint256 amount) external returns (bool);
}

contract TronStake {
ITRC20 public token;
uint256 public rewardPercent = 6150; // 61.5% (ضریب 100 * 61.5)
uint256 public stakingDuration = 30 days;

struct StakeInfo {
    uint256 amount;
    uint256 startTime;
    bool withdrawn;
}

mapping(address => StakeInfo) public stakes;

constructor(address tokenAddress) public {
    token = ITRC20(tokenAddress);
}

function stake(uint256 amount) public {
    require(amount > 0, "Amount must be > 0");
    require(stakes[msg.sender].amount == 0, "Already staking");

    require(token.transferFrom(msg.sender, address(this), amount), "Transfer failed");

    stakes[msg.sender] = StakeInfo(amount, now, false);
}

function withdraw() public {
    StakeInfo storage stakeData = stakes[msg.sender];
    require(stakeData.amount > 0, "No stake found");
    require(!stakeData.withdrawn, "Already withdrawn");
    require(now >= stakeData.startTime + stakingDuration, "Staking period not finished");

    uint256 reward = stakeData.amount * rewardPercent / 10000;
    stakeData.withdrawn = true;

    require(token.transfer(msg.sender, stakeData.amount + reward), "Transfer failed");
}

function emergencyWithdraw() public {
    StakeInfo storage stakeData = stakes[msg.sender];
    require(stakeData.amount > 0, "No stake found");
    require(!stakeData.withdrawn, "Already withdrawn");

    stakeData.withdrawn = true;

    require(token.transfer(msg.sender, stakeData.amount), "Transfer failed");
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions