Skip to content

Instantly share code, notes, and snippets.

@codekoriko
Last active February 16, 2024 04:54
Show Gist options
  • Save codekoriko/d00f1217f62100dd148cba721d72a980 to your computer and use it in GitHub Desktop.
Save codekoriko/d00f1217f62100dd148cba721d72a980 to your computer and use it in GitHub Desktop.
Solidity

Console Logging

console.log("loanAmount: %s", loanAmount);

Forge <-> Viem

impersonation

OWNER = makeAddr("Owner");

vm.startPrank(OWNER);
morpho.enableIrm(address(0));
morpho.enableIrm(address(irm));
morpho.enableLltv(0);
morpho.setFeeRecipient(FEE_RECIPIENT);
vm.stopPrank();

Modifiers

  • It's a special kind of function that modifies the behavior of other functions
  • underscore _:
    • placeholder that represents the body of the function being modified
    • execution sequence: code before _ -> code of modified function -> code after _
modifier exampleModifier {
    // This code is executed first
    require(msg.sender == owner, "Not the contract owner");

    _; // The function body is inserted here

    // This code is executed after the function body
    emit FunctionWasCalled(msg.sender);
}

function doSomething() public exampleModifier {
    // This is the function body
}

Snippets

Inspect list of events

filters (Optional)

  • address: The address of the contract to listen.
  • eventName: The name of the event from the ABI. (erc20: Transfer, Approval)
import ERC20ABI from "@openzeppelin/contracts/build/contracts/ERC20.json";


const publicClient = await hre.viem.getPublicClient();
const logs = await publicClient.getContractEvents({
    abi: ERC20ABI.abi,
    address: loanToken.address,
    eventName: 'Approval'
})
console.log(logs);

Inspect transaction receipt

const tx1Hash = await loanToken.write.approve([myContractDeployed.address, maxUint256]);
const tx1Receipt = await hre.ethers.provider.getTransactionReceipt(tx1Hash);
console.log(tx1Receipt);

Output

  • status: 1 = success / 0 = failure
  • root: Deprecated Ethereum's pre-Byzantium fork era (replaced by status)
TransactionReceipt {
  provider: HardhatEthersProvider {
    _hardhatProvider: LazyInitializationProviderAdapter {
      _providerFactory: [AsyncFunction (anonymous)],
      _emitter: [EventEmitter],
      _initializingPromise: [Promise],
      provider: [BackwardsCompatibilityProviderAdapter]
    },
    _networkName: 'hardhat',
    _blockListeners: [],
    _transactionHashListeners: Map(0) {},
    _eventListeners: []
  },
  to: '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0',
  from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
  contractAddress: null,
  hash: '0x8d9cb0afed3c1becc9fa8275fdcd133410c95a70636f983635a9b49555e5687a',
  index: 0,
  blockHash: '0x4d3bb26b857e3eeab99d6ce67517ef728f88119ca2ba10a04d999a63cbd2dbf0',
  blockNumber: 8,
  logsBloom: '0x...',
  gasUsed: 46200n,
  cumulativeGasUsed: 46200n,
  gasPrice: 0n,
  type: 0,
  status: 1,
  root: undefined
}

Impersonate Account

inspect transaction receipt

const tx1Hash = await loanToken.write.approve([myContractDeployed.address, maxUint256]);
const tx1Receipt = await hre.ethers.provider.getTransactionReceipt(tx1Hash);
console.log(tx1Receipt);

Output

  • status: 1 = success / 0 = failure
  • root: Deprecated Ethereum's pre-Byzantium fork era (replaced by status)
TransactionReceipt {
  provider: HardhatEthersProvider {
    _hardhatProvider: LazyInitializationProviderAdapter {
      _providerFactory: [AsyncFunction (anonymous)],
      _emitter: [EventEmitter],
      _initializingPromise: [Promise],
      provider: [BackwardsCompatibilityProviderAdapter]
    },
    _networkName: 'hardhat',
    _blockListeners: [],
    _transactionHashListeners: Map(0) {},
    _eventListeners: []
  },
  to: '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0',
  from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
  contractAddress: null,
  hash: '0x8d9cb0afed3c1becc9fa8275fdcd133410c95a70636f983635a9b49555e5687a',
  index: 0,
  blockHash: '0x4d3bb26b857e3eeab99d6ce67517ef728f88119ca2ba10a04d999a63cbd2dbf0',
  blockNumber: 8,
  logsBloom: '0x...',
  gasUsed: 46200n,
  cumulativeGasUsed: 46200n,
  gasPrice: 0n,
  type: 0,
  status: 1,
  root: undefined
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment