I have been following through the HTS documentation for create and transfer an NFT using a solidity contract here
When I try create the NFT via my smart contract it fails with the error:
CONTRACT_REVERT_EXECUTED
Any ideas on whats wrong?
I have the following Solidity contract:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
import './HederaResponseCodes.sol';
import './IHederaTokenService.sol';
import './HederaTokenService.sol';
import './ExpiryHelper.sol';
import './KeyHelper.sol';
contract NFTCreator is ExpiryHelper, KeyHelper, HederaTokenService {
function createNft(
string memory name,
string memory symbol,
string memory memo,
int64 maxSupply,
int64 autoRenewPeriod
) external payable returns (address){
IHederaTokenService.TokenKey[] memory keys = new IHederaTokenService.TokenKey[](1);
// Set this contract as supply
keys[0] = getSingleKey(KeyType.SUPPLY, KeyValueType.CONTRACT_ID, address(this));
IHederaTokenService.HederaToken memory token;
token.name = name;
token.symbol = symbol;
token.memo = memo;
token.treasury = address(this);
token.tokenSupplyType = true; // set supply to FINITE
token.maxSupply = maxSupply;
token.tokenKeys = keys;
token.freezeDefault = false;
token.expiry = createAutoRenewExpiry(address(this), autoRenewPeriod); // Contract automatically renew by himself
(int responseCode, address createdToken) = HederaTokenService.createNonFungibleToken(token);
if(responseCode != HederaResponseCodes.SUCCESS){
//revert("Failed to create non-fungible token");
}
return createdToken;
}
function mintNft(
address token,
bytes[] memory metadata
) external returns(int64){
(int response, , int64[] memory serial) = HederaTokenService.mintToken(token, 0, metadata);
if(response != HederaResponseCodes.SUCCESS){
revert("Failed to mint non-fungible token");
}
return serial[0];
}
function transferNft(
address token,
address receiver,
int64 serial
) external returns(int){
HederaTokenService.associateToken(receiver, token);
int response = HederaTokenService.transferNFT(token, address(this), receiver, serial);
if(response != HederaResponseCodes.SUCCESS){
revert("Failed to transfer non-fungible token");
}
return response;
}
function getValue() public pure returns(uint256){
return 100;
}
}
Which is deployed to the testnet.
I then run the following script to mint an NFT against it:
require("dotenv").config();
const {
AccountId,
PrivateKey,
Client,
FileCreateTransaction,
FileAppendTransaction,
ContractCreateTransaction,
ContractFunctionParameters,
ContractExecuteTransaction,
ContractCallQuery,
Hbar,
TokenId
} = require("@hashgraph/sdk");
const fs = require("fs");
// Configure accounts and client
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PRIVATE_KEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
async function main() {
const contractId="0.0.XXXXXXX";
// Create NFT from precompile
const createToken = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(300000) // Increase if revert
.setPayableAmount(20) // Increase if revert
.setFunction("createNft",
new ContractFunctionParameters()
.addString("MY NFT") // NFT name
.addString("SYMBOL") // NFT symbol
.addString("Just a memo") // NFT memo
.addInt64(100) // NFT max supply
.addUint32(7000000) // Expiration: Needs to be between 6999999 and 8000001
);
const createTokenTx = await createToken.execute(client);
const createTokenRx = await createTokenTx.getRecord(client);
const tokenIdSolidityAddr = createTokenRx.contractFunctionResult.getAddress(0);
const tokenId = AccountId.fromSolidityAddress(tokenIdSolidityAddr);
console.log(`Token created with ID: ${tokenId} \n`);
}
main();
but when I do it reverts with an error:
ReceiptStatusError: receipt for transaction 0.0.6929@1678096644.318059968 contained error status CONTRACT_REVERT_EXECUTED
Any pointers appreciated.