Skip to content

Commit

Permalink
remove farming
Browse files Browse the repository at this point in the history
  • Loading branch information
catel committed Aug 31, 2023
1 parent a8be1f3 commit a023e5c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/node_modules/
/*.iml
/artifacts/
/coverage/
7 changes: 3 additions & 4 deletions contracts/TrendsAirdrop.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {MerkleProof} from "@openzeppelin/contracts/utils/cryptography/MerkleProo
import {SafeERC20, IERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {TrendsSharesV1} from "./TrendsSharesV1.sol";


contract TrendsAirdrop {
using SafeERC20 for IERC20;

Expand Down Expand Up @@ -45,7 +44,8 @@ contract TrendsAirdrop {
event VestingStarted(address indexed user, uint256 amount);
event Claimed(address indexed user, uint256 amount);

constructor(TrendsSharesV1 _trendsShare,
constructor(
TrendsSharesV1 _trendsShare,
address _trendsToken,
bytes32 _merkleRoot,
uint256 _deadline,
Expand Down Expand Up @@ -84,7 +84,6 @@ contract TrendsAirdrop {
emit VestingStarted(msg.sender, amount);
}


function claimVestedAirdrop() external {
Vesting storage v = vesting[msg.sender];
if (v.amount == 0) revert NoVestedAmount();
Expand All @@ -104,4 +103,4 @@ contract TrendsAirdrop {
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? b : a;
}
}
}
2 changes: 0 additions & 2 deletions contracts/TrendsOFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity >=0.8.0 <0.9.0;
import "@layerzerolabs/solidity-examples/contracts/token/oft/extension/PausableOFT.sol";

contract TrendsOFT is PausableOFT {

constructor(string memory _name, string memory _symbol, address _lzEndpoint, uint256 _mintAmount) PausableOFT(_name, _symbol, _lzEndpoint) {
if (_mintAmount > 0) {
_mint(msg.sender, _mintAmount);
Expand All @@ -14,5 +13,4 @@ contract TrendsOFT is PausableOFT {
function burn(uint256 _amount) external {
_burn(_msgSender(), _amount);
}

}
55 changes: 20 additions & 35 deletions contracts/TrendsSharesV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ contract TrendsSharesV1 is Ownable {
event Trade(
address trader,
bytes32 subject,
bool isBuy,
bool isBuy,
uint256 shares,
uint256 price,
uint256 protocolFee,
uint256 lpFarmingFee,
uint256 creatorFee,
uint256 holderFee,
uint256 supply
Expand All @@ -42,10 +41,8 @@ contract TrendsSharesV1 is Ownable {

IERC20 public immutable TRENDS;
address public protocolFeeDestination;
address public lpFarmingAddress;

uint256 public protocolFeePercent;
uint256 public lpFarmingFeePercent;
uint256 public creatorFeePercent;
uint256 public holderFeePercent;

Expand Down Expand Up @@ -74,7 +71,8 @@ contract TrendsSharesV1 is Ownable {
function createShares(bytes32 subject, uint24 declineRatio) external {
if (sharesCreator[subject] != address(0)) revert ShareCreated();
sharesCreator[subject] = msg.sender;
if (declineRatio * (1 ether / declineRatio) != 1 ether) revert InvalidDeclineRatio(); // Make sure declineRatio is fully divided in calculation later
if (declineRatio * (1 ether / declineRatio) != 1 ether) revert InvalidDeclineRatio();
// Make sure declineRatio is fully divided in calculation later
sharesDeclineRatio[subject] = declineRatio;
emit Create(msg.sender, subject);
_buyShares(msg.sender, subject, 1, 0);
Expand All @@ -90,18 +88,18 @@ contract TrendsSharesV1 is Ownable {
if (recipient == address(0)) revert Address0();
if (supply == 0 && sharesCreator[subject] != recipient) revert ShareNotExists();
uint256 price = getPrice(supply, shares, sharesDeclineRatio[subject]);
(uint256 protocolFee, uint256 lpFarmingFee, uint256 creatorFee, uint256 holderFee) = _getFees(price);
uint256 totalInAmount = price + protocolFee + lpFarmingFee + creatorFee + holderFee;
(uint256 protocolFee, uint256 creatorFee, uint256 holderFee) = _getFees(price);
uint256 totalInAmount = price + protocolFee + creatorFee + holderFee;
if (totalInAmount > maxInAmount) revert InAmountNotEnough();
//update shares reward
_updateSharesReward(subject, holderFee, recipient);
sharesBalance[subject][recipient] = sharesBalance[subject][recipient] + shares;
sharesBalance[subject][recipient] += shares;
uint256 totalSupply = supply + shares;
sharesSupply[subject] = totalSupply;
emit Trade(recipient, subject, true, shares, price, protocolFee, lpFarmingFee, creatorFee, holderFee, totalSupply);
emit Trade(recipient, subject, true, shares, price, protocolFee, creatorFee, holderFee, totalSupply);
if (price > 0) {
TRENDS.safeTransferFrom(msg.sender, address(this), totalInAmount);
_collectFees(subject, protocolFee, lpFarmingFee, creatorFee);
_collectFees(subject, protocolFee, creatorFee);
}
}

Expand All @@ -110,18 +108,18 @@ contract TrendsSharesV1 is Ownable {
if (shares >= supply) revert CannotSellLastShare();
if (shares > sharesBalance[subject][msg.sender]) revert InsufficientShares();
uint256 price = getPrice(supply - shares, shares, sharesDeclineRatio[subject]);
(uint256 protocolFee, uint256 lpFarmingFee, uint256 creatorFee, uint256 holderFee) = _getFees(price);
uint256 totalOutAmount = price - protocolFee - lpFarmingFee - creatorFee - holderFee;
(uint256 protocolFee, uint256 creatorFee, uint256 holderFee) = _getFees(price);
uint256 totalOutAmount = price - protocolFee - creatorFee - holderFee;
if (totalOutAmount < minOutAmount) revert OutAmountNotEnough();
//update shares reward
_updateSharesReward(subject, holderFee, msg.sender);
sharesBalance[subject][msg.sender] = sharesBalance[subject][msg.sender] - shares;
sharesBalance[subject][msg.sender] -= shares;
uint256 totalSupply = supply - shares;
sharesSupply[subject] = totalSupply;
emit Trade(msg.sender, subject, false, shares, price, protocolFee, lpFarmingFee, creatorFee, holderFee, totalSupply);
emit Trade(msg.sender, subject, false, shares, price, protocolFee, creatorFee, holderFee, totalSupply);
if (price > 0) {
TRENDS.safeTransfer(recipient, totalOutAmount);
_collectFees(subject, protocolFee, lpFarmingFee, creatorFee);
_collectFees(subject, protocolFee, creatorFee);
}
}

Expand All @@ -142,30 +140,26 @@ contract TrendsSharesV1 is Ownable {

function getBuyPriceWithFees(bytes32 subject, uint256 amount) external view returns (uint256) {
uint256 price = getBuyPrice(subject, amount);
(uint256 protocolFee, uint256 lpFarmingFee, uint256 creatorFee, uint256 holderFee) = _getFees(price);
return price + protocolFee + lpFarmingFee + creatorFee + holderFee;
(uint256 protocolFee, uint256 creatorFee, uint256 holderFee) = _getFees(price);
return price + protocolFee + creatorFee + holderFee;
}

function getSellPriceWithFees(bytes32 subject, uint256 amount) external view returns (uint256) {
uint256 price = getSellPrice(subject, amount);
(uint256 protocolFee, uint256 lpFarmingFee, uint256 creatorFee, uint256 holderFee) = _getFees(price);
return price - protocolFee - lpFarmingFee - creatorFee - holderFee;
(uint256 protocolFee, uint256 creatorFee, uint256 holderFee) = _getFees(price);
return price - protocolFee - creatorFee - holderFee;
}

function _getFees(uint256 price) internal view returns (uint256 protocolFee, uint256 lpFarmingFee, uint256 creatorFee, uint256 holderFee) {
function _getFees(uint256 price) internal view returns (uint256 protocolFee, uint256 creatorFee, uint256 holderFee) {
protocolFee = (price * protocolFeePercent) / 1 ether;
lpFarmingFee = (price * lpFarmingFeePercent) / 1 ether;
creatorFee = (price * creatorFeePercent) / 1 ether;
holderFee = (price * holderFeePercent) / 1 ether;
}

function _collectFees(bytes32 subject, uint256 protocolFee, uint256 lpFarmingFee, uint256 creatorFee) internal {
function _collectFees(bytes32 subject, uint256 protocolFee, uint256 creatorFee) internal {
if (protocolFee > 0) {
TRENDS.safeTransfer(protocolFeeDestination, protocolFee);
}
if (lpFarmingFee > 0) {
//TODO
}
if (creatorFee > 0) {
TRENDS.safeTransfer(sharesCreator[subject], creatorFee);
}
Expand All @@ -188,7 +182,7 @@ contract TrendsSharesV1 is Ownable {
if (newReward == 0 || sharesSupply[subject] == 0) {
return;
}
rewardPerShareStored[subject] += newReward * (1 ether) / sharesSupply[subject];
rewardPerShareStored[subject] += (newReward * (1 ether)) / sharesSupply[subject];
_updateHolderReward(subject, holder);
}

Expand All @@ -209,20 +203,11 @@ contract TrendsSharesV1 is Ownable {
protocolFeeDestination = _protocolFeeDestination;
}

function setLpFarmingAddress(address _lpFarmingAddress) external onlyOwner {
lpFarmingAddress = _lpFarmingAddress;
}

function setProtocolFeePercent(uint256 _protocolFeePercent) external onlyOwner {
if (_protocolFeePercent >= 1 ether) revert InvalidParams();
protocolFeePercent = _protocolFeePercent;
}

function setLpFarmingFeePercent(uint256 _lpFarmingFeePercent) external onlyOwner {
if (_lpFarmingFeePercent >= 1 ether) revert InvalidParams();
lpFarmingFeePercent = _lpFarmingFeePercent;
}

function setCreatorFeePercent(uint256 _creatorFeePercent) external onlyOwner {
if (_creatorFeePercent >= 1 ether) revert InvalidParams();
creatorFeePercent = _creatorFeePercent;
Expand Down
13 changes: 0 additions & 13 deletions test/TrendsSharesV1.owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ contract('TrendsSharesV1', function (accounts) {
await expectRevert(trendsSharesV1.setProtocolFeeDestination(acc1, {from: acc1}), onlyOwnerError);
});

it('set lpFarming address only owner', async function () {
await trendsSharesV1.setLpFarmingAddress(acc1, {from: developer});
expect(await trendsSharesV1.lpFarmingAddress()).to.eq(acc1);
await expectRevert(trendsSharesV1.setLpFarmingAddress(acc1, {from: acc1}), onlyOwnerError);
});

it('set protocol fee percent only owner', async function () {
await trendsSharesV1.setProtocolFeePercent(1, {from: developer});
expect(await trendsSharesV1.protocolFeePercent()).to.be.bignumber.equal(new BN(1));
Expand All @@ -40,13 +34,6 @@ contract('TrendsSharesV1', function (accounts) {

});

it('set lpFarming fee percent only owner', async function () {
await trendsSharesV1.setLpFarmingFeePercent(1, {from: developer});
expect(await trendsSharesV1.lpFarmingFeePercent()).to.be.bignumber.equal(new BN(1));
await expectRevert(trendsSharesV1.setLpFarmingFeePercent(1, {from: acc1}), onlyOwnerError);
await expectRevertCustomError(trendsSharesV1.setLpFarmingFeePercent(eth_1, {from: developer}), invalidFeeError);
});

it('set holder fee percent only owner', async function () {
await trendsSharesV1.setHolderFeePercent(1, {from: developer});
expect(await trendsSharesV1.holderFeePercent()).to.be.bignumber.equal(new BN(1));
Expand Down
6 changes: 6 additions & 0 deletions test/TrendsSharesV1.reward.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ contract('TrendsSharesV1', function (accounts) {
share2Price.mul(holderFeePercent).div(eth_1).divn(2)));
});

it('reward increases after buyer1 buy shares the second times', async function () {
await trendsSharesV1.buyShares(buyer1, subject0, 1, maxInAmount, {from: buyer1});
await trendsSharesV1.buyShares(buyer1, subject0, 2, maxInAmount, {from: buyer1});
expect(await trendsSharesV1.getReward(subject0, buyer1)).to.be.bignumber.equal(share2Price.add(share3Price).mul(holderFeePercent).div(eth_1).divn(2));
});

it('reward increases after sell shares', async function () {
await trendsSharesV1.buyShares(buyer1, subject0, 1, maxInAmount, {from: buyer1});
await trendsSharesV1.buyShares(buyer2, subject0, 1, maxInAmount, {from: buyer2});
Expand Down
15 changes: 3 additions & 12 deletions test/TrendsSharesV1.shares.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ const {ZERO_ADDRESS} = require("@openzeppelin/test-helpers/src/constants");


const protocolFeePercent = new BN(toWei('1', 'ether')).divn(100);
const lpFarmingFeePercent = new BN(toWei('2', 'ether')).divn(100);
const holderFeePercent = new BN(toWei('4', 'ether')).divn(100);
const creatorFeePercent = new BN(toWei('8', 'ether')).divn(100);
const protocolFee = share1Price.mul(protocolFeePercent).div(eth_1);
const lpFarmingFee = share1Price.mul(lpFarmingFeePercent).div(eth_1);
const creatorFee = share1Price.mul(creatorFeePercent).div(eth_1);
const holderFee = share1Price.mul(holderFeePercent).div(eth_1);
const totalFees = protocolFee.add(lpFarmingFee).add(creatorFee).add(holderFee);
const totalFees = protocolFee.add(creatorFee).add(holderFee);

let trendsToken;
let trendsSharesV1;
Expand Down Expand Up @@ -55,7 +53,6 @@ contract('TrendsSharesV1', function (accounts) {
shares: new BN(1),
price: new BN(0),
protocolFee: new BN(0),
lpFarmingFee: new BN(0),
creatorFee: new BN(0),
holderFee: new BN(0),
supply: new BN(1)
Expand Down Expand Up @@ -101,7 +98,6 @@ contract('TrendsSharesV1', function (accounts) {
shares: new BN(1),
price: share1Price,
protocolFee: new BN(0),
lpFarmingFee: new BN(0),
creatorFee: new BN(0),
holderFee: new BN(0),
supply: new BN(2)
Expand Down Expand Up @@ -187,7 +183,6 @@ contract('TrendsSharesV1', function (accounts) {
shares: new BN(1),
price: share1Price,
protocolFee: new BN(0),
lpFarmingFee: new BN(0),
creatorFee: new BN(0),
holderFee: new BN(0),
supply: new BN(1)
Expand Down Expand Up @@ -265,7 +260,6 @@ contract('TrendsSharesV1', function (accounts) {
shares: new BN(1),
price: share1Price,
protocolFee: share1Price.mul(protocolFeePercent).div(eth_1),
lpFarmingFee: share1Price.mul(lpFarmingFeePercent).div(eth_1),
creatorFee: share1Price.mul(creatorFeePercent).div(eth_1),
holderFee: share1Price.mul(holderFeePercent).div(eth_1),
supply: new BN(2)
Expand All @@ -275,7 +269,7 @@ contract('TrendsSharesV1', function (accounts) {
it('distribute fees correct after buy shares', async function () {
await trendsSharesV1.buyShares(buyer1, subject0, 1, maxInAmount, {from: buyer1});
expect(await trendsToken.balanceOf(protocolFeeDestination)).to.be.bignumber.equal(protocolFee);
expect(await trendsToken.balanceOf(trendsSharesV1.address)).to.be.bignumber.equal(share1Price.add(lpFarmingFee).add(holderFee));
expect(await trendsToken.balanceOf(trendsSharesV1.address)).to.be.bignumber.equal(share1Price.add(holderFee));
expect(await trendsToken.balanceOf(creator1)).to.be.bignumber.equal(creatorFee);
});

Expand All @@ -296,7 +290,6 @@ contract('TrendsSharesV1', function (accounts) {
shares: new BN(1),
price: share1Price,
protocolFee: share1Price.mul(protocolFeePercent).div(eth_1),
lpFarmingFee: share1Price.mul(lpFarmingFeePercent).div(eth_1),
creatorFee: share1Price.mul(creatorFeePercent).div(eth_1),
holderFee: share1Price.mul(holderFeePercent).div(eth_1),
supply: new BN(1)
Expand All @@ -307,7 +300,7 @@ contract('TrendsSharesV1', function (accounts) {
await trendsSharesV1.buyShares(buyer1, subject0, 1, maxInAmount, {from: buyer1});
await trendsSharesV1.sellShares(buyer1, subject0, 1, minOutAmount, {from: buyer1});
expect(await trendsToken.balanceOf(protocolFeeDestination)).to.be.bignumber.equal(protocolFee.muln(2));
expect(await trendsToken.balanceOf(trendsSharesV1.address)).to.be.bignumber.equal((lpFarmingFee.add(holderFee)).muln(2));
expect(await trendsToken.balanceOf(trendsSharesV1.address)).to.be.bignumber.equal((holderFee).muln(2));
expect(await trendsToken.balanceOf(buyer1)).to.be.bignumber.equal(initBalance.sub(totalFees.muln(2)));
});

Expand Down Expand Up @@ -382,9 +375,7 @@ contract('TrendsSharesV1', function (accounts) {

async function initFee() {
await trendsSharesV1.setProtocolFeeDestination(protocolFeeDestination, {from: developer});
await trendsSharesV1.setLpFarmingAddress(lpFarmingAddress, {from: developer});
await trendsSharesV1.setProtocolFeePercent(protocolFeePercent, {from: developer});
await trendsSharesV1.setLpFarmingFeePercent(lpFarmingFeePercent, {from: developer});
await trendsSharesV1.setHolderFeePercent(holderFeePercent, {from: developer});
await trendsSharesV1.setCreatorFeePercent(creatorFeePercent, {from: developer});
}

0 comments on commit a023e5c

Please sign in to comment.