Skip to content

Instantly share code, notes, and snippets.

@samlaf
samlaf / ko-build-flags.md
Last active May 27, 2024 05:21
Ko build flags
@samlaf
samlaf / gasOracles.rs
Created May 23, 2022 13:15
Gas Oracles (ethers-rs)
use ethers::prelude::*;
use eyre::Result;
use gas_oracle::{
EthGasStation, Etherchain, Etherscan,
GasCategory::{Fast, Fastest, SafeLow, Standard},
GasOracle,
};
#[tokio::main]
async fn main() -> Result<()> {
@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,
});
}
@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 / 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 / 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 / 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 / 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 / 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: