Skip to content

Instantly share code, notes, and snippets.

@srivatsa31
Forked from Chmarusso/DamnSimpleNftStake.sol
Created March 14, 2022 10:30
Show Gist options
  • Save srivatsa31/4037f1d8ad11c4238e83bf55915d1b36 to your computer and use it in GitHub Desktop.
Save srivatsa31/4037f1d8ad11c4238e83bf55915d1b36 to your computer and use it in GitHub Desktop.

Revisions

  1. @Chmarusso Chmarusso created this gist Feb 13, 2022.
    47 changes: 47 additions & 0 deletions DamnSimpleNftStake.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.7;

    import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
    import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Receiver.sol";

    contract NftStaker {
    IERC1155 public parentNFT;

    struct Stake {
    uint256 tokenId;
    uint256 amount;
    uint256 timestamp;
    }

    // map staker address to stake details
    mapping(address => Stake) public stakes;

    // map staker to total staking time
    mapping(address => uint256) public stakingTime;

    constructor() {
    parentNFT = IERC1155(0xd9145CCE52D386f254917e481eB44e9943F39138); // Change it to your NFT contract addr
    }

    function stake(uint256 _tokenId, uint256 _amount) public {
    stakes[msg.sender] = Stake(_tokenId, _amount, block.timestamp);
    parentNFT.safeTransferFrom(msg.sender, address(this), _tokenId, _amount, "0x00");
    }

    function unstake() public {
    parentNFT.safeTransferFrom(address(this), msg.sender, stakes[msg.sender].tokenId, stakes[msg.sender].amount, "0x00");
    stakingTime[msg.sender] += (block.timestamp - stakes[msg.sender].timestamp);
    delete stakes[msg.sender];
    }

    function onERC1155Received(
    address operator,
    address from,
    uint256 id,
    uint256 value,
    bytes calldata data
    ) external returns (bytes4) {
    return bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"));
    }

    }