Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding unit tests for Solidity Crypto Units and Time Units #440

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions contracts/solidity/units/cryptoUnits.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

contract CryptoUnits {
constructor() {
}

function get1Wei() public pure returns (uint) {
return 1 wei;
}

function get1GWei() public pure returns (uint256) {
return 1 gwei;
}

function get1Eth() public pure returns (uint256) {
return 1 ether;
}
}
27 changes: 27 additions & 0 deletions contracts/solidity/units/timeUnits.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

contract TimeUnits {
constructor() {
}

function get1Second() public pure returns (uint) {
return 1 seconds;
}

function get1Minute() public pure returns (uint) {
return 1 minutes;
}

function get1Hour() public pure returns (uint) {
return 1 hours;
}

function get1Day() public pure returns (uint) {
return 1 days;
}

function get1Week() public pure returns (uint) {
return 1 weeks;
}
}
13 changes: 13 additions & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ const GAS_LIMIT_8000000 = { gasLimit: 8000000 }
const TOKEN_NAME = 'tokenName'
const TOKEN_SYMBOL = 'tokenSymbol'
const TX_SUCCESS_CODE = 22
const SECOND = WEI = 1
const MINUTE = 60 * SECOND
const HOUR = 60 * MINUTE
const DAY = 24 * HOUR
const WEEK = 7 * DAY
const GWEI = 1e9

module.exports = {
Events,
Expand All @@ -122,4 +128,11 @@ module.exports = {
TOKEN_NAME,
TOKEN_SYMBOL,
TX_SUCCESS_CODE,
SECOND,
MINUTE,
HOUR,
DAY,
WEEK,
WEI,
GWEI
}
34 changes: 34 additions & 0 deletions test/solidity/units/cryptoUnits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { expect } = require('chai')
const { ethers } = require('hardhat')
const { WEI, GWEI } = require('../../constants')

describe('Crypto Units tests', function () {
let signers
let contract

before(async function () {
signers = await ethers.getSigners()

const factory = await ethers.getContractFactory('CryptoUnits')
contract = await factory.deploy()
})

it('confirm 1 wei == 1', async function () {
const res = await contract.get1Wei()

expect(res).to.equal(WEI)
})

it('confirm 1 gwei == 1e9', async function () {
const res = await contract.get1GWei()

expect(res).to.equal(GWEI)
})

it('confirm 1 ether == 1e18', async function () {
const res = await contract.get1Eth()

expect(res / 1e9).to.equal(GWEI)
})

})
46 changes: 46 additions & 0 deletions test/solidity/units/timeUnits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { expect } = require('chai')
const { ethers } = require('hardhat')
const { SECOND, MINUTE, HOUR, DAY, WEEK } = require('../../constants')

describe('Time Units tests', function () {
let signers
let contract

before(async function () {
signers = await ethers.getSigners()

const factory = await ethers.getContractFactory('TimeUnits')
contract = await factory.deploy()
})

it('confirm 1 == 1 seconds', async function () {
const res = await contract.get1Second()

expect(res).to.equal(SECOND)
})

it('confirm 1 minutes == 60 seconds', async function () {
const res = await contract.get1Minute()

expect(res).to.equal(MINUTE)
})

it('confirm 1 hours == 60 minutes', async function () {
const res = await contract.get1Hour()

expect(res).to.equal(HOUR)
})

it('confirm 1 days == 24 hours', async function () {
const res = await contract.get1Day()

expect(res).to.equal(DAY)
})

it('confirm 1 weeks == 7 days', async function () {
const res = await contract.get1Week()

expect(res).to.equal(WEEK)
})

})