Skip to content

Commit

Permalink
update and deploy to testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhe8x committed Feb 14, 2024
1 parent 2d8ff8e commit 14627e8
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 121 deletions.
65 changes: 39 additions & 26 deletions contracts/root/Vesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ contract Vesting is Ownable {
uint128 claimed;
}

uint256 constant SCALE_FACTOR = 1e12;

address public token;
address public vtToken;
uint256 public vestingStartDate;
Expand Down Expand Up @@ -71,24 +73,23 @@ contract Vesting is Ownable {
emit VestingPlanAdded(plans.length - 1, _lockPeriod, _vestingPeriod, _initialUnlockPercent);
}

function allocateVesting(address user, uint64 planId, uint256 allocation) public onlyOwner {
_saveUserAllocation(user, planId, allocation);
totalAllocation += allocation;
}
// function allocateVesting(address user, uint64 planId, uint64 allocation) public onlyOwner {
// _saveUserAllocation(user, planId, allocation);
// totalAllocation += allocation;
// }

function batchAllocateVesting(
uint64[] calldata _planIds,
address[] calldata _users,
uint256[] calldata _allocations
uint64[] calldata _allocations
) external onlyOwner {
require(_users.length > 0, 'V005');
require(_users.length == _allocations.length, 'V006');
require(_users.length == _planIds.length, 'V006');

uint256 _total;
for (uint256 i = 0; i < _users.length; i++) {
_saveUserAllocation(_users[i], _planIds[i], _allocations[i]);
_total += _allocations[i];
_total += _saveUserAllocation(_users[i], _planIds[i], _allocations[i]);
}
totalAllocation += _total;
}
Expand Down Expand Up @@ -121,24 +122,29 @@ contract Vesting is Ownable {
UserAllocation memory ua = userAllocation(user);
require(ua.allocation != 0, 'V011');

uint256 amount = claimableAmount(user);
require(amount > 0, 'V012');

_updateUserAllocation(user, amount);
ISQToken(vtToken).burnFrom(user, amount);
uint128 scaledAmount = _claimableAmount(user);
require(scaledAmount > 0, 'V012');
uint256 fullAmount = uint256(scaledAmount) * SCALE_FACTOR;
require(ua.claimed + fullAmount <= ua.allocation, 'V012');

totalClaimed += amount;
_updateUserAllocation(user, scaledAmount);
ISQToken(vtToken).burnFrom(user, fullAmount);

require(ua.claimed + amount <= ua.allocation, 'V012');
totalClaimed += fullAmount;

require(IERC20(token).transfer(user, amount), 'V008');
emit VestingClaimed(user, amount);
require(IERC20(token).transfer(user, fullAmount), 'V008');
emit VestingClaimed(user, fullAmount);
}

function claimableAmount(address user) public view returns (uint256) {
return uint256(_claimableAmount(user)) * SCALE_FACTOR;
}

function _claimableAmount(address user) internal view returns (uint128) {
uint256 amount = unlockedAmount(user);
uint256 vtSQTAmount = IERC20(vtToken).balanceOf(user);
return vtSQTAmount >= amount ? amount : vtSQTAmount;
uint256 claimable = vtSQTAmount >= amount ? amount : vtSQTAmount;
return uint128(claimable / SCALE_FACTOR);
}

function unlockedAmount(address user) public view returns (uint256) {
Expand Down Expand Up @@ -176,30 +182,37 @@ contract Vesting is Ownable {
function userAllocation(address user) public view returns (UserAllocation memory) {
StorageUserAllocation memory ua = allocations[user];

uint256 allocation = uint256(ua.allocation) * 1e18;
uint256 claimed = uint256(ua.claimed);
uint256 allocation = uint256(ua.allocation) * SCALE_FACTOR;
uint256 claimed = uint256(ua.claimed) * SCALE_FACTOR;

return UserAllocation(ua.planId, allocation, claimed);
}

function _saveUserAllocation(address user, uint64 planId, uint256 allocation) private {
function _saveUserAllocation(
address user,
uint64 planId,
uint64 allocation
) private returns (uint256) {
StorageUserAllocation storage ua = allocations[user];

require(user != address(0x0), 'V002');
require(ua.allocation == 0, 'V003');
require(planId < plans.length, 'PM012');
require(allocation > 0, 'V004');
require(allocation / 1e18 < 18446744073709551616, 'V005'); // 2 ** 64
// require(allocation / 1e18 < 18446744073709551616, 'V005'); // 2 ** 64

ua.planId = planId;
ua.allocation = uint64(allocation / 1e18);
ua.allocation = allocation;

uint256 fullLengthAllocation = uint256(allocation) * SCALE_FACTOR;

ISQToken(vtToken).mint(user, allocation);
emit VestingAllocated(user, planId, allocation);
ISQToken(vtToken).mint(user, fullLengthAllocation);
emit VestingAllocated(user, planId, fullLengthAllocation);
return fullLengthAllocation;
}

function _updateUserAllocation(address user, uint256 amount) private {
function _updateUserAllocation(address user, uint128 amount) private {
StorageUserAllocation storage ua = allocations[user];
ua.claimed += uint128(amount);
ua.claimed += amount;
}
}
98 changes: 27 additions & 71 deletions publish/ABI/Vesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,61 +135,19 @@
{
"inputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
},
{
"internalType": "uint256",
"name": "planId",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "allocation",
"type": "uint256"
}
],
"name": "allocateVesting",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "allocations",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256[]",
"internalType": "uint64[]",
"name": "_planIds",
"type": "uint256[]"
"type": "uint64[]"
},
{
"internalType": "address[]",
"name": "_addrs",
"name": "_users",
"type": "address[]"
},
{
"internalType": "uint256[]",
"internalType": "uint64[]",
"name": "_allocations",
"type": "uint256[]"
"type": "uint64[]"
}
],
"name": "batchAllocateVesting",
Expand All @@ -208,7 +166,7 @@
"inputs": [
{
"internalType": "address",
"name": "account",
"name": "user",
"type": "address"
}
],
Expand Down Expand Up @@ -236,25 +194,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "claimed",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
Expand Down Expand Up @@ -405,16 +344,33 @@
"inputs": [
{
"internalType": "address",
"name": "",
"name": "user",
"type": "address"
}
],
"name": "userPlanId",
"name": "userAllocation",
"outputs": [
{
"internalType": "uint256",
"components": [
{
"internalType": "uint64",
"name": "planId",
"type": "uint64"
},
{
"internalType": "uint256",
"name": "allocation",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "claimed",
"type": "uint256"
}
],
"internalType": "struct Vesting.UserAllocation",
"name": "",
"type": "uint256"
"type": "tuple"
}
],
"stateMutability": "view",
Expand Down
6 changes: 3 additions & 3 deletions publish/testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
},
"Vesting": {
"innerAddress": "",
"address": "0xdb50e2Cd0484007000fBb64326777AE52A84Eb75",
"bytecodeHash": "a95aa683c52274b6cb4b2e4a1faad76b08aea91aa640f8da9410e1172d3feba9",
"lastUpdate": "Mon, 12 Feb 2024 17:30:57 GMT"
"address": "0x61562768175432072cdD6C25De9ED74e24f1a458",
"bytecodeHash": "6474fb048ba8a9246340861f1dfdbb9dcacea8c13266c132a856e22442cb2507",
"lastUpdate": "Wed, 14 Feb 2024 07:56:53 GMT"
}
},
"child": {
Expand Down
Loading

0 comments on commit 14627e8

Please sign in to comment.