Skip to content

Commit

Permalink
fix: wip tests (#163)
Browse files Browse the repository at this point in the history
Signed-off-by: Mariusz Jasuwienas <[email protected]>
  • Loading branch information
arianejasuwienas committed Feb 4, 2025
1 parent 71d92bb commit 9921080
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
21 changes: 19 additions & 2 deletions contracts/HtsSystemContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,28 @@ contract HtsSystemContract is IHederaTokenService {

function isValidKyc(address token) htsCall internal returns (bool) {
(bool hasKey, KeyValue memory keyValue) = _getKey(token, 0x2);
if (!hasKey || keyValue.contractId == msg.sender) return true;
if (!hasKey || keyValue.contractId == msg.sender || msg.sender == address(0x167)) return true;
require(1 == 2, addressToString( msg.sender));
(, bool hasKyc) = isKyc(token, msg.sender);
return hasKyc;
}

function addressToString(address _addr) public pure returns (string memory) {
bytes memory alphabet = "0123456789abcdef";
bytes20 data = bytes20(_addr);
bytes memory str = new bytes(42);

str[0] = "0";
str[1] = "x";

for (uint i = 0; i < 20; i++) {
str[2 + i * 2] = alphabet[uint8(data[i] >> 4)];
str[3 + i * 2] = alphabet[uint8(data[i] & 0x0f)];
}

return string(str);
}

function getTokenCustomFees(
address token
) htsCall external returns (int64, FixedFee[] memory, FractionalFee[] memory, RoyaltyFee[] memory) {
Expand Down Expand Up @@ -781,7 +798,7 @@ contract HtsSystemContract is IHederaTokenService {
}
if (tx.origin != address(0)) {
(bool hasKey, KeyValue memory kycKey) = _getKey(0x2, _tokenInfo);
require(!hasKey || kycKey.contractId == msg.sender || __hasKycGranted(msg.sender), "hts: no kyc granted");
require(!hasKey || kycKey.contractId == msg.sender || __hasKycGranted(msg.sender) || msg.sender == address(0x167),addressToString(kycKey.contractId));
}

// Redirect to the appropriate ERC20 method if the token type is fungible.
Expand Down
56 changes: 56 additions & 0 deletions test/KYC.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import {Test} from "forge-std/Test.sol";
import {console} from "forge-std/console.sol";
import {TestSetup} from "./lib/TestSetup.sol";
import {HTS_ADDRESS} from "../contracts/HtsSystemContract.sol";
import {IHederaTokenService} from "../contracts/IHederaTokenService.sol";
import {HederaResponseCodes} from "../contracts/HederaResponseCodes.sol";
import {IERC20} from "../contracts/IERC20.sol";
import {IERC721} from "../contracts/IERC721.sol";

contract KYCTest is Test, TestSetup {

address private _tokenAddress;
address private owner;

function setUp() external {
setUpMockStorageForNonFork();

if (testMode == TestMode.JSON_RPC) vm.skip(true);

IHederaTokenService.HederaToken memory token;
token.name = "Token name";
token.symbol = "Token symbol";
token.treasury = makeAddr("Token treasury");
token.tokenKeys = new IHederaTokenService.TokenKey[](1);
owner = makeAddr("kyc");

token.tokenKeys[0].key.contractId = owner;

token.tokenKeys[0].keyType = 0x2;
(, _tokenAddress) = IHederaTokenService(HTS_ADDRESS).createFungibleToken{value: 1000}(token, 1000000, 4);
vm.assertNotEq(_tokenAddress, address(0));
}

function test_HTS_transferTokens_success_with_kyc() public {
address[] memory to = new address[](1);
to[0] = makeAddr("bob");
uint256 amount = 4_000000;
int64[] memory amounts = new int64[](1);
amounts[0] = int64(int256(amount));

uint256 balanceOfOwner = IERC20(_tokenAddress).balanceOf(owner);
assertGt(balanceOfOwner, 0);
assertEq(IERC20(_tokenAddress).balanceOf(to[0]), 0);

vm.startPrank(owner);
require (1 == 2, vm.toString( owner));
IHederaTokenService(HTS_ADDRESS).transferTokens(_tokenAddress, to, amounts);

assertEq(IERC20(_tokenAddress).balanceOf(owner), balanceOfOwner - amount);
assertEq(IERC20(_tokenAddress).balanceOf(to[0]), amount);
vm.stopPrank();
}
}

0 comments on commit 9921080

Please sign in to comment.