From ea5cdcd4a53cdaa8c49d1e547131934978ba053f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Migone?= Date: Mon, 18 Mar 2024 16:27:02 -0300 Subject: [PATCH] chore: boilerplate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Migone --- .../contracts/staking/IHorizonStaking.sol | 2 +- .../contracts/SubgraphService.sol | 4 +- packages/subgraph-service/package.json | 3 +- packages/subgraph-service/remappings.txt | 5 + packages/subgraph-service/test/Lock.ts | 115 ------------------ .../subgraph-service/test/SimpleTest.t.sol | 18 +++ packages/subgraph-service/test/SimpleTest.ts | 23 ++++ 7 files changed, 52 insertions(+), 118 deletions(-) create mode 100644 packages/subgraph-service/remappings.txt delete mode 100644 packages/subgraph-service/test/Lock.ts create mode 100644 packages/subgraph-service/test/SimpleTest.t.sol create mode 100644 packages/subgraph-service/test/SimpleTest.ts diff --git a/packages/contracts/contracts/staking/IHorizonStaking.sol b/packages/contracts/contracts/staking/IHorizonStaking.sol index 2de5c1f28..a4101ff52 100644 --- a/packages/contracts/contracts/staking/IHorizonStaking.sol +++ b/packages/contracts/contracts/staking/IHorizonStaking.sol @@ -4,7 +4,7 @@ pragma solidity >=0.7.6 <0.9.0; pragma abicoder v2; interface Test { - function test() external; + function test() external returns (uint256); } interface IHorizonStaking { diff --git a/packages/subgraph-service/contracts/SubgraphService.sol b/packages/subgraph-service/contracts/SubgraphService.sol index d7d423a1f..ef4da97da 100644 --- a/packages/subgraph-service/contracts/SubgraphService.sol +++ b/packages/subgraph-service/contracts/SubgraphService.sol @@ -4,5 +4,7 @@ pragma solidity >=0.4.0 <0.9.0; import {Test} from "@graphprotocol/contracts/contracts/staking/IHorizonStaking.sol"; contract SimpleTest is Test { - function test() external {} + function test() external pure returns (uint256) { + return 42; + } } diff --git a/packages/subgraph-service/package.json b/packages/subgraph-service/package.json index cae82865d..02a913c3a 100644 --- a/packages/subgraph-service/package.json +++ b/packages/subgraph-service/package.json @@ -9,7 +9,8 @@ "lint:sol": "forge fmt", "lint": "yarn lint:ts && yarn lint:sol", "clean": "rm -rf build cache typechain-types", - "build": "hardhat compile" + "build": "hardhat compile", + "test": "forge test" }, "devDependencies": { "@graphprotocol/contracts": "workspace:^7.0.0", diff --git a/packages/subgraph-service/remappings.txt b/packages/subgraph-service/remappings.txt new file mode 100644 index 000000000..1bd6482cd --- /dev/null +++ b/packages/subgraph-service/remappings.txt @@ -0,0 +1,5 @@ +@graphprotocol/contracts/=node_modules/@graphprotocol/contracts/ +forge-std/=lib/forge-std/src/ +ds-test/=lib/forge-std/lib/ds-test/src/ +eth-gas-reporter/=node_modules/eth-gas-reporter/ +hardhat/=node_modules/hardhat/ diff --git a/packages/subgraph-service/test/Lock.ts b/packages/subgraph-service/test/Lock.ts deleted file mode 100644 index 20c7227b2..000000000 --- a/packages/subgraph-service/test/Lock.ts +++ /dev/null @@ -1,115 +0,0 @@ -import { - loadFixture, - time, -} from '@nomicfoundation/hardhat-toolbox/network-helpers' -import { anyValue } from '@nomicfoundation/hardhat-chai-matchers/withArgs' -import { expect } from 'chai' -import hardhat from 'hardhat' - -const ethers = hardhat.ethers - -describe('Lock', function () { - // We define a fixture to reuse the same setup in every test. - // We use loadFixture to run this setup once, snapshot that state, - // and reset Hardhat Network to that snapshot in every test. - async function deployOneYearLockFixture() { - const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60 - const ONE_GWEI = 1_000_000_000 - - const lockedAmount = ONE_GWEI - const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS - - // Contracts are deployed using the first signer/account by default - const [owner, otherAccount] = await ethers.getSigners() - - const Lock = await ethers.getContractFactory('Lock') - const lock = await Lock.deploy(unlockTime, { value: lockedAmount }) - - return { lock, unlockTime, lockedAmount, owner, otherAccount } - } - - describe('Deployment', function () { - it('Should set the right unlockTime', async function () { - const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture) - - expect(await lock.unlockTime()).to.equal(unlockTime) - }) - - it('Should set the right owner', async function () { - const { lock, owner } = await loadFixture(deployOneYearLockFixture) - - expect(await lock.owner()).to.equal(owner.address) - }) - - it('Should receive and store the funds to lock', async function () { - const { lock, lockedAmount } = await loadFixture( - deployOneYearLockFixture, - ) - - expect(await ethers.provider.getBalance(lock.target)).to.equal( - lockedAmount, - ) - }) - - it('Should fail if the unlockTime is not in the future', async function () { - // We don't use the fixture here because we want a different deployment - const latestTime = await time.latest() - const Lock = await ethers.getContractFactory('Lock') - await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith( - 'Unlock time should be in the future', - ) - }) - }) - - describe('Withdrawals', function () { - describe('Validations', function () { - it('Should revert with the right error if called too soon', async function () { - const { lock } = await loadFixture(deployOneYearLockFixture) - - await expect(lock.withdraw()).to.be.revertedWith( - 'You can\'t withdraw yet', - ) - }) - - it('Shouldn\'t fail if the unlockTime has arrived and the owner calls it', async function () { - const { lock, unlockTime } = await loadFixture( - deployOneYearLockFixture, - ) - - // Transactions are sent using the first signer by default - await time.increaseTo(unlockTime) - - await expect(lock.withdraw()).not.to.be.reverted - }) - }) - - describe('Events', function () { - it('Should emit an event on withdrawals', async function () { - const { lock, unlockTime, lockedAmount } = await loadFixture( - deployOneYearLockFixture, - ) - - await time.increaseTo(unlockTime) - - await expect(lock.withdraw()) - .to.emit(lock, 'Withdrawal') - .withArgs(lockedAmount, anyValue) // We accept any value as `when` arg - }) - }) - - describe('Transfers', function () { - it('Should transfer the funds to the owner', async function () { - const { lock, unlockTime, lockedAmount, owner } = await loadFixture( - deployOneYearLockFixture, - ) - - await time.increaseTo(unlockTime) - - await expect(lock.withdraw()).to.changeEtherBalances( - [owner, lock], - [lockedAmount, -lockedAmount], - ) - }) - }) - }) -}) diff --git a/packages/subgraph-service/test/SimpleTest.t.sol b/packages/subgraph-service/test/SimpleTest.t.sol new file mode 100644 index 000000000..5ae467c66 --- /dev/null +++ b/packages/subgraph-service/test/SimpleTest.t.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity 0.8.10; + +import "forge-std/Test.sol"; +import { SimpleTest } from "../contracts/SubgraphService.sol"; + +contract ContractTest is Test { + SimpleTest simpleTest; + + function setUp() public { + simpleTest = new SimpleTest(); + } + + function test_NumberIs42() public { + assertEq(simpleTest.test(), 42); + } + +} \ No newline at end of file diff --git a/packages/subgraph-service/test/SimpleTest.ts b/packages/subgraph-service/test/SimpleTest.ts new file mode 100644 index 000000000..cfcfb1443 --- /dev/null +++ b/packages/subgraph-service/test/SimpleTest.ts @@ -0,0 +1,23 @@ +import hardhat from 'hardhat' + +import { expect } from 'chai' +import { loadFixture } from '@nomicfoundation/hardhat-toolbox/network-helpers' + +const ethers = hardhat.ethers + +describe('SimpleTest', function () { + async function deployFixture() { + const [owner] = await ethers.getSigners() + const SimpleTest = await ethers.getContractFactory('SimpleTest') + const simpleTest = await SimpleTest.deploy() + return { simpleTest, owner } + } + + describe('Deployment', function () { + it('Should return 42', async function () { + const { simpleTest } = await loadFixture(deployFixture) + + expect(await simpleTest.test()).to.equal(42) + }) + }) +})