run-contract-test.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/sh
  2. set -e
  3. set -x
  4. # Accept path to `drk` binary as arg or use default
  5. DEFAULT_DRK="../../../drk -c drk0.toml"
  6. DRK="${1:-$DEFAULT_DRK}"
  7. # Path to contract to be deployed
  8. WASM="../../../../smart-contract/membership.wasm"
  9. CLIENT="../../../../smart-contract/membership"
  10. # Script configuration
  11. OUTPUT_FOLDER=/tmp/darkfi
  12. mkdir -p $OUTPUT_FOLDER
  13. SLEEP_TIME=5
  14. # First run the darkfid nodes and the miners:
  15. #
  16. # ./clean.sh
  17. # ./init-wallets.sh
  18. # ./tmux_sessions.sh
  19. #
  20. # Set the right path for WASM and CLIENT binaries for your membership contract.
  21. # Now you can run this script
  22. token_balance() {
  23. BALANCE="$($1 wallet balance 2>/dev/null)"
  24. # No tokens received at all yet
  25. if echo "$BALANCE" | grep -q "No unspent balances found"; then
  26. echo 0
  27. return
  28. fi
  29. BALANCE="$(echo "$BALANCE" | grep -q "$2")"
  30. # Not received yet so no entry
  31. if [ $? = 1 ]; then
  32. echo 0
  33. return
  34. fi
  35. # OK we have the token, show the actual balance
  36. echo "$BALANCE" | awk '{print $5}'
  37. }
  38. wait_token() {
  39. while [ "$(token_balance "$1" "$2")" = 0 ]; do
  40. sleep $SLEEP_TIME
  41. sh ./sync-wallet.sh "$1" > /dev/null
  42. done
  43. }
  44. deployment_status() {
  45. STATUS="$($1 contract list $2)"
  46. # contract deployment hasn't been confirmed yet
  47. if echo "$STATUS" | grep -q "No history records found"; then
  48. echo 0
  49. return
  50. fi
  51. echo 1
  52. }
  53. wait_deployment() {
  54. while [ "$(deployment_status "$1" "$2")" = 0 ]; do
  55. sleep $SLEEP_TIME
  56. sh ./sync-wallet.sh "$1" > /dev/null
  57. done
  58. }
  59. deploy_contract() {
  60. ADDRESS="$($1 contract generate-deploy | awk 'NR==3 {print $3}')"
  61. $1 contract deploy $ADDRESS $WASM | tee $OUTPUT_FOLDER/deploy-$ADDRESS.tx | $1 broadcast >/dev/null 2>&1
  62. echo "$ADDRESS"
  63. }
  64. tx_status() {
  65. STATUS="$($1 explorer txs-history | grep $2 | awk '{print $3}')"
  66. if echo "$STATUS" | grep -q "Broadcasted"; then
  67. echo 0
  68. return
  69. fi
  70. echo 1
  71. }
  72. wait_tx() {
  73. while [ "$(tx_status "$1" "$2")" = 0 ]; do
  74. sleep $SLEEP_TIME
  75. sh ./sync-wallet.sh "$1" > /dev/null
  76. done
  77. }
  78. generate_key() {
  79. SECRET_KEY="$($CLIENT generate | awk 'NR==1 {print $3}')"
  80. echo "$SECRET_KEY"
  81. }
  82. register_call() {
  83. TX_ID="$($CLIENT register $2 $3 | tee $OUTPUT_FOLDER/register-$2-$3.call | $1 tx-from-calls | tee $OUTPUT_FOLDER/register-$2-$3.tx | $1 broadcast | awk 'NR==4 {print $3}')"
  84. echo "$TX_ID"
  85. }
  86. deregister_call() {
  87. TX_ID="$($CLIENT deregister $2 $3 | tee $OUTPUT_FOLDER/deregister-$2-$3.call | $1 tx-from-calls | tee $OUTPUT_FOLDER/deregister-$2-$3.tx | $1 broadcast | awk 'NR==4 {print $3}')"
  88. echo "$TX_ID"
  89. }
  90. wait_token "$DRK" DRK
  91. CONTRACT_ADDRESS="$(deploy_contract "$DRK")"
  92. wait_deployment "$DRK" "$CONTRACT_ADDRESS"
  93. wait_token "$DRK" DRK
  94. SECRET_KEY="$(generate_key "$DRK" "$CONTRACT_ADDRESS")"
  95. TX_ID="$(register_call "$DRK" "$CONTRACT_ADDRESS" "$SECRET_KEY")"
  96. wait_tx "$DRK" "$TX_ID"
  97. TX_ID="$(deregister_call "$DRK" "$CONTRACT_ADDRESS" "$SECRET_KEY")"
  98. wait_tx "$DRK" "$TX_ID"