1
0

Mo changes

This commit is contained in:
Scott E. Graves
2017-04-11 12:38:17 -05:00
parent 3c48883a8b
commit fe50099c00
6 changed files with 132 additions and 88 deletions

View File

@@ -161,7 +161,7 @@
<p>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.</p>
<p class="footnote">*contract fees are non-refundable. They will be subtracted from the allowance that you set.</p>
<h2>Allocated Funds</h2>
<input type="number" id="ID_RenterSetFunds"><label>Estimated GB:</label><div id="ID_RenterCalcStorage"></div><br><br>
<input type="number" id="ID_RenterSetFunds"><label id="ID_RenterCalcStorage"></label><br><br>
<h2>Number of Hosts</h2>
<input type="number" id="ID_RenterSetHosts"><br><br>
<h2>Contract Period</h2>

View File

@@ -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');

View File

@@ -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;

View File

@@ -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<SiaCurrency> 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<BOOL()> func, std::uint16_t retryCount, const DWORD& retryDelay);
BOOL SIADRIVE_EXPORTABLE RetryDeleteFileIfExists(const SString& filePath);

View File

@@ -206,6 +206,20 @@ public:
cb->ExecuteFunctionWithContext(context, nullptr, args);
return true;
}
else if (name == "calculateEstimatedStorage")
{
CefRefPtr<CefV8Value> funds = arguments[0];
CefRefPtr<CefV8Value> 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)

View File

@@ -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<std::string>());
SiaCurrency unspent = HastingsStringToSiaCurrency(result["financialmetrics"]["unspent"].get<std::string>());
std::uint64_t hosts = result["settings"]["allowance"]["hosts"].get<std::uint64_t>();
std::uint64_t period = result["settings"]["allowance"]["period"].get<std::uint64_t>();
std::uint64_t renewWindow = result["settings"]["allowance"]["renewwindow"].get<std::uint64_t>();
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<std::string>() : 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<std::string>());
SiaCurrency unspent = HastingsStringToSiaCurrency(result["financialmetrics"]["unspent"].get<std::string>());
std::uint64_t hosts = result["settings"]["allowance"]["hosts"].get<std::uint64_t>();
std::uint64_t period = result["settings"]["allowance"]["period"].get<std::uint64_t>();
std::uint64_t renewWindow = result["settings"]["allowance"]["renewwindow"].get<std::uint64_t>();
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<std::uint32_t>(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<std::uint32_t>(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<SiaCurrency> parser;
parser.Parse((fundsHastings.ToWString() + " / " + _storageterabytemonth.ToWString() + " * 1e12").str());
resultInBytes = parser.stack[0].value;
}
else
{
Hastings fundsHastings = SiaCurrencyToHastings(funds);
ttmath::Parser<SiaCurrency> parser;
parser.Parse(fundsHastings.ToString() + " / " + result["storageterabytemonth"].get<std::string>() + " * 1000000000000");
resultGb = parser.stack[0].value;
resultInBytes = 0;
}
//const estimate = new BigNumber(SiaAPI.siacoinsToHastings(action.funds)).dividedBy(response.storageterabytemonth).times(1e12)
return ret;
}