Skip to content

Commit

Permalink
Add CreateFileExample
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Walworth <[email protected]>
  • Loading branch information
rwalworth committed Oct 12, 2023
1 parent b6740a1 commit 800c433
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
4 changes: 4 additions & 0 deletions sdk/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(AUTO_CREATE_ACCOUNT_TRANSFER_TRANSACTION_EXAMPLE_NAME ${PROJECT_NAME}-auto-c
set(CONSENSUS_PUB_SUB_EXAMPLE_NAME ${PROJECT_NAME}-consensus-pub-sub-example)
set(CONSENSUS_PUB_SUB_CHUNKED_EXAMPLE_NAME ${PROJECT_NAME}-consensus-pub-sub-chunked-example)
set(CREATE_ACCOUNT_EXAMPLE_NAME ${PROJECT_NAME}-create-account-example)
set(CREATE_FILE_EXAMPLE_NAME ${PROJECT_NAME}-create-file-example)
set(CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME ${PROJECT_NAME}-create-simple-contract-example)
set(CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME ${PROJECT_NAME}-create-stateful-contract-example)
set(CONTRACT_NONCES_EXAMPLE_NAME ${PROJECT_NAME}-contract-nonces-example)
Expand Down Expand Up @@ -38,6 +39,7 @@ add_executable(${AUTO_CREATE_ACCOUNT_TRANSFER_TRANSACTION_EXAMPLE_NAME} AutoCrea
add_executable(${CONSENSUS_PUB_SUB_EXAMPLE_NAME} ConsensusPubSubExample.cc)
add_executable(${CONSENSUS_PUB_SUB_CHUNKED_EXAMPLE_NAME} ConsensusPubSubChunkedExample.cc)
add_executable(${CREATE_ACCOUNT_EXAMPLE_NAME} CreateAccountExample.cc)
add_executable(${CREATE_FILE_EXAMPLE_NAME} CreateFileExample.cc)
add_executable(${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME} CreateSimpleContractExample.cc)
add_executable(${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME} CreateStatefulContractExample.cc)
add_executable(${CONTRACT_NONCES_EXAMPLE_NAME} ContractNoncesExample.cc)
Expand Down Expand Up @@ -88,6 +90,7 @@ target_link_libraries(${AUTO_CREATE_ACCOUNT_TRANSFER_TRANSACTION_EXAMPLE_NAME} P
target_link_libraries(${CONSENSUS_PUB_SUB_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CONSENSUS_PUB_SUB_CHUNKED_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CREATE_ACCOUNT_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CREATE_FILE_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CONTRACT_NONCES_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
Expand Down Expand Up @@ -123,6 +126,7 @@ install(TARGETS
${CONSENSUS_PUB_SUB_EXAMPLE_NAME}
${CONSENSUS_PUB_SUB_CHUNKED_EXAMPLE_NAME}
${CREATE_ACCOUNT_EXAMPLE_NAME}
${CREATE_FILE_EXAMPLE_NAME}
${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME}
${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME}
${CONTRACT_NONCES_EXAMPLE_NAME}
Expand Down
59 changes: 59 additions & 0 deletions sdk/examples/CreateFileExample.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*-
*
* Hedera C++ SDK
*
* Copyright (C) 2020 - 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "Client.h"
#include "ED25519PrivateKey.h"
#include "FileCreateTransaction.h"
#include "FileId.h"
#include "PublicKey.h"
#include "TransactionReceipt.h"
#include "TransactionResponse.h"

#include <iostream>
#include <string>

using namespace Hedera;

int main(int argc, char** argv)
{
if (argc < 3)
{
std::cout << "Please input account ID and private key" << std::endl;
return 1;
}

// Get a client for the Hedera testnet, and set the operator account ID and key such that all generated transactions
// will be paid for by this account and be signed by this key.
Client client = Client::forTestnet();
client.setOperator(AccountId::fromString(argv[1]), ED25519PrivateKey::fromString(argv[2]));

// The file is required to be a byte array, you can easily use the bytes of a file instead.
std::string fileContents = "Hedera hashgraph is great!";

const FileId fileId = FileCreateTransaction()
.setKeys({ client.getOperatorPublicKey() })
.setContents(fileContents)
.execute(client)
.getReceipt(client)
.mFileId.value();

std::cout << "Created new file with ID " << fileId.toString() << std::endl;

return 0;
}
1 change: 1 addition & 0 deletions sdk/main/include/FileCreateTransaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class FileCreateTransaction : public Transaction<FileCreateTransaction>
* @throws IllegalStateException If this FileCreateTransaction is frozen.
*/
FileCreateTransaction& setContents(const std::vector<std::byte>& contents);
FileCreateTransaction& setContents(std::string_view contents);

/**
* Set the memo for the new file. The memo cannot exceed 100 bytes.
Expand Down
7 changes: 7 additions & 0 deletions sdk/main/src/FileCreateTransaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ FileCreateTransaction& FileCreateTransaction::setContents(const std::vector<std:
return *this;
}

//-----
FileCreateTransaction& FileCreateTransaction::setContents(std::string_view contents)
{
requireNotFrozen();
return setContents(internal::Utilities::stringToByteVector(contents));
}

//-----
FileCreateTransaction& FileCreateTransaction::setFileMemo(std::string_view memo)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TEST_F(FileDeleteTransactionIntegrationTest, ExecuteFileDeleteTransaction)
FileId fileId;
ASSERT_NO_THROW(fileId = FileCreateTransaction()
.setKeys({ operatorKey->getPublicKey() })
.setContents({})
.setContents("")
.execute(getTestClient())
.getReceipt(getTestClient())
.mFileId.value());
Expand All @@ -68,7 +68,7 @@ TEST_F(FileDeleteTransactionIntegrationTest, CannotDeleteFileWithNoAdminKey)
FileId fileId;
ASSERT_NO_THROW(
fileId =
FileCreateTransaction().setContents({}).execute(getTestClient()).getReceipt(getTestClient()).mFileId.value());
FileCreateTransaction().setContents("").execute(getTestClient()).getReceipt(getTestClient()).mFileId.value());

// When / Then
EXPECT_THROW(const TransactionReceipt txReceipt =
Expand Down

0 comments on commit 800c433

Please sign in to comment.