Skip to content

Commit 0f2a50e

Browse files
authored
Merge pull request #120 from OffchainLabs/add-contract-deposit
Add contract deposit
2 parents 65ef450 + 0861086 commit 0f2a50e

8 files changed

Lines changed: 394 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ yarn install
2525
- ⤵️ 🔹 [Withdraw Ether](./packages/eth-withdraw/)
2626
- ⤴️ 💸 [Deposit Token](./packages/token-deposit/)
2727
- ⤵️ 💸 [Withdraw token](./packages/token-withdraw/)
28+
- ⤴️ 🔹 [L2 Alias Control and Fund Transfer Guide](./packages/contract-deposit/)
2829

29-
#### :white_check_mark: General Interop
30+
#### :white_check_mark: General interop
3031

3132
- 🤝 [Greeter](./packages/greeter/) (L1 to L2)
3233
- 📤 [Outbox](./packages/outbox-execute/) (L2 to L1)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This is a sample .env file for use in local development.
2+
3+
# Duplicate this file as .env here
4+
5+
# Your Private key
6+
7+
DEVNET_PRIVKEY="0x your key here"
8+
9+
# Hosted aggregator node (JSON-RPC endpoint). This is Arbitrum Sepolia Testnet. You can use any Arbitrum chain.
10+
11+
L2RPC="https://sepolia-rollup.arbitrum.io/rpc"
12+
13+
# Ethereum RPC; i.e., for Sepolia https://sepolia.infura.io/v3/<your infura key>
14+
15+
L1RPC=""
16+
17+
# The address that you will transfer the funds from your l2 alias to
18+
19+
TransferTo=
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
artifacts/
2+
cache/
3+
build/
4+
node_modules/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# L2 alias control and fund transfer guide
2+
3+
`L2 Alias Control and Fund Transfer Guide` is a simple demonstration of how Arbitrum facilitates sending deposits from an L1 contract to L2.
4+
5+
It will deposit funds to l2 via a l1 contract, and because the inbox contract will alias the sender address if it is a contract,
6+
this tutorial shows how to control the alias address on l2 via its l1 contract address.
7+
8+
This tutorial demonstrates depositing funds to L2 using an L1 contract. Since the Inbox contract changes the sender address if it's a contract [(a.k.a address aliasing)](https://docs.arbitrum.io/how-arbitrum-works/arbos/l1-l2-messaging#address-aliasing), it explains how to manage the new L2 address using the L1 contract address. It's a basic example of how an L1 contract controls its L2 alias and transfers its alias funds to another address. For practical use, we recommend our [funds recovery tool]((https://github.com/OffchainLabs/arbitrum-funds-recovery-tool)).
9+
10+
The script and contracts demonstrate how to interact with Arbitrum's core bridge contracts to create these retryable messages, how to calculate and forward appropriate fees from L1 to L2, and how to use Arbitrum's L1-to-L2 message [address aliasing](https://developer.offchainlabs.com/docs/l1_l2_messages#address-aliasing).
11+
12+
See [./exec.js](./scripts/exec.js) for inline explanations.
13+
14+
[Click here](https://developer.offchainlabs.com/docs/l1_l2_messages) for more info on retryable tickets.
15+
16+
### Run demo:
17+
18+
```
19+
yarn start
20+
```
21+
22+
## Config environment variables
23+
24+
Set the values shown in `.env-sample` as environmental variables. To copy it into a `.env` file:
25+
26+
```shell
27+
cp .env-sample .env
28+
```
29+
30+
(you'll still need to edit some variables, i.e., `DEVNET_PRIVKEY`)
31+
32+
<p align="left">
33+
<img width="350" height="150" src= "../../assets/logo.svg" />
34+
</p>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity >=0.6.11;
3+
4+
import "@arbitrum/nitro-contracts/src/bridge/Inbox.sol";
5+
6+
contract EthDeposit {
7+
IInbox public inbox;
8+
9+
event EthDeposited(uint256 indexed ticketId);
10+
event RetryableTicketCreated(uint256 indexed ticketId);
11+
12+
constructor(address _inbox) {
13+
inbox = IInbox(_inbox);
14+
}
15+
16+
function depositToL2() public payable returns (uint256) {
17+
uint256 ticketID = inbox.depositEth{ value: msg.value }();
18+
19+
emit EthDeposited(ticketID);
20+
return ticketID;
21+
}
22+
23+
function moveFundsFromL2AliasToAnotherAddress(
24+
address to,
25+
uint256 l2callvalue,
26+
uint256 maxSubmissionCost,
27+
uint256 maxGas,
28+
uint256 gasPriceBid
29+
) public payable returns (uint256) {
30+
/**
31+
* We are using unsafeCreateRetryableTicket because the safe one will check if
32+
* the msg.value can be used to pay for the l2 callvalue while we will use l2's
33+
* balance to pay for the l2 callvalue rather than l1 msg.value.
34+
*/
35+
uint256 ticketID = inbox.unsafeCreateRetryableTicket{ value: msg.value }(
36+
to,
37+
l2callvalue,
38+
maxSubmissionCost,
39+
msg.sender,
40+
msg.sender,
41+
maxGas,
42+
gasPriceBid,
43+
""
44+
);
45+
emit RetryableTicketCreated(ticketID);
46+
return ticketID;
47+
}
48+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require('@nomiclabs/hardhat-ethers')
2+
const { hardhatConfig } = require('arb-shared-dependencies')
3+
4+
module.exports = hardhatConfig
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "contract-deposit",
3+
"license": "Apache-2.0",
4+
"version": "1.0.0",
5+
"scripts": {
6+
"build": "hardhat compile",
7+
"start": "hardhat run scripts/exec.js"
8+
},
9+
"devDependencies": {
10+
"@nomiclabs/hardhat-ethers": "^2.0.2",
11+
"ethers": "^5.1.2",
12+
"hardhat": "^2.2.0"
13+
},
14+
"dependencies": {
15+
"@arbitrum/sdk": "^v3.1.9",
16+
"@arbitrum/nitro-contracts": "v1.0.2",
17+
"dotenv": "^8.2.0"
18+
}
19+
}

0 commit comments

Comments
 (0)