From 43e520b5e0664c03093851b1439fbcfbd7e991c1 Mon Sep 17 00:00:00 2001 From: Armando Andini Date: Mon, 20 Sep 2021 15:56:03 -0300 Subject: [PATCH] Built in vars --- misc/contracts/BuiltInVars.sol | 32 ++++++++++++++++++++++++++++++++ misc/test/built_in_vars_test.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 misc/contracts/BuiltInVars.sol create mode 100644 misc/test/built_in_vars_test.ts diff --git a/misc/contracts/BuiltInVars.sol b/misc/contracts/BuiltInVars.sol new file mode 100644 index 0000000..593a332 --- /dev/null +++ b/misc/contracts/BuiltInVars.sol @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity ^0.8.7; + +import "hardhat/console.sol"; + +contract BuiltInVars { + constructor() {} + + function printGlobals() public { + console.log("gasleft"); + console.log(gasleft()); + console.log("this"); + console.log(address(this)); + console.log("block.timestamp"); + console.log(block.timestamp); + console.log("block.number"); + console.log(block.number); + console.log("block.difficulty"); + console.log(block.difficulty); + console.log("tx.gasprice"); + console.log(tx.gasprice); + } + + function countGas() public view returns (uint256) { + uint256 start = gasleft(); + uint256 j = 1235 * 127396; + uint256 end = gasleft(); + + return start - end; + } +} diff --git a/misc/test/built_in_vars_test.ts b/misc/test/built_in_vars_test.ts new file mode 100644 index 0000000..623a887 --- /dev/null +++ b/misc/test/built_in_vars_test.ts @@ -0,0 +1,27 @@ +const { expect } = require("chai"); +import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; +import { BigNumber, Contract } from 'ethers'; +import hre, {ethers} from 'hardhat' + +describe("BuiltInVars", function () { + let contract: Contract; + let signer: SignerWithAddress; + + beforeEach(async () => { + // Deploy contact + const factory = await ethers.getContractFactory("BuiltInVars"); + contract = await factory.deploy(); + await contract.deployed(); + + // Store signer + signer = (await ethers.getSigners())[0] + }); + + describe("function", function () { + it("prints globals", async () => { + await contract.printGlobals() + + console.log(`Test gas consumption: ${await contract.countGas()}`) + }); + }); +}); \ No newline at end of file