From 25c7403c8c4bf4e25676f068b84dfb9cbbccb044 Mon Sep 17 00:00:00 2001
From: Robert Martin-Legene <robert@martin-legene.dk>
Date: Thu, 3 Dec 2020 17:23:37 -0300
Subject: [PATCH] soportar JSON que solamente tiene un ABI

---
 bin/libbfa/__init__.py | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/bin/libbfa/__init__.py b/bin/libbfa/__init__.py
index c06f92c..e540c4a 100755
--- a/bin/libbfa/__init__.py
+++ b/bin/libbfa/__init__.py
@@ -286,7 +286,6 @@ class CompiledContract:
         # Did read give us json, or should we compile it?
         if self.json is None:
             self.compile()
-            self.writetextfile()
 
     def dockerargs(self):
         return [
@@ -313,7 +312,14 @@ class CompiledContract:
         # Make a copy with a fixed name
         # Mostly so people can use symlinks to the source files
         # which won't work when we call docker.
-        with open('{}.sol'.format(self.name), 'r') as infile:
+        candidate_sol = '{}.sol'.format(self.name)
+        if os.path.exists(self.name):
+            filename = self.name
+        elif os.path.exists(candidate_sol):
+            filename = candidate_sol
+        else:
+            filename = self.name
+        with open(filename, 'r') as infile:
             with open('contract.sol', 'w') as outfile:
                 outfile.write(infile.read())
         try:
@@ -324,14 +330,17 @@ class CompiledContract:
         txt = solc.stdout
         output = txt.decode('utf-8')
         self.json = json.loads(output)
+        self.writetextfile()
 
     def readtextfile(self):
-        filename = self.name + '.compiled.json'
-        try:
-            with open(filename, 'rt', encoding='utf-8') as infile:
-                output = infile.read()
-        except FileNotFoundError:
-            return
+        for filename in ( '{}.compiled.json'.format(self.name), self.name ):
+            if os.path.exists( filename ):
+                with open(filename, 'rt', encoding='utf-8') as infile:
+                    output = infile.read()
+                    break
+        if output is None:
+            print("File not found.", file=sys.stderr)
+            raise FileNotFound
         if len(output) < 2:
             print("The JSON file is too small ({} bytes read from {}).".format(len(output), filename), file=sys.stderr)
             raise NameError
@@ -372,7 +381,12 @@ class CompiledContract:
     def abi(self):
         # Old method which also works, but our own class has a nicer __str__:
         # return json.loads(self.json['contracts'][where]['abi'])
-        return Abi(json.loads(self._where()['abi']))
+        where = self._where()
+        if type(where) is str:
+            where = json.loads(where)
+        if (type(where) is dict) and 'abi' in where:
+            where = where['abi']
+        return Abi(where)
 
     def instance(self, *args, **kwargs):
         addr = kwargs.get('address')
-- 
GitLab