Skip to content

Commit

Permalink
token, treasury, vesting test
Browse files Browse the repository at this point in the history
  • Loading branch information
cblanquera committed Mar 7, 2022
1 parent 94af202 commit 69cc4c6
Show file tree
Hide file tree
Showing 8 changed files with 842 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface IERC20Capped is IERC20 {

error InvalidAmount();

contract GryphEconomyEngine is
contract GryphEconomy is
AccessControlEnumerable,
ReentrancyGuard,
Pausable
Expand Down
4 changes: 2 additions & 2 deletions contracts/GryphToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract GryphToken is
* to the account that deploys the contract.
*/
constructor()
ERC20("Arkonia", "AOD")
ERC20("GRYPH", "GRYPH")
ERC20Capped(1000000000 ether)
{
address sender = _msgSender();
Expand All @@ -52,7 +52,7 @@ contract GryphToken is
* @dev Creates `amount` new tokens for `to`.
*/
function mint(address to, uint256 amount)
public virtual onlyRole(MINTER_ROLE)
public virtual whenNotPaused onlyRole(MINTER_ROLE)
{
_mint(to, amount);
}
Expand Down
47 changes: 30 additions & 17 deletions contracts/GryphVesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ interface IGryphToken is IERC20 {

// ============ Errors ============

error InvalidStage();
error InvalidRefund();
error InvalidRelease();
error InvalidVesting();
error InvalidRefund();
error InvalidWithdraw();

contract GryphVesting is
Expand Down Expand Up @@ -117,7 +118,7 @@ contract GryphVesting is
* Default implementation is a linear vesting curve.
*/
function totalReleasableAmount(address beneficiary, uint64 timestamp)
public view virtual returns (uint256)
public view returns (uint256)
{
uint amount = totalVestedAmount(beneficiary, timestamp);
return amount - releasedTokens[beneficiary];
Expand All @@ -128,7 +129,7 @@ contract GryphVesting is
* Default implementation is a linear vesting curve.
*/
function totalVestedAmount(address beneficiary, uint64 timestamp)
public view virtual returns (uint256)
public view returns (uint256)
{
//if time now is more than the vested date
if (timestamp > VESTED_DATE) {
Expand All @@ -155,7 +156,7 @@ contract GryphVesting is
/**
* @dev Returns the unlock date
*/
function unlockDate() public virtual view returns(uint64) {
function unlockDate() public view returns(uint64) {
if (_unlockedDate > 0) {
return _unlockedDate;
}
Expand All @@ -168,7 +169,7 @@ contract GryphVesting is
* @dev Allows anyone to invest during the current stage for an `amount`
*/
function buy(address beneficiary, uint256 amount)
external virtual payable nonReentrant
external payable nonReentrant
{
// if no amount
if (amount == 0
Expand All @@ -179,7 +180,7 @@ contract GryphVesting is
//if the amount exceeds the token limit
|| (currentTokenAllocated + amount) > currentTokenLimit
//calculate eth amount = 1000 * 0.000005 ether
|| msg.value < (amount * currentTokenPrice)
|| msg.value < ((amount * currentTokenPrice) / 1 ether)
) revert InvalidVesting();

//track ether collected for refund
Expand All @@ -193,7 +194,7 @@ contract GryphVesting is
*
* Emits a {TokensReleased} event.
*/
function release(address beneficiary) public virtual nonReentrant {
function release(address beneficiary) public nonReentrant {
//if paused or not unlocked yet
if (paused() || uint64(block.timestamp) < unlockDate())
revert InvalidRelease();
Expand Down Expand Up @@ -222,7 +223,7 @@ contract GryphVesting is
/**
* @dev Release $GRYPH that have already vested.
*/
function refund(address beneficiary) public virtual nonReentrant {
function refund(address beneficiary) public nonReentrant {
//should not refund if paused
if (paused()
//should not refund if not refunding
Expand Down Expand Up @@ -253,42 +254,54 @@ contract GryphVesting is
/**
* @dev Pauses all token transfers.
*/
function pause() public virtual onlyRole(PAUSER_ROLE) {
function pause() public onlyRole(PAUSER_ROLE) {
_pause();
}

/**
* @dev Unpauses all token transfers.
*/
function refundAll(bool yes)
public virtual onlyRole(DEFAULT_ADMIN_ROLE)
public onlyRole(DEFAULT_ADMIN_ROLE)
{
//dont allow refund if something was withdrawn
if (currentTotalWithdrawn > 0) revert InvalidRefund();
refunding = yes;
}

/**
* @dev Updates the sale stage
*/
function stage(uint256 price, uint256 limit)
public onlyRole(DEFAULT_ADMIN_ROLE)
{
if (price < currentTokenPrice || limit < currentTokenLimit)
revert InvalidStage();
currentTokenPrice = price;
currentTokenLimit = limit;
}

/**
* @dev Unlocks vesting tokens
*/
function unlock(uint64 timestamp)
public virtual onlyRole(DEFAULT_ADMIN_ROLE)
public onlyRole(DEFAULT_ADMIN_ROLE)
{
_unlockedDate = timestamp;
}

/**
* @dev Unpauses all token transfers.
*/
function unpause() public virtual onlyRole(PAUSER_ROLE) {
function unpause() public onlyRole(PAUSER_ROLE) {
_unpause();
}

/**
* @dev Allow an admin to manually vest a `beneficiary` for an `amount`
*/
function vest(address beneficiary, uint256 amount)
public virtual onlyRole(DEFAULT_ADMIN_ROLE)
public onlyRole(DEFAULT_ADMIN_ROLE)
{
_vest(beneficiary, amount);
}
Expand All @@ -297,7 +310,7 @@ contract GryphVesting is
* @dev Sends the specified `amount` to the treasury
*/
function sendToTreasury(uint256 amount)
external virtual nonReentrant onlyRole(DEFAULT_ADMIN_ROLE)
external nonReentrant onlyRole(DEFAULT_ADMIN_ROLE)
{
//don't allow to send if refunding
if (refunding) revert InvalidWithdraw();
Expand All @@ -311,7 +324,7 @@ contract GryphVesting is
* @dev Sends the specified `amount` to the economy
*/
function sendToEconomy(uint256 amount)
external virtual nonReentrant onlyRole(DEFAULT_ADMIN_ROLE)
external nonReentrant onlyRole(DEFAULT_ADMIN_ROLE)
{
//don't allow to send if refunding
if (refunding) revert InvalidWithdraw();
Expand All @@ -325,7 +338,7 @@ contract GryphVesting is
* This method exists to transfer out tokens funds.
*/
function withdraw(address erc20, address to, uint256 amount)
external virtual nonReentrant onlyRole(DEFAULT_ADMIN_ROLE)
external nonReentrant onlyRole(DEFAULT_ADMIN_ROLE)
{
SafeERC20.safeTransfer(IERC20(erc20), to, amount);
}
Expand All @@ -335,7 +348,7 @@ contract GryphVesting is
/**
* @dev Vest a `beneficiary` for an `amount`
*/
function _vest(address beneficiary, uint256 amount) internal virtual {
function _vest(address beneficiary, uint256 amount) internal {
// if no amount or refunding
if (amount == 0 || refunding) revert InvalidVesting();
//now add to the beneficiary
Expand Down
39 changes: 39 additions & 0 deletions tests/GryphEconomy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { expect } = require('chai');
require('dotenv').config()

if (process.env.BLOCKCHAIN_NETWORK != 'hardhat') {
console.error('Exited testing with network:', process.env.BLOCKCHAIN_NETWORK)
process.exit(1);
}

async function deploy(name, ...params) {
//deploy the contract
const ContractFactory = await ethers.getContractFactory(name)
const contract = await ContractFactory.deploy(...params)
await contract.deployed()

return contract
}

async function getSigners(name, ...params) {
//deploy the contract
const contract = await deploy(name, ...params)

//get the signers
const signers = await ethers.getSigners()
//attach contracts
for (let i = 0; i < signers.length; i++) {
const Contract = await ethers.getContractFactory(name, signers[i])
signers[i].withContract = await Contract.attach(contract.address)
}

return signers
}

describe('GryphEconomy Tests', function () {
before(async function() {

})

it('Should test', async function () {})
})
49 changes: 49 additions & 0 deletions tests/GryphNamespaces.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { expect } = require('chai');
require('dotenv').config()

if (process.env.BLOCKCHAIN_NETWORK != 'hardhat') {
console.error('Exited testing with network:', process.env.BLOCKCHAIN_NETWORK)
process.exit(1);
}

async function deploy(name, ...params) {
//deploy the contract
const ContractFactory = await ethers.getContractFactory(name)
const contract = await ContractFactory.deploy(...params)
await contract.deployed()

return contract
}

async function getSigners(name, ...params) {
//deploy the contract
const contract = await deploy(name, ...params)

//get the signers
const signers = await ethers.getSigners()
//attach contracts
for (let i = 0; i < signers.length; i++) {
const Contract = await ethers.getContractFactory(name, signers[i])
signers[i].withContract = await Contract.attach(contract.address)
}

return signers
}

describe('GryphNamespaces Tests', function () {
before(async function() {
const [
owner,
holder1,
holder2
] = await getSigners('GryphNamespaces')

this.signers = {
owner,
holder1,
holder2
}
})

it('Should test', async function () {})
})
89 changes: 89 additions & 0 deletions tests/GryphToken.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const { expect } = require('chai');
require('dotenv').config()

if (process.env.BLOCKCHAIN_NETWORK != 'hardhat') {
console.error('Exited testing with network:', process.env.BLOCKCHAIN_NETWORK)
process.exit(1);
}

async function deploy(name, ...params) {
//deploy the contract
const ContractFactory = await ethers.getContractFactory(name)
const contract = await ContractFactory.deploy(...params)
await contract.deployed()

return contract
}

async function getSigners(name, ...params) {
//deploy the contract
const contract = await deploy(name, ...params)

//get the signers
const signers = await ethers.getSigners()
//attach contracts
for (let i = 0; i < signers.length; i++) {
const Contract = await ethers.getContractFactory(name, signers[i])
signers[i].withContract = await Contract.attach(contract.address)
}

return signers
}

describe('GryphToken Tests', function () {
before(async function() {
const [
owner,
holder1,
holder2,
holder3,
holder4
] = await getSigners('GryphToken')

this.signers = {
owner,
holder1,
holder2,
holder3,
holder4
}
})

it('Should not mint when paused', async function () {
const { owner, holder1 } = this.signers
await expect(
owner.withContract.mint(holder1.address, ethers.utils.parseEther('10'))
).to.revertedWith('Pausable: paused')
})

it('Should mint', async function () {
const { owner, holder1 } = this.signers

await owner.withContract.unpause()
await owner.withContract.mint(holder1.address, ethers.utils.parseEther('10'))
expect(await owner.withContract.balanceOf(holder1.address)).to.equal(
ethers.utils.parseEther('10')
)
})

it('Should transfer', async function () {
const { owner, holder1, holder2 } = this.signers

await holder1.withContract.transfer(holder2.address, ethers.utils.parseEther('5'))
expect(await owner.withContract.balanceOf(holder1.address)).to.equal(
ethers.utils.parseEther('5')
)

expect(await owner.withContract.balanceOf(holder2.address)).to.equal(
ethers.utils.parseEther('5')
)
})

it('Should not transfer when paused', async function () {
const { owner, holder1, holder2 } = this.signers
await owner.withContract.pause()
await expect(
holder1.withContract.transfer(holder2.address, ethers.utils.parseEther('5'))
).to.revertedWith('Token transfer while paused')
})
})
Loading

0 comments on commit 69cc4c6

Please sign in to comment.