decred.org/dcrdex@v1.0.5/dex/networks/erc20/contracts/updatecontract.sh (about)

     1  #!/usr/bin/env bash
     2  # This script does the following:
     3  #
     4  # 1. Updates contract.go to reflect updated solidity code.
     5  # 2. Generates the runtime bytecoode. This is used to compare against the runtime
     6  #    bytecode on chain in order to verify that the expected contract is deployed.
     7  # 3. Updates the bytecode for ERC20Swap and the test token contract in the harness test.
     8  
     9  if [ "$#" -ne 1 ]
    10  then
    11    echo "Usage: $0 version" >&2
    12    exit 1
    13  fi
    14  
    15  VERSION=$1
    16  PKG_NAME=v${VERSION}
    17  CONTRACT_NAME=ERC20Swap
    18  SOLIDITY_FILE=./${CONTRACT_NAME}V${VERSION}.sol
    19  TEST_TOKEN=./TestToken.sol
    20  if [ ! -f ${SOLIDITY_FILE} ]
    21  then
    22      echo "${SOLIDITY_FILE} does not exist" >&2
    23      exit 1
    24  fi
    25  if [ ! -f ${TEST_TOKEN} ]
    26  then
    27      echo "${TEST_TOKEN} does not exist" >&2
    28      exit 1
    29  fi
    30  if [ ! -f ${TEST_USDC} ]
    31  then
    32      echo "${TEST_USDC} does not exist" >&2
    33      exit 1
    34  fi
    35  
    36  mkdir temp
    37  
    38  solc --abi --bin --bin-runtime --overwrite --optimize ${SOLIDITY_FILE} -o ./temp/
    39  BYTECODE=$(<./temp/${CONTRACT_NAME}.bin-runtime)
    40  
    41  cat > "./${PKG_NAME}/BinRuntimeV${VERSION}.go" <<EOF
    42  // Code generated - DO NOT EDIT.
    43  // This file is a generated binding and any manual changes will be lost.
    44  
    45  package ${PKG_NAME}
    46  
    47  const ${CONTRACT_NAME}RuntimeBin = "${BYTECODE}"
    48  EOF
    49  
    50  CONTRACT_FILE=./${PKG_NAME}/contract.go 
    51  abigen --abi ./temp/${CONTRACT_NAME}.abi --bin ./temp/${CONTRACT_NAME}.bin \
    52   --pkg ${PKG_NAME} --type ${CONTRACT_NAME} --out ./${PKG_NAME}/contract.go
    53  
    54  BYTECODE=$(<./temp/${CONTRACT_NAME}.bin)
    55  
    56  solc --bin --optimize ${TEST_TOKEN} -o ./temp
    57  TEST_TOKEN_BYTECODE=$(<./temp/TestToken.bin)
    58  
    59  for HARNESS_PATH in "$(realpath ../../../testing/eth/harness.sh)" "$(realpath ../../../testing/polygon/harness.sh)"; do
    60    sed -i.tmp "s/ERC20_SWAP_V${VERSION}=.*/ERC20_SWAP_V${VERSION}=\"${BYTECODE}\"/" "${HARNESS_PATH}"
    61    # mac needs a temp file specified above.
    62    rm "${HARNESS_PATH}.tmp"
    63  
    64    sed -i.tmp "s/TEST_TOKEN=.*/TEST_TOKEN=\"${TEST_TOKEN_BYTECODE}\"/" "${HARNESS_PATH}"
    65    # mac needs a temp file specified above.
    66    rm "${HARNESS_PATH}.tmp"
    67  done
    68  
    69  rm -fr temp
    70  
    71  if [ "$VERSION" -eq "0" ]; then
    72    # Replace a few generated types with the ETH contract versions for interface compatibility.
    73    perl -0pi -e 's/go-ethereum\/event"/go-ethereum\/event"\n\tethv0 "decred.org\/dcrdex\/dex\/networks\/eth\/contracts\/v0"/' $CONTRACT_FILE
    74  
    75    perl -0pi -e 's/\/\/ ERC20SwapInitiation[^}]*}\n\n//' $CONTRACT_FILE
    76    perl -0pi -e 's/ERC20SwapInitiation/ethv0.ETHSwapInitiation/g' $CONTRACT_FILE
    77  
    78    perl -0pi -e 's/\/\/ ERC20SwapRedemption[^}]*}\n\n//' $CONTRACT_FILE
    79    perl -0pi -e 's/ERC20SwapRedemption/ethv0.ETHSwapRedemption/g' $CONTRACT_FILE
    80  
    81    perl -0pi -e 's/\/\/ ERC20SwapSwap[^}]*}\n\n//' $CONTRACT_FILE
    82    perl -0pi -e 's/ERC20SwapSwap/ethv0.ETHSwapSwap/g' $CONTRACT_FILE
    83  
    84    # Reorder the imports since we rewrote go-ethereum/event to a dcrdex package.
    85    gofmt -s -w "$CONTRACT_FILE"
    86  fi