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

fix #57 .Env + Readme fixes #58

Merged
merged 39 commits into from
Jan 11, 2024
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
34fac42
various fixes
uri-99 Jan 5, 2024
0b9180a
rewrite .env.exampole formats
Jan 8, 2024
4019988
fix .env lowercase
Jan 8, 2024
359523b
add foundry usage to write the starknet address to eth contract
Jan 8, 2024
489a32d
fix ENVIRONMENT var in .env.example
Jan 8, 2024
daab42a
Merge branch 'main' into 57-fix-envs
Jan 8, 2024
cd1e8e2
revert mm.env.example
Jan 8, 2024
29b1a14
fix .env comment format
Jan 8, 2024
113a307
revert env import fix
Jan 8, 2024
bda0538
set-escrow in makefile
Jan 9, 2024
0dff589
improve make and .sh of post-deploy functions
Jan 9, 2024
d8d82ba
update readme with new make functions
Jan 9, 2024
e544c87
fix .env comments
Jan 9, 2024
0912eda
fix .env comments
Jan 9, 2024
7503efc
Update contracts/cairo/.env.example
uri-99 Jan 9, 2024
929d330
Update README.md
uri-99 Jan 9, 2024
871c654
remove unnesecary print
Jan 9, 2024
edb8cd6
run set escrow after cairo deploy
Jan 10, 2024
49cfcbf
Update contracts/cairo/.env.example
uri-99 Jan 10, 2024
d6e2554
clarify how to get withdraw-selector value
Jan 10, 2024
c75577a
NTH print
Jan 10, 2024
3418b9f
set-withdraw-selector executes automatically in cairo/deploy.sh
Jan 10, 2024
8f67e10
format
Jan 10, 2024
b8a69f8
ignore comments fix in .env import
Jan 10, 2024
2436a58
added env vars, removed them from deploy.sh
Jan 10, 2024
32abb05
created make starknet-deploy-and-connect
Jan 10, 2024
bc1c61f
Add available herodotus addresses in readme
Jan 11, 2024
f328d3f
read snMessagingAddress from .env
Jan 11, 2024
39b3c6d
update readme
Jan 11, 2024
21a112f
add herodotus registry addresses on readme
Jan 11, 2024
7a4df41
add keystore abs path clarification
Jan 11, 2024
dae1764
update readme
Jan 11, 2024
885db85
fix ETH_CONTRACT_ADDR format in env.example
Jan 11, 2024
3990708
add clarification in readme
Jan 11, 2024
b54a9b3
update readme address format
Jan 11, 2024
f59a3e6
add possible values in env.example
Jan 11, 2024
2bbbdfd
clarify WITHDRAW_NAME in readme
Jan 11, 2024
8656c20
final details on readme.md
Jan 11, 2024
80ba11b
eof in .gitignore
Jan 11, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.env
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -24,6 +24,12 @@ ethereum-test: ethereum-clean
ethereum-deploy: ethereum-clean
@./contracts/solidity/deploy.sh

ethereum-set-escrow:
@./contracts/solidity/set_escrow.sh

ethereum-set-withdraw-selector:
@./contracts/solidity/set_withdraw_selector.sh

starknet-clean:
@cd ./contracts/cairo/ && scarb clean

@@ -34,4 +40,9 @@ starknet-test: starknet-clean
@cd ./contracts/cairo/ && snforge test

starknet-deploy: starknet-build
@./contracts/cairo/deploy.sh
@./contracts/cairo/deploy.sh

starknet-deploy-and-connect: starknet-build
@$(MAKE) starknet-deploy
@$(MAKE) ethereum-set-escrow
@$(MAKE) ethereum-set-withdraw-selector
121 changes: 98 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ Follow the steps below to set up a testnet smart wallet using `starkli`:
```bash
export STARKNET_RPC="<ALCHEMY_API_HTTPS_URL>"
```

2. **Create a Keystore**: A Keystore is a encrypted `json` file that stores the
private keys.

@@ -57,6 +58,7 @@ Follow the steps below to set up a testnet smart wallet using `starkli`:
```bash
mkdir -p ~/.starkli-wallets
```

2. **Generate a new Keystore file**: Run the following command to create a
new private key stored in the file. It will **ask for a password** to
encrypt the file:
@@ -72,6 +74,7 @@ Follow the steps below to set up a testnet smart wallet using `starkli`:
```bash
export STARKNET_KEYSTORE="~/.starkli-wallets/keystore.json"
```

3. **Account Creation**: In Starknet every account is a smart contract, so to
create one it will need to be deployed.

@@ -80,6 +83,7 @@ Follow the steps below to set up a testnet smart wallet using `starkli`:
```bash
starkli account oz init --keystore ~/.starkli-wallets/keystore.json ~/.starkli-wallets/account.json
```

2. **Deploy the account by running**:

```bash
@@ -101,47 +105,118 @@ Follow the steps below to set up a testnet smart wallet using `starkli`:

## Declare and Deploy Contracts in Testnet

### Ethereum smart contract

First, the Ethereum smart contracts must be deployed. For Ethereum the deployment process you will need to:

1. Create your `.env` file: you need to configure the following variables in your own .env file on the contracts/solidity/ folder. You can use the env.example file as a template for creating your .env file, paying special attention to the formats provided
rcatalan98 marked this conversation as resolved.
Show resolved Hide resolved

```bash
ETH_RPC_URL = Infura or Alchemy RPC URL
ETH_PRIVATE_KEY = private key of your ETH wallet
ETHERSCAN_API_KEY = API Key to use etherscan to read the Ethereum blockchain
SN_MESSAGING_ADDRESS = Starknet Messaging address
```

**NOTE**:

- You can generate ETHERSCAN_API_KEY [following this steps](https://docs.etherscan.io/getting-started/creating-an-account).
- For the deploy, you will need some GoerliETH that you can get from this [faucet](https://goerlifaucet.com/).
- Current SN_MESSAGING_ADDRESS values:
- SN_MESSAGING_ADDRESS is for when a L1 contract initiates a message to a L2 contract on Starknet. It does so by calling the sendMessageToL2 function on the Starknet Core Contract with the message parameters. Starknet Core Contracts are the following:
- Goerli: `0xde29d060D45901Fb19ED6C6e959EB22d8626708e`
- Sepolia: `0xE2Bb56ee936fd6433DC0F6e7e3b8365C906AA057`
- Mainnet: `0xc662c410C0ECf747543f5bA90660f6ABeBD9C8c4`

2. Deploy Solidity contract

```bash
make ethereum-deploy
```

### Starknet smart contracts

After the Ethereum smart contract is deployed, the Starknet smart contracts must be declared and deployed.
On Starknet, the deployment process is in two steps:

- Declaring the class of your contract, or sending your contract’s code to the
network
- Deploying a contract or creating an instance of the previously declared code
with the necessary parameters

1. Updated `.env` file: Please modify the variables with your Testnet account and your RPC provider.
For this, you will need to:

uri-99 marked this conversation as resolved.
Show resolved Hide resolved
1. Create your `.env` file: you need to configure the following variables in your own .env file on the contracts/solidity folder. You can use the env.example file as a template for creating your .env file, paying special attention to the formats provided

```
STARKNET_ACCOUNT = Absolute path of your starknet testnet account, created at the start of this README
STARKNET_KEYSTORE = Absolute path of your starknet testnet keystore, created at the start of this README
SN_RPC_URL = Infura or Alchemy RPC URL
ETH_CONTRACT_ADDR = newly created ETH contract address
MM_SN_WALLET_ADDR = Starknet wallet of the MarketMaker
WITHDRAW_NAME = The exact name of the withdraw function that is called from L1, case sensitive. Example: withdraw_fallback
HERODOTUS_FACTS_REGISTRY = Herodotus' Facts Registry Smart Contract in Starknet
MM_ETHEREUM_WALLET = Ethereum wallet of the MArketMaker
NATIVE_TOKEN_ETH_STARKNET = Ethereum's erc20 token handler contract in Starkent
ESCROW_CONTRACT_ADDRESS = Address of the Starknet smart contract, this value should be empty, and is automatically updated after deploy.sh is run
```
**Note**
- Herodotus Facts Registry:
- Starknet Goerli: `0x01b2111317EB693c3EE46633edd45A4876db14A3a53ACDBf4E5166976d8e869d`
- Starknet Sepolia: `0x07d3550237ecf2d6ddef9b78e59b38647ee511467fe000ce276f245a006b40bc`
- Starknet Mainnet: `0x014bf62fadb41d8f899bb5afeeb2da486fcfd8431852def56c5f10e45ae72765`
2. Declare and Deploy: We sequentially declare and deploy the contracts, and connect it to our Ethereum smart contract.
uri-99 marked this conversation as resolved.
Show resolved Hide resolved
### First alternative: automatic deploy and connect of Escrow and YABTransfer.
```bash
// For the deploy, you just need to configure the following variables in the .env file on the mm-bot folder
..
SN_RPC_URL=<STARKNET_RPC_HTTPS_URL> // Infura or Alchemy RPC URL
ETH_CONTRACT_ADDR=0xdd69db25f6d620a7bad3023c5d32761d353d3de9 // GoerliETH
SN_CONTRACT_ADDR=<STARKNET_MM_CONTRACT_ADDR>
..
make starknet-deploy-and-connect
```
2. Declare and Deploy: We sequentially declare and deploy the contracts.

This make target consists of 4 steps:

1. make starknet-build; builds the project
2. make starknet-deploy; deploys the smart contract on the blockchain
3. make ethereum-set-escrow; sets the newly created Starknet contract address on the Ethereum smart contract, so that the L1 contract can communicate with the L2 contract
4. make ethereum-set-withdraw-selector; sets the Starknet _withdraw_ function name on the Ethereum smart contract, so that the L1 contract can communicate with the L2 contract

### Second alternative: manual deploy and connect of Escrow and YABTransfer

This may be better suited for you if you plan to change some of the automatically declared .env vars, or if you simply want to make sure you understand the process.

1. Declare and Deploy: We sequentially declare and deploy the contracts.

```bash
make starknet-deploy
```

For Ethereum the deployment process you will need:
This script also sets an important .env variable, **ESCROW_CONTRACT_ADDRESS**

3. Updated `.env` file: Please modify the variables with your Testnet account and your RPC provider.
2. Setting _EscrowAddress_

After the Starknet smart contracts are declared and deployed, the variable _EscrowAddress_ from the Ethereum smart contract must be updated with the newly created Starknet smart contract address.

To do this, you can use

```bash
// For the deploy, you just need to configure the following variables in the .env file on the contracts/solidity/ folder
..
ETH_RPC_URL=<ETH_RPC_URL> // Infura or Alchemy RPC URL
ETH_PRIVATE_KEY=<ETH_PRIVATE_KEY>
ETHERSCAN_API_KEY=<ETHERSCAN_API_KEY>
..
```
**NOTE**:
make ethereum-set-escrow
```

- You can generate ETHERSCAN_API_KEY [following this steps](https://docs.etherscan.io/getting-started/viewing-api-usage-statistics).
- For the deploy, you will need some GoerliETH that you can get from this [faucet](https://goerlifaucet.com/).
4. Deploy Solidity contract
This script uses the previously set .env variable, **ESCROW_CONTRACT_ADDRESS**

```bash
make ethereum-deploy
3. Setting _EscrowWithdrawSelector_

Ethereum's smart contract has another variable that must be configured, _EscrowWithdrawSelector_, which is for specifying the _withdraw_ function's name in the Starknet Escrow smart contract.
You can set and change Ethereum's _EscrowWithdrawSelector_ variable, doing the following:

rcatalan98 marked this conversation as resolved.
Show resolved Hide resolved
```
make ethereum-set-withdraw-selector
```

This script uses the WITHDRAW_NAME .env variable to automatically generate the selector in the necesary format

## Recap

After following this complete README, we should have an ETH smart contract as well as a Starknet smart contract, both connected to act as a bridge between these two chains.
10 changes: 10 additions & 0 deletions contracts/cairo/.env.example
rcatalan98 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
STARKNET_ACCOUNT=<starkli_account_path>
STARKNET_KEYSTORE=<starkli_keystore_path>
SN_RPC_URL=<starknet_rpc_url>
ETH_CONTRACT_ADDR=<ethereum_contract_address> #in hexa with the 0x prefix, with upper and lower cases like shown by etherscan
MM_SN_WALLET_ADDR=<MarketMaker_starknet_contract_address> #in lowercase hexa with the 0x prefix
WITHDRAW_NAME=<withdraw_function_name> #must match the exact name of the function that withdraws from the starknet smart contract
HERODOTUS_FACTS_REGISTRY=<0x01b2111317EB693c3EE46633edd45A4876db14A3a53ACDBf4E5166976d8e869d|0x07d3550237ecf2d6ddef9b78e59b38647ee511467fe000ce276f245a006b40bc|0x014bf62fadb41d8f899bb5afeeb2da486fcfd8431852def56c5f10e45ae72765> # Goerli | Sepolia | Mainnet
MM_ETHEREUM_WALLET=<MarketMaker_ethereum_contract_address> #in lowercase hexa with the 0x prefix
NATIVE_TOKEN_ETH_STARKNET=<eth_erc20_in_starknet> #in lowercase hexa with the 0x prefix
ESCROW_CONTRACT_ADDRESS=<ESCROW_CONTRACT_ADDRESS> #this value is automatically updated after deploy.sh is run
22 changes: 12 additions & 10 deletions contracts/cairo/deploy.sh
Original file line number Diff line number Diff line change
@@ -6,37 +6,39 @@ PURPLE='\033[1;34m'
PINK='\033[1;35m'
COLOR_RESET='\033[0m'

HERODOTUS_FACTS_REGISTRY=0x01b2111317EB693c3EE46633edd45A4876db14A3a53ACDBf4E5166976d8e869d
MM_ETHEREUM_WALLET=0xE8504996d2e25735FA88B3A0a03B4ceD2d3086CC
NATIVE_TOKEN_ETH_STARKNET=0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7

cd "$(dirname "$0")"

load_env() {
unamestr=$(uname)
if [ "$unamestr" = 'Linux' ]; then
export $(grep -v '^#' ../../mm-bot/.env | xargs -d '\n')
export $(sed '/^#/d; s/#.*$//' .env | xargs -d '\n')
elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'Darwin' ]; then
export $(grep -v '^#' ../../mm-bot/.env | xargs -0)
export $(sed '/^#/d; s/#.*$//' .env | xargs -0)
fi
}
load_env

echo -e "${GREEN}\n=> [SN] Declare Escrow${COLOR_RESET}"
ESCROW_CLASS_HASH=$(starkli declare --watch --rpc $SN_RPC_URL target/dev/yab_Escrow.contract_class.json)
ESCROW_CLASS_HASH=$(starkli declare \
--account $STARKNET_ACCOUNT --keystore $STARKNET_KEYSTORE \
--watch target/dev/yab_Escrow.contract_class.json)

echo -e "- ${PURPLE}[SN] Escrow ClassHash: $ESCROW_CLASS_HASH${COLOR_RESET}"
echo -e "- ${PURPLE}[SN] Herodotus Facts Registry: $HERODOTUS_FACTS_REGISTRY${COLOR_RESET}"
echo -e "- ${PURPLE}[SN] Market Maker: $SN_WALLET_ADDR${COLOR_RESET}"
echo -e "- ${PURPLE}[SN] Market Maker: $MM_SN_WALLET_ADDR${COLOR_RESET}"
echo -e "- ${PURPLE}[SN] Ethereum ContractAddress $NATIVE_TOKEN_ETH_STARKNET${COLOR_RESET}"
echo -e "- ${PINK}[ETH] Ethereum ContractAddress: $ETH_CONTRACT_ADDR${COLOR_RESET}"
echo -e "- ${PINK}[ETH] Market Maker: $MM_ETHEREUM_WALLET${COLOR_RESET}"

echo -e "${GREEN}\n=> [SN] Deploy Escrow${COLOR_RESET}"
ESCROW_CONTRACT_ADDRESS=$(starkli deploy --watch --rpc $SN_RPC_URL $ESCROW_CLASS_HASH \
ESCROW_CONTRACT_ADDRESS=$(starkli deploy \
--account $STARKNET_ACCOUNT --keystore $STARKNET_KEYSTORE \
--watch $ESCROW_CLASS_HASH \
$HERODOTUS_FACTS_REGISTRY \
$ETH_CONTRACT_ADDR \
$MM_ETHEREUM_WALLET \
$SN_WALLET_ADDR \
$MM_SN_WALLET_ADDR \
$NATIVE_TOKEN_ETH_STARKNET)
echo -e "- ${PURPLE}[SN] Escrow ContractAddress: $ESCROW_CONTRACT_ADDRESS${COLOR_RESET}"

sed -i "s/^ESCROW_CONTRACT_ADDRESS=.*/ESCROW_CONTRACT_ADDRESS=$ESCROW_CONTRACT_ADDRESS/" ".env" || echo "ESCROW_CONTRACT_ADDRESS=$ESCROW_CONTRACT_ADDRESS" >> ".env"
7 changes: 4 additions & 3 deletions contracts/solidity/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
GOERLI_RPC_URL=
PRIVATE_KEY=
ETHERSCAN_API_KEY=
ETH_RPC_URL=<ethereum_rpc_url>
ETH_PRIVATE_KEY=<eth_private_key> #in lowercase hexa with the 0x prefix
ETHERSCAN_API_KEY=<etherscan_api_key>
SN_MESSAGING_ADDRESS=<0xde29d060D45901Fb19ED6C6e959EB22d8626708e|0xE2Bb56ee936fd6433DC0F6e7e3b8365C906AA057|0xc662c410C0ECf747543f5bA90660f6ABeBD9C8c4> # Goerli | Sepolia | Mainnet
6 changes: 3 additions & 3 deletions contracts/solidity/deploy.sh
Original file line number Diff line number Diff line change
@@ -9,12 +9,12 @@ cd "$(dirname "$0")"
load_env() {
unamestr=$(uname)
if [ "$unamestr" = 'Linux' ]; then
export $(grep -v '^#' ./.env | xargs -d '\n')
export $(sed '/^#/d; s/#.*$//' .env | xargs -d '\n')
elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'Darwin' ]; then
export $(grep -v '^#' ./.env | xargs -0)
export $(sed '/^#/d; s/#.*$//' .env | xargs -0)
fi
}
load_env

echo -e "${GREEN}\n=> [ETH] Deploy Escrow${COLOR_RESET}"
forge script ./script/Deploy.s.sol --fork-url $GOERLI_RPC_URL --broadcast --verify -vvvv
forge script ./script/Deploy.s.sol --fork-url $ETH_RPC_URL --broadcast --verify -vvvv
7 changes: 4 additions & 3 deletions contracts/solidity/script/Deploy.s.sol
Original file line number Diff line number Diff line change
@@ -9,9 +9,10 @@ contract Deploy is Script {
uint256 deployerPrivateKey = vm.envUint("ETH_PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);

address snMessagingAddress = 0xde29d060D45901Fb19ED6C6e959EB22d8626708e;
uint256 snEscrowAddress = 0x0;
uint256 snEscrowWithdrawSelector = 0x0;
address snMessagingAddress = vm.envAddress("SN_MESSAGING_ADDRESS");
uint256 snEscrowAddress = 0x0; //this value is set in a call to the smart contract, once deployed
uint256 snEscrowWithdrawSelector = 0x0; //this value is set in a call to the smart contract, once deployed


new YABTransfer(
snMessagingAddress,
28 changes: 28 additions & 0 deletions contracts/solidity/set_escrow.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

# ANSI format
GREEN='\e[32m'
COLOR_RESET='\033[0m'


echo -e "${GREEN}\n=> [ETH] Setting Starknet Escrow Address on ETH Smart Contract${COLOR_RESET}"

if [ -f ./contracts/solidity/.env ]; then
echo "Sourcing solidity/.env file..."
source ./contracts/solidity/.env
else
echo "Error: solidity/.env file not found!"
exit 1
fi
if [ -f ./contracts/cairo/.env ]; then
echo "Sourcing cairo/.env file..."
source ./contracts/cairo/.env
else
echo "Error: cairo/.env file not found!"
exit 1
fi

echo "Smart contract being modified:" $ETH_CONTRACT_ADDR
echo "New Escrow address:" $ESCROW_CONTRACT_ADDRESS

cast send --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEY $ETH_CONTRACT_ADDR "setEscrowAddress(uint256)" $ESCROW_CONTRACT_ADDRESS
27 changes: 27 additions & 0 deletions contracts/solidity/set_withdraw_selector.sh
uri-99 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
if [ -f ./contracts/solidity/.env ]; then
echo "Sourcing solidity/.env file..."
source ./contracts/solidity/.env
else
echo "Error: solidity/.env file not found!"
exit 1
fi
if [ -f ./contracts/cairo/.env ]; then
echo "Sourcing cairo/.env file..."
source ./contracts/cairo/.env
else
echo "Error: cairo/.env file not found!"
exit 1
fi

# ANSI format
GREEN='\e[32m'
COLOR_RESET='\033[0m'

echo -e "${GREEN}\n=> [ETH] Setting Starknet Withdraw Selector on ETH Smart Contract${COLOR_RESET}"
echo "Smart contract being modified:" $ETH_CONTRACT_ADDR

WITHDRAW_SELECTOR=$(starkli selector $WITHDRAW_NAME)
echo "New Withdraw Selector: ${WITHDRAW_SELECTOR}"

cast send --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEY $ETH_CONTRACT_ADDR "setEscrowWithdrawSelector(uint256)" "${WITHDRAW_SELECTOR}"
2 changes: 1 addition & 1 deletion contracts/solidity/test/Transfer.sol
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ contract TransferTest is Test {
function setUp() public {
address snMessagingAddress = 0xde29d060D45901Fb19ED6C6e959EB22d8626708e;
uint256 snEscrowAddress = 0x0;
uint256 snEscrowWithdrawSelector = 0x0;
uint256 snEscrowWithdrawSelector = 0x15511cc3694f64379908437d6d64458dc76d02482052bfb8a5b33a72c054c77;
rcatalan98 marked this conversation as resolved.
Show resolved Hide resolved
yab = new YABTransfer(snMessagingAddress, snEscrowAddress, snEscrowWithdrawSelector);
}