From cab694f95e667ae9148b283c2d876338fe256e30 Mon Sep 17 00:00:00 2001
From: Robert Martin-Legene <robert@nic.ar>
Date: Wed, 1 Aug 2018 22:58:18 -0300
Subject: [PATCH] Contract support moving into libbfa.sh

---
 bin/libbfa.sh | 48 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 45 insertions(+), 3 deletions(-)

diff --git a/bin/libbfa.sh b/bin/libbfa.sh
index 2ea6f9d..6cfbe1c 100644
--- a/bin/libbfa.sh
+++ b/bin/libbfa.sh
@@ -35,19 +35,28 @@ function    cleanup
 function    geth_attach
 {
     bfaconfig node
-    geth    --cache 0 "$@" attach ipc:${BFANODEDIR}/geth.ipc
+    local cat=cat
+    if echo $- | grep -q x
+    then
+        cat="tee /dev/tty |"
+    fi
+    $cat | geth --cache 0 "$@" attach ipc:${BFANODEDIR}/geth.ipc
 }
 
 function    geth_exec_js
 {
     test    -r "$1"
-    geth_attach --exec "loadScript(\"$1\")"
+    if echo $- | grep -q x
+    then
+        sed "s//input: /" "$1"
+    fi
+    geth_attach --exec "loadScript(\"$1\")" </dev/null
 }
 
 function    geth_exec
 {
     test    -n "$1"
-    geth_attach --exec "$1"
+    geth_attach --exec "$1" </dev/null
 }
 
 function    geth_rpc
@@ -288,3 +297,36 @@ function gen_genesis
 	}
 	EOCONF
 }
+
+function contract
+{
+    bfaconfig network
+    local   contract="${BFANETWORKDIR}/contracts/${1}"
+    local   realdir=$(  realpath "${contract}"      )
+    test    -r "${realdir}"
+    local   address=$(  basename "${realdir}"       )
+    test    -n "${address}"
+    abi=$(              cat ${contract}/abi         )
+    test    -n "${abi}"
+    echo    "eth.contract(${abi}).at(\"${address}\")"
+}
+
+function contractCall
+{
+    local   name=$1
+    local   func=$2
+    shift   2
+    echo    "var contract = $( contract "${name}" );"
+    local   args=
+    for x in $*
+    do
+        args="${args}, ${x}"
+    done
+    args="${args:1},"
+    if [ "$args" = "," ]
+    then
+        args=
+    fi
+    echo    "contract.${func}.sendTransaction(${args} {from: eth.accounts[0], gas: 1000000} )"
+}
+
-- 
GitLab