Skip to content

Instantly share code, notes, and snippets.

@samlaf
samlaf / make_plots.ipynb
Last active November 21, 2020 02:31
Hypothesis Testing widgets
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@samlaf
samlaf / plot.ts
Created December 6, 2021 16:04
Plot the number of eips published per year
import { Layout, plot, Plot } from 'nodeplotlib';
import { readFileSync } from 'fs';
(async () => {
let readVal = readFileSync('eips.json', {
encoding: "utf-8"
});
const eips = JSON.parse(readVal);
@samlaf
samlaf / latest-block-timestamp-in-date-format.txt
Last active October 28, 2023 20:22
Get ethereum latest block timestamp in date format
// hardhat task
task('blockTimestamp', 'Prints the block timestamp', async (_, { ethers }) => {
const currentBlock = await ethers.provider.getBlockNumber();
const blockTimestamp = (await ethers.provider.getBlock(currentBlock)).timestamp;
console.log(blockTimestamp);
const date = new Date(blockTimestamp * 1000); // Date requires ms, whereas block.timestamp is in s
console.log(date)
});
@samlaf
samlaf / transfer-vs-send-vs-call.sol
Last active March 11, 2022 21:07
Sending Ether (transfer, send, call)
// In https://solidity-by-example.org/sending-ether/
// They say: call in combination with re-entrancy guard is the recommended method to use after December 2019.
// (see https://consensys.github.io/smart-contract-best-practices/recommendations/#dont-use-transfer-or-send for an explanation)
// and compare the gas fees of the 3 methods:
// transfer (2300 gas, throws error)
// send (2300 gas, returns bool)
// call (forward all gas or set gas, returns bool)
// I tested all 3 methods, by sending 10 eth to a contract and then withdrawing using these functions:
@samlaf
samlaf / solidity-weirdness.sol
Created January 18, 2022 22:44
Solidity Weirdness
function test(uint256 a) public pure returns (uint256) {
uint256 fooERROR = 2 * 997 / 1000; // ERROR: Type rational_const 997 / 500 is not implicitly convertible to expected type uint256. Try converting to type ufixed16x3 or use an explicit conversion.
uint256 fooOK = a * 997 / 1000; // this works... somehow
uint256 fooERROR2 = a * (997 / 1000); // Operator * not compatible with types uint256 and rational_const 997 / 1000
}
@samlaf
samlaf / compound-ceth.sol
Created January 20, 2022 22:45
toy implementation of compound and cEth (rebasing tokens)
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "hardhat/console.sol";
contract cEth is IERC20, Ownable {
@samlaf
samlaf / YourContract.sol
Created January 25, 2022 22:39
Listening/Filtering events using ethersjs
// This is the basic scaffold-eth contract. Note the SetPurpose event :)
pragma solidity >=0.8.0 <0.9.0;
import "hardhat/console.sol";
contract YourContract {
// `indexed` is necessary to filter!
event SetPurpose(address sender, string indexed purpose);
@samlaf
samlaf / pga-latency-benchmark.ts
Last active February 23, 2022 20:11
PGA latency benchmark (alchemy vs moralis vs infura)
import { ethers, deployments, network } from "hardhat";
async function main() {
const acct = (await ethers.provider.listAccounts())[0];
const signer = (await ethers.getSigners())[0];
const greeterAddress = (await deployments.get("Greeter")).address;
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = Greeter.attach(greeterAddress);
@samlaf
samlaf / cb-vs-promise-vs-asyncawait.ts
Created March 18, 2022 05:34
js callback vs promise vs async/await
function performLongTask(cb: (err:any, res: any) => any) {
const returnValue = 42;
setTimeout(() => cb(null, returnValue), 3000)
}
performLongTask((err, res) => {
if (err) throw new Error('we got an error');
console.log("callback:", res)
})
@samlaf
samlaf / filedb.ts
Created March 26, 2022 02:45
mongodb vs filedb
import fs from "fs";
const n = 5000000;
const s = [];
for (let i = 0; i < n; i++) {
s.push({
idx: i,
});
}