Skip to content

Instantly share code, notes, and snippets.

View 0xmikko's full-sized avatar
😃

0xmikko.eth 0xmikko

😃
View GitHub Profile
@0xmikko
0xmikko / transfer.sol
Created April 6, 2023 14:59
Transfer using WETHGateway
for(uint256 i; i<tokens.length; ++i) {
address token = tokens[i];
uint256 balance = balances[i];
if (address == WETH) {
safeTransferToken(WETH, WETHGateway, amount);
} else {
safeTransferToken(token, to, amount);
}
}
for(uint256 i; i<tokens.length; ++i) {
address token = tokens[i];
uint256 balance = balances[i];
if (address == WETH) {
_sendETH(to, amount);
} else {
safeTranferToken(token, to, amount);
}
}
@0xmikko
0xmikko / optimisation.sol
Created June 3, 2022 21:01
Solidity gas optimisation example
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.10;
contract Test {
// Gas used for empy function: 22094
function blank(uint16) public pure returns (string memory) {}
// Gas used: 31922 - 22094 = 9828
function toBinary1(uint16 n) public pure returns (string memory) {
require(n < 1024);
for (uint256 i = 0; i < len; ) {
address allowedContract = creditConfigurator.allowedContracts(i);
address adapter = creditFacade.contractToAdapter(allowedContract);
AdapterType aType = IAdapter(adapter)._gearboxAdapterType();
if (aType == AdapterType.CONVEX_V1_BASE_REWARD_POOL) {
uint256 pid = IConvexV1BaseRewardPoolAdapter(adapter).pid();
address stakedPhantomToken = IConvexV1BaseRewardPoolAdapter(
adapter
).stakedPhantomToken();
@0xmikko
0xmikko / TerminatorFlash.sol
Created March 4, 2022 08:45
Liquidation using Flashloan
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.7.4;
pragma abicoder v2;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ILendingPool} from "./interfaces/ILendingPool.sol";
import {ILendingPoolAddressesProvider} from "./interfaces/ILendingPoolAddressesProvider.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import childProcess from "child_process";
import fs from "fs";
const v1Metrics = "gasV1.json";
const v2Metrics = "gasV2.json";
export type GasMeasures =
| "openCreditAccount"
| "swapOnUniswapFastCheck"
| "swapOnUniswapFullCheck"
function _fullCheck(address creditAccount) internal {
uint256 borrowAmountPlusInterestRateUSD = priceOracle.convertToUSD(
calcCreditAccountAccruedInterest(creditAccount),
underlyingToken
);
uint256 total;
uint256 tokenMask;
uint256 eTokens = enabledTokens[creditAccount];
uint256 len = allowedTokens.length;
/// @dev Calculates Threshold Weighted Total Value
/// More: https://dev.gearbox.fi/developers/credit/economy#threshold-weighted-value
///
/// @param creditAccount Credit account address
function calcThresholdWeightedValue(address creditAccount)
public
view
override
returns (uint256 total)
{
@0xmikko
0xmikko / swap.sol
Last active February 15, 2021 19:31
Gearbox swap
// store balances before swap
uint256 balanceInBefore = _vaultService.getBalance(tokenIn);
uint256 balanceOutBefore = _vaultService.getBalance(tokenOut);
// swapTokens
_vaultService.swapTokensForExactTokens(
amountOut,
amountInMax,
path,
deadline
pragma solidity ^0.4.25;
import "https://raw.githubusercontent.com/Arachnid/solidity-stringutils/master/src/strings.sol";
contract HappyBirthdayDima {
using strings for *;
event SPBlockchainCommuntyGreeting (address sender, string greeting);
modifier onlyRazVGodu (){