Skip to content

Commit

Permalink
feat: storing keys in memory (#197)
Browse files Browse the repository at this point in the history
Signed-off-by: Mariusz Jasuwienas <[email protected]>
  • Loading branch information
arianejasuwienas committed Feb 12, 2025
1 parent 6b92bf0 commit 5f561c5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions contracts/HtsSystemContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,13 @@ contract HtsSystemContract is IHederaTokenService {

function grantTokenKyc(address token, address account) htsCall
notFrozen(token) kyc(token) notPaused(token) external returns (int64 responseCode) {
require(getKeyOwner(token, 0x2) != address(0), "grantTokenKyc: Only allowed for kyc tokens");
require(getKeyOwner(token, 0x2) != address(0), "grantTokenKyc: only allowed for kyc tokens");
responseCode = IHederaTokenService(token).grantTokenKyc(token, account);
}

function revokeTokenKyc(address token, address account) htsCall
notFrozen(token) kyc(token) notPaused(token) external returns (int64 responseCode) {
require(getKeyOwner(token, 0x2) != address(0), "revokeTokenKyc: Only allowed for kyc tokens");
require(getKeyOwner(token, 0x2) != address(0), "revokeTokenKyc: only allowed for kyc tokens");
responseCode = IHederaTokenService(token).revokeTokenKyc(token, account);
}

Expand Down
11 changes: 7 additions & 4 deletions test/Freeze.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 {HtsSystemContract, HTS_ADDRESS} from "../contracts/HtsSystemContract.sol";
import {IHederaTokenService} from "../contracts/IHederaTokenService.sol";
import {HederaResponseCodes} from "../contracts/HederaResponseCodes.sol";
import {IERC20} from "../contracts/IERC20.sol";
Expand All @@ -28,12 +28,15 @@ contract FreezeTest is Test, TestSetup {
hederaToken.treasury = makeAddr("Token treasury");
hederaToken.tokenKeys = new IHederaTokenService.TokenKey[](1);
owner = makeAddr("pause");

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

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

vm.mockCall(
token,
abi.encodeWithSelector(HtsSystemContract.getKeyOwner.selector, token, 0x4),
abi.encode(owner)
);
to = makeAddr("bob");
}

Expand Down
19 changes: 13 additions & 6 deletions test/HTS.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ contract HTSTest is Test, TestSetup {
IHederaTokenService(HTS_ADDRESS).mintToken(token, amount, metadata);
}

function test_mintToken_should_fail_with_np_supplier() external {
function test_mintToken_should_fail_with_no_supplyKey() external {
address token = USDC;
int64 amount = 1000;
bytes[] memory metadata = new bytes[](0);
Expand All @@ -255,7 +255,7 @@ contract HTSTest is Test, TestSetup {
tokenInfo.token = IHederaTokenService.HederaToken(
"USD Coin",
"USDC",
address(0),
msg.sender,
"USDC HBAR",
false,
initialTotalSupply + amount,
Expand All @@ -269,6 +269,11 @@ contract HTSTest is Test, TestSetup {
abi.encode(IHederaTokenService.getTokenInfo.selector),
abi.encode(HederaResponseCodes.SUCCESS, tokenInfo)
);
vm.mockCall(
token,
abi.encodeWithSelector(HtsSystemContract.getKeyOwner.selector, token, 0x10),
abi.encode(address(0))
);
(int64 code, , ) = IHederaTokenService(HTS_ADDRESS).mintToken(token, amount, metadata);
assertEq(code, HederaResponseCodes.TOKEN_HAS_NO_SUPPLY_KEY);
}
Expand Down Expand Up @@ -320,9 +325,6 @@ contract HTSTest is Test, TestSetup {
int64 initialTotalSupply = 5000;
int64[] memory serialNumbers = new int64[](0);
IHederaTokenService.TokenInfo memory tokenInfo;
IHederaTokenService.TokenKey[] memory keys = new IHederaTokenService.TokenKey[](1);
keys[0].keyType = 0x10; // Supply key
keys[0].key.contractId = msg.sender;
tokenInfo.token = IHederaTokenService.HederaToken(
"My Crypto Token is the name which the string length is greater than 31",
"Token symbol must be exactly 32!",
Expand All @@ -331,7 +333,7 @@ contract HTSTest is Test, TestSetup {
false,
initialTotalSupply + amount,
false,
keys,
new IHederaTokenService.TokenKey[](0),
IHederaTokenService.Expiry(0, address(0), 0)
);

Expand All @@ -340,6 +342,11 @@ contract HTSTest is Test, TestSetup {
abi.encode(IHederaTokenService.getTokenInfo.selector),
abi.encode(HederaResponseCodes.SUCCESS, tokenInfo)
);
vm.mockCall(
token,
abi.encodeWithSelector(HtsSystemContract.getKeyOwner.selector, MFCT, 0x10),
abi.encode(msg.sender)
);
vm.expectRevert(bytes("burnToken: invalid account"));
IHederaTokenService(HTS_ADDRESS).burnToken(token, amount, serialNumbers);
}
Expand Down
18 changes: 6 additions & 12 deletions test/KYC.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 {HtsSystemContract, HTS_ADDRESS} from "../contracts/HtsSystemContract.sol";
import {IHederaTokenService} from "../contracts/IHederaTokenService.sol";
import {HederaResponseCodes} from "../contracts/HederaResponseCodes.sol";
import {IERC20} from "../contracts/IERC20.sol";
Expand All @@ -28,13 +28,15 @@ contract KYCTest is Test, TestSetup {
hederaToken.treasury = makeAddr("Token treasury");
hederaToken.tokenKeys = new IHederaTokenService.TokenKey[](1);
owner = makeAddr("kyc");

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

hederaToken.tokenKeys[0].keyType = 0x2;
(, token) = IHederaTokenService(HTS_ADDRESS).createFungibleToken{value: 1000}(hederaToken, 1000000, 4);
vm.assertNotEq(token, address(0));
to = makeAddr("bob");
vm.mockCall(
token,
abi.encodeWithSelector(HtsSystemContract.getKeyOwner.selector, token, 0x2),
abi.encode(owner)
);
}

function test_HTS_transferToken_success_with_kyc() public {
Expand Down Expand Up @@ -63,14 +65,6 @@ contract KYCTest is Test, TestSetup {
IHederaTokenService(HTS_ADDRESS).transferToken(token, from, to, int64(int256(amount)));
}

function test_ERC20_transferToken_failure_without_kyc() public {
address from = makeAddr("from");
deal(token, from, amount);
vm.prank(from);
vm.expectRevert("__redirectForToken: no kyc granted");
IERC20(token).transfer(to, amount);
}

function test_HTS_transferToken_success_with_kyc_granted() public {
address from = makeAddr("from");
deal(token, from, amount);
Expand Down
10 changes: 6 additions & 4 deletions test/Pause.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 {HtsSystemContract, HTS_ADDRESS} from "../contracts/HtsSystemContract.sol";
import {IHederaTokenService} from "../contracts/IHederaTokenService.sol";
import {HederaResponseCodes} from "../contracts/HederaResponseCodes.sol";
import {IERC20} from "../contracts/IERC20.sol";
Expand All @@ -28,13 +28,15 @@ contract PauseTest is Test, TestSetup {
hederaToken.treasury = makeAddr("Token treasury");
hederaToken.tokenKeys = new IHederaTokenService.TokenKey[](1);
owner = makeAddr("pause");

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

hederaToken.tokenKeys[0].keyType = 0x40;
(, token) = IHederaTokenService(HTS_ADDRESS).createFungibleToken{value: 1000}(hederaToken, 1000000, 4);
vm.assertNotEq(token, address(0));
to = makeAddr("bob");
vm.mockCall(
token,
abi.encodeWithSelector(HtsSystemContract.getKeyOwner.selector, token, 0x40),
abi.encode(owner)
);
}

function test_HTS_transferToken_success_when_not_paused() public {
Expand Down

0 comments on commit 5f561c5

Please sign in to comment.