-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
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. |
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? |
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]));
})
}); |
Apparently |
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 😅 |
sure! Will be using it much more so I'm glad to receive such feedback, |
If you have any additional feedback, please let me know! I want to make |
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 :
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] ])
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
The text was updated successfully, but these errors were encountered: