Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.7 kB
4
Indexable
Never
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.10;

import {DSTestPlus} from "./utils/DSTestPlus.sol";
import {DSInvariantTest} from "./utils/DSInvariantTest.sol";
import {MockERC20} from "./utils/mocks/MockERC20.sol";

import "../src/Exchange.sol";

contract ExchangeTest is DSTestPlus {
    Exchange internal exchange;

    address internal owner;
    uint256 internal ownerPrivateKey;
    address internal userA;
    uint256 internal userAPrivateKey;
    address internal userB;
    uint256 internal userBPrivateKey;
    address internal userC;
    uint256 internal userCPrivateKey;
    uint256 internal transferringTrustedPrivateKey;
    uint256 internal transferringTrustedSignerPrivateKey;
    uint256 internal withdrawCommissionSignerPrivateKey;

    MockERC20 internal token;
    uint256 internal amount;

    bytes32 internal transferringMessageHash;
    bytes internal transferringSignature;

    bytes32 internal withdrawCommissionMessageHash;
    bytes internal withdrawCommissionSignature;

    function setUp() public {
        amount = 1000 ether;

        ownerPrivateKey = 10;
        owner = hevm.addr(ownerPrivateKey);
        userAPrivateKey = 15;
        userA = hevm.addr(userAPrivateKey);
        userBPrivateKey = 16;
        userB = hevm.addr(userBPrivateKey);
        userCPrivateKey = 17;
        userC = hevm.addr(userCPrivateKey);

        transferringTrustedPrivateKey = 20;
        transferringTrustedSignerPrivateKey = 30;
        withdrawCommissionSignerPrivateKey = 40;

        token = new MockERC20("Token", "TKN", 18);
        token.mint(userA, amount);

        hevm.prank(owner);
        exchange = new Exchange();
        hevm.prank(userA);
        token.approve(address(exchange), amount);

        token.mint(address(exchange), amount);

        hevm.startPrank(owner);
        exchange.setTransferringTrustedSignerAddress(hevm.addr(transferringTrustedSignerPrivateKey));
        exchange.setTransferringTrustedAddress(hevm.addr(transferringTrustedPrivateKey));
        exchange.setTransferThresholdAmount(address(token), amount);
        hevm.stopPrank();

        //
        transferringMessageHash = keccak256(
            abi.encodePacked(
                [address(token)], [userB], [amount], exchange.getNonce(hevm.addr(transferringTrustedSignerPrivateKey))
            )
        );
        hevm.startPrank(hevm.addr(transferringTrustedSignerPrivateKey));
        bytes32 digest = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", transferringMessageHash));
        (uint8 v, bytes32 r, bytes32 s) = hevm.sign(transferringTrustedSignerPrivateKey, digest);
        transferringSignature = abi.encodePacked(r, s, v);
        hevm.stopPrank();

        //
        hevm.startPrank(hevm.addr(withdrawCommissionSignerPrivateKey));
        withdrawCommissionMessageHash = keccak256(
            abi.encode(
                exchange._TYPEHASH,
                hevm.addr(withdrawCommissionSignerPrivateKey),
                address(token),
                userC,
                amount,
                exchange.getNonce(hevm.addr(withdrawCommissionSignerPrivateKey)),
                1 days
            )
        );
        bytes32 digest1 =
            keccak256(abi.encodePacked("\x19\x01", exchange.DOMAIN_SEPARATOR(), withdrawCommissionMessageHash));
        (uint8 v1, bytes32 r1, bytes32 s1) = hevm.sign(withdrawCommissionSignerPrivateKey, digest1);
        withdrawCommissionSignature = abi.encodePacked(r1, s1, v1);
        hevm.stopPrank();
    }

    function testDeposit() public {
        hevm.prank(userA);
        exchange.deposit(address(token), amount);
        assertEq(token.balanceOf(userA), 0);
    }