Skip to content

Commit

Permalink
halfway till vtho withdraw, found logic problems
Browse files Browse the repository at this point in the history
  • Loading branch information
laalaguer committed Aug 10, 2021
1 parent ee6f5fa commit 992c81b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
106 changes: 106 additions & 0 deletions uniswap-v2-core/contracts/Loyalty.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity =0.5.16;

// Loyalty tracks users contribution to a collective program.
// contribution = points x time;
// The longer the time period, the better; More points, the better.

import './libraries/SafeMath.sol';

contract Loyalty {
using SafeMath for uint256;

struct User {
uint256 points; // The total points a user earned.
uint256 contribution; // Accumulated sum of a user's contribution.
uint256 lastUpdatedTime; // Last time the above sum was updated.
}

// A map to track all users' points and contribution;
mapping(address => User) private users; // user address => user

// A total registry to track the whole program's points and contribution;
User private total;

constructor() public {
total.points = 0;
total.contribution = 0;
total.lastUpdatedTime = block.timestamp;
}

// Add points to a user
function addPoints (address _who, uint256 _amount) internal {
update(_who);
users[_who].points += _amount;
total.points += _amount;
}

// Remove points from user
function removePoints (address _who, uint256 _amount) internal {
update(_who);
require(users[_who].points >= _amount, "Loyalty: points not enough");
users[_who].points -= _amount;
total.points -= _amount;
}

// View points of a single user
function viewPoints (address _who) public view returns (uint256) {
return users[_who].points;
}

// View points of the whole program
function viewTotalPoints() public view returns (uint256) {
return total.points;
}

// View contribution of a single user
function viewContribution (address _who) public view returns (uint256) {
User memory user = users[_who];
if (user.lastUpdatedTime == 0) {
return 0;
}
return user.contribution + calculateContribution(user.lastUpdatedTime, block.timestamp, user.points);
}

// View contribution of the whole program
function viewTotalContribution () public view returns (uint256) {
return total.contribution + calculateContribution(total.lastUpdatedTime, block.timestamp, total.points);
}

// Update the user contribution + the total contribution;
function update (address _who) internal {
uint256 currentTime = block.timestamp; // save gas
User memory user = users[_who]; // save gas

// If existing user, update his profile
if (user.lastUpdatedTime > 0) {
assert(user.lastUpdatedTime <= currentTime);
users[_who].contribution += calculateContribution(
user.lastUpdatedTime,
currentTime,
user.points
);
}
// Existing or new uesr, update the time
users[_who].lastUpdatedTime = currentTime;

// Update the total
assert(total.lastUpdatedTime <= currentTime);
total.contribution += calculateContribution(
total.lastUpdatedTime,
currentTime,
total.points
);
total.lastUpdatedTime = currentTime;
}

// Calculate the contribution during a time period;
function calculateContribution (
uint256 t1,
uint256 t2,
uint256 points
) internal pure returns (uint256) {
require(t1 <= t2, "t1 should be <= t2");
return (t2 - t1) * points;
}
}
7 changes: 6 additions & 1 deletion uniswap-v2-core/contracts/UniswapV2ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ pragma solidity =0.5.16;

import './interfaces/IUniswapV2ERC20.sol';
import './libraries/SafeMath.sol';
import './Loyalty.sol';

contract UniswapV2ERC20 is IUniswapV2ERC20 {
contract UniswapV2ERC20 is IUniswapV2ERC20, Loyalty {
using SafeMath for uint;

string public constant name = 'TinySwap';
Expand Down Expand Up @@ -40,12 +41,14 @@ contract UniswapV2ERC20 is IUniswapV2ERC20 {
function _mint(address to, uint value) internal {
totalSupply = totalSupply.add(value);
balanceOf[to] = balanceOf[to].add(value);
addPoints(to, value); // loyalty
emit Transfer(address(0), to, value);
}

function _burn(address from, uint value) internal {
balanceOf[from] = balanceOf[from].sub(value);
totalSupply = totalSupply.sub(value);
removePoints(from, value); // loyalty
emit Transfer(from, address(0), value);
}

Expand All @@ -57,6 +60,8 @@ contract UniswapV2ERC20 is IUniswapV2ERC20 {
function _transfer(address from, address to, uint value) private {
balanceOf[from] = balanceOf[from].sub(value);
balanceOf[to] = balanceOf[to].add(value);
removePoints(from, value); // loyalty
addPoints(to, value); // loyalty
emit Transfer(from, to, value);
}

Expand Down
20 changes: 20 additions & 0 deletions uniswap-v2-core/contracts/UniswapV2Pair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {
using SafeMath for uint;
using UQ112x112 for uint224;

address public constant VTHO_CONTRACT_ADDRESS = 0x0000000000000000000000000000456e65726779;
uint public constant MINIMUM_LIQUIDITY = 10**3;
bytes4 private constant SELECTOR = bytes4(keccak256(bytes('transfer(address,uint256)')));

Expand Down Expand Up @@ -198,4 +199,23 @@ contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {
function sync() external lock {
_update(IERC20(token0).balanceOf(address(this)), IERC20(token1).balanceOf(address(this)), reserve0, reserve1);
}

// View total generated VTHO this pool holds.
// Three types of pools:
// 1) VET/VTHO: vet generates vtho, vtho is also supplied by LPs.
// 2) OCE/VTHO: no generate of vtho, vtho is only supplied by LPs.
// 3) OCE/SHA: no generate of vtho, vtho is 0.
function viewTotalGeneratedVTHO() public view (uint256) {
return 0;
}

// User Generated VTHO = (Total Generated VTHO) x (User contribution)
function viewUserGeneratedVTHO(address who) public view (uint256) {
return 0;
}

// msg.sender claims the vtho that he holds, to the receiver
function claimGeneratedVTHO(address receiver, uint256 amount) external {
;
}
}

0 comments on commit 992c81b

Please sign in to comment.