From f3c41fb674378e57664b239207c88db348ffbdc8 Mon Sep 17 00:00:00 2001 From: Mariusz Jasuwienas Date: Tue, 14 Jan 2025 11:10:28 +0100 Subject: [PATCH] feat: deploy token (#151) Signed-off-by: Mariusz Jasuwienas --- contracts/HRC719TokenProxy.sol | 26 ++++++++++++++++++++++++++ contracts/HtsSystemContract.sol | 24 ++++++++++++++++++++++++ contracts/IHederaTokenService.sol | 10 +++++----- 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 contracts/HRC719TokenProxy.sol diff --git a/contracts/HRC719TokenProxy.sol b/contracts/HRC719TokenProxy.sol new file mode 100644 index 0000000..ac72b3a --- /dev/null +++ b/contracts/HRC719TokenProxy.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.8.0; + +import {IHederaTokenService} from "./IHederaTokenService.sol"; + +/// @dev HTS Token Proxy contract defined in HIP-719, Specification section. +/// +/// For more information, +/// see https://github.com/hashgraph/hedera-smart-contracts/issues/885. +contract HRC719TokenProxy { + fallback() external payable { + address tokenAddress = address(this); + assembly { + let precompileAddress := 0x167 + let dynamicData := sload(tokenAddress) + mstore(0, dynamicData) + calldatacopy(32, 0, calldatasize()) + let result := call(gas(), precompileAddress, 0, 0, add(32, calldatasize()), 0, 0) + let size := returndatasize() + returndatacopy(0, 0, size) + switch result + case 0 { revert(0, size) } + default { return(0, size) } + } + } +} diff --git a/contracts/HtsSystemContract.sol b/contracts/HtsSystemContract.sol index e0a0ad9..c402933 100644 --- a/contracts/HtsSystemContract.sol +++ b/contracts/HtsSystemContract.sol @@ -6,6 +6,7 @@ import {IERC721, IERC721Events} from "./IERC721.sol"; import {IHRC719} from "./IHRC719.sol"; import {IHederaTokenService} from "./IHederaTokenService.sol"; import {IERC165} from "../lib/forge-std/src/interfaces/IERC165.sol"; +import {HRC719TokenProxy} from "./HRC719TokenProxy.sol"; address constant HTS_ADDRESS = address(0x167); @@ -427,6 +428,29 @@ contract HtsSystemContract is IHederaTokenService, IERC20Events, IERC721Events { return __redirectForToken(); } + function createFungibleToken( + HederaToken memory token, + int64 initialTotalSupply, + int32 decimals + ) htsCall external payable returns (int64 responseCode, address tokenAddress) { + HRC719TokenProxy deployed = new HRC719TokenProxy(); + tokenAddress = address(token); + HtsSystemContract(tokenAddress) + .initTokenAsHts(token, initialTotalSupply, decimals); + responseCode = 22; + } + + function initTokenAsHts(IHederaTokenService.HederaToken memory token, int64 initialTotalSupply, int32 decimals) public { + require(msg.sender == address(0x167)); + tokenType = "FUNGIBLE_COMMON"; + name = token.name; + symbol = token.symbol; + decimals = decimals; + totalSupply = uint256(uint64(totalSupply)); + _tokenInfo.totalSupply = initialTotalSupply; + _tokenInfo.token = token; + } + /** * @dev Addresses are word right-padded starting from memory position `28`. */ diff --git a/contracts/IHederaTokenService.sol b/contracts/IHederaTokenService.sol index e33a610..db82980 100644 --- a/contracts/IHederaTokenService.sol +++ b/contracts/IHederaTokenService.sol @@ -410,11 +410,11 @@ interface IHederaTokenService { /// @param decimals the number of decimal places a token is divisible by /// @return responseCode The response code for the status of the request. SUCCESS is 22. /// @return tokenAddress the created token's address - // function createFungibleToken( - // HederaToken memory token, - // int64 initialTotalSupply, - // int32 decimals - // ) external payable returns (int64 responseCode, address tokenAddress); + function createFungibleToken( + HederaToken memory token, + int64 initialTotalSupply, + int32 decimals + ) external payable returns (int64 responseCode, address tokenAddress); /// Creates a Fungible Token with the specified properties /// @param token the basic properties of the token being created