Skip to content

Commit

Permalink
add: onetimereward
Browse files Browse the repository at this point in the history
  • Loading branch information
sirpy committed May 19, 2024
1 parent 930db95 commit d9eaef4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
87 changes: 87 additions & 0 deletions contracts/invite/OneTimeReward.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

import "../Interfaces.sol";
import "../utils/NameService.sol";
import "../utils/DAOUpgradeableContract.sol";

contract OneTimeReward is Ownable, DAOContract {
bool public isActive;
uint public rewardAmount;
mapping(address => bool) public claimed;

event RewardClaimed(address indexed user, uint amount);

constructor(uint256 _rewardAmount, INameService _nameService) {
rewardAmount = _rewardAmount;
isActive = true;
setDAO(_nameService);
}

function getIdentity() public view returns (IIdentityV2) {
return IIdentityV2(nameService.getAddress("IDENTITY"));
}

function updateSettings(
bool _isActive,
uint _rewardAmount
) external onlyOwner {
isActive = _isActive;
rewardAmount = _rewardAmount;
}

function checkActiveAndBalance() public view returns (bool) {
if (!isActive) {
return false;
}

if (nativeToken().balanceOf(address(this)) < rewardAmount) {
return false;
}

return true;
}

function checkCanClaimReward(address _user) public view returns (bool) {
address whitelistedRoot = getIdentity().getWhitelistedRoot(_user);
return canClaimReward(whitelistedRoot);
}

function canClaimReward(
address whitelistedRoot
) internal view returns (bool) {
if (checkActiveAndBalance() == false) {
return false;
}

if (whitelistedRoot == address(0)) {
return false;
}

if (claimed[whitelistedRoot]) {
return false;
}

return true;
}

function claimReward(address _user) public {
address whitelistedRoot = getIdentity().getWhitelistedRoot(_user);
require(canClaimReward(whitelistedRoot), "User cannot claim reward");
claimed[whitelistedRoot] = true;

nativeToken().transfer(_user, rewardAmount);

emit RewardClaimed(_user, rewardAmount);
}

function withdrawAll(address _token) external onlyOwner {
uint balance = IERC20(_token).balanceOf(address(this));
require(balance > 0, "No tokens to withdraw");

IERC20(_token).transfer(msg.sender, balance);
}
}
10 changes: 6 additions & 4 deletions scripts/deployFullDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export const createDAO = async () => {
.deploy()
.then(printDeploy)) as Contract;

await BancorFormula.init()

const AddFounders = (await AddFoundersFactory.deploy().then(
printDeploy
)) as Contract;
Expand Down Expand Up @@ -421,14 +423,14 @@ const deployMainnet = async (Avatar, Identity) => {
let DAIEthOracle = daiEthOracleAddr
? await ethers.getContractAt("DaiEthPriceMockOracle", daiEthOracleAddr)
: ((await (await ethers.getContractFactory("DaiEthPriceMockOracle"))
.deploy()
.then(printDeploy)) as Contract);
.deploy()
.then(printDeploy)) as Contract);

let ETHUsdOracle = ethUsdOracleAddr
? await ethers.getContractAt("EthUSDMockOracle", ethUsdOracleAddr)
: ((await (await ethers.getContractFactory("EthUSDMockOracle"))
.deploy()
.then(printDeploy)) as Contract);
.deploy()
.then(printDeploy)) as Contract);

return {
Contribution,
Expand Down
2 changes: 2 additions & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ export const createDAO = async (tokenType: "super" | "regular" = "super") => {
await ethers.getContractFactory("BancorFormula")
).deploy();

await BancorFormula.init();

console.log("deploy upgradeable identity...");

const Identity = await upgrades.deployProxy(
Expand Down
3 changes: 2 additions & 1 deletion test/reserve/GoodReserveCDai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ describe("GoodReserve - staking with cDAI mocks", () => {
"BancorFormula",
await marketMaker.getBancor()
);

const expectedReturn = await bancor.calculateSaleReturn(
supplyBefore.toString(),
reserveBalanceBefore.toString(),
Expand Down Expand Up @@ -966,7 +967,7 @@ describe("GoodReserve - staking with cDAI mocks", () => {
parseInt(reserveBalance.toString()) *
(1 -
(1 - amount.toNumber() / parseInt(supply.toString())) **
(1000000 / reserveToken.reserveRatio));
(1000000 / reserveToken.reserveRatio));

expected = Math.ceil((0.8 * expected) / 100) * 100; //deduct 20% contribution, allow 5 points precission mismatch (due to bancor pow estimation?), match solidity no floating point
//expected = Math.floor(0.8 * expected);
Expand Down

0 comments on commit d9eaef4

Please sign in to comment.