Initial commit: smart contract templates for bash, go, python, and typescript
This commit is contained in:
89
bash/process.sh
Executable file
89
bash/process.sh
Executable file
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env bash
|
||||
# =============================================================================
|
||||
# Smart Contract Processing Logic
|
||||
#
|
||||
# This file contains the transaction processing logic for your smart contract.
|
||||
# Modify this script to implement your business logic.
|
||||
#
|
||||
# Input:
|
||||
# $1 - Transaction JSON string
|
||||
# Environment - Server env vars and secrets are exported as environment variables
|
||||
#
|
||||
# Output (stdout):
|
||||
# A JSON object with the following fields:
|
||||
# {
|
||||
# "data": { ... }, // Your result data (any JSON object)
|
||||
# "output_to_chain": true, // Whether to persist the result on chain
|
||||
# "error": "" // Error message (empty string if no error)
|
||||
# }
|
||||
#
|
||||
# Logs (stderr):
|
||||
# Anything written to stderr is captured and sent back as logs.
|
||||
#
|
||||
# Exit code:
|
||||
# 0 = success (stdout is parsed as JSON result)
|
||||
# non-zero = error (stderr is used as the error message)
|
||||
# =============================================================================
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Transaction JSON is passed as the first argument
|
||||
TX_JSON="$1"
|
||||
|
||||
# =============================================================================
|
||||
# TODO: Implement your smart contract logic here
|
||||
# =============================================================================
|
||||
#
|
||||
# Parse transaction fields using jq:
|
||||
# TXN_ID=$(echo "$TX_JSON" | jq -r '.header.txn_id')
|
||||
# TXN_TYPE=$(echo "$TX_JSON" | jq -r '.header.txn_type')
|
||||
# PAYLOAD=$(echo "$TX_JSON" | jq '.payload')
|
||||
#
|
||||
# Access environment variables (set by the server):
|
||||
# echo "Smart Contract Name: $SMART_CONTRACT_NAME" >&2
|
||||
# echo "Dragonchain ID: $DRAGONCHAIN_ID" >&2
|
||||
#
|
||||
# Access secrets (set by the server):
|
||||
# MY_SECRET="${SC_SECRET_MY_SECRET:-}"
|
||||
#
|
||||
# Process based on payload action:
|
||||
# ACTION=$(echo "$TX_JSON" | jq -r '.payload.action // empty')
|
||||
# case "$ACTION" in
|
||||
# create)
|
||||
# # Handle create operation
|
||||
# ;;
|
||||
# update)
|
||||
# # Handle update operation
|
||||
# ;;
|
||||
# *)
|
||||
# echo '{"data": null, "output_to_chain": false, "error": "Unknown action: '"$ACTION"'"}'
|
||||
# exit 0
|
||||
# ;;
|
||||
# esac
|
||||
#
|
||||
# Log messages to stderr (captured and sent back with response):
|
||||
# echo "Processing transaction..." >&2
|
||||
|
||||
# Parse transaction data
|
||||
TXN_ID=$(echo "$TX_JSON" | jq -r '.header.txn_id')
|
||||
TXN_TYPE=$(echo "$TX_JSON" | jq -r '.header.txn_type')
|
||||
PAYLOAD=$(echo "$TX_JSON" | jq -c '.payload')
|
||||
|
||||
echo "Processing transaction $TXN_ID (type: $TXN_TYPE)" >&2
|
||||
|
||||
# Default implementation: echo back the transaction
|
||||
jq -n \
|
||||
--arg txn_id "$TXN_ID" \
|
||||
--arg txn_type "$TXN_TYPE" \
|
||||
--argjson payload "$PAYLOAD" \
|
||||
'{
|
||||
"data": {
|
||||
"status": "processed",
|
||||
"transaction_id": $txn_id,
|
||||
"txn_type": $txn_type,
|
||||
"payload": $payload,
|
||||
"message": "Transaction processed successfully"
|
||||
},
|
||||
"output_to_chain": true,
|
||||
"error": ""
|
||||
}'
|
||||
Reference in New Issue
Block a user