Skip to content

Commit

Permalink
feat: export Solidity interfaces (#229)
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Mastrangelo <[email protected]>
  • Loading branch information
acuarica authored Feb 5, 2025
1 parent 2e3bd9b commit 0850f4c
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 275 deletions.
16 changes: 8 additions & 8 deletions contracts/HtsSystemContract.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import {IERC20Events, IERC20} from "./IERC20.sol";
import {IERC721, IERC721Events} from "./IERC721.sol";
import {IERC20} from "./IERC20.sol";
import {IERC721} from "./IERC721.sol";
import {IHRC719} from "./IHRC719.sol";
import {IHederaTokenService} from "./IHederaTokenService.sol";
import {HederaResponseCodes} from "./HederaResponseCodes.sol";
Expand Down Expand Up @@ -574,7 +574,7 @@ contract HtsSystemContract is IHederaTokenService {
address to = address(bytes20(msg.data[72:92]));
uint256 amount = uint256(bytes32(msg.data[92:124]));
_approve(from, to, amount);
emit IERC20Events.Approval(from, to, amount);
emit IERC20.Approval(from, to, amount);
return abi.encode(true);
}
if (selector == this.approveNFT.selector) {
Expand Down Expand Up @@ -670,7 +670,7 @@ contract HtsSystemContract is IHederaTokenService {
uint256 amount = uint256(bytes32(msg.data[60:92]));
address owner = msg.sender;
_approve(owner, spender, amount);
emit IERC20Events.Approval(owner, spender, amount);
emit IERC20.Approval(owner, spender, amount);
return abi.encode(true);
}
return _redirectForHRC719(selector);
Expand Down Expand Up @@ -844,7 +844,7 @@ contract HtsSystemContract is IHederaTokenService {
require(from != address(0), "hts: invalid sender");
require(to != address(0), "hts: invalid receiver");
_update(from, to, amount);
emit IERC20Events.Transfer(from, to, amount);
emit IERC20.Transfer(from, to, amount);
}

function _transferNFT(address sender, address from, address to, uint256 serialId) private {
Expand Down Expand Up @@ -872,7 +872,7 @@ contract HtsSystemContract is IHederaTokenService {

// Set the new owner
assembly { sstore(slot, to) }
emit IERC721Events.Transfer(from, to, serialId);
emit IERC721.Transfer(from, to, serialId);
}

function _update(address from, address to, uint256 amount) public {
Expand Down Expand Up @@ -917,7 +917,7 @@ contract HtsSystemContract is IHederaTokenService {
bytes32 slot = _getApprovedSlot(uint32(serialId));
address newApproved = isApproved ? spender : address(0);
assembly { sstore(slot, newApproved) }
emit IERC721Events.Approval(owner, spender, serialId);
emit IERC721.Approval(owner, spender, serialId);
}

/**
Expand All @@ -938,6 +938,6 @@ contract HtsSystemContract is IHederaTokenService {
require(operator != address(0) && operator != sender, "setApprovalForAll: invalid operator");
bytes32 slot = _isApprovedForAllSlot(sender, operator);
assembly { sstore(slot, approved) }
emit IERC721Events.ApprovalForAll(sender, operator, approved);
emit IERC721.ApprovalForAll(sender, operator, approved);
}
}
24 changes: 12 additions & 12 deletions contracts/IERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@
pragma solidity ^0.8.0;

/**
* These events should be emitted by `transfer|transferFrom` and `approve` respectively.
* @dev Interface of the ERC-20 standard as defined in the ERC.
*
* See https://ethereum.org/en/developers/docs/standards/tokens/erc-20/#events for more information.
* https://hips.hedera.com/hip/hip-218
* https://hips.hedera.com/hip/hip-376
*/
interface IERC20Events {
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*
* This event should be emitted by `transfer` and `transferFrom` methods.
*
* See https://ethereum.org/en/developers/docs/standards/tokens/erc-20/#events for more information.
*/
event Transfer(address indexed from, address indexed to, uint256 amount);

/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*
* This event should be emitted by the `approve` method.
*
* See https://ethereum.org/en/developers/docs/standards/tokens/erc-20/#events for more information.
*/
event Approval(address indexed owner, address indexed spender, uint256 amount);
}

/**
* No need to inherit `IERC20Events` here.
* This interface is used to get the selectors and for testing.
*
* https://hips.hedera.com/hip/hip-218
* https://hips.hedera.com/hip/hip-376
*/
interface IERC20 {
/**
* @dev Returns the name of the token.
*/
Expand Down
27 changes: 16 additions & 11 deletions contracts/IERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
pragma solidity ^0.8.0;

/**
* These events should be emitted by `transfer|transferFrom`, `approve` and `setApprovalForAll` respectively.
* @dev Required interface of an ERC-721 compliant contract.
*
* See https://ethereum.org/en/developers/docs/standards/tokens/erc-721/#events for more information.
* https://hips.hedera.com/hip/hip-218
* https://hips.hedera.com/hip/hip-376
*/
interface IERC721Events {
interface IERC721 {
/**
* @dev Emitted when ownership of any NFT changes by any mechanism.
* This event emits when NFTs are created (`from` == 0) and destroyed (`to` == 0).
Expand All @@ -16,30 +17,34 @@ interface IERC721Events {
* Exception: during contract creation, any number of NFTs may be created and assigned without emitting Transfer.
*
* At the time of any transfer, the approved address for that NFT (if any) is reset to none.
*
* This event should be emitted by `transfer` and `transferFrom` methods.
*
* See https://ethereum.org/en/developers/docs/standards/tokens/erc-721/#events for more information.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

/**
* @dev Emitted when the approved address for an NFT is changed or reaffirmed from {from} to {to} address.
* The zero {to} address indicates there will be no approved address.
* Additionally the approved address for that NFT (if any) is reset to none.
*
* This event should be emitted by the `approve` method.
*
* See https://ethereum.org/en/developers/docs/standards/tokens/erc-721/#events for more information.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

/**
* @dev Emitted when an operator {operator} is enabled or disabled {approved}
* for an owner {owner}. The operator {operator} can than manage all NFTs of the owner {owner}.
*
* This event should be emitted by the `setApprovalForAll` method.
*
* See https://ethereum.org/en/developers/docs/standards/tokens/erc-721/#events for more information.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
}

/**
* This interface is used to get the selectors and for testing.
*
* https://hips.hedera.com/hip/hip-218
* https://hips.hedera.com/hip/hip-376
*/
interface IERC721 {
/**
* @dev Returns the token collection name.
*/
Expand Down
12 changes: 11 additions & 1 deletion examples/foundry-hts/NFT.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import {Test} from "forge-std/Test.sol";
import {Test, console} from "forge-std/Test.sol";
import {htsSetup} from "hedera-forking/contracts/htsSetup.sol";
import {IERC721} from "hedera-forking/contracts/IERC721.sol";

Expand Down Expand Up @@ -30,4 +30,14 @@ contract NFTExampleTest is Test {
function test_dealt_nft_assigned_to_local_account() view external {
assertEq(IERC721(NFT_mainnet).ownerOf(3), user);
}

function test_using_console_log() view external {
string memory name = IERC721(NFT_mainnet).name();
string memory symbol = IERC721(NFT_mainnet).symbol();
string memory tokenURI = IERC721(NFT_mainnet).tokenURI(1);
assertEq(name, "THE BARKANEERS");
assertEq(symbol, "BARKANEERS");
assertEq(tokenURI, "ipfs://bafkreif4hpsgflzzvd7c4abx5u5xwrrjl7wkimbjtndvkxodklxdam5upm");
console.log("name: %s, symbol: %s, tokenURI: %s", name, symbol, tokenURI);
}
}
25 changes: 0 additions & 25 deletions examples/foundry-hts/NFTConsole.t.sol

This file was deleted.

23 changes: 22 additions & 1 deletion examples/foundry-hts/USDC.t.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import {Test} from "forge-std/Test.sol";
import {Test, console} from "forge-std/Test.sol";
import {htsSetup} from "hedera-forking/contracts/htsSetup.sol";
import {IERC20} from "hedera-forking/contracts/IERC20.sol";
import {IHederaTokenService} from "hedera-forking/contracts/IHederaTokenService.sol";
import {HederaResponseCodes} from "hedera-forking/contracts/HederaResponseCodes.sol";

contract USDCExampleTest is Test {
// https://hashscan.io/mainnet/token/0.0.456858
Expand All @@ -28,4 +30,23 @@ contract USDCExampleTest is Test {
function test_dealt_balance_of_local_account() view external {
assertEq(IERC20(USDC_mainnet).balanceOf(user1), 1000 * 10e8);
}

function test_using_console_log() view external {
string memory name = IERC20(USDC_mainnet).name();
string memory symbol = IERC20(USDC_mainnet).symbol();
uint8 decimals = IERC20(USDC_mainnet).decimals();
assertEq(name, "USD Coin");
assertEq(symbol, "USDC");
assertEq(decimals, 6);

console.log("name: %s, symbol: %s, decimals: %d", name, symbol, decimals);
}

function test_HTS_getTokenInfo() external {
(int64 responseCode, IHederaTokenService.TokenInfo memory tokenInfo) = IHederaTokenService(address(0x167)).getTokenInfo(USDC_mainnet);
assertEq(responseCode, HederaResponseCodes.SUCCESS);
assertEq(tokenInfo.token.name, "USD Coin");
assertEq(tokenInfo.token.symbol, "USDC");
assertEq(tokenInfo.token.memo, "USDC HBAR");
}
}
26 changes: 0 additions & 26 deletions examples/foundry-hts/USDCConsole.t.sol

This file was deleted.

2 changes: 1 addition & 1 deletion examples/hardhat-hts/contracts/CallNFT.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import {IERC721} from "./IERC721.sol";
import {IERC721} from "@hashgraph/system-contracts-forking/contracts/IERC721.sol";
import {console} from "hardhat/console.sol";

contract CallNFT {
Expand Down
14 changes: 13 additions & 1 deletion examples/hardhat-hts/contracts/CallToken.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import {IERC20} from "./IERC20.sol";
import {IERC20} from "@hashgraph/system-contracts-forking/contracts/IERC20.sol";
import {IHederaTokenService} from "@hashgraph/system-contracts-forking/contracts/IHederaTokenService.sol";
import {HederaResponseCodes} from "@hashgraph/system-contracts-forking/contracts/HederaResponseCodes.sol";
import {console} from "hardhat/console.sol";

contract CallToken {

event GetTokenInfo(string name, string symbol, string memo);

function getTokenName(address tokenAddress) external view returns (string memory) {
return IERC20(tokenAddress).name();
}
Expand All @@ -15,4 +20,11 @@ contract CallToken {
console.log("Transferring from %s to %s %s tokens", msg.sender, to, amount);
IERC20(tokenAddress).transferFrom(msg.sender, to, amount);
}

function invokeHTSDirectly(address tokenAddress) external {
(int64 responseCode, IHederaTokenService.TokenInfo memory tokenInfo) = IHederaTokenService(address(0x167)).getTokenInfo(tokenAddress);
require(responseCode == HederaResponseCodes.SUCCESS, "HTS call failed");
console.log("getTokenInfo: %s (%s) memo: %s ", tokenInfo.token.name, tokenInfo.token.symbol, tokenInfo.token.memo);
emit GetTokenInfo(tokenInfo.token.name, tokenInfo.token.symbol, tokenInfo.token.memo);
}
}
19 changes: 0 additions & 19 deletions examples/hardhat-hts/contracts/IERC20.sol

This file was deleted.

Loading

0 comments on commit 0850f4c

Please sign in to comment.