Skip to content

Instantly share code, notes, and snippets.

@TomiOhl
TomiOhl / ArrayParameterTest.sol
Created June 9, 2021 11:39
Comparing the gas cost of functions with an array parameter in different data locations
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
// Testing the gas cost of two identical functions with array parameters supplied in different data locations
// Costs only apply when called from another contract function
contract ArrayParameterTest {
// Transaction cost 22978 gas (length 1), 28639 gas (length 10), 85249 gas (length 100)
// Execution cost 1130 gas (length 1), 5063 gas (length 10), 44393 gas (length 100)
function addNumbersFromCalldata(uint256[] calldata numbers) external pure returns (uint256) {
uint256 sum;
@TomiOhl
TomiOhl / StorageVsFunctionTest.sol
Last active June 18, 2021 10:24
Experimenting what would be the best way to store constants in Solidity.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
// A simple contract to test the differences of gas usage between accessing a storage variable and using a return value of a function.
// Result: more than 2000 less gass consumed by getFromInline() and getFromImmutable() than getFromStorage() (if called in a transaction).
// Conclusion: it's better not to use storage variables for constants.
contract StorageVsFunctionTest {
address internal uniAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address internal immutable uniAddressImmutable;
@TomiOhl
TomiOhl / ExternalPureTest.sol
Last active May 10, 2021 16:02
An experiment to see how much do gas costs differ for the same action via different approaches
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
interface IUniSwapRouter02 {
function WETH() external pure returns (address);
}
contract ExternalPureTest {
address internal uniAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address public addr = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
@TomiOhl
TomiOhl / DecimalsTest.sol
Created April 28, 2021 21:13
Converting an amount from one token's decimals to another in Solidity
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
interface IERC20 {
function decimals() external view returns (uint8);
}
contract DecimalsTest {
function changeDecimals(address _fromToken, address _desToken, uint _amount) external view returns (uint) {
@TomiOhl
TomiOhl / keybase.md
Last active April 7, 2021 19:05
keybase.md

Keybase proof

I hereby claim:

  • I am tomiohl on github.
  • I am tomi_ohl (https://keybase.io/tomi_ohl) on keybase.
  • I have a public key ASCWqP1y1QIRqknZOl6ZaPet89RAPhIj1o1W-bS_iR0rugo

To claim this, I am signing this object:

@TomiOhl
TomiOhl / Timelock.md
Last active March 5, 2023 13:41
Timelock possibilities

Timelock implementation possibilities in smart contracts

A timelock is used in smart contracts as a way to restrict the spending of some tokens until a specified future time or block number.

Possible solutions

Some solutions to the problem are described below, from the most primitive to the best we came up with. Note that the term expiration time is used, but it can also refer to block numbers.

Less precise solutions