require(
    grantorVesting.totalAmount - grantorVesting.amountClaimed >=
        _amount,
    "SS_StepVesting: insufficient balance"
);
grantorVesting.totalAmount -= _amount;
grantorVesting.releaseRate = grantorVesting.totalAmount / numOfSteps;
_createVesting(
    _beneficiary,
    _amount,
    grantorVesting.stepsClaimed,
    true
);
import "lib/forge-std/src/Test.sol";
import "lib/forge-std/src/console2.sol";
import {SecondSwap_Marketplace} from "../contracts/SecondSwap_Marketplace.sol";
import {SecondSwap_MarketplaceSetting} from "../contracts/SecondSwap_MarketplaceSetting.sol";
import {SecondSwap_StepVesting} from "../contracts/SecondSwap_StepVesting.sol";
import {SecondSwap_VestingDeployer} from "../contracts/SecondSwap_VestingDeployer.sol";
import {SecondSwap_VestingManager} from "../contracts/SecondSwap_VestingManager.sol";
import {TestToken1} from "../contracts/USDT.sol";

contract sl1Test is Test {
    SecondSwap_Marketplace public marketplace;
    SecondSwap_MarketplaceSetting public marketplaceSettings;
    SecondSwap_VestingDeployer public vestingDeployer;
    SecondSwap_VestingManager public vestingManager;
    TestToken1 public USDT;

    address admin = makeAddr("admin");
    address whitelist;

    function setUp() public {
        vm.startPrank(admin);
        USDT = new TestToken1();
        vestingManager = new SecondSwap_VestingManager();
        vestingManager.initialize(admin);
        marketplaceSettings = new SecondSwap_MarketplaceSetting(
            admin,
            admin,
            whitelist,
            address(vestingManager),
            address(USDT)
        );
        marketplace = new SecondSwap_Marketplace();
        marketplace.initialize(address(USDT), address(marketplaceSettings));
        vestingDeployer = new SecondSwap_VestingDeployer();
        vestingDeployer.initialize(admin, address(vestingManager));

        vestingManager.setVestingDeployer(address(vestingDeployer));
        vestingManager.setMarketplace(address(marketplace));
        vm.stopPrank();
    }

    function test_sl1TokenIssuerCanTransferAlreadyVestedTokens() public {
        address alice = makeAddr("alice");
        address bob = makeAddr("bob");

        vm.startPrank(admin);
        vestingDeployer.setTokenOwner(address(USDT), admin);
        vestingDeployer.deployVesting(
            address(USDT),
            block.timestamp,
            block.timestamp + 10000,
            10000,
            "1"
        );
        // Got the address by console logging it in deployVesting func
        address stepVesting = 0xD478411c1478E645A6bb53209E689080aE5101A1;

        USDT.approve(stepVesting, 10000e18);
        vestingDeployer.createVesting(alice, 10000e18, stepVesting);
        vm.stopPrank();

        // half of the vesting schedule has passed
        vm.warp(block.timestamp + 5000);

        uint256 snapshot = vm.snapshot();

        assertEq(USDT.balanceOf(alice), 0);
        vm.prank(alice);
        SecondSwap_StepVesting(stepVesting).claim();
        // alice is able to claim 5000 tokens
        assertEq(USDT.balanceOf(alice), 5000e18);

        vm.revertTo(snapshot);

        vm.prank(admin);
        // Before alice was able to claim her 5000 vested tokens, 5000 tokens are transferred from her
        vestingDeployer.transferVesting(alice, bob, 5000e18, stepVesting, "1");

        vm.prank(alice);
        SecondSwap_StepVesting(stepVesting).claim();
        // Even though alice has already vested 5000 tokens, now she is only able to claim 2500
        assertEq(USDT.balanceOf(alice), 2500e18);
    }
