mirror of
https://github.com/FlipsideCrypto/web3.py.git
synced 2026-02-06 10:56:47 +00:00
Formatters refactor
This commit is contained in:
parent
c9ae655fd9
commit
1e2396339c
@ -12,8 +12,9 @@ from web3 import formatters
|
||||
('0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae')
|
||||
]
|
||||
)
|
||||
def test_inputAddressFormatter(value, expected):
|
||||
assert formatters.inputAddressFormatter(value) == expected
|
||||
def test_input_address_formatter(value, expected):
|
||||
assert formatters.input_address_formatter(value) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
@ -25,9 +26,10 @@ def test_inputAddressFormatter(value, expected):
|
||||
('0x'),
|
||||
]
|
||||
)
|
||||
def test_inputAddressFormatter2(value):
|
||||
def test_input_address_formatter_errors(value):
|
||||
with pytest.raises(ValueError):
|
||||
formatters.inputAddressFormatter(value)
|
||||
formatters.input_address_formatter(value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value,expected",
|
||||
@ -149,8 +151,8 @@ def test_inputPostFormatter(value, expected):
|
||||
}
|
||||
)]
|
||||
)
|
||||
def test_inputTransactionFormatter(value, expected):
|
||||
assert formatters.inputTransactionFormatter(value) == expected
|
||||
def test_input_transaction_formatter(value, expected):
|
||||
assert formatters.input_transaction_formatter(value) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@ -331,5 +333,5 @@ def test_outputPostFormatter(value, expected):
|
||||
})
|
||||
]
|
||||
)
|
||||
def test_outputTransactionFormatter(value, expected):
|
||||
assert formatters.outputTransactionFormatter(value) == expected
|
||||
def test_output_transaction_formatter(value, expected):
|
||||
assert formatters.output_transaction_formatter(value) == expected
|
||||
|
||||
@ -154,14 +154,14 @@ class Eth(object):
|
||||
"""
|
||||
raise NotImplementedError("TODO")
|
||||
|
||||
@apply_formatters_to_return(formatters.outputTransactionFormatter)
|
||||
@apply_formatters_to_return(formatters.output_transaction_formatter)
|
||||
def getTransaction(self, txn_hash):
|
||||
return self.request_manager.request_blocking(
|
||||
"eth_getTransactionByHash",
|
||||
[txn_hash],
|
||||
)
|
||||
|
||||
@apply_formatters_to_return(formatters.outputTransactionFormatter)
|
||||
@apply_formatters_to_return(formatters.output_transaction_formatter)
|
||||
def getTransactionFromBlock(self, block_identifier, txn_index):
|
||||
"""
|
||||
`eth_getTransactionByBlockHashAndIndex`
|
||||
|
||||
@ -4,6 +4,7 @@ from web3.iban import Iban
|
||||
|
||||
from web3.utils.string import (
|
||||
force_text,
|
||||
coerce_args_to_text,
|
||||
coerce_return_to_text,
|
||||
)
|
||||
from web3.utils.address import (
|
||||
@ -24,6 +25,9 @@ from web3.utils.encoding import (
|
||||
from_decimal,
|
||||
to_decimal,
|
||||
)
|
||||
from web3.utils.functional import (
|
||||
identity,
|
||||
)
|
||||
import web3.utils.config as config
|
||||
|
||||
|
||||
@ -48,60 +52,62 @@ def inputBlockNumberFormatter(blockNumber):
|
||||
return to_hex(blockNumber)
|
||||
|
||||
|
||||
@coerce_args_to_text
|
||||
@coerce_return_to_text
|
||||
def inputCallFormatter(options):
|
||||
"""
|
||||
Formats the input of a transaction and converts all values to HEX
|
||||
"""
|
||||
|
||||
options.setdefault("from", config.defaultAccount)
|
||||
|
||||
if options.get("from"):
|
||||
options["from"] = inputAddressFormatter(options["from"])
|
||||
|
||||
if options.get("to"):
|
||||
options["to"] = inputAddressFormatter(options["to"])
|
||||
|
||||
for key in ("gasPrice", "gas", "value", "nonce"):
|
||||
if key in options:
|
||||
options[key] = from_decimal(options[key])
|
||||
|
||||
return options
|
||||
def input_call_formatter(txn):
|
||||
defaults = {
|
||||
'from': config.defaultAccount,
|
||||
}
|
||||
formatters = {
|
||||
'from': input_address_formatter,
|
||||
'to': input_address_formatter,
|
||||
'gasPrice': from_decimal,
|
||||
'gas': from_decimal,
|
||||
'value': from_decimal,
|
||||
'nonce': from_decimal,
|
||||
}
|
||||
return {
|
||||
key: formatters.get(key, identity)(txn.get(key, defaults.get(key)))
|
||||
for key in set(tuple(txn.keys()) + tuple(defaults.keys()))
|
||||
}
|
||||
|
||||
|
||||
@coerce_args_to_text
|
||||
@coerce_return_to_text
|
||||
def inputTransactionFormatter(options):
|
||||
"""
|
||||
Formats the input of a transaction and converts all values to HEX
|
||||
"""
|
||||
options.setdefault("from", config.defaultAccount)
|
||||
options["from"] = inputAddressFormatter(options["from"])
|
||||
|
||||
if options.get("to"):
|
||||
options["to"] = inputAddressFormatter(options["to"])
|
||||
|
||||
for key in ("gasPrice", "gas", "value", "nonce"):
|
||||
if key in options:
|
||||
options[key] = from_decimal(options[key])
|
||||
|
||||
return options
|
||||
def input_transaction_formatter(txn):
|
||||
defaults = {
|
||||
'from': config.defaultAccount,
|
||||
}
|
||||
formatters = {
|
||||
'from': input_address_formatter,
|
||||
'to': input_address_formatter,
|
||||
'gasPrice': from_decimal,
|
||||
'gas': from_decimal,
|
||||
'value': from_decimal,
|
||||
'nonce': from_decimal,
|
||||
}
|
||||
return {
|
||||
key: formatters.get(key, identity)(txn.get(key, defaults.get(key)))
|
||||
for key in set(tuple(txn.keys()) + tuple(defaults.keys()))
|
||||
}
|
||||
|
||||
|
||||
@coerce_args_to_text
|
||||
@coerce_return_to_text
|
||||
def outputTransactionFormatter(tx):
|
||||
"""
|
||||
Formats the output of a transaction to its proper values
|
||||
"""
|
||||
if tx.get("blockNumber"):
|
||||
tx["blockNumber"] = to_decimal(tx["blockNumber"])
|
||||
if tx.get("transactionIndex"):
|
||||
tx["transactionIndex"] = to_decimal(tx["transactionIndex"])
|
||||
def output_transaction_formatter(txn):
|
||||
formatters = {
|
||||
'blockNumber': lambda v: None if v is None else to_decimal(v),
|
||||
'transactionIndex': lambda v: None if v is None else to_decimal(v),
|
||||
'nonce': to_decimal,
|
||||
'gas': to_decimal,
|
||||
'gasPrice': to_decimal,
|
||||
'value': to_decimal,
|
||||
}
|
||||
|
||||
tx["nonce"] = to_decimal(tx["nonce"])
|
||||
tx["gas"] = to_decimal(tx["gas"])
|
||||
tx["gasPrice"] = to_decimal(tx["gasPrice"])
|
||||
tx["value"] = to_decimal(tx["value"])
|
||||
return tx
|
||||
return {
|
||||
key: formatters.get(key, identity)(value)
|
||||
for key, value in txn.items()
|
||||
}
|
||||
|
||||
|
||||
@coerce_return_to_text
|
||||
@ -204,7 +210,7 @@ def outputPostFormatter(post):
|
||||
return post
|
||||
|
||||
|
||||
def inputAddressFormatter(addr):
|
||||
def input_address_formatter(addr):
|
||||
iban = Iban(addr)
|
||||
if iban.isValid() and iban.isDirect():
|
||||
return "0x" + iban.address()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user