Skip to content

Commit

Permalink
Built in vars
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Sep 20, 2021
1 parent 05ebd03 commit 43e520b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
32 changes: 32 additions & 0 deletions misc/contracts/BuiltInVars.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 27 additions & 0 deletions misc/test/built_in_vars_test.ts
Original file line number Diff line number Diff line change
@@ -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()}`)
});
});
});

0 comments on commit 43e520b

Please sign in to comment.