diff --git a/graphenebase/operationids.py b/graphenebase/operationids.py index 257b28ed..5772851c 100644 --- a/graphenebase/operationids.py +++ b/graphenebase/operationids.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- #: Operation ids ops = [ "demooepration", @@ -10,10 +11,26 @@ operations = {o: ops.index(o) for o in ops} -def getOperationNameForId(i): +def getOperationNameForId(i: int): """ Convert an operation id into the corresponding string """ + assert isinstance(i, (int)), "This method expects an integer argument" for key in operations: if int(operations[key]) is int(i): return key raise ValueError("Unknown Operation ID %d" % i) + + +def getOperationName(id: str): + """ This method returns the name representation of an operation given + its value as used in the API + """ + if isinstance(id, str): + # Some graphene chains (e.g. steem) do not encode the + # operation_type as id but in its string form + assert id in operations.keys(), "Unknown operation {}".format(id) + return id + elif isinstance(id, int): + return getOperationNameForId(id) + else: + raise ValueError diff --git a/graphenecommon/blockchain.py b/graphenecommon/blockchain.py index 79d72ddf..218cf21c 100644 --- a/graphenecommon/blockchain.py +++ b/graphenecommon/blockchain.py @@ -214,7 +214,7 @@ def ops(self, start=None, stop=None, **kwargs): for tx in block["transactions"]: for op in tx["operations"]: # Replace opid by op name - op[0] = self.operationids.getOperationNameForId(op[0]) + op[0] = self.operationids.getOperationName(op[0]) yield { "block_num": block["block_num"], "op": op, diff --git a/tests/fixtures.py b/tests/fixtures.py index 263afb18..cd365105 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -42,8 +42,13 @@ Demooepration, Account_create, ) -from graphenebase import operations as operations_module -from graphenebase.operationids import ops, getOperationNameForId, operations +from graphenebase import operations as operations_module, operationids +from graphenebase.operationids import ( + ops, + getOperationNameForId, + getOperationName, + operations, +) from graphenebase import bip38 from graphenebase.bip38 import encrypt, decrypt @@ -76,6 +81,7 @@ from graphenecommon.asset import Asset as GAsset from graphenecommon.committee import Committee as GCommittee from graphenecommon.block import Block as GBlock, BlockHeader as GBlockHeader +from graphenecommon.blockchain import Blockchain as GBLockchain from graphenecommon.message import Message as GMessage from graphenecommon.blockchainobject import ObjectCache, BlockchainObject from graphenecommon.price import Price as GPrice @@ -259,6 +265,13 @@ def define_classes(self): self.amount_class = Amount +@BlockchainInstance.inject +class Blockchain(GBLockchain): + def define_classes(self): + self.block_class = Block + self.operationids = operationids + + @BlockchainInstance.inject class Wallet(GWallet): def define_classes(self): diff --git a/tests/test_operationids.py b/tests/test_operationids.py index f1f0b6b6..83d42d57 100644 --- a/tests/test_operationids.py +++ b/tests/test_operationids.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- import unittest -from .fixtures import ops, operations, getOperationNameForId +from .fixtures import ops, operations, getOperationNameForId, getOperationName class Testcases(unittest.TestCase): @@ -17,3 +17,9 @@ def test_getOperationNameForId(self): with self.assertRaises(ValueError): getOperationNameForId(20) + + def test_operation_type_decode(self): + self.assertEqual(getOperationName(0), "demooepration") + self.assertEqual(getOperationName("account_create"), "account_create") + with self.assertRaises(AssertionError): + self.assertEqual(getOperationName("-not-exist-"), "account_create")