Skip to content

Commit

Permalink
feat: combine contracts for l1 -> l2 and l2 -> l1 (#726)
Browse files Browse the repository at this point in the history
* feat: combine contracts for l1 -> l2 and l2 -> l1

* chore: circle-ci

* chore: circle-ci

* chore: add natspec in TokenPortal

* chore: remove rollup_native_asset_contract.json
  • Loading branch information
LHerskind authored Jun 1, 2023
1 parent 66e82d4 commit 42c4219
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 881 deletions.
21 changes: 4 additions & 17 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ jobs:
name: "Test"
command: cond_spot_run_tests end-to-end e2e_nested_contract.test.ts

e2e-l1-to-l2-messaging:
e2e-cross-chain-messaging:
docker:
- image: aztecprotocol/alpine-build-image
resource_class: small
Expand All @@ -438,18 +438,7 @@ jobs:
- *setup_env
- run:
name: "Test"
command: cond_spot_run_tests end-to-end e2e_l1_to_l2_msg.test.ts

e2e-l2-to-l1-messaging:
docker:
- image: aztecprotocol/alpine-build-image
resource_class: small
steps:
- *checkout
- *setup_env
- run:
name: "Test"
command: cond_spot_run_tests end-to-end e2e_rollup_native_asset_contract.test.ts
command: cond_spot_run_tests end-to-end e2e_cross_chain_messaging.test.ts

integration-l1-publisher:
docker:
Expand Down Expand Up @@ -611,8 +600,7 @@ workflows:
- e2e-block-building: *e2e_test
- e2e-nested-contract: *e2e_test
- e2e-public-token-contract: *e2e_test
- e2e-l2-to-l1-messaging: *e2e_test
- e2e-l1-to-l2-messaging: *e2e_test
- e2e-cross-chain-messaging: *e2e_test
- integration-l1-publisher: *e2e_test
- e2e-p2p: *e2e_test

Expand All @@ -623,8 +611,7 @@ workflows:
- e2e-block-building
- e2e-nested-contract
- e2e-public-token-contract
- e2e-l2-to-l1-messaging
- e2e-l1-to-l2-messaging
- e2e-cross-chain-messaging
- integration-l1-publisher
- e2e-p2p
<<: *defaults
77 changes: 0 additions & 77 deletions l1-contracts/test/portals/RNA.t.sol

This file was deleted.

37 changes: 0 additions & 37 deletions l1-contracts/test/portals/RollupNativeAsset.sol

This file was deleted.

24 changes: 24 additions & 0 deletions l1-contracts/test/portals/TokenPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,28 @@ contract TokenPortal {
// Send message to rollup
return inbox.sendL2Message{value: msg.value}(actor, _deadline, contentHash, _secretHash);
}

/**
* @notice Withdraw funds from the portal
* @dev Second part of withdraw, must be initiated from L2 first as it will consume a message from outbox
* @param _amount - The amount to withdraw
* @param _recipient - The address to send the funds to
* @return The key of the entry in the Outbox
*/
function withdraw(uint256 _amount, address _recipient) external returns (bytes32) {
DataStructures.L2ToL1Msg memory message = DataStructures.L2ToL1Msg({
sender: DataStructures.L2Actor(l2TokenAddress, 1),
recipient: DataStructures.L1Actor(address(this), block.chainid),
content: Hash.sha256ToField(
abi.encodeWithSignature("withdraw(uint256,address)", _amount, _recipient)
)
});

// @todo: (issue #624) handle different versions
bytes32 entryKey = registry.getOutbox().consume(message);

underlying.transfer(_recipient, _amount);

return entryKey;
}
}
34 changes: 34 additions & 0 deletions l1-contracts/test/portals/TokenPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Registry} from "@aztec/core/messagebridge/Registry.sol";
import {Outbox} from "@aztec/core/messagebridge/Outbox.sol";
import {DataStructures} from "@aztec/core/libraries/DataStructures.sol";
import {Hash} from "@aztec/core/libraries/Hash.sol";
import {Errors} from "@aztec/core/libraries/Errors.sol";

// Interfaces
import {IRegistry} from "@aztec/core/interfaces/messagebridge/IRegistry.sol";
Expand All @@ -30,6 +31,7 @@ contract TokenPortalTest is Test {
bytes32 content,
bytes32 secretHash
);
event MessageConsumed(bytes32 indexed entryKey, address indexed recipient);

Registry internal registry;
Inbox internal inbox;
Expand Down Expand Up @@ -103,4 +105,36 @@ contract TokenPortalTest is Test {
DataStructures.Entry memory entry = inbox.get(entryKey);
assertEq(entry.count, 1);
}

function testWithdraw() public {
uint256 withdrawAmount = 654;
portalERC20.mint(address(tokenPortal), withdrawAmount);
address _recipient = address(0xdead);
bytes32[] memory entryKeys = new bytes32[](1);
entryKeys[0] = outbox.computeEntryKey(
DataStructures.L2ToL1Msg({
sender: DataStructures.L2Actor({actor: l2TokenAddress, version: 1}),
recipient: DataStructures.L1Actor({actor: address(tokenPortal), chainId: block.chainid}),
content: Hash.sha256ToField(
abi.encodeWithSignature("withdraw(uint256,address)", withdrawAmount, _recipient)
)
})
);

// Insert messages into the outbox (impersonating the rollup contract)
vm.prank(address(rollup));
outbox.sendL1Messages(entryKeys);

assertEq(portalERC20.balanceOf(_recipient), 0);

vm.expectEmit(true, true, true, true);
emit MessageConsumed(entryKeys[0], address(tokenPortal));
bytes32 entryKey = tokenPortal.withdraw(withdrawAmount, _recipient);
// Should have received 654 RNA tokens
assertEq(portalERC20.balanceOf(_recipient), withdrawAmount);

// Should not be able to withdraw again
vm.expectRevert(abi.encodeWithSelector(Errors.Outbox__NothingToConsume.selector, entryKey));
tokenPortal.withdraw(withdrawAmount, _recipient);
}
}
Loading

0 comments on commit 42c4219

Please sign in to comment.