Skip to content

Instantly share code, notes, and snippets.

@crypt0summer
Created June 20, 2022 17:48
Show Gist options
  • Save crypt0summer/7aeae440e5592edbd43d23ea688591fe to your computer and use it in GitHub Desktop.
Save crypt0summer/7aeae440e5592edbd43d23ea688591fe to your computer and use it in GitHub Desktop.
Ethreum-Storing Structs is costing you gas
import { Address } from "cluster";
import { BigNumber, Contract, Signer } from "ethers";
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Gas Test", function () {
let account1: Signer;
let account2: Signer;
let gasTest: Contract;
beforeEach(async function () {
[account1, account2] = await ethers.getSigners();
const GasContract = await ethers.getContractFactory("GasTest");
gasTest = await GasContract.deploy();
await gasTest.deployed();
});
it("Should success1 - gas test", async function () {
const tx = await gasTest
.connect(account1)
.save1(Date.now(), 123123, 8, 0, 3);
const receipt = await tx.wait();
console.log(receipt.gasUsed);
});
it("Should success2 - gas test", async function () {
const tx = await gasTest
.connect(account1)
.save2(Date.now(), 123123, 8, 0, 3);
const receipt = await tx.wait();
console.log(receipt.gasUsed);
});
it("Should success3 - gas test", async function () {
const tx = await gasTest
.connect(account1)
.save3(Date.now(), 123123, 8, 0, 3);
const receipt = await tx.wait();
console.log(receipt.gasUsed);
});
it("Should success4 - gas test", async function () {
const tx = await gasTest
.connect(account1)
.setCharacter(0, account1.getAddress(), Date.now(), 8, 0, 3, 123123);
const receipt = await tx.wait();
console.log(receipt.gasUsed);
});
});
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.13;
// import "./Vault.sol";
import "hardhat/console.sol";
contract GasTest {
//Type1
address owner;
uint64 creationTime;
uint256 dna;
uint16 strength;
uint16 race;
uint16 class;
mapping(uint256 => address) owners;
mapping(uint256 => uint64) creationTimes;
mapping(uint256 => uint256) dnas;
mapping(uint256 => uint16) strengths;
mapping(uint256 => uint16) races;
mapping(uint256 => uint16) classes;
function save1(
uint64 _creationTime,
uint256 _dna,
uint16 _strength,
uint16 _race,
uint16 _class
) external {
owners[dna] = msg.sender;
creationTimes[dna] = _creationTime;
dnas[dna] = _dna;
strengths[dna] = _strength;
races[dna] = _race;
classes[dna] = _class;
}
//Type 2
struct GameCharacter2 {
address owner;
uint64 creationTime;
uint256 dna;
uint16 strength;
uint16 race;
uint16 class;
}
mapping(uint256 => GameCharacter2) characters2;
function save2(
uint64 _creationTime,
uint256 _dna,
uint16 _strength,
uint16 _race,
uint16 _class
) external {
characters2[_dna] = GameCharacter2({
owner: msg.sender,
creationTime: _creationTime,
dna: _dna,
strength: _strength,
race: _race,
class: _class
});
}
//Type3
struct GameCharacter3 {
address owner;
uint48 creationTime;
uint16 strength;
uint16 race;
uint16 class;
uint256 dna;
}
mapping(uint256 => GameCharacter3) characters3;
function save3(
uint48 _creationTime,
uint256 _dna,
uint16 _strength,
uint16 _race,
uint16 _class
) external {
characters3[_dna] = GameCharacter3({
owner: msg.sender,
creationTime: _creationTime,
dna: _dna,
strength: _strength,
race: _race,
class: _class
});
}
//Type4
struct GameCharacter4 {
address owner;
uint256 creationTime;
uint256 strength;
uint256 race;
uint256 class;
uint256 dna;
}
mapping(uint256 => uint256) characters4;
mapping(uint256 => uint256) dnaRecords4;
function setCharacter(
uint256 _id,
address owner,
uint256 creationTime,
uint256 strength,
uint256 race,
uint256 class,
uint256 dna
) external {
uint256 character = uint256(uint160(owner));
character |= creationTime << 160;
character |= strength << 208;
character |= race << 224;
character |= class << 240;
characters4[_id] = character;
dnaRecords4[_id] = dna;
}
function getCharacter(uint256 _id)
external
view
returns (
address owner,
uint256 creationTime,
uint256 strength,
uint256 race,
uint256 class,
uint256 dna
)
{
uint256 character = characters4[_id];
dna = dnaRecords4[_id];
owner = address(uint160(character));
creationTime = uint256(uint40(character >> 160));
strength = uint256(uint16(character >> 208));
race = uint256(uint16(character >> 224));
class = uint256(uint16(character >> 240));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment