Contract Changes
Many are familiar with yield farming protocols including Yam, Sushi and Lua.
Many may expect that Bao follows the same exact patterns of these systems. For transparency we wanted to outline some key changes that this system has.

Authorization Module

1
contract Authorizable is Ownable {
2
​
3
mapping(address => bool) public authorized;
4
​
5
modifier onlyAuthorized() {
6
require(authorized[msg.sender] || owner() == msg.sender);
7
_;
8
}
9
​
10
function addAuthorized(address _toAdd) onlyOwner public {
11
authorized[_toAdd] = true;
12
}
13
​
14
function removeAuthorized(address _toRemove) onlyOwner public {
15
require(_toRemove != msg.sender);
16
authorized[_toRemove] = false;
17
}
18
​
19
}
Copied!
The authorization module is an extension of the ownable module standard for ERC-20 contracts. This allows us to create list of approved wallets who can use specific commands as if they were an owner. We did this because the BaoMasterFarmer contract must be an owner of the Bao Token contract, but in the future we will wish to migrate this to new contracts such as synthetic asset contracts.
The authorization module is used only in reclaiming ownership and minting the initial issuance of tokens for seeding the ecosystem.

Timelocker Module

Similar to the Authorization Module extension, we have created a "Timelocker" status as well.
The Timelocker status locks the migration (and only the migration) so that it will always need to go through the timelock contract.

Locking System

Unlike Sushi distributions, we also add the concept of "Locking". When a user earns the bao token, 95% of it is locked up and distributed to them over a 3-year time period and only 5% can be harvested and available immediately. The dev fund and founders fund have these same lockups.
The lockups do not begin to release until after 1-year, and they unlock linearly, with every block over the 3 years. This means that while users earn their bao early, they will vest their tokens over time just like the teams do, in order to create harmonious users who are aligned with the community goals.
Because the lock of the token is part of the bao token natively, this means once a user earns it, the Bao Team can not retrieve the tokens from the user. It is in their possession even if it is locked. They will be able to claim and use the token once the timelock expires.
1
function canUnlockAmount(address _holder) public view returns (uint256) {
2
if (block.number < lockFromBlock) {
3
return 0;
4
}
5
else if (block.number >= lockToBlock) {
6
return _locks[_holder];
7
}
8
else {
9
uint256 releaseBlock = block.number.sub(_lastUnlockBlock[_holder]);
10
uint256 numberLockBlock = lockToBlock.sub(_lastUnlockBlock[_holder]);
11
return _locks[_holder].mul(releaseBlock).div(numberLockBlock);
12
}
13
}
14
​
15
function unlock() public {
16
require(_locks[msg.sender] > 0, "ERC20: cannot unlock");
17
18
uint256 amount = canUnlockAmount(msg.sender);
19
// just for sure
20
if (amount > balanceOf(address(this))) {
21
amount = balanceOf(address(this));
22
}
23
_transfer(address(this), msg.sender, amount);
24
_locks[msg.sender] = _locks[msg.sender].sub(amount);
25
_lastUnlockBlock[msg.sender] = block.number;
26
_totalLock = _totalLock.sub(amount);
27
}
28
​
29
// This function is for dev address migrate all balance to a multi sig address
30
function transferAll(address _to) public {
31
_locks[_to] = _locks[_to].add(_locks[msg.sender]);
32
​
33
if (_lastUnlockBlock[_to] < lockFromBlock) {
34
_lastUnlockBlock[_to] = lockFromBlock;
35
}
36
​
37
if (_lastUnlockBlock[_to] < _lastUnlockBlock[msg.sender]) {
38
_lastUnlockBlock[_to] = _lastUnlockBlock[msg.sender];
39
}
40
​
41
_locks[msg.sender] = 0;
42
_lastUnlockBlock[msg.sender] = 0;
43
​
44
_transfer(msg.sender, _to, balanceOf(msg.sender));
45
}
Copied!

Block Deltas and Fee/Penalty System

As part of each user object, we now include tracking of deposit and withdrawal blocks. This is to allow us to track the time between deposits and withdrawals. We use this for our fee and penalty system that discourages short term users from rapidly leaving the system
1
// Info of each user.
2
struct UserInfo {
3
uint256 amount; // How many LP tokens the user has provided.
4
uint256 rewardDebt; // Reward debt. See explanation below.
5
uint256 rewardDebtAtBlock; // the last block user stake
6
uint256 lastWithdrawBlock; // the last block a user withdrew at.
7
uint256 firstDepositBlock; // the last block a user deposited at.
8
uint256 blockdelta; //time passed since withdrawals
9
uint256 lastDepositBlock;
Copied!
When users makes a withdraw, we check how long they have been in the pool and apply a small fee or a large penalty depending on the behavior. For example a fee of 0.25% is taken if the user was in the pool for more than 2 weeks but less than 4 weeks, but if the user tries to do a flash loan exploit and leave in the same block they have entered then we apply 25% penalty for discouragement.
​
1
if(user.blockdelta == 0 || block.number == user.lastDepositBlock){
2
//25% fee for withdrawals of LP tokens in the same block this is to prevent abuse from flashloans
3
pool.lpToken.safeTransfer(address(msg.sender), _amount.mul(75).div(100));
4
pool.lpToken.safeTransfer(address(devaddr), _amount.mul(25).div(100));
5
} else if (user.blockdelta > 0 && user.blockdelta <= 274){
6
//8% fee if a user deposits and withdraws in under between same block and 59 minutes.
7
pool.lpToken.safeTransfer(address(msg.sender), _amount.mul(92).div(100));
8
pool.lpToken.safeTransfer(address(devaddr), _amount.mul(8).div(100));
9
} else if (user.blockdelta >= 275 && user.blockdelta <= 6600){
10
//4% fee if a user deposits and withdraws after 1 hour but before 1 day.
11
pool.lpToken.safeTransfer(address(msg.sender), _amount.mul(96).div(100));
12
pool.lpToken.safeTransfer(address(devaddr), _amount.mul(4).div(100));
13
} else if (user.blockdelta >= 6601 && user.blockdelta <= 19800){
14
//2% fee if a user deposits and withdraws between after 1 day but before 3 days.
15
pool.lpToken.safeTransfer(address(msg.sender), _amount.mul(98).div(100));
16
pool.lpToken.safeTransfer(address(devaddr), _amount.mul(2).div(100));
17
} else if (user.blockdelta >= 19441 && user.blockdelta <= 33000){
18
//1% fee if a user deposits and withdraws after 3 days but before 5 days.
19
pool.lpToken.safeTransfer(address(msg.sender), _amount.mul(99).div(100));
20
pool.lpToken.safeTransfer(address(devaddr), _amount.mul(1).div(100));
21
} else if (user.blockdelta >= 33001 && user.blockdelta <= 90720){
22
//0.5% fee if a user deposits and withdraws if the user withdraws after 5 days but before 2 weeks.
23
pool.lpToken.safeTransfer(address(msg.sender), _amount.mul(995).div(1000));
24
pool.lpToken.safeTransfer(address(devaddr), _amount.mul(5).div(1000));
25
} else if (user.blockdelta >= 90721 && user.blockdelta <= 181440){
26
//0.25% fee if a user deposits and withdraws after 2 weeks.
27
pool.lpToken.safeTransfer(address(msg.sender), _amount.mul(9975).div(10000));
28
pool.lpToken.safeTransfer(address(devaddr), _amount.mul(25).div(10000));
29
} else if (user.blockdelta > 181441) {
30
//0.1% fee if a user deposits and withdraws after 4 weeks.
31
pool.lpToken.safeTransfer(address(msg.sender), _amount.mul(9999).div(10000));
32
pool.lpToken.safeTransfer(address(devaddr), _amount.mul(1).div(10000));
33
}
Copied!

Revision Functions:

Since tracking block delta is experimental, and there may be other issues around fee tracking the owner has two commands to manually update the user deposit and withdraw numbers in case of emergency:
1
function reviseWithdraw(uint _pid, address _user, uint256 _block) public onlyOwner {
2
UserInfo storage user = userInfo[_pid][_user];
3
user.lastWithdrawBlock = _block;
4
​
5
}
6
​
7
function reviseDeposit(uint _pid, address _user, uint256 _block) public onlyOwner {
8
UserInfo storage user = userInfo[_pid][_user];
9
user.firstDepositBlock = _block;
10
​
11
}
Copied!

Manual Mint

The authorized owner of the Bao Token contract has a specific command for manual minting the initial allocation of Bao. This allows them to only create up to the approved cap and then never mint again:
1
function manualMint(address _to, uint256 _amount) public onlyAuthorized {
2
if(manualMinted < manualMintLimit){
3
_mint(_to, _amount);
4
_moveDelegates(address(0), _delegates[_to], _amount);
5
manualMinted = manualMinted + _amount;
6
}
7
}
Copied!

Reclaim Owner

Since the Bao Token contract needs to be owned by the BaoMasterFarmer contract we have added a function for reclaiming ownership back to the deployer address:
1
function reclaimOwnership(address newOwner) public virtual onlyPreviousOwner {
2
require(newOwner == _previousOwner, "Ownable: new owner must be previous owner");
3
emit OwnershipTransferred(_owner, newOwner);
4
_owner = newOwner;
5
}
Copied!
Last modified 9mo ago