#include "stdafx.h" #include "SiaApi.h" #include "SQLiteCpp/Database.h" using namespace Sia::Api; /*{ // Settings that control the behavior of the renter. "settings": { // Allowance dictates how much the renter is allowed to spend in a given // period. Note that funds are spent on both storage and bandwidth. "allowance": { // Amount of money allocated for contracts. Funds are spent on both // storage and bandwidth. "funds": "1234", // hastings // Number of hosts that contracts will be formed with. "hosts":24, // Duration of contracts formed, in number of blocks. "period": 6048, // blocks // If the current blockheight + the renew window >= the height the // contract is scheduled to end, the contract is renewed automatically. // Is always nonzero. "renewwindow": 3024 // blocks } }, // Metrics about how much the Renter has spent on storage, uploads, and // downloads. "financialmetrics": { // How much money, in hastings, the Renter has spent on file contracts, // including fees. "contractspending": "1234", // hastings // Amount of money spent on downloads. "downloadspending": "5678", // hastings // Amount of money spend on storage. "storagespending": "1234", // hastings // Amount of money spent on uploads. "uploadspending": "5678", // hastings // Amount of money in the allowance that has not been spent. "unspent": "1234" // hastings } }*/ CSiaApi::_CSiaRenter::_CSiaRenter(const CSiaCurl& siaCurl) : CSiaBase(siaCurl), CAutoThread(siaCurl), _Funds(0), _Hosts(0), _Unspent(0), _TotalUsedBytes(0), _TotalUploadProgress(100) { StartAutoThread(); } CSiaApi::_CSiaRenter::~_CSiaRenter() { StopAutoThread(); } void CSiaApi::_CSiaRenter::AutoThreadCallback(const CSiaCurl& siaCurl) { json result; if (ApiSuccess(siaCurl.Get(L"/renter", result))) { SiaCurrency funds = HastingsStringToSiaCurrency(CA2W(result["settings"]["allowance"]["funds"].get().c_str()).m_psz); SiaCurrency unspent = HastingsStringToSiaCurrency(CA2W(result["financialmetrics"]["unspent"].get().c_str()).m_psz); std::uint64_t hosts = result["settings"]["allowance"]["hosts"].get(); SetFunds(funds); SetHosts(hosts); SetUnspent(unspent); CSiaFileTreePtr fileTree(new CSiaFileTree(siaCurl)); if (ApiSuccess(siaCurl.Get(L"/renter/files", result))) { fileTree->BuildTree(result); auto fileList = fileTree->GetFileList(); if (fileList.size()) { std::uint64_t total = std::accumulate(std::next(fileList.begin()), fileList.end(), fileList[0]->GetFileSize(), [](const std::uint64_t& sz, const CSiaFilePtr& file) { return sz + file->GetFileSize(); }); std::uint32_t totalProgress = std::accumulate(std::next(fileList.begin()), fileList.end(), fileList[0]->GetUploadProgress(), [](const std::uint32_t& progress, const CSiaFilePtr& file) { return progress + file->GetUploadProgress(); }) / fileList.size(); SetTotalUsedBytes(total); SetTotalUploadProgress(totalProgress); } else { SetTotalUsedBytes(0); SetTotalUploadProgress(100); } } else { SetTotalUsedBytes(0); SetTotalUploadProgress(100); } } else { SetFunds(0); SetHosts(0); SetUnspent(0); SetTotalUsedBytes(0); SetTotalUploadProgress(100); } } SiaApiError CSiaApi::_CSiaRenter::FileExists(const String& siaPath, bool& exists) const { CSiaFileTreePtr siaFileTree; SiaApiError ret = GetFileTree(siaFileTree); if (ApiSuccess(ret)) { exists = siaFileTree->FileExists(siaPath); } return ret; } SiaApiError CSiaApi::_CSiaRenter::DeleteFile(const String& siaPath) { return SiaApiError::NotImplemented; } SiaApiError CSiaApi::_CSiaRenter::DownloadFile(const String& siaPath, const String& location) const { return SiaApiError::NotImplemented; } SiaApiError CSiaApi::_CSiaRenter::QueueUploadFile(const String& siaPath, const String& filePath) { return SiaApiError::NotImplemented; } SiaApiError CSiaApi::_CSiaRenter::GetFileTree(CSiaFileTreePtr& siaFileTree) const { SiaApiError ret = SiaApiError::RequestError; siaFileTree.reset(new CSiaFileTree(GetSiaCurl())); json result; if (ApiSuccess(GetSiaCurl().Get(L"/renter/files", result))) { siaFileTree->BuildTree(result); ret = SiaApiError::Success; } return ret; }