Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation and a unit test for returning multiple arrays in a smocked function #1228

Closed
wei3erHase opened this issue Jul 7, 2021 · 9 comments · Fixed by #1247
Closed
Labels
C-feature Category: features

Comments

@wei3erHase
Copy link

When trying to mock an UniswapV3Pool, to be accessed through the Oracle library, we're pointing to the observe(secondsAgo) function.

Code to be used in our contract:
(int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s) = IUniswapV3Pool(pool).observe(secondsAgos);

UniswapV3Pool.sol :

Oracle.Observation[65535] public override observations;
    [...]
    /// LINE 236
    function observe(uint32[] calldata secondsAgos)
        external
        view
        override
        noDelegateCall
        returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s)
    {  ...  }

When trying to smock the answer of the address, have tried multiple options:
pool.smocked.observe.will.return.with([ 1, 1 ])
pool.smocked.observe.will.return.with([ [1], [1] ])

'tickCumulatives':1,
'secondsPerLiquidityCumulativeX128s':1
})
pool.smocked.observe.will.return.with({
'tickCumulatives':[1,2,3],
'secondsPerLiquidityCumulativeX128s':[1,2,3]
})

I've tried also replacing the numbers with BigNumber.from(0) but still, every try I'm getting the following error Error: Transaction reverted: function returned an unexpected amount of data

@smartcontracts
Copy link
Contributor

Hmmm 🤔 nice catch. I assume the ideal syntax here is return.with([ 1234 ], [ 5678 ]) or alternatively the same thing with the return value wrapped in an array. I'll do some testing and see why this isn't working correctly.

@smartcontracts
Copy link
Contributor

Hmmm very weird. I can't seem to replicate this locally. What version of hardhat are you using and what version of smock are you using?

@smartcontracts smartcontracts added C-bug Category: bugs S-unconfirmed Status: Issue might be valid, but it’s not yet confirmed labels Jul 7, 2021
@wei3erHase
Copy link
Author

wei3erHase commented Jul 7, 2021

return.with([ 1234 ], [ 5678 ]) doesn't seem to be compilable with typescript:
image

TSError: ⨯ Unable to compile TypeScript:
test/unit/UniV3PoolTest.spec.ts:27:50 - error TS2554: Expected 0-1 arguments, but got 2.
27     pool.smocked.observe.will.return.with([1234],[5678]);

I'm using:

    "hardhat": "2.4.1",
    "@typechain/hardhat": "2.1.1",
    "@eth-optimism/smock": "1.1.6",

@wei3erHase
Copy link
Author

A test contract:

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;

import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol';

contract QuoterTest {
  address public token;
  address public weth;
  address public uniswapV3Pool;

  constructor(
    address _token,
    address _weth,
    address _uniswapV3Pool
  ) {
    token = _token;
    weth = _weth;
    uniswapV3Pool = _uniswapV3Pool;
  }

  uint32 public constant TWAP_PERIOD = 1 days;

  function observe(uint32[] calldata secondsAgos) public view returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s) {
    (tickCumulatives, secondsPerLiquidityCumulativeX128s) = IUniswapV3Pool(uniswapV3Pool).observe(secondsAgos);
  }
}

A unit test:

import { ethers } from 'hardhat';
import { BigNumber, utils } from 'ethers';
import { smockit, MockContract } from '@eth-optimism/smock';
import { QuoterTest, QuoterTest__factory } from '@types';

import IUniswapV3Pool from '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json';
// import WeightedOracleLibrary from '@contracts/for-test/WeightedOracleLibraryForTest.sol/WeightedOracleLibrary.json';

describe.only('IQuoterTest', () => {
  let quoter: QuoterTest;
  let pool: MockContract;

  // examples for e2e checking
  const WETH = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2';
  const TOKEN = '0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9';
  const POOL = '0x5ab53ee1d50eef2c1dd3d5402789cd27bb52c1bb';

  beforeEach(async () => {
    pool = await smockit(IUniswapV3Pool.abi);

    const quoterFactory = (await ethers.getContractFactory('QuoterTest')) as unknown as QuoterTest__factory;
    quoter = await quoterFactory.deploy(TOKEN, WETH, pool.address);

    // pool.observe >> (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s)
    pool.smocked.observe.will.return.with([1234],[5678]);
  });

  it('should deploy ok');
  it('should observe token', async() => {
    console.log(await quoter.observe([1]));
  })
});

@wei3erHase
Copy link
Author

Apparently will.return([ [1234] , [5678] ]) is getting the job done!
Thanks

@smartcontracts
Copy link
Contributor

Glad you figured it out, sorry about that! I'll add a unit test for this and update the docs, then will close this issue. Sometimes I make NFTs for people who make issues and I'm glad someone is using smock so just drop an ETH address if you want a smock-related hand-drawn NFT 😅

@smartcontracts smartcontracts changed the title There's no documentation on how to smock with a Struct of Arrays return Add documentation and a unit test for returning multiple arrays in a smocked function Jul 8, 2021
@smartcontracts smartcontracts added C-feature Category: features M-smock and removed C-bug Category: bugs S-unconfirmed Status: Issue might be valid, but it’s not yet confirmed labels Jul 8, 2021
@wei3erHase
Copy link
Author

sure! Will be using it much more so I'm glad to receive such feedback,
0xBad58e133138549936D2576ebC33251bE841d3e9
can't wait to see what this is about 😮

@smartcontracts
Copy link
Contributor

If you have any additional feedback, please let me know! I want to make smock as useful as possible :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature Category: features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants