Skip to content

juliancabmar/audit-my_helper_notes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

My Helper Notes


Using enums and structs

Access enum from another contract

function testAccessEnum() public pure {
    console.log(uint256(Counter.Weather.SUNNY));
    console.log(uint256(Counter.Weather.RAINY));
    console.log(uint256(Counter.Weather.SNOW));
}

Using struct from another contract

function testUsingStruct() public pure {
    Counter.People memory vero = Counter.People({name: "Veronica", lastName: "Moreno", age: 45});

    console.log("Name: ", vero.name);
    console.log("Lastname: ", vero.lastName);
    console.log("Age: ", vero.age);
}

Handle Errors

With Try/Catch

function testCatchErrorRequireReasonByTryCatch() public {
    try Counter(counterAddr).setNumber(0) {}
    // Get require message
    catch Error(string memory reason) {
        console.log(reason);
    }
}
function testCatchErrorRevertSelectorByTryCatch() public {
    try Counter(counterAddr).setNumber(666) {}
    catch (bytes memory message) {
        if (bytes4(message) == Counter.Counter__EvilNotAllowedHere.selector) {
            console.log("Do Something");
        }
    }
}
function testCatchErrorRevertCalldataByTryCatch() public {
    try Counter(counterAddr).setNumber(666) {}
    catch (bytes memory message) {
        if (
            keccak256(message)
                == keccak256(abi.encodeWithSelector(Counter.Counter__EvilNotAllowedHere.selector, 666))
        ) {
            console.log("Do Something");
        }
    }
}
function testCatchErrorRevertParamByTryCatch() public {
    try Counter(counterAddr).setNumber(666) {}
    catch (bytes memory message) {
        // slice the param desire segment
        bytes memory param = slice(message, 4, message.length - 4);
        console.log(uint256(bytes32(param)));
    }
}

With vm.expectRevert/PartialRevert

function testCatchErrorByMessage() public {
    vm.expectRevert("Zero is not allowed");
    Counter(counterAddr).setNumber(0);
}
function testCatchErrorBySelectorOnly() public {
    // For this I need to use expectPartialRevert
    vm.expectPartialRevert(Counter.Counter__EvilNotAllowedHere.selector);
    Counter(counterAddr).setNumber(666);
}
function testCatchErrorBySelectorAndParams() public {
    vm.expectRevert(abi.encodeWithSelector(Counter.Counter__EvilNotAllowedHere.selector, 666));
    Counter(counterAddr).setNumber(666);
}

Handle Events

With vm.expectEmit

function testExpectEventSelectorOnly() public {
    vm.expectEmit(false, false, false, false);
    // The input parameters passed will don't check, only the event selector
    emit Counter.SumExecuted(3, 3, 3);

    Counter(counterAddr).sum(1, 2);
}
function testExpectEventFullMatch() public {
    vm.expectEmit();
    // The event expect pass if all the params match
    emit Counter.SumExecuted(2, 3, 5);

    Counter(counterAddr).sum(2, 3);
}
function testExpectEventWithNonIndexedParamsMatch() public {
    vm.expectEmit(false, false, false, true);
    // Only check the not indexed params
    emit Counter.SumExecuted(44, 3, 25);

    Counter(counterAddr).sum(2, 3);
}
function testExpectEventWithSomeIndexedParamsMatch() public {
    vm.expectEmit(false, true, false, false);
    // Only check the second indexed param 'result'
    emit Counter.SumExecuted(44, 23, 5);

    Counter(counterAddr).sum(2, 3);
}

Getting Params

function testExtractEventParams() public {
    // Asumme we have the next event:
    // event LargeSumExecuted(uint256 indexed a,uint256 indexed b,uint256 indexed c,uint256 d,uint256 e,uint256 f,uint256 g)
    // Now we need to get the b and f params

    vm.recordLogs(); // Start recording events

    Counter(counterAddr).sum(2, 3); // This should emit the event

    Vm.Log[] memory logs = vm.getRecordedLogs();

    for (uint256 i = 0; i < logs.length; i++) {
        Vm.Log memory log = logs[i];

        // Check if it's the event you're interested in (compare selector)
        if (log.topics[0] == keccak256("LargeSumExecuted(uint256,uint256,uint256,uint256,uint256,uint256,uint256)"))
        {
            // Get 'b' (2nd indexed param)
            uint256 b = uint256(log.topics[2]);

            // Get 'f' (3th value in 'data' => index 2)
            (uint256 d, uint256 e, uint256 f, uint256 g) =
                abi.decode(log.data, (uint256, uint256, uint256, uint256));

            // // Now you can use them:
            console.log(b);
            console.log(f);
        }
    }
}

Helper Functions

function slice(bytes memory data, uint256 start, uint256 length) internal pure returns (bytes memory) {
    require(start + length <= data.length, "Slice out of bounds");

    bytes memory result = new bytes(length);

    for (uint256 i = 0; i < length; i++) {
        result[i] = data[start + i];
    }

    return result;
}

About

My helper notes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages