Skip to content
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

Add Bridge example #36

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "examples/bridge/l1/lib/forge-std"]
path = examples/bridge/l1/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "examples/bridge/l1/lib/create3-factory"]
path = examples/bridge/l1/lib/create3-factory
url = https://github.com/zeframlou/create3-factory
9 changes: 8 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
[workspace]
members = ["crates", "examples/*", "token"]
members = [
"crates",
"examples/chess",
"examples/hex_map",
"examples/market",
"examples/projectile",
"token",
]

[workspace.package]
version = "0.5.0"
Expand Down
41 changes: 41 additions & 0 deletions examples/bridge/DEPLOYMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
in /l1 & /sn Makefile, import the right .env
use `make` without argument to check current env vars

## L1

- set STARKNET_ADDRESS (for goerli & mainnet)
- set TOKEN_ADDRESS (for mainnet)
- Pre-compute L1DojoBridge address with create3

## Starknet

- set L1_BRIDGE_ADDRESS in sn/ .env
- Deploy contracts
- Set auth
- Initialize contracts

## L1

- set L2_BRIDGE_ADDRESS in l1/ .env
- Deploy L1DojoBridge



## Constructor / initiliazers

### L1
L1DojoBridge constructor :

`constructor(address _starknet, address _l1Token, uint256 _l2Bridge)`

### SN

dojo_bridge initializer :

`fn initializer(ref self: ContractState, l1_bridge: felt252, l2_token: ContractAddress)`

dojo_token initializer :

`fn initializer(ref self: ContractState, name: felt252, symbol: felt252, l2_bridge_address: ContractAddress)`


89 changes: 89 additions & 0 deletions examples/bridge/DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
For local use
Make sure both Makefiles use .env.local
You can just use `make` without argument to check current env vars

# ETH

### Terminal 1

Launch anvil

```sh
cd l1
make anvil
```

### Terminal 2

Deploy create3 contract for deterministic contract address
```sh
cd l1
make create3
```

Deploy eth $TOKEN & L1DojoBridge
```sh
make deploy
```

# Starknet

### Terminal 1

Launch katana with messaging

```sh
cd sn
make katana_msg
# or
katana --messaging anvil.messaging.json
```

### Terminal 2

Migrate & initialize contracts
```sh
cd sn
make migrate_and_init
# or
make migrate
make initialize
```

Fund an address in ETH
```sh
./scripts/fund.sh 0x1234
```

# Bridging

## from ETH to Starknet

it mint tokens & approve bridge & call deposit on ETH bridge
```sh
make deposit
```

## from Starknet to ETH

get balance on Starknet
```sh
scarb run get_balance
```

withdraw from Starknet to ETH
```sh
scarb run withdraw
```

## after

withdraw tokens from ETH bridge
```sh
make withdraw
```

check token balance on ETH
```sh
make get_balance
```
14 changes: 14 additions & 0 deletions examples/bridge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## DOJO BRIDGE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## DOJO BRIDGE
## Dojo Bridge


Adaptation of https://github.com/BibliothecaDAO/Starknet-ERC20-bridge & https://github.com/glihm/starknet-messaging-dev for Dojo.


## Requirements

Please before starting, install:

- [scarb](https://docs.swmansion.com/scarb/) to build cairo contracts.
- [starkli](https://github.com/xJonathanLEI/starkli) to interact with Katana.
- [foundry](https://book.getfoundry.sh/getting-started/installation) to interact with Anvil.
- git submodules forge-std & create3-factory.

13 changes: 13 additions & 0 deletions examples/bridge/l1/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ENV=local

ETH_RPC_URL=http://127.0.0.1:8545

ACCOUNT_PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
ACCOUNT_ADDRESS=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

# local
CREATE3_FACTORY_ADDRESS=0x5FbDB2315678afecb367f032d93F642f64180aa3
STARKNET_ADDRESS=0x0000000000000000000000000000000000000000
TOKEN_ADDRESS=0x0000000000000000000000000000000000000000

L2_BRIDGE_ADDRESS=0x2ff2f9994ba7e039f50190cb3b3dc538d9abf7201acbe5a6a7aff686dd40d89
14 changes: 14 additions & 0 deletions examples/bridge/l1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Dotenv file
.env

# Compiler files
cache/
out/
broadcast/


lib/forge-std

/logs

.gas-snapshot
44 changes: 44 additions & 0 deletions examples/bridge/l1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
include .env.local

export

PARAMS := --broadcast --rpc-url ${ETH_RPC_URL} -v

all:
@echo "********************************************************************"
@echo "ENV : ${ENV}"
@echo "********************************************************************"
@echo "ETH_RPC_URL : ${ETH_RPC_URL}"
@echo "ACCOUNT_ADDRESS : ${ACCOUNT_ADDRESS}"
@echo "CREATE3_FACTORY_ADDRESS : ${CREATE3_FACTORY_ADDRESS}"
@echo "STARKNET_ADDRESS : ${STARKNET_ADDRESS}"
@echo "TOKEN_ADDRESSS : ${TOKEN_ADDRESSS}"
@echo "L2_BRIDGE_ADDRESS : ${L2_BRIDGE_ADDRESS}"
@echo "********************************************************************"

anvil:
anvil --chain-id 1337

anvil_slow:
anvil --chain-id 1337 --block-time 10

create3:
forge script ./script/Deploy.s.sol:Create3 ${PARAMS}

get_bridge_address:
forge script ./script/Deploy.s.sol:GetBridgeAddress ${PARAMS}

deploy:
forge script ./script/Deploy.s.sol:Deploy ${PARAMS}

deposit:
forge script ./script/Calls.s.sol:Deposit ${PARAMS}

withdraw:
forge script ./script/Calls.s.sol:Withdraw ${PARAMS}

get_balance:
forge script ./script/Calls.s.sol:GetBalance ${PARAMS}

mint_token:
forge script ./script/Calls.s.sol:MintToken ${PARAMS}
20 changes: 20 additions & 0 deletions examples/bridge/l1/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[profile.default]
solc-version = "0.8.13"
src = "src"
out = "out"
libs = ["lib"]
fs_permissions = [{ access = "read-write", path = "./logs"}]
remappings = [
"@forge-std/=lib/forge-std/src/",
"@create3-factory/=lib/create3-factory/src/",
"@openzeppelin/=lib/@openzeppelin/",
"@starknet/=lib/starknet/",
]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options


# since the foundry.toml is commited it's not recommened to have the api key explicitly
# [rpc_endpoints]
# local = "${ETH_RPC_URL}"

71 changes: 71 additions & 0 deletions examples/bridge/l1/lib/@openzeppelin/contracts/access/Ownable.sol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should use submodules and no embed these

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;

event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_setOwner(_msgSender());
}

/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}

/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}

/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_setOwner(address(0));
}

/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_setOwner(newOwner);
}

function _setOwner(address newOwner) private {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
Loading