Skip to content

Instantly share code, notes, and snippets.

View CJ42's full-sized avatar

Jean Cvllr CJ42

View GitHub Profile
@CJ42
CJ42 / TimeUnits.md
Last active February 19, 2023 11:07
Markdown table to convert the number of seconds for each time units available in Solidity
Solidity Time Unit Number of seconds
seconds 1
minutes 60
hours 3,600
days 86,400
weeks 604,800
@CJ42
CJ42 / TimeUnits.sol
Created February 19, 2023 09:01
Basic of time units and the number of seconds returned by each suffix
// SPDC-License-Identifier: Apache-2.0
pragma solidity ^0.8.10;
contract TimeUnits {
function Seconds() public pure returns (uint256) {
// 1
return 1 seconds;
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract EtherUnits {
function assignEtherUnits() public {
uint256 gasWithExtraFee = 2 gwei + 5_000 wei;
}
@CJ42
CJ42 / EtherUnitsAssignment.sol
Last active February 13, 2023 07:51
Explain that the ether units is assigned as an exponent
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract EtherUnits {
function example() public {
uint256 a = 20;
// test = 5,000,000,000,000,000,020
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract StakeHolders {
uint256 immutable numberOfStakeholders;
constructor(uint256 numberOfStakeholders_) {
numberOfStakeholders = numberOfStakeholders_;
}
@CJ42
CJ42 / EtherUnitsInvalid.sol
Created February 13, 2023 07:36
Examples of values that cannot be suffixed with ether units
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract EtherUnits {
function notWorking() public {
uint256 total;
// TypeError: Hexadecimal numbers cannot be used with unit denominations.
// You can use an expression of the form "0x1234 * 1 day" instead.
Unit ➕ Number of decimals 0s ➖ Number of fractional 0s 1 Ether =
wei 18 1,000,000,000,000,000,000 Wei
kwei 15 1,000,000,000,000,000 Kwei
mwei 12 1,000,000,000,000 Mwei
gwei 9 1,000,000,000 Gwei
ether ... ... ...
kether 3 decimals 0.001 Kether
mether 6 decimals 0.000001 Mether
gether 9 decimals 0.000000001 Gether
@CJ42
CJ42 / EthersUnits.sol
Created February 3, 2023 19:13
example to show the minimum type required
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract EtherUnits {
function minimumTypes() public {
// minimum `uintN` type for wei = any
// OK
uint8 a = 1 wei;
@CJ42
CJ42 / Ownable.sol
Created January 29, 2023 15:38
Ownable contract used as example to dis-assemble the EVM bytecode of the dispatcher
pragma solidity >=0.7.0 <0.9.0;
contract Ownable {
address private owner;
// event for EVM logging
event OwnerSet(address indexed oldOwner, address indexed newOwner);
// modifier to check if caller is owner
modifier isOwner() {
@CJ42
CJ42 / ContractImmutable.sol
Last active January 23, 2023 17:54
Example to show how `contract` type can be used when defining `constant` or `immutable`
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract ContractTemplate {
// some logic here...
}
abstract contract MyContract {
// This will not compile and throw the error