Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes calculations #12

Open
wants to merge 2 commits into
base: 000-dapp-catalog
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions contracts/DAppStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import './token/MiniMeTokenInterface.sol';

contract DAppStore {

uint256 constant percentage_num = 1;
uint256 constant percentage_den = 100000;
// if num = 1, and den = 100000, num/den = 0.000001
uint256 constant rate = 1.6;
uint256 constant curve = 1 / rate;

MiniMeTokenInterface SNT;

Expand All @@ -26,7 +25,11 @@ contract DAppStore {
bytes32 id;
uint256 SNTBalance;
uint256 effectiveBalance;
uint256 prevMinted;

Vote[] votes;
uint256 up;
uint256 down;
}

Dapp[] public dapps;
Expand All @@ -52,6 +55,7 @@ contract DAppStore {
d.name = _name;
d.id = _id;
d.SNTBalance = _amountToStake;
d.prevMinted = _amountToStake / rate;

id2index[_id] = dappIdx;

Expand All @@ -60,29 +64,26 @@ contract DAppStore {

// -- TEST
function numVotesToMint(Dapp storage d, uint256 _SNTBalance) internal view returns(uint256) {
uint TOTAL_SNT = d.SNTBalance;
uint SNT_PROPORTION = TOTAL_SNT * percentage_num;
assert(SNT_PROPORTION / TOTAL_SNT == percentage_num);
SNT_PROPORTION /= percentage_den;

uint num_votes_to_mint_at_1 = percentage_den / percentage_num;

if (_SNTBalance <= SNT_PROPORTION) {
return num_votes_to_mint_at_1;
}

if (_SNTBalance > SNT_PROPORTION) {
uint current_interval_index = _SNTBalance / SNT_PROPORTION;
return num_votes_to_mint_at_1 + ((current_interval_index * (((_SNTBalance/100) - 1) / d.effectiveBalance)) * num_votes_to_mint_at_1);
}
uint256 cost = costOfMinting(d.id, _SNT);
uint256 num_to_mint = 1 / cost;
return num_to_mint;
}

// -- TEST
function costOfMinting(bytes32 _id, uint256 _SNT) public view returns(uint256) {
uint dappIdx = id2index[_id];
Dapp storage d = dapps[dappIdx];
require(d.id == _id);
return numVotesToMint(d, _SNT);

uint256 prev_minted = d.prevMinted;
uint256 num_to_mint_1 = prev_minted + ((d.SNTBalance * prev_minted)/(d.SNTBalance + _SNT))**(curve);
uint256 num_to_mint_percent_avail = curve * num_to_mint_1;
uint256 percent_votes = (d.effectiveBalance / d.SNTBalance) * 100;
uint256 votes_per_snt = percent_votes * num_to_mint_percent_avail;
uint256 num_to_mint = num_to_mint_percent_avail - votes_per_snt;
uint256 cost = 1 / num_to_mint;

return cost;
}

// -- TEST
Expand All @@ -96,6 +97,8 @@ contract DAppStore {
require(SNT.allowance(msg.sender, address(this)) >= _amountToStake);
require(SNT.transferFrom(msg.sender, address(this), _amountToStake));

uint newMinted = d.prevMinted + ((d.SNTBalance * d.prevMinted)/(d.SNTBalance + _amountToStake))**(curve);
d.prevMinted == newMinted;
d.SNTBalance += _amountToStake;

emit IncreasedDAppStake(_id, _amountToStake);
Expand All @@ -110,6 +113,7 @@ contract DAppStore {

uint256 dappvotes = numVotesToMint(d, _amount);
mint(d, dappvotes, true);
d.up += dappvotes;

require(SNT.allowance(msg.sender, d.developer) >= _amount);
require(SNT.transferFrom(msg.sender, d.developer, _amount));
Expand All @@ -124,15 +128,7 @@ contract DAppStore {

uint dappvotes = numVotesToMint(d, _amount);
mint(d, dappvotes, false);

uint negative_votes_before = d.effectiveBalance;
uint negative_votes_now = d.effectiveBalance + dappvotes;
uint negative_percent = ((negative_votes_now - negative_votes_before) * 100 / negative_votes_now );
d.effectiveBalance -= d.effectiveBalance * negative_percent / 100;

assert(d.effectiveBalance < negative_votes_before);

// TODO: what happens if effectiveValance reaches 0;
d.down += dappvotes;

require(SNT.allowance(msg.sender, d.developer) >= _amount);
require(SNT.transferFrom(msg.sender, d.developer, _amount));
Expand All @@ -148,7 +144,13 @@ contract DAppStore {
require(_amount != 0);
require(d.SNTBalance >= _amount);
require(d.developer == msg.sender);


// TODO: this is the biggest problem with tracking prevMinted - not sure how to handle it when
// the developer withdraws stake. What happens if more votes per SNT havebeen cast than votes
// are available after the withdrawal, for instance?
uint newMinted = d.prevMinted - ((d.SNTBalance * d.prevMinted)/(d.SNTBalance + _amount))**(curve);
d.prevMinted == newMinted;

d.SNTBalance -= _amount; // TODO: what happens if balance is 0? Dapp is deleted?

require(SNT.transferFrom(address(this), msg.sender, _amount));
Expand All @@ -159,4 +161,4 @@ contract DAppStore {
d.votes.push(Vote(_amount, _positive));
}

}
}