Skip to content

Instantly share code, notes, and snippets.

@cyborgdennett
Last active November 3, 2022 14:38
Show Gist options
  • Select an option

  • Save cyborgdennett/f5b80caaf072386dc4a3a91f810e1424 to your computer and use it in GitHub Desktop.

Select an option

Save cyborgdennett/f5b80caaf072386dc4a3a91f810e1424 to your computer and use it in GitHub Desktop.

Revisions

  1. cyborgdennett revised this gist Nov 3, 2022. 1 changed file with 35 additions and 0 deletions.
    35 changes: 35 additions & 0 deletions OpenZeppelinMulticallCaller.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    // An example using hardhat to create a contract and multicall the increment function n-times in a single transaction.
    import { artifacts, ethers } from "hardhat";

    async function main() {
    const Clicker = await ethers.getContractFactory("CookieClicker");

    const clicker = await Clicker.deploy()

    await clicker.deployed()

    const amt = 10 // times to multicall the same smart contract function

    // get the abi of the function you wish to call
    const inc = await getIncAbi()

    // call the multicall function
    var tx = await clicker.multicall(
    Array(amt).fill(inc) // makes an array with n-times the same element
    )
    }

    async function getIncAbi() {
    const artifact = await ethers.getContractFactory("CookieClicker");

    var functions = Object.keys(artifact.interface.functions)
    functions.forEach(function (part, index) {
    this[index] = "function " + part;
    }, functions);

    let iface = new ethers.utils.Interface(functions);

    return iface.encodeFunctionData("inc")
    }

    main()
  2. cyborgdennett revised this gist Oct 31, 2022. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions OpenZeppelinMulticallERC20Token.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    pragma solidity ^0.8.0;

    import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
    import "@openzeppelin/contracts/utils/Multicall.sol";

    contract Token is ERC20, Multicall {
    constructor(uint256 initialSupply) ERC20("Gold", "GLD") {
    _mint(msg.sender, initialSupply);
    }
    }
  3. cyborgdennett revised this gist Oct 29, 2022. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions OpenZeppelinMulticallCookieClicker.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;

    import "@openzeppelin/contracts/utils/Multicall.sol";

    contract CookieClicker is Multicall {
    int public cookies = 0;

    function inc() public {
    cookies++;
    }
    }
  4. cyborgdennett revised this gist Oct 29, 2022. 2 changed files with 48 additions and 0 deletions.
    48 changes: 48 additions & 0 deletions MakerDAOMulticall.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    // SPDX-License-Identifier: MIT


    pragma solidity >=0.5.0;
    pragma experimental ABIEncoderV2;

    /// @title Multicall - Aggregate results from multiple read-only function calls
    /// @author Michael Elliot <[email protected]>
    /// @author Joshua Levine <[email protected]>
    /// @author Nick Johnson <[email protected]>

    contract Multicall {
    struct Call {
    address target;
    bytes callData;
    }
    function aggregate(Call[] memory calls) public returns (uint256 blockNumber, bytes[] memory returnData) {
    blockNumber = block.number;
    returnData = new bytes[](calls.length);
    for(uint256 i = 0; i < calls.length; i++) {
    (bool success, bytes memory ret) = calls[i].target.call(calls[i].callData);
    require(success);
    returnData[i] = ret;
    }
    }
    // Helper functions
    function getEthBalance(address addr) public view returns (uint256 balance) {
    balance = addr.balance;
    }
    function getBlockHash(uint256 blockNumber) public view returns (bytes32 blockHash) {
    blockHash = blockhash(blockNumber);
    }
    function getLastBlockHash() public view returns (bytes32 blockHash) {
    blockHash = blockhash(block.number - 1);
    }
    function getCurrentBlockTimestamp() public view returns (uint256 timestamp) {
    timestamp = block.timestamp;
    }
    function getCurrentBlockDifficulty() public view returns (uint256 difficulty) {
    difficulty = block.difficulty;
    }
    function getCurrentBlockGasLimit() public view returns (uint256 gaslimit) {
    gaslimit = block.gaslimit;
    }
    function getCurrentBlockCoinbase() public view returns (address coinbase) {
    coinbase = block.coinbase;
    }
    }
    File renamed without changes.
  5. cyborgdennett created this gist Oct 29, 2022.
    24 changes: 24 additions & 0 deletions Multicall.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.5.0) (utils/Multicall.sol)

    pragma solidity ^0.8.0;

    import "./Address.sol";

    /**
    * @dev Provides a function to batch together multiple calls in a single external call.
    *
    * _Available since v4.1._
    */
    abstract contract Multicall {
    /**
    * @dev Receives and executes a batch of function calls on this contract.
    */
    function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
    results = new bytes[](data.length);
    for (uint256 i = 0; i < data.length; i++) {
    results[i] = Address.functionDelegateCall(address(this), data[i]);
    }
    return results;
    }
    }