From fe50099c00a81707a78f1a971d7ce7038025627f Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Tue, 11 Apr 2017 12:38:17 -0500 Subject: [PATCH] Mo changes --- htdocs/index.html | 2 +- htdocs/js/index.js | 12 ++- include/siadrive_api/siaapi.h | 3 +- include/siadrive_api/siacommon.h | 38 ++++++-- src/siadrive/siadriveapp.cpp | 15 ++++ src/siadrive_api/siarenter.cpp | 150 ++++++++++++++++--------------- 6 files changed, 132 insertions(+), 88 deletions(-) diff --git a/htdocs/index.html b/htdocs/index.html index 8ccc022..b4816f7 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -161,7 +161,7 @@

Your storage allowance automatically refills every 6 weeks. Your computer must be online with your wallet unlocked to complete the refill. If Sia fails to refill the allowance by the end of the lock-in period, your data may be lost.

*contract fees are non-refundable. They will be subtracted from the allowance that you set.

Allocated Funds

-


+

Number of Hosts



Contract Period

diff --git a/htdocs/js/index.js b/htdocs/js/index.js index 3c66a7a..898cc00 100644 --- a/htdocs/js/index.js +++ b/htdocs/js/index.js @@ -63,6 +63,9 @@ setValue('ID_RenterSetHosts', allowance.Hosts); setValue('ID_RenterSetPeriod', allowance.Period); setValue('ID_RenterSetRenewWindow', allowance.RenewWindowInBlocks); + AppActions.calculateEstimatedStorage(allowance.Funds, (res)=> { + setInnerText('ID_RenterCalcStorage', res); + }); } } }; @@ -175,8 +178,8 @@ window.appActions.setRenterSettings(allowance, cb); } - function _calculateEstimatedStorage(funds) { - return window.appActions.calculateEstimatedStorage(funds) + function _calculateEstimatedStorage(funds, cb) { + window.appActions.calculateEstimatedStorage(funds, cb) } return { createWallet: _createWallet, @@ -273,10 +276,11 @@ function handleRenterEditSettings() { setMainWindow('renter_settings_window'); - const estStorage = document.getElementById('ID_RenterCalcStorage'); const funds = document.getElementById('ID_RenterSetFunds'); funds.oninput = () => { - estStorage.innerText = AppActions.calculateEstimatedStorage(funds.value); + AppActions.calculateEstimatedStorage(funds.value, (res)=> { + setInnerText('ID_RenterCalcStorage', res); + }); }; const defaultsButton = document.getElementById('ID_RenterSettingsDefaults'); diff --git a/include/siadrive_api/siaapi.h b/include/siadrive_api/siaapi.h index ea84bc5..e9b71ce 100644 --- a/include/siadrive_api/siaapi.h +++ b/include/siadrive_api/siaapi.h @@ -171,12 +171,13 @@ public: private: _SiaRenterAllowance _currentAllowance; std::shared_ptr<_CSiaFileTree> _fileTree; + SiaCurrency _storageterabytemonth; private: void Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig); public: - CSiaError<_SiaApiErrorCode> CalculateEstimatedStorage(const SiaCurrency& funds, SiaCurrency& resultGb) const; + CSiaError<_SiaApiErrorCode> CalculateEstimatedStorage(const SiaCurrency& funds, SiaCurrency& resultInBytes) const; CSiaError<_SiaApiErrorCode> DownloadFile(const SString& siaPath, const SString& location) const; CSiaError<_SiaApiErrorCode> FileExists(const SString& siaPath, bool& exists) const; _SiaRenterAllowance GetAllowance() const; diff --git a/include/siadrive_api/siacommon.h b/include/siadrive_api/siacommon.h index 172d106..39143f8 100644 --- a/include/siadrive_api/siacommon.h +++ b/include/siadrive_api/siacommon.h @@ -182,15 +182,6 @@ inline static SString SiaCurrencyToHastingsString(const SiaCurrency& value) return ret; } -inline static SString SiaCurrencyToGB(const SiaCurrency& value) -{ - ttmath::Conv conv; - conv.base = 10; - conv.round = 3; - - return value.ToWString(conv); -} - inline static Hastings SiaCurrencyToHastings(const SiaCurrency& currency) { ttmath::Parser parser; @@ -203,6 +194,35 @@ inline static Hastings SiaCurrencyToHastings(const SiaCurrency& currency) return parser.stack[0].value.ToString(conv); } +inline static SString SiaCurrencyToGB(const SiaCurrency& value) +{ + ttmath::Conv conv; + conv.base = 10; + conv.round = 3; + + return value.ToWString(conv); +} +inline static SString BytesToFriendlyDisplay(const SiaCurrency& bytes) +{ + SString units[] = { "B", "KB", "MB", "GB", "TB", "PB" }; + SString readableunit = "B"; + SiaCurrency readablesize = bytes; + for (const auto& unit : units) + { + if (readablesize < 1000) + { + readableunit = unit; + break; + } + readablesize /= 1000; + } + ttmath::Conv conv; + conv.scient_from = 256; + conv.base = 10; + conv.round = 2; + return readablesize.ToWString(conv) + ' ' + readableunit; +} + BOOL SIADRIVE_EXPORTABLE RetryAction(std::function func, std::uint16_t retryCount, const DWORD& retryDelay); BOOL SIADRIVE_EXPORTABLE RetryDeleteFileIfExists(const SString& filePath); diff --git a/src/siadrive/siadriveapp.cpp b/src/siadrive/siadriveapp.cpp index b401499..c7274cf 100644 --- a/src/siadrive/siadriveapp.cpp +++ b/src/siadrive/siadriveapp.cpp @@ -206,6 +206,20 @@ public: cb->ExecuteFunctionWithContext(context, nullptr, args); return true; } + else if (name == "calculateEstimatedStorage") + { + CefRefPtr funds = arguments[0]; + CefRefPtr cb = arguments[1]; + + SiaCurrency resultInBytes; + _siaApi.GetRenter()->CalculateEstimatedStorage(funds->GetStringValue().ToWString(), resultInBytes); + + CefV8ValueList args; + args.push_back(CefV8Value::CreateString(BytesToFriendlyDisplay(resultInBytes).str())); + cb->ExecuteFunctionWithContext(context, nullptr, args); + + return true; + } // Function does not exist. return false; } @@ -310,6 +324,7 @@ void CSiaDriveApp::OnContextCreated( obj->SetValue("unmountDrive", CefV8Value::CreateFunction("unmountDrive", handler), V8_PROPERTY_ATTRIBUTE_NONE); obj->SetValue("shutdown", CefV8Value::CreateFunction("shutdown", handler), V8_PROPERTY_ATTRIBUTE_NONE); obj->SetValue("setRenterSettings", CefV8Value::CreateFunction("setRenterSettings", handler), V8_PROPERTY_ATTRIBUTE_NONE); + obj->SetValue("calculateEstimatedStorage", CefV8Value::CreateFunction("calculateEstimatedStorage", handler), V8_PROPERTY_ATTRIBUTE_NONE); global->SetValue("appActions", obj, V8_PROPERTY_ATTRIBUTE_NONE); _refreshThread.reset(new CAutoThread(*_siaCurl, _siaDriveConfig.get(), [this, context](const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) diff --git a/src/siadrive_api/siarenter.cpp b/src/siadrive_api/siarenter.cpp index d332fbc..4bbec9e 100644 --- a/src/siadrive_api/siarenter.cpp +++ b/src/siadrive_api/siarenter.cpp @@ -65,72 +65,80 @@ CSiaApi::_CSiaRenter::~_CSiaRenter() void CSiaApi::_CSiaRenter::Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) { - json result; - if (ApiSuccess(siaCurl.Get(L"/renter", result))) - { - SiaCurrency funds = HastingsStringToSiaCurrency(result["settings"]["allowance"]["funds"].get()); - SiaCurrency unspent = HastingsStringToSiaCurrency(result["financialmetrics"]["unspent"].get()); - std::uint64_t hosts = result["settings"]["allowance"]["hosts"].get(); - std::uint64_t period = result["settings"]["allowance"]["period"].get(); - std::uint64_t renewWindow = result["settings"]["allowance"]["renewwindow"].get(); - SetFunds(funds); - SetHosts(hosts); - SetUnspent(unspent); - SetRenewWindow(renewWindow); - SetPeriod(period); - _currentAllowance = { funds, hosts, period, renewWindow }; + { + json result; + SiaCurlError cerror = GetSiaCurl().Get(L"/renter/prices", {}, result); + _storageterabytemonth = (ApiSuccess(cerror)) ? result["storageterabytemonth"].get() : 0; + } - if (_currentAllowance.Funds == 0) + { + json result; + if (ApiSuccess(siaCurl.Get(L"/renter", result))) { - _currentAllowance.Funds = SIA_DEFAULT_MINIMUM_FUNDS; - _currentAllowance.Hosts = SIA_DEFAULT_HOST_COUNT; - _currentAllowance.Period = SIA_DEFAULT_CONTRACT_LENGTH; - _currentAllowance.RenewWindowInBlocks = SIA_DEFAULT_RENEW_WINDOW; + SiaCurrency funds = HastingsStringToSiaCurrency(result["settings"]["allowance"]["funds"].get()); + SiaCurrency unspent = HastingsStringToSiaCurrency(result["financialmetrics"]["unspent"].get()); + std::uint64_t hosts = result["settings"]["allowance"]["hosts"].get(); + std::uint64_t period = result["settings"]["allowance"]["period"].get(); + std::uint64_t renewWindow = result["settings"]["allowance"]["renewwindow"].get(); + SetFunds(funds); + SetHosts(hosts); + SetUnspent(unspent); + SetRenewWindow(renewWindow); + SetPeriod(period); + _currentAllowance = { funds, hosts, period, renewWindow }; + + if (_currentAllowance.Funds == 0) + { + _currentAllowance.Funds = SIA_DEFAULT_MINIMUM_FUNDS; + _currentAllowance.Hosts = SIA_DEFAULT_HOST_COUNT; + _currentAllowance.Period = SIA_DEFAULT_CONTRACT_LENGTH; + _currentAllowance.RenewWindowInBlocks = SIA_DEFAULT_RENEW_WINDOW; + } + if (ApiSuccess(RefreshFileTree())) + { + CSiaFileTreePtr fileTree; + GetFileTree(fileTree); + + auto fileList = fileTree->GetFileList(); + if (fileList->size()) + { + std::uint64_t total = std::accumulate(std::next(fileList->begin()), fileList->end(), fileList->at(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->at(0)->GetUploadProgress(), [](const std::uint32_t& progress, const CSiaFilePtr& file) + { + return progress + min(100, file->GetUploadProgress()); + }) / static_cast(fileList->size()); + + SetTotalUsedBytes(total); + SetTotalUploadProgress(totalProgress); + } + else + { + SetTotalUsedBytes(0); + SetTotalUploadProgress(100); + } + } + else + { + SetTotalUsedBytes(0); + SetTotalUploadProgress(100); + } } - if (ApiSuccess(RefreshFileTree())) + else { - CSiaFileTreePtr fileTree; - GetFileTree(fileTree); - - auto fileList = fileTree->GetFileList(); - if (fileList->size()) - { - std::uint64_t total = std::accumulate(std::next(fileList->begin()), fileList->end(), fileList->at(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->at(0)->GetUploadProgress(), [](const std::uint32_t& progress, const CSiaFilePtr& file) - { - return progress + min(100, file->GetUploadProgress()); - }) / static_cast(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); - SetPeriod(0); - SetRenewWindow(0); - _currentAllowance = { 0,0,0,0 }; - } + SetFunds(0); + SetHosts(0); + SetUnspent(0); + SetTotalUsedBytes(0); + SetTotalUploadProgress(100); + SetPeriod(0); + SetRenewWindow(0); + _currentAllowance = { 0,0,0,0 }; + } + } } SiaApiError CSiaApi::_CSiaRenter::RefreshFileTree( ) @@ -226,24 +234,20 @@ SiaApiError CSiaApi::_CSiaRenter::SetAllowance(const SiaRenterAllowance& renterA return ret; } -SiaApiError CSiaApi::_CSiaRenter::CalculateEstimatedStorage(const SiaCurrency& funds, SiaCurrency& resultGb) const +SiaApiError CSiaApi::_CSiaRenter::CalculateEstimatedStorage(const SiaCurrency& funds, SiaCurrency& resultInBytes) const { SiaApiError ret; - json result; - SiaCurlError cerror = GetSiaCurl().Get(L"/renter/prices", {}, result); - if (ApiSuccess(cerror)) + if (_storageterabytemonth > 0 && funds > 0) { - ret = { SiaApiErrorCode::RequestError, cerror.GetReason() }; + Hastings fundsHastings = SiaCurrencyToHastings(funds); + ttmath::Parser parser; + parser.Parse((fundsHastings.ToWString() + " / " + _storageterabytemonth.ToWString() + " * 1e12").str()); + resultInBytes = parser.stack[0].value; } else { - Hastings fundsHastings = SiaCurrencyToHastings(funds); - ttmath::Parser parser; - parser.Parse(fundsHastings.ToString() + " / " + result["storageterabytemonth"].get() + " * 1000000000000"); - resultGb = parser.stack[0].value; + resultInBytes = 0; } - //const estimate = new BigNumber(SiaAPI.siacoinsToHastings(action.funds)).dividedBy(response.storageterabytemonth).times(1e12) - return ret; } \ No newline at end of file