Skip to content

Commit

Permalink
economy test
Browse files Browse the repository at this point in the history
  • Loading branch information
cblanquera committed Mar 8, 2022
1 parent 69cc4c6 commit 55cb851
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 19 deletions.
47 changes: 36 additions & 11 deletions contracts/GryphEconomy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ contract GryphEconomy is

//this is the contract address for $GRYPH
IERC20Capped public immutable GRYPH_TOKEN;
//this is the contract address for the $GRYPH treasury
address public immutable GRYPH_TREASURY;
//this is the token cap of $GRYPH
uint256 public immutable TOKEN_CAP;

Expand All @@ -71,13 +73,15 @@ contract GryphEconomy is
* @dev Grants `DEFAULT_ADMIN_ROLE` to the account that deploys the
* contract.
*/
constructor(IERC20Capped token) payable {
constructor(IERC20Capped token, address treasury) payable {
//set up roles for the contract creator
address sender = _msgSender();
_setupRole(DEFAULT_ADMIN_ROLE, sender);
_setupRole(PAUSER_ROLE, sender);
//set the token address
//set the $GRYPH addresses
GRYPH_TOKEN = token;
GRYPH_TREASURY = treasury;
//set the token cap
TOKEN_CAP = token.cap();
//start paused
_pause();
Expand Down Expand Up @@ -118,16 +122,14 @@ contract GryphEconomy is
* @dev Returns the ether amount we are willing to buy $GRYPH for
*/
function buyingFor(uint256 amount) public view returns(uint256) {
// (eth / cap) * amount
return balanceEther().mul(amount).div(TOKEN_CAP).mul(_buyFor).div(1000);
return _buyingFor(amount, balanceEther());
}

/**
* @dev Returns the ether amount we are willing to sell $GRYPH for
*/
function sellingFor(uint256 amount) public view returns(uint256) {
// (eth / cap) * amount
return balanceEther().mul(amount).div(TOKEN_CAP).mul(_sellFor).div(1000);
return _sellingFor(amount, balanceEther());
}

// ============ Write Methods ============
Expand All @@ -138,29 +140,35 @@ contract GryphEconomy is
function buy(address recipient, uint256 amount)
public payable whenNotPaused nonReentrant
{
uint256 value = buyingFor(amount);
uint256 value = _sellingFor(amount, balanceEther() - msg.value);
if (value == 0
|| msg.value < value
|| balanceToken() < amount
) revert InvalidAmount();
//we already received the ether
//so just send the tokens
SafeERC20.safeTransfer(GRYPH_TOKEN, recipient, amount);
//send the interest
Address.sendValue(
payable(GRYPH_TREASURY),
msg.value.mul(_interest).div(10000)
);
emit ERC20Sent(recipient, amount);
}

/**
* @dev Sells `amount` of $GRYPH
*/
function sell(uint256 amount) public whenNotPaused nonReentrant {
address recipient = _msgSender();
function sell(address recipient, uint256 amount)
public whenNotPaused nonReentrant
{
//check allowance
if(GRYPH_TOKEN.allowance(recipient, address(this)) < amount)
revert InvalidAmount();
//send the ether
Address.sendValue(payable(recipient), buyingFor(amount));
//now accept the payment
SafeERC20.safeTransferFrom(GRYPH_TOKEN, recipient, address(this), amount);
//send the ether
Address.sendValue(payable(recipient), sellingFor(amount));
emit ERC20Received(recipient, amount);
}

Expand Down Expand Up @@ -206,4 +214,21 @@ contract GryphEconomy is
function unpause() public virtual onlyRole(PAUSER_ROLE) {
_unpause();
}

// ============ Internal Methods ============
/**
* @dev Returns the ether amount we are willing to buy $GRYPH for
*/
function _buyingFor(uint256 amount, uint256 balance) internal view returns(uint256) {
// (eth / cap) * amount
return balance.mul(amount).mul(_buyFor).div(TOKEN_CAP).div(1000);
}

/**
* @dev Returns the ether amount we are willing to sell $GRYPH for
*/
function _sellingFor(uint256 amount, uint256 balance) internal view returns(uint256) {
// (eth / cap) * amount
return balance.mul(amount).mul(_sellFor).div(TOKEN_CAP).div(1000);
}
}
155 changes: 148 additions & 7 deletions tests/GryphEconomy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,166 @@ async function deploy(name, ...params) {
return contract
}

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

async function getSigners(token, treasury, economy) {
//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)
const Token = await ethers.getContractFactory('GryphToken', signers[i])
const Treasury = await ethers.getContractFactory('GryphTreasury', signers[i])
const Economy = await ethers.getContractFactory('GryphEconomy', signers[i])
signers[i].withToken = await Token.attach(token.address)
signers[i].withTreasury = await Treasury.attach(treasury.address)
signers[i].withEconomy = await Economy.attach(economy.address)
}

return signers
}

describe('GryphEconomy Tests', function () {
before(async function() {
this.contracts = {}
this.contracts.token = await deploy('GryphToken')
this.contracts.treasury = await deploy('GryphTreasury')
this.contracts.economy = await deploy(
'GryphEconomy',
this.contracts.token.address,
this.contracts.treasury.address
)

const [
owner,
user1,
user2,
user3,
user4,
fund
] = await getSigners(
this.contracts.token,
this.contracts.treasury,
this.contracts.economy
)

//send some ether and tokens
await owner.withEconomy.unpause()
await fund.sendTransaction({
to: owner.withEconomy.address,
value: ethers.utils.parseEther('10')
})

await owner.withToken.unpause()
await owner.withToken.mint(
owner.withEconomy.address,
ethers.utils.parseEther('100')
)

this.signers = {
owner,
user1,
user2,
user3,
user4
}
})

it('Should have a balance', async function () {
const { owner } = this.signers

expect(await owner.withEconomy.provider.getBalance(owner.withEconomy.address)).to.equal(
ethers.utils.parseEther('10')
)

expect(await owner.withToken.balanceOf(owner.withEconomy.address)).to.equal(
ethers.utils.parseEther('100')
)

expect(await owner.withEconomy.balanceEther()).to.equal(
ethers.utils.parseEther('10')
)

expect(await owner.withEconomy.balanceToken()).to.equal(
ethers.utils.parseEther('100')
)
})

it('Should have a buy and sell price', async function () {
const { owner } = this.signers

console.log('- ', (await owner.withEconomy.balanceEther()).toString())
expect(await owner.withEconomy.buyingFor(
ethers.utils.parseEther('10')
)).to.equal(
ethers.utils.parseEther('0.0000005')
)

expect(await owner.withEconomy.sellingFor(
ethers.utils.parseEther('10')
)).to.equal(
ethers.utils.parseEther('0.000002')
)
})

it('Should test', async function () {})
it('Should buy', async function () {
const { owner, user1 } = this.signers

await owner.withEconomy.buy(
user1.address,
ethers.utils.parseEther('10'),
{ value: ethers.utils.parseEther('0.000002') }
)

expect(await owner.withToken.balanceOf(user1.address)).to.equal(
ethers.utils.parseEther('10')
)

expect(await owner.withEconomy.balanceEther()).to.equal(
ethers.utils.parseEther('10.000001')
)

expect(await owner.provider.getBalance(owner.withEconomy.address)).to.equal(
ethers.utils.parseEther('10.000001')
)

expect(await owner.provider.getBalance(owner.withTreasury.address)).to.equal(
ethers.utils.parseEther('0.000001')
)

expect(await owner.withEconomy.balanceToken()).to.equal(
ethers.utils.parseEther('90')
)
})

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

await user1.withToken.approve(
owner.withEconomy.address,
ethers.utils.parseEther('1')
)

await owner.withEconomy.sell(
user1.address,
ethers.utils.parseEther('1')
)

expect(await owner.withToken.balanceOf(user1.address)).to.equal(
ethers.utils.parseEther('9')
)

expect(await owner.withEconomy.balanceEther()).to.equal(
ethers.utils.parseEther('10.000000949999995')
)

expect(await owner.provider.getBalance(owner.withEconomy.address)).to.equal(
ethers.utils.parseEther('10.000000949999995')
)

expect(await owner.provider.getBalance(owner.withTreasury.address)).to.equal(
ethers.utils.parseEther('0.000001')
)

expect(await owner.withEconomy.balanceToken()).to.equal(
ethers.utils.parseEther('91')
)
})
})
3 changes: 2 additions & 1 deletion tests/GryphVesting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ describe('GryphVesting Tests', function () {
this.contracts.treasury = await deploy('GryphTreasury')
this.contracts.economy = await deploy(
'GryphEconomy',
this.contracts.token.address
this.contracts.token.address,
this.contracts.treasury.address
)
this.contracts.vesting = await deploy(
'GryphVesting',
Expand Down

0 comments on commit 55cb851

Please sign in to comment.