diff --git a/cmd/devnet/gen-devnet.sh b/cmd/devnet/gen-devnet.sh new file mode 100755 index 000000000..2f8326f9f --- /dev/null +++ b/cmd/devnet/gen-devnet.sh @@ -0,0 +1,222 @@ +#!/usr/bin env sh +# Receive the following command line parameters: +# - DApp machine template hash (used to deploy the dapp) +# - Path to the anvil state file output +# - Path to the deployment info file (we need a file that contains the addresses of the deployed contracts) + +function debug { + echo "DEBUG> $1" +} + +tmp_dir=$(mktemp -d) + +# Download the rollups contracts (see pkg/contracts/generate/main.go) +CONTRACTS_VERSION=1.1.0 +CONTRACTS_FILE="rollups-${CONTRACTS_VERSION}.tgz" +CONTRACTS_URL="https://registry.npmjs.org/@cartesi/rollups/-/${CONTRACTS_FILE}" +rollups_path="${tmp_dir}/rollups" +rollups_contracts_path="${rollups_path}/package/contracts" +wget $CONTRACTS_URL --directory-prefix $tmp_dir +mkdir $rollups_path +tar vzxf $tmp_dir/$CONTRACTS_FILE --directory $rollups_path + +# Extract dependencies versions +solidity_util_version=$(\ + cat ${rollups_path}/package/package.json \ + | jq '.dependencies."@cartesi/util"' \ + | sed 's/"//g') +openzeppelin_version=v$(\ + cat ${rollups_path}/package/package.json \ + | jq '.dependencies."@openzeppelin/contracts"' \ + | sed 's/"//g') + +# Download solidity-util +solidity_util_file="util-${solidity_util_version}.tgz" +solidity_util_url="https://registry.npmjs.org/@cartesi/util/-/${solidity_util_file}" +solidity_util_path="${tmp_dir}/solidity_util" +solidity_util_contracts_path="${solidity_util_path}/package/contracts" +wget $solidity_util_url --directory-prefix $tmp_dir +mkdir $solidity_util_path +tar vzxf $tmp_dir/$solidity_util_file --directory $solidity_util_path + +# Start Anvil in the background with --dump-state (https://github.com/foundry-rs/foundry/tree/master/crates/anvil) +anvil_state_file="${tmp_dir}/anvil-state.json" +anvil --dump-state $anvil_state_file & +anvil_pid=$! + +######################################################################### +######################################################################### +# Deploy contracts using forge (https://book.getfoundry.sh/forge/deploying) +# TODO Get defaults from Foundry the same way the cli is doing. +RPC_URL=http://localhost:8545 + +cd $tmp_dir +mkdir forge_prj +cd forge_prj +forge init . + +# Install rollups-contracts dependencies +forge install --shallow --no-git openzeppelin/openzeppelin-contracts@${openzeppelin_version} +mv lib/openzeppelin-contracts/ lib/@openzeppelin + +#forge install --shallow --no-git cartesi/solidity-util@${cartesi_util_version} +#mv lib/solidity-util/ lib/@cartesi/util +mkdir -p lib/@cartesi/util +# TODO Do not copy test contracts +cp -vpr $solidity_util_contracts_path lib/@cartesi/util + +rm -rf script/* src/* test/* +cp -vpr $contracts_path/* src + +#find src -name "*.sol" | xargs grep openzeppelin +#find src -name "*.sol" | xargs sed -i 's/@openzeppelin/openzeppelin-contracts/g' + +# TODO Check whether it's possible to not deploy already deployed contracts +# TODO Verify what parameters should be made available to make anvil configuration adjustabled in a controlled manner +# We may used foundry defaults for everything for now +account_0_address="0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" +account_0_private_key=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 + +######################################################################### +# Libs +mathv2_address=$(\ + forge create \ + --json \ + --rpc-url $RPC_URL \ + --private-key $account_0_private_key \ + lib/@cartesi/util/contracts/CartesiMathV2.sol:CartesiMathV2 \ + | jq '.deployedTo' \ + | sed 's/"//g' +) +mathv2_lib="lib/@cartesi/util/contracts/CartesiMathV2.sol:CartesiMathV2:$mathv2_address" +debug "mathv2_lib=$mathv2_lib" + +merklev2_address=$(\ + forge create \ + --json \ + --rpc-url $RPC_URL \ + --private-key $account_0_private_key \ + --libraries $mathv2_lib \ + lib/@cartesi/util/contracts/MerkleV2.sol:MerkleV2 \ + | jq '.deployedTo' \ + | sed 's/"//g' +) +merklev2_lib="lib/@cartesi/util/contracts/MerkleV2.sol:MerkleV2:$merklev2_address" +debug "merklev2_lib=$merklev2_lib" + +bitmask_address=$(\ + forge create \ + --json \ + --rpc-url $RPC_URL \ + --private-key $account_0_private_key \ + lib/@cartesi/util/contracts/Bitmask.sol:Bitmask \ + | jq '.deployedTo' \ + | sed 's/"//g' +) +bitmask_lib="lib/@cartesi/util/contracts/Bitmask.sol:Bitmask:${bitmask_address}" +debug "bitmask_lib=$bitmask_lib" + +inputbox_address=$(\ + forge create \ + --json \ + --rpc-url $RPC_URL \ + --private-key $account_0_private_key \ + src/inputs/InputBox.sol:InputBox \ + | jq '.deployedTo' \ + | sed 's/"//g' +) +debug "inputbox_address=$inputbox_address" + +############ Portals +declare -a portals=( + src/portals/ERC1155BatchPortal.sol:ERC1155BatchPortal + src/portals/ERC1155SinglePortal.sol:ERC1155SinglePortal + src/portals/ERC20Portal.sol:ERC20Portal + src/portals/ERC721Portal.sol:ERC721Portal + src/portals/EtherPortal.sol:EtherPortal +) + +for p in "${portals[@]}" +do + portal_address=$(\ + forge create \ + --json \ + --rpc-url $RPC_URL \ + --private-key $account_0_private_key \ + $p \ + --constructor-args $inputbox_address \ + | jq '.deployedTo' \ + | sed 's/"//g' + ) + debug "$p=$portal_address" +done + + +######################################################################### +# DApp infra + +factory_address=$(\ + forge create \ + --json \ + --rpc-url $RPC_URL \ + --private-key $account_0_private_key \ + --libraries $mathv2_lib \ + --libraries $merklev2_lib \ + --libraries $bitmask_lib \ + src/dapp/CartesiDAppFactory.sol:CartesiDAppFactory \ + | jq '.deployedTo' \ + | sed 's/"//g' +) +debug "$factory_address=$factory_address" + +authority_address=$(\ + forge create \ + --json \ + --rpc-url $RPC_URL \ + --private-key $account_0_private_key \ + src/consensus/authority/Authority.sol:Authority \ + --constructor-args $account_0_address \ + | jq '.deployedTo' \ + | sed 's/"//g' +) +debug "authority_address=$authority_address" + +relay_address=$(\ + forge create \ + --json \ + --rpc-url $RPC_URL \ + --private-key $account_0_private_key \ + src/relays/DAppAddressRelay.sol:DAppAddressRelay \ + --constructor-args $account_0_address \ + | jq '.deployedTo' \ + | sed 's/"//g' +) +debug "relay_address=$relay_address" + +history_address=$(\ + forge create \ + --json \ + --rpc-url $RPC_URL \ + --private-key $account_0_private_key \ + src/history/History.sol:History \ + --constructor-args $account_0_address \ + | jq '.deployedTo' \ + | sed 's/"//g' +) +debug "history_address=$history_address" + + +# Call the authority/history/dapp factory to deploy the authority, history, and DApp contracts using cast (https://book.getfoundry.sh/cast/) + +#cast call \ +# --rpc-url $RPC_URL \ +# $factory_address \ +# "newApplication(address,address,bytes32,bytes32)" \ +# $authority_address \ +# $account_0_address \ +# $template_hash \ +# $overrides + + +# Stop anvil +#kill $anvil_pid