Skip to content

Commit

Permalink
Merge pull request #3 from luciditytech/feature/add-token-calculations
Browse files Browse the repository at this point in the history
feat(*): add calculation for total token balance per shard
  • Loading branch information
DZariusz authored Dec 12, 2018
2 parents 6fea868 + 1fabf07 commit 1a1796b
Show file tree
Hide file tree
Showing 14 changed files with 887 additions and 235 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Unifying editor configurations for all developers:
# For details see http://editorconfig.org/

root = true

[*]
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
26 changes: 26 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"extends": "airbnb-base",
"env": {
"mocha": true
},
"globals": {
"artifacts": true,
"assert": true,
"beforeEach": true,
"contract": true,
"describe": true,
"it": true,
"web3": true
},
"rules": {
"no-console": "off",
"prefer-destructuring": ["error", {
"AssignmentExpression": {
"array": false, // this will allow for: var bar = foo[0] <- access array with index
"object": true
}
}, {
"enforceForRenamedProperties": false
}]
}
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sol linguist-language=Solidity
8 changes: 8 additions & 0 deletions .solcover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
copyPackages: ['zeppelin-solidity', 'token-sale-contracts'],
port: 8555,
norpc: false,
compileCommand: 'truffle compile --all',
testCommand: 'truffle test --network coverage',
skipFiles: []
}
5 changes: 5 additions & 0 deletions .soliumignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
helpers
migrations
test
contracts/Migrations.sol
16 changes: 16 additions & 0 deletions .soliumrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "solium:recommended",
"plugins": [
"security"
],
"rules": {
"quotes": [
"error",
"double"
],
"indentation": [
"error",
2
]
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## Unreleased
### Changed
- Makes verifiers per shard configurable
- Total token calculation per shard

## [0.1.0] - 2018-10-03
### Added
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ A collection of smart contracts for various types of on-chain registries.
## Testing smart contracts

> Be sure compiled contracts are latest before testing
1. `truffle test`
1. With code coverage: `npm test`
1. `npm run lint`
1. `npm run test`
1. With code coverage: `npm run coverage`

---

Expand Down
50 changes: 34 additions & 16 deletions contracts/VerifierRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pragma solidity ^0.4.18;
pragma solidity ^0.4.24;

import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
import 'token-sale-contracts/contracts/Token.sol';
import 'token-sale-contracts/contracts/HumanStandardToken.sol';
import "zeppelin-solidity/contracts/ownership/Ownable.sol";
import "token-sale-contracts/contracts/Token.sol";
import "token-sale-contracts/contracts/HumanStandardToken.sol";

contract VerifierRegistry is Ownable {
event LogVerifierRegistered(
Expand All @@ -21,6 +21,8 @@ contract VerifierRegistry is Ownable {
uint256 shard
);

event LogBalancePerShard(uint256 shard, uint256 balance);

struct Verifier {
address id;
string location;
Expand All @@ -31,19 +33,23 @@ contract VerifierRegistry is Ownable {

mapping(address => Verifier) public verifiers;

/// @dev shard => balance
mapping(uint256 => uint256) public balancesPerShard;

address[] public addresses;
address public tokenAddress;
uint256 public verifiersPerShard;

function VerifierRegistry(address _tokenAddress, uint256 _verifiersPerShard) {
constructor(address _tokenAddress, uint256 _verifiersPerShard)
public {
tokenAddress = _tokenAddress;
verifiersPerShard = _verifiersPerShard;
}

function create(string _location) public {
Verifier storage verifier = verifiers[msg.sender];

require(!verifier.created);
require(!verifier.created, "verifier already exists");

verifier.id = msg.sender;
verifier.location = _location;
Expand All @@ -52,7 +58,7 @@ contract VerifierRegistry is Ownable {

addresses.push(verifier.id);

LogVerifierRegistered(
emit LogVerifierRegistered(
verifier.id,
verifier.location,
verifier.created,
Expand All @@ -70,23 +76,29 @@ contract VerifierRegistry is Ownable {

uint256 allowance = token.allowance(_from, this);

require(allowance > 0);
require(allowance > 0, "nothing to approve");

require(token.transferFrom(_from, this, allowance));
require(token.transferFrom(_from, this, allowance), "transferFrom failed");

verifiers[_from].balance += allowance;

uint256 shard = verifiers[_from].shard;
uint256 shardBalance = balancesPerShard[shard] + allowance;
balancesPerShard[shard] = shardBalance;

emit LogBalancePerShard(shard, shardBalance);

return true;
}

function update(string _location) public {
Verifier storage verifier = verifiers[msg.sender];

require(verifier.created);
require(verifier.created, "verifier do not exists");

verifier.location = _location;

LogVerifierUpdated(
emit LogVerifierUpdated(
verifier.id,
verifier.location,
verifier.created,
Expand All @@ -95,28 +107,34 @@ contract VerifierRegistry is Ownable {
);
}

function withdraw(uint256 _value) public returns (bool success) {
function withdraw(uint256 _value) public returns (bool) {
Verifier storage verifier = verifiers[msg.sender];

require(_value > 0 && verifier.balance >= _value);
require(_value > 0 && verifier.balance >= _value, "nothing to withdraw");

verifier.balance -= _value;

uint256 shard = verifier.shard;
uint256 shardBalance = balancesPerShard[shard] - _value;
balancesPerShard[shard] = shardBalance;

emit LogBalancePerShard(shard, shardBalance);

Token token = Token(tokenAddress);

require(token.transfer(msg.sender, _value));
require(token.transfer(msg.sender, _value), "transfer failed");

return true;
}

function updateTokenAddress(address _newTokenAddress) public onlyOwner {
require(_newTokenAddress != address(0));
require(_newTokenAddress != address(0), "empty token address");

tokenAddress = _newTokenAddress;
}

function updateVerifiersPerShard(uint256 _newVerifiersPerShard) public onlyOwner {
require(_newVerifiersPerShard > 0);
require(_newVerifiersPerShard > 0, "_newVerifiersPerShard is empty");

verifiersPerShard = _newVerifiersPerShard;
}
Expand Down
Loading

0 comments on commit 1a1796b

Please sign in to comment.