Skip to content

Commit

Permalink
Fix getLastPool and Path tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wirew0lf committed Aug 2, 2024
1 parent 9c9c0d2 commit 44a1023
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions AxelarHandler/src/libraries/Path.sol
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ library Path {
/// @param path The bytes encoded swap path
/// @return The segment containing all data necessary to target the first pool in the path
function getLastPool(bytes memory path) internal pure returns (bytes memory) {
return path.slice(0, POP_OFFSET);
return path.slice(path.length - POP_OFFSET, POP_OFFSET);
}

/// @notice Skips a token + fee element from the buffer and returns the remainder
/// @param path The swap path
/// @return The remaining token + fee elements in the path
function skipToken(bytes memory path) internal pure returns (bytes memory) {
return path.slice(NEXT_OFFSET, path.length - NEXT_OFFSET);
return path.slice(path.length, path.length - NEXT_OFFSET);
}
}
46 changes: 46 additions & 0 deletions AxelarHandler/test/Path.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

import "forge-std/Test.sol";

import {BytesLib, Path} from "src/libraries/Path.sol";

contract PathTest is Test {
using Path for bytes;

function test_toBytes() public {
address testAddr = makeAddr("test");
bytes memory testAddrBytes = BytesLib.toBytes(testAddr);

assertEq(testAddr, BytesLib.toAddress(testAddrBytes, 0), "Address to bytes lib");
assertEq(testAddr, address(uint160(bytes20(testAddrBytes))), "Address to bytes typecast");
}

function test_decodeFirstPool() public {
address token1 = makeAddr("TOKEN 1");
address token2 = makeAddr("TOKEN 2");
address token3 = makeAddr("TOKEN 3");

bytes memory testPath = abi.encodePacked(token1, uint24(100), token2, uint24(3000), token3);

(address tokenA, address tokenB, uint24 fee) = testPath.decodeFirstPool();

assertEq(tokenA, token1, "Token A not properly decoded");
assertEq(tokenB, token2, "Token B not properly decoded");
assertEq(uint24(100), fee, "fee not properly decoded");
}

function test_decodeLastPool() public {
address token1 = makeAddr("TOKEN 1");
address token2 = makeAddr("TOKEN 2");
address token3 = makeAddr("TOKEN 3");

bytes memory testPath = abi.encodePacked(token1, uint24(100), token2, uint24(3000), token3);

(address tokenA, address tokenB, uint24 fee) = testPath.decodeLastPool();

assertEq(tokenA, token2, "Token A not properly decoded");
assertEq(tokenB, token3, "Token B not properly decoded");
assertEq(uint24(3000), fee, "fee not properly decoded");
}
}

0 comments on commit 44a1023

Please sign in to comment.