Skip to content

Commit

Permalink
feat(tests): add scratch shell script for setting anvil up
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelstanley committed Nov 23, 2023
1 parent 5b18d9d commit 83185ee
Showing 1 changed file with 230 additions and 0 deletions.
230 changes: 230 additions & 0 deletions cmd/devnet/gen-devnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
#!/usr/bin env sh

# TODO 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 {
[[ $DEBUG ]] && echo "DEBUG> $1"
}

################################################################################
# Download rollups contracts
CONTRACTS_VERSION=1.1.0
CONTRACTS_FILE="rollups-${CONTRACTS_VERSION}.tgz"
CONTRACTS_URL="https://registry.npmjs.org/@cartesi/rollups/-/${CONTRACTS_FILE}"

TMP_DIR=$(mktemp -d)
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 -r '.dependencies."@cartesi/util"' \
)
openzeppelin_version=v$(\
cat ${ROLLUPS_PATH}/package/package.json \
| jq -r '.dependencies."@openzeppelin/contracts"' \
)

################################################################################
# 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
anvil_state_file="${TMP_DIR}/anvil-state.json"
anvil --dump-state $anvil_state_file &
anvil_pid=$!

################################################################################
# Prepare forge environment

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

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 $ROLLUPS_CONTRACTS_PATH/* src

################################################################################
# Deployments

RPC_URL=http://localhost:8545

# TODO Check whether it's possible to not deploy already deployed contracts
# TODO Verify what parameters should be made available to make anvil configuration
# adjustable in a controlled manner
# We may used foundry defaults for everything for now
account_0_address="0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
account_0_private_key=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

deploy() {
contract=$1
contract_name=$(echo $contract | cut -d ":" -f 2)
shift

contract_address=$(\
forge create \
--json \
--rpc-url $RPC_URL \
--private-key $account_0_private_key \
$contract \
$@ \
| jq -r ".deployedTo"
)
debug "contract_address=$contract_address"
lib_ref="$contract:$contract_address"
debug "$contract_name=$lib_ref"
}

################################################################################
# Libs

deploy lib/@cartesi/util/contracts/CartesiMathV2.sol:CartesiMathV2
mathv2_lib=$lib_ref

deploy lib/@cartesi/util/contracts/MerkleV2.sol:MerkleV2 --libraries $mathv2_lib
merklev2_lib=$lib_ref

deploy lib/@cartesi/util/contracts/Bitmask.sol:Bitmask
bitmask_lib=$lib_ref

################################################################################
# Portals

deploy src/inputs/InputBox.sol:InputBox
inputbox_address=$contract_address
debug "inputbox_address=$inputbox_address"

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
deploy $p --constructor-args $inputbox_address
done

################################################################################
# DApp infra

deploy src/dapp/CartesiDAppFactory.sol:CartesiDAppFactory \
--libraries $mathv2_lib \
--libraries $merklev2_lib \
--libraries $bitmask_lib
dapp_factory_address=$contract_address

deploy src/relays/DAppAddressRelay.sol:DAppAddressRelay \
--constructor-args $account_0_address
relay_address=$contract_address

deploy src/consensus/authority/AuthorityFactory.sol:AuthorityFactory \
--constructor-args $account_0_address
authority_factory_address=$contract_address

deploy src/consensus/authority/AuthorityFactory.sol:AuthorityFactory \
--constructor-args $account_0_address
authority_factory_address=$contract_address

deploy src/history/HistoryFactory.sol:HistoryFactory \
--constructor-args $account_0_address
history_factory_address=$contract_address

deploy src/consensus/authority/AuthorityHistoryPairFactory.sol:AuthorityHistoryPairFactory \
--constructor-args \
$authority_factory_address \
$history_factory_address
auth_hist_factory_address=$contract_address

################################################################################
# DApp

#keccak256("salt")
salt=0xa05e334153147e75f3f416139b5109d1179cb56fef6a4ecb4c4cbc92a7c37b70
#keccak256("template_hash")
template_hash=0x9686509214c897e8f656feac5b3191e66860dba0c73c555edc9273cf2b4dad99

IFS=$'\n' \
addresses=(\
$(\
cast call \
--rpc-url $RPC_URL \
$auth_hist_factory_address \
"newAuthorityHistoryPair(address,bytes32)(address,address)" \
$account_0_address \
$salt
)
)
authority_address="${addresses[0]}"
history_address="${addresses[1]}"

cast send \
--rpc-url $RPC_URL \
--private-key $account_0_private_key \
$auth_hist_factory_address \
"newAuthorityHistoryPair(address,bytes32)(address,address)" \
$account_0_address \
$salt

debug "dapp_factory_address = $dapp_factory_address"
debug "authority_address = $authority_address"
debug "history_address = $history_address"
debug "template_hash = $template_hash"
debug "salt = $salt"

dapp_address=$(\
cast call \
--rpc-url $RPC_URL \
$dapp_factory_address \
"newApplication(address,address,bytes32,bytes32)" \
$authority_address \
$account_0_address \
$template_hash \
$salt
)
debug "dapp_address = $dapp_address"

cast send \
--rpc-url $RPC_URL \
--private-key $account_0_private_key \
$dapp_factory_address \
"newApplication(address,address,bytes32,bytes32)" \
$authority_address \
$account_0_address \
$template_hash \
$salt

################################################################################
# Stop anvil
kill $anvil_pid

0 comments on commit 83185ee

Please sign in to comment.