-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat(ctp): introduce TeleportrWithdrawer #2709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@eth-optimism/contracts-periphery': patch | ||
| --- | ||
|
|
||
| Adds new TeleportrWithdrawer contract for withdrawing from Teleportr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/contracts-periphery/contracts/testing/helpers/MockTeleportr.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.9; | ||
|
|
||
| contract MockTeleportr { | ||
| function withdrawBalance() external { | ||
| payable(msg.sender).transfer(address(this).balance); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
packages/contracts-periphery/contracts/universal/TeleportrWithdrawer.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.9; | ||
|
|
||
| import { AssetReceiver } from "./AssetReceiver.sol"; | ||
|
|
||
| /** | ||
| * @notice Stub interface for Teleportr. | ||
| */ | ||
| interface Teleportr { | ||
| function withdrawBalance() external; | ||
| } | ||
|
|
||
| /** | ||
| * @title TeleportrWithdrawer | ||
| * @notice The TeleportrWithdrawer is a simple contract capable of withdrawing funds from the | ||
| * TeleportrContract and sending them to some recipient address. | ||
| */ | ||
| contract TeleportrWithdrawer is AssetReceiver { | ||
| /** | ||
| * @notice Address of the Teleportr contract. | ||
| */ | ||
| address public teleportr; | ||
|
|
||
| /** | ||
| * @notice Address that will receive Teleportr withdrawals. | ||
| */ | ||
| address public recipient; | ||
|
|
||
| /** | ||
| * @notice Data to be sent to the recipient address. | ||
| */ | ||
| bytes public data; | ||
|
|
||
| /** | ||
| * @param _owner Initial owner of the contract. | ||
| */ | ||
| constructor(address _owner) AssetReceiver(_owner) {} | ||
|
|
||
| /** | ||
| * @notice Allows the owner to update the recipient address. | ||
| * | ||
| * @param _recipient New recipient address. | ||
| */ | ||
| function setRecipient(address _recipient) external onlyOwner { | ||
| recipient = _recipient; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Allows the owner to update the Teleportr contract address. | ||
| * | ||
| * @param _teleportr New Teleportr contract address. | ||
| */ | ||
| function setTeleportr(address _teleportr) external onlyOwner { | ||
| teleportr = _teleportr; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Allows the owner to update the data to be sent to the recipient address. | ||
| * | ||
| * @param _data New data to be sent to the recipient address. | ||
| */ | ||
| function setData(bytes memory _data) external onlyOwner { | ||
| data = _data; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Withdraws the full balance of the Teleportr contract to the recipient address. | ||
| * Anyone is allowed to trigger this function since the recipient address cannot be | ||
| * controlled by the msg.sender. | ||
| */ | ||
| function withdrawFromTeleportr() external { | ||
| Teleportr(teleportr).withdrawBalance(); | ||
| (bool success, ) = recipient.call{ value: address(this).balance }(data); | ||
| require(success, "TeleportrWithdrawer: send failed"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/contracts-periphery/deploy/TeleportrWithdrawer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* Imports: External */ | ||
| import { DeployFunction } from 'hardhat-deploy/dist/types' | ||
|
|
||
| import { getDeployConfig } from '../src' | ||
|
|
||
| const deployFn: DeployFunction = async (hre) => { | ||
| const { deployer } = await hre.getNamedAccounts() | ||
|
|
||
| const config = getDeployConfig(hre.network.name) | ||
|
|
||
| const { deploy } = await hre.deployments.deterministic( | ||
| 'TeleportrWithdrawer', | ||
| { | ||
| salt: hre.ethers.utils.solidityKeccak256( | ||
| ['string'], | ||
| ['TeleportrWithdrawer'] | ||
| ), | ||
| from: deployer, | ||
| args: [config.ddd], | ||
| log: true, | ||
| } | ||
| ) | ||
|
|
||
| await deploy() | ||
| } | ||
|
|
||
| deployFn.tags = ['TeleportrWithdrawer'] | ||
|
|
||
| export default deployFn | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/contracts-periphery/test/contracts/universal/TeleportrWithdrawer.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import hre from 'hardhat' | ||
| import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' | ||
| import { Contract } from 'ethers' | ||
| import { toRpcHexString } from '@eth-optimism/core-utils' | ||
|
|
||
| import { expect } from '../../setup' | ||
| import { deploy } from '../../helpers' | ||
|
|
||
| describe('TeleportrWithdrawer', () => { | ||
| let signer1: SignerWithAddress | ||
| let signer2: SignerWithAddress | ||
| before('signer setup', async () => { | ||
| ;[signer1, signer2] = await hre.ethers.getSigners() | ||
| }) | ||
|
|
||
| let SimpleStorage: Contract | ||
| let MockTeleportr: Contract | ||
| let TeleportrWithdrawer: Contract | ||
| beforeEach('deploy contracts', async () => { | ||
| SimpleStorage = await deploy('SimpleStorage') | ||
| MockTeleportr = await deploy('MockTeleportr') | ||
| TeleportrWithdrawer = await deploy('TeleportrWithdrawer', { | ||
| signer: signer1, | ||
| args: [signer1.address], | ||
| }) | ||
| }) | ||
|
|
||
| describe('setRecipient', () => { | ||
| describe('when called by authorized address', () => { | ||
| it('should set the recipient', async () => { | ||
| await TeleportrWithdrawer.setRecipient(signer1.address) | ||
| expect(await TeleportrWithdrawer.recipient()).to.equal(signer1.address) | ||
| }) | ||
| }) | ||
|
|
||
| describe('when called by not authorized address', () => { | ||
| it('should revert', async () => { | ||
| await expect( | ||
| TeleportrWithdrawer.connect(signer2).setRecipient(signer2.address) | ||
| ).to.be.revertedWith('UNAUTHORIZED') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('setTeleportr', () => { | ||
| describe('when called by authorized address', () => { | ||
| it('should set the recipient', async () => { | ||
| await TeleportrWithdrawer.setTeleportr(MockTeleportr.address) | ||
| expect(await TeleportrWithdrawer.teleportr()).to.equal( | ||
| MockTeleportr.address | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('when called by not authorized address', () => { | ||
| it('should revert', async () => { | ||
| await expect( | ||
| TeleportrWithdrawer.connect(signer2).setTeleportr(signer2.address) | ||
| ).to.be.revertedWith('UNAUTHORIZED') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('setData', () => { | ||
| const data = `0x${'ff'.repeat(64)}` | ||
|
|
||
| describe('when called by authorized address', () => { | ||
| it('should set the data', async () => { | ||
| await TeleportrWithdrawer.setData(data) | ||
| expect(await TeleportrWithdrawer.data()).to.equal(data) | ||
| }) | ||
| }) | ||
|
|
||
| describe('when called by not authorized address', () => { | ||
| it('should revert', async () => { | ||
| await expect( | ||
| TeleportrWithdrawer.connect(signer2).setData(data) | ||
| ).to.be.revertedWith('UNAUTHORIZED') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('withdrawTeleportrBalance', () => { | ||
| const recipient = `0x${'11'.repeat(20)}` | ||
| const amount = hre.ethers.constants.WeiPerEther | ||
| beforeEach(async () => { | ||
| await hre.ethers.provider.send('hardhat_setBalance', [ | ||
| MockTeleportr.address, | ||
| toRpcHexString(amount), | ||
| ]) | ||
| await TeleportrWithdrawer.setRecipient(recipient) | ||
| await TeleportrWithdrawer.setTeleportr(MockTeleportr.address) | ||
| }) | ||
|
|
||
| describe('when target is an EOA', () => { | ||
| it('should withdraw the balance', async () => { | ||
| await TeleportrWithdrawer.withdrawFromTeleportr() | ||
| expect(await hre.ethers.provider.getBalance(recipient)).to.equal(amount) | ||
| }) | ||
| }) | ||
|
|
||
| describe('when target is a contract', () => { | ||
| it('should withdraw the balance and trigger code', async () => { | ||
| const key = `0x${'dd'.repeat(32)}` | ||
| const val = `0x${'ee'.repeat(32)}` | ||
| await TeleportrWithdrawer.setRecipient(SimpleStorage.address) | ||
| await TeleportrWithdrawer.setData( | ||
| SimpleStorage.interface.encodeFunctionData('set', [key, val]) | ||
| ) | ||
|
|
||
| await TeleportrWithdrawer.withdrawFromTeleportr() | ||
|
|
||
| expect( | ||
| await hre.ethers.provider.getBalance(SimpleStorage.address) | ||
| ).to.equal(amount) | ||
| expect(await SimpleStorage.get(key)).to.equal(val) | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.