I am testing on mainnet-fork and it always fails when particular contracts like (Uniswap) are called more than once.

Is there any fix for this or is this problem outside the scope of brownie? Thanks

Here is the error

FAILED tests/test_uniswap.py::test_uniswap - brownie.exceptions.VirtualMachineError: revert: UniswapV2: LOCKED

Here is the code. It swaps DAI for USDC

contract TestUniswap {
    address constant private usdc = address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
    address constant private dai = address(0x6B175474E89094C44Da98b954EedeAC495271d0F);

    address constant private uniswap = address(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
    address constant private weth = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);

    function swap() external {
        uint daiBal = IERC20(dai).balanceOf(address(this));
        IERC20(dai).approve(uniswap, daiBal);

        address[] memory path = new address[](3);
        path[0] = dai;
        path[1] = weth;
        path[2] = usdc;

            daiBal, uint(0), path, address(this), now + 1800

Here is the test

def test_uniswap(accounts, testUniswap, dai, stable_coin_holder):
    dai_amount = 10 * 10 ** 18

    # check dai balance
    stable_coin_holder_bal = dai.balanceOf(stable_coin_holder)
    assert stable_coin_holder_bal >= dai_amount

    # transfer DAI to testUniswap
    dai.transfer(testUniswap, dai_amount, {'from': stable_coin_holder})
    assert dai.balanceOf(testUniswap) == dai_amount


For USDT and USDC pools, the tokens have 6 decimal places, so you have to adjust the number by 10**12.

As far as the 'LOCKED' message goes, you'll get that verbiage somewhere in the message for almost every Uniswap failure, so you should ignore it. Look at the rest of the message to try to determine why it's failing.


It seems that LOCK is a reenter guard for Uniswap. if just one call of testUniswap.swap() works, the second call may reenter the same contract that's why it reverts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.