Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds SafeHTS library #41

Merged
merged 12 commits into from
Aug 29, 2022

Conversation

stoqnkpL
Copy link
Contributor

@stoqnkpL stoqnkpL commented Aug 5, 2022

Adds SafeHTS.sol providing functionality allowing to execute safe operations on the HTS precompiled contract.
Adds SafeOperations.sol which showcases SafeHTS.sol usage.

Related issue(s):

Fixes #43

Notes for reviewer:

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

Signed-off-by: Stoyan Panayotov <[email protected]>
@Nana-EC Nana-EC requested review from lukelee-sl and shemnon August 5, 2022 20:37
@Nana-EC Nana-EC added HTS Hedera Token Service related P2 labels Aug 5, 2022
@MiroslavGatsanoga MiroslavGatsanoga marked this pull request as draft August 17, 2022 14:18
@MiroslavGatsanoga MiroslavGatsanoga self-assigned this Aug 17, 2022
shemnon
shemnon previously approved these changes Aug 18, 2022
Copy link
Contributor

@shemnon shemnon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the address->IHederaTokenService changes that came in the force push need to be undone.

require(responseCode == HederaResponseCodes.SUCCESS, "Safe crypto transfer failed!");
}

function safeMintToken(IHederaTokenService token, uint64 amount, bytes[] memory metadata) internal
Copy link
Contributor

@shemnon shemnon Aug 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should all go back to address. IHederaTokenService is the interface for the precompile, not the type of the tokens. These contracts are untyped.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After going back to address the example usages of the library in SafeOperations.sol are in the form SafeHTS.safe<MethodName> which stutters. Perhaps I can update the library functions so that they don't start with safe to avoid this stuttering in the usage e.g. resulting in SafeHTS.<methodName>. Any thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MiroslavGatsanoga MiroslavGatsanoga marked this pull request as ready for review August 22, 2022 13:48
shemnon
shemnon previously approved these changes Aug 23, 2022
function safeMintToken(address token, uint64 amount, bytes[] memory metadata) internal
returns (uint64 newTotalSupply, int64[] memory serialNumbers) {
int32 responseCode;
(bool success, bytes memory result) = precompileAddress.delegatecall(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we deliberately use delegatetcall here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not added in the commits made by me, but looking at the hedera-services PR that precedes this PR it seems to be fixed there.

I don't see an obvious reason to use delegatecall here so I will change it to call.

(bool success, bytes memory result) = precompileAddress.call(
abi.encodeWithSelector(IHederaTokenService.setApprovalForAll.selector, token, operator, approved));
responseCode = success ? abi.decode(result, (int32)) : HederaResponseCodes.UNKNOWN;
require(responseCode == HederaResponseCodes.SUCCESS, "Safe set token approval failed!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message could be changed to: "Safe set token approval for all failed!"

(bool success, bytes memory result) = precompileAddress.call(
abi.encodeWithSelector(IHederaTokenService.isApprovedForAll.selector, token, owner, operator));
(responseCode, approved) = success ? abi.decode(result, (int32, bool)) : (HederaResponseCodes.UNKNOWN, false);
require(responseCode == HederaResponseCodes.SUCCESS, "Safe query for if approved for all failed!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safe is approved for all failed!

(bool success, bytes memory result) = precompileAddress.call(
abi.encodeWithSelector(IHederaTokenService.updateTokenInfo.selector, token, tokenInfo));
responseCode = success ? abi.decode(result, (int32)) : HederaResponseCodes.UNKNOWN;
require(responseCode == HederaResponseCodes.SUCCESS, "Safe update token info operation failed!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safe update token info failed!

(bool success, bytes memory result) = precompileAddress.call(
abi.encodeWithSelector(IHederaTokenService.updateTokenExpiryInfo.selector, token, expiryInfo));
responseCode = success ? abi.decode(result, (int32)) : HederaResponseCodes.UNKNOWN;
require(responseCode == HederaResponseCodes.SUCCESS, "Safe update token expiry info operation failed!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safe update token expiry info failed!

(bool success, bytes memory result) = precompileAddress.call(
abi.encodeWithSelector(IHederaTokenService.updateTokenKeys.selector, token, keys));
responseCode = success ? abi.decode(result, (int32)) : HederaResponseCodes.UNKNOWN;
require(responseCode == HederaResponseCodes.SUCCESS, "Safe update token keys operation failed!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safe update token keys failed!

(bool success, bytes memory result) = precompileAddress.call(
abi.encodeWithSelector(IHederaTokenService.isToken.selector, token));
(responseCode, isToken) = success ? abi.decode(result, (int32, bool)) : (HederaResponseCodes.UNKNOWN, false);
require(responseCode == HederaResponseCodes.SUCCESS, "Safe query for if valid token at address failed!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safe is token failed!

(bool success, bytes memory result) = precompileAddress.call(
abi.encodeWithSelector(IHederaTokenService.getTokenType.selector, token));
(responseCode, tokenType) = success ? abi.decode(result, (int32, int32)) : (HederaResponseCodes.UNKNOWN, int32(0));
require(responseCode == HederaResponseCodes.SUCCESS, "Safe query for token type failed!");
Copy link

@IvanKavaldzhiev IvanKavaldzhiev Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safe get token type failed!


contract SafeOperations {

function safeTokenAssociate(address sender, address tokenAddress) external {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep the same names for all methods as they are in the SafeHTS library, e.g. safeAssociateToken

Signed-off-by: Miroslav Gatsanoga <[email protected]>
Copy link

@IvanKavaldzhiev IvanKavaldzhiev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

shemnon
shemnon previously approved these changes Aug 25, 2022
@@ -435,7 +435,7 @@ library SafeHTS {
(bool success, bytes memory result) = precompileAddress.call(
abi.encodeWithSelector(IHederaTokenService.isToken.selector, token));
(responseCode, isToken) = success ? abi.decode(result, (int32, bool)) : (HederaResponseCodes.UNKNOWN, false);
require(responseCode == HederaResponseCodes.SUCCESS, "Safe query for if valid token at address failed!");
require(responseCode == HederaResponseCodes.SUCCESS, "Safe is token failed!");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads weird. While all the other variants split out the words I think we should consider keeping this camelCase - Safe isToken failed! or Safe 'is token' failed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Nana-EC
Nana-EC previously approved these changes Aug 25, 2022
Signed-off-by: Miroslav Gatsanoga <[email protected]>
Copy link

@IvanKavaldzhiev IvanKavaldzhiev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MiroslavGatsanoga MiroslavGatsanoga merged commit 2955081 into hashgraph:main Aug 29, 2022
@MiroslavGatsanoga MiroslavGatsanoga deleted the add-safe-hts-library branch August 29, 2022 07:27
@Nana-EC Nana-EC added this to the 0.2.0 milestone Oct 11, 2022
@Nana-EC Nana-EC added the enhancement New feature or request label Oct 12, 2022
georg-getz pushed a commit to georg-getz/hedera-smart-contracts that referenced this pull request Dec 12, 2022
* Adds SafeHTS library

Signed-off-by: Stoyan Panayotov <[email protected]>

* Use IHederaTokenService instead of IHTS

Signed-off-by: Miroslav Gatsanoga <[email protected]>

* Update solidity pragma in SafeOperations

Signed-off-by: Miroslav Gatsanoga <[email protected]>

* Add HIP-514 functions to SafeHTS library

Signed-off-by: Miroslav Gatsanoga <[email protected]>

* Use address instead of IHederaTokenService for token params

Signed-off-by: Miroslav Gatsanoga <[email protected]>

* Delete unused IHTS.sol

Signed-off-by: Miroslav Gatsanoga <[email protected]>

* Update SafeOperations.sol

Signed-off-by: Miroslav Gatsanoga <[email protected]>

* Add HIP-514 functions to SafeOperations.sol

Signed-off-by: Miroslav Gatsanoga <[email protected]>

* Improve error messages

Signed-off-by: Miroslav Gatsanoga <[email protected]>

* Use same method names in SafeOperations as in SafeHTS

Signed-off-by: Miroslav Gatsanoga <[email protected]>

* Use call instead of delegatecall in safeMintToken

Signed-off-by: Miroslav Gatsanoga <[email protected]>

* Update safeIsToken error message

Signed-off-by: Miroslav Gatsanoga <[email protected]>

Signed-off-by: Stoyan Panayotov <[email protected]>
Signed-off-by: Miroslav Gatsanoga <[email protected]>
Co-authored-by: Miroslav Gatsanoga <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request HTS Hedera Token Service related P2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add new functions from HIP-514 to the SafeHTS library
5 participants