Skip to content

Commit

Permalink
test: hts methods (#151)
Browse files Browse the repository at this point in the history
Signed-off-by: Mariusz Jasuwienas <[email protected]>
  • Loading branch information
arianejasuwienas committed Jan 17, 2025
1 parent 5161bb6 commit bb36af0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
39 changes: 15 additions & 24 deletions contracts/HtsSystemContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ contract HtsSystemContract is IHederaTokenService, IERC20Events, IERC721Events {
return transferToken(token, sender, recipient, int64(int256(amount)));
}

function allowance(address token, address owner, address spender) htsCall external view returns (int64, uint256) {
return (HederaResponseCodes.SUCCESS, IERC20(token).allowance(owner, spender));
}

function approveNFT(
address token,
address approved,
Expand Down Expand Up @@ -235,23 +239,26 @@ contract HtsSystemContract is IHederaTokenService, IERC20Events, IERC721Events {
address token
) htsCall external returns (int64, FixedFee[] memory, FractionalFee[] memory, RoyaltyFee[] memory) {
require(token != address(0), "getTokenCustomFees: invalid token");
return IHederaTokenService(token).getTokenCustomFees(token);
(int64 responseCode, TokenInfo memory tokenInfo) = IHederaTokenService(token).getTokenInfo(token);
return (responseCode, tokenInfo.fixedFees, tokenInfo.fractionalFees, tokenInfo.royaltyFees);
}

function getTokenDefaultFreezeStatus(address token) htsCall external returns (int64, bool) {
require(token != address(0), "getTokenDefaultFreezeStatus: invalid address");
return IHederaTokenService(token).getTokenDefaultFreezeStatus(token);
(int64 responseCode, TokenInfo memory tokenInfo) = IHederaTokenService(token).getTokenInfo(token);
return (responseCode, tokenInfo.token.freezeDefault);
}

function getTokenDefaultKycStatus(address token) htsCall external returns (int64, bool) {
require(token != address(0), "getTokenDefaultKycStatus: invalid address");
return IHederaTokenService(token).getTokenDefaultKycStatus(token);
(int64 responseCode, TokenInfo memory tokenInfo) = IHederaTokenService(token).getTokenInfo(token);
return (responseCode, tokenInfo.defaultKycStatus);
}

function getTokenExpiryInfo(address token) htsCall external returns (int64 responseCode, Expiry memory tokenInfo) {
function getTokenExpiryInfo(address token) htsCall external returns (int64, Expiry memory expiry) {
require(token != address(0), "getTokenExpiryInfo: invalid token");

(responseCode, tokenInfo) = IHederaTokenService(token).getTokenExpiryInfo(token);
(int64 responseCode, TokenInfo memory tokenInfo) = IHederaTokenService(token).getTokenInfo(token);
return (responseCode, tokenInfo.token.expiry);
}

function getFungibleTokenInfo(address token) htsCall external returns (int64, FungibleTokenInfo memory) {
Expand All @@ -266,10 +273,10 @@ contract HtsSystemContract is IHederaTokenService, IERC20Events, IERC721Events {
return (responseCode, fungibleTokenInfo);
}

function getTokenInfo(address token) htsCall external returns (int64 responseCode, TokenInfo memory tokenInfo) {
function getTokenInfo(address token) htsCall external returns (int64, TokenInfo memory) {
require(token != address(0), "getTokenInfo: invalid token");

(responseCode, tokenInfo) = IHederaTokenService(token).getTokenInfo(token);
return IHederaTokenService(token).getTokenInfo(token);
}

function getTokenKey(address token, uint keyType) htsCall external returns (int64, KeyValue memory) {
Expand Down Expand Up @@ -401,22 +408,6 @@ contract HtsSystemContract is IHederaTokenService, IERC20Events, IERC721Events {
require(msg.data.length >= 28, "getTokenInfo: Not enough calldata");
return abi.encode(HederaResponseCodes.SUCCESS, _tokenInfo);
}
if (selector == this.getTokenCustomFees.selector) {
require(msg.data.length >= 28, "getTokenCustomFees: Not enough calldata");
return abi.encode(HederaResponseCodes.SUCCESS, _tokenInfo.fixedFees, _tokenInfo.fractionalFees, _tokenInfo.royaltyFees);
}
if (selector == this.getTokenDefaultKycStatus.selector) {
require(msg.data.length >= 28, "getTokenDefaultKycStatus: Not enough calldata");
return abi.encode(HederaResponseCodes.SUCCESS, _tokenInfo.defaultKycStatus);
}
if (selector == this.getTokenDefaultFreezeStatus.selector) {
require(msg.data.length >= 28, "getTokenDefaultFreezeStatus: Not enough calldata");
return abi.encode(HederaResponseCodes.SUCCESS, _tokenInfo.token.freezeDefault);
}
if (selector == this.getTokenExpiryInfo.selector) {
require(msg.data.length >= 28, "getTokenExpiryInfo: Not enough calldata");
return abi.encode(HederaResponseCodes.SUCCESS, _tokenInfo.token.expiry);
}
if (selector == this.associateToken.selector) {
require(msg.data.length >= 48, "associateToken: Not enough calldata");
address account = address(bytes20(msg.data[40:60]));
Expand Down
10 changes: 5 additions & 5 deletions contracts/IHederaTokenService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,11 @@ interface IHederaTokenService {
/// @param spender the spender of the tokens
/// @return responseCode The response code for the status of the request. SUCCESS is 22.
/// @return allowance The amount which spender is still allowed to withdraw from owner.
// function allowance(
// address token,
// address owner,
// address spender
// ) external returns (int64 responseCode, uint256 allowance);
function allowance(
address token,
address owner,
address spender
) external returns (int64 responseCode, uint256 allowance);

/// Allow or reaffirm the approved address to transfer an NFT the approved address does not own.
/// Only Applicable to NFT Tokens
Expand Down
19 changes: 19 additions & 0 deletions test/HTS.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,25 @@ contract HTSTest is Test, TestSetup {
vm.stopPrank();
}

function test_HTS_allowance_from_remote() external {
// https://hashscan.io/testnet/account/0.0.4233295
address owner = address(0x000000000000000000000000000000000040984F);
// https://hashscan.io/testnet/account/0.0.1335
address spender = 0x0000000000000000000000000000000000000537;
(int64 responseCode, uint256 allowance) = IHederaTokenService(HTS_ADDRESS).allowance(USDC, owner, spender);
assertEq(responseCode, HederaResponseCodes.SUCCESS);
assertEq(allowance, 5_000000);
}

function test_HTS_allowance_empty() external {
// https://hashscan.io/testnet/account/0.0.4233295
address owner = address(0x000000000000000000000000000000000040984F);
address spender = makeAddr("alice");
(int64 responseCode, uint256 allowance) = IHederaTokenService(HTS_ADDRESS).allowance(USDC, owner, spender);
assertEq(responseCode, HederaResponseCodes.SUCCESS);
assertEq(allowance, 0);
}

function test_HTS_approveNFT() external {
address token = CFNFTFF;
address newSpender = makeAddr("NEW_SPENDER");
Expand Down

0 comments on commit bb36af0

Please sign in to comment.