Skip to content

Commit

Permalink
fix: ERC-20 portal docs (amount -> value)
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Apr 1, 2024
1 parent 8601c58 commit 66a776a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions contracts/common/InputEncoding.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ library InputEncoding {
/// @notice Encode an ERC-20 token deposit.
/// @param token The token contract
/// @param sender The token sender
/// @param amount The amount of tokens being sent
/// @param value The amount of tokens being sent
/// @param execLayerData Additional data to be interpreted by the execution layer
/// @return The encoded input payload
function encodeERC20Deposit(
IERC20 token,
address sender,
uint256 amount,
uint256 value,
bytes calldata execLayerData
) internal pure returns (bytes memory) {
return
abi.encodePacked(
token, // 20B
sender, // 20B
amount, // 32B
value, // 32B
execLayerData // arbitrary size
);
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/portals/ERC20Portal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ contract ERC20Portal is IERC20Portal, Portal {
function depositERC20Tokens(
IERC20 token,
address appContract,
uint256 amount,
uint256 value,
bytes calldata execLayerData
) external override {
bool success = token.transferFrom(msg.sender, appContract, amount);
bool success = token.transferFrom(msg.sender, appContract, value);

if (!success) {
revert ERC20TransferFailed();
Expand All @@ -35,7 +35,7 @@ contract ERC20Portal is IERC20Portal, Portal {
bytes memory payload = InputEncoding.encodeERC20Deposit(
token,
msg.sender,
amount,
value,
execLayerData
);

Expand Down
6 changes: 3 additions & 3 deletions contracts/portals/IERC20Portal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ interface IERC20Portal is IPortal {
/// @notice Transfer ERC-20 tokens to an application contract
/// and add an input to the application's input box to signal such operation.
///
/// The caller must allow the portal to withdraw at least `amount` tokens
/// The caller must allow the portal to withdraw at least `value` tokens
/// from their account beforehand, by calling the `approve` function in the
/// token contract.
///
/// @param token The ERC-20 token contract
/// @param appContract The application contract address
/// @param amount The amount of tokens to be transferred
/// @param value The amount of tokens to be transferred
/// @param execLayerData Additional data to be interpreted by the execution layer
function depositERC20Tokens(
IERC20 token,
address appContract,
uint256 amount,
uint256 value,
bytes calldata execLayerData
) external;
}
48 changes: 24 additions & 24 deletions test/foundry/portals/ERC20Portal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ contract ERC20PortalTest is ERC165Test {
assertEq(address(_portal.getInputBox()), address(_inputBox));
}

function testTokenReturnsTrue(uint256 amount, bytes calldata data) public {
bytes memory transferFrom = _encodeTransferFrom(amount);
function testTokenReturnsTrue(uint256 value, bytes calldata data) public {
bytes memory transferFrom = _encodeTransferFrom(value);

vm.mockCall(address(_token), transferFrom, abi.encode(true));

bytes memory payload = _encodePayload(_token, amount, data);
bytes memory payload = _encodePayload(_token, value, data);

bytes memory addInput = _encodeAddInput(payload);

Expand All @@ -74,15 +74,15 @@ contract ERC20PortalTest is ERC165Test {
vm.expectCall(address(_inputBox), addInput, 1);

vm.prank(_alice);
_portal.depositERC20Tokens(_token, _appContract, amount, data);
_portal.depositERC20Tokens(_token, _appContract, value, data);
}

function testTokenReturnsFalse(uint256 amount, bytes calldata data) public {
bytes memory transferFrom = _encodeTransferFrom(amount);
function testTokenReturnsFalse(uint256 value, bytes calldata data) public {
bytes memory transferFrom = _encodeTransferFrom(value);

vm.mockCall(address(_token), transferFrom, abi.encode(false));

bytes memory payload = _encodePayload(_token, amount, data);
bytes memory payload = _encodePayload(_token, value, data);

bytes memory addInput = _encodeAddInput(payload);

Expand All @@ -91,19 +91,19 @@ contract ERC20PortalTest is ERC165Test {
vm.expectRevert(IERC20Portal.ERC20TransferFailed.selector);

vm.prank(_alice);
_portal.depositERC20Tokens(_token, _appContract, amount, data);
_portal.depositERC20Tokens(_token, _appContract, value, data);
}

function testTokenReverts(
uint256 amount,
uint256 value,
bytes calldata data,
bytes memory errorData
) public {
bytes memory transferFrom = _encodeTransferFrom(amount);
bytes memory transferFrom = _encodeTransferFrom(value);

vm.mockCallRevert(address(_token), transferFrom, errorData);

bytes memory payload = _encodePayload(_token, amount, data);
bytes memory payload = _encodePayload(_token, value, data);

bytes memory addInput = _encodeAddInput(payload);

Expand All @@ -112,25 +112,25 @@ contract ERC20PortalTest is ERC165Test {
vm.expectRevert(errorData);

vm.prank(_alice);
_portal.depositERC20Tokens(_token, _appContract, amount, data);
_portal.depositERC20Tokens(_token, _appContract, value, data);
}

function testNormalToken(
uint256 supply,
uint256 amount,
uint256 value,
bytes calldata data
) public {
amount = bound(amount, 0, supply);
value = bound(value, 0, supply);

NormalToken token = new NormalToken(_alice, supply);

bytes memory payload = _encodePayload(token, amount, data);
bytes memory payload = _encodePayload(token, value, data);

bytes memory addInput = _encodeAddInput(payload);

vm.startPrank(_alice);

token.approve(address(_portal), amount);
token.approve(address(_portal), value);

vm.mockCall(address(_inputBox), addInput, abi.encode(bytes32(0)));

Expand All @@ -142,32 +142,32 @@ contract ERC20PortalTest is ERC165Test {
vm.expectCall(address(_inputBox), addInput, 1);

vm.expectEmit(true, true, false, true, address(token));
emit IERC20.Transfer(_alice, _appContract, amount);
emit IERC20.Transfer(_alice, _appContract, value);

// deposit tokens
_portal.depositERC20Tokens(token, _appContract, amount, data);
_portal.depositERC20Tokens(token, _appContract, value, data);

vm.stopPrank();

// balances after
assertEq(token.balanceOf(_alice), supply - amount);
assertEq(token.balanceOf(_appContract), amount);
assertEq(token.balanceOf(_alice), supply - value);
assertEq(token.balanceOf(_appContract), value);
assertEq(token.balanceOf(address(_portal)), 0);
}

function _encodePayload(
IERC20 token,
uint256 amount,
uint256 value,
bytes calldata data
) internal view returns (bytes memory) {
return InputEncoding.encodeERC20Deposit(token, _alice, amount, data);
return InputEncoding.encodeERC20Deposit(token, _alice, value, data);
}

function _encodeTransferFrom(
uint256 amount
uint256 value
) internal view returns (bytes memory) {
return
abi.encodeCall(IERC20.transferFrom, (_alice, _appContract, amount));
abi.encodeCall(IERC20.transferFrom, (_alice, _appContract, value));
}

function _encodeAddInput(
Expand Down

0 comments on commit 66a776a

Please sign in to comment.