from ethjsonrpc import EthJsonRpc, ParityEthJsonRpc, ConnectionError

methods = [
    'web3_clientVersion',
    'net_version',
    'net_peerCount',
    'net_listening',
    'eth_protocolVersion',
    'eth_syncing',
    'eth_coinbase',
    'eth_mining',
    'eth_hashrate',
    'eth_gasPrice',
    'eth_accounts',
    'eth_blockNumber',
    'eth_getCompilers',
    'eth_newPendingTransactionFilter',
#   'eth_getWork',
#   'shh_version',
#   'shh_newIdentity',
#   'shh_newGroup',
]

geth_client = EthJsonRpc(port=8545)
try:
    parity = True
    parity_client = ParityEthJsonRpc(port=8080)
    parity_client.web3_clientVersion()
except ConnectionError:
    parity = False


print(len(methods))
for m in methods:
    meth = getattr(geth_client, m)
    result = meth()
    print('%s: %s (%s)' % (m, result, type(result)))

################################################################################
print('*' * 80)

addr = '0x1dcb8d1f0fcc8cbc8c2d76528e877f915e299fbe'
for x in ['earliest', 'latest', 'pending', 150000]:
    result = geth_client.eth_getTransactionCount(addr, x)
    print('eth_getTransactionCount: %s (%s)' % (result, type(result)))

b = (231301, '0x9476018748ba1dae5bdf5e3725f8966df1fa127d49f58e66f621bf6868a23c85')
result = geth_client.eth_getBlockTransactionCountByHash(b[1])
print('eth_getBlockTransactionCountByHash: %s (%s)' % (result, type(result)))

for x in ['earliest', 'latest', 'pending', b[0]]:
    result = geth_client.eth_getBlockTransactionCountByNumber(x)
    print('eth_getBlockTransactionCountByNumber: %s (%s)' % (result, type(result)))


b = (199583, '0x19d761c6f944eefe91ad70b9aff3d2d76c972e5bb68c443eea7c0eaa144cef9f')
result = geth_client.eth_getUncleCountByBlockHash(b[1])
print('eth_getUncleCountByBlockHash: %s (%s)' % (result, type(result)))

for x in ['earliest', 'latest', 'pending', b[0]]:
    result = geth_client.eth_getUncleCountByBlockNumber(x)
    print('eth_getUncleCountByBlockNumber: %s (%s)' % (result, type(result)))

################################################################################

# ______________THIS IS DEPRECATED_______________ #

# print('*' * 80)

# db_name = 'db_name'
# k = 'my_key'
# v = 'my_value'
# print(geth_client.db_putString(db_name, k, v))
# x = geth_client.db_getString(db_name, k)
# print(x)
# assert v == x

# db_name = 'db_name'
# k = 'my_key'
# v = '0xabcdef'
# print(geth_client.db_putHex(db_name, k, v))
# x = geth_client.db_getHex(db_name, k)
# print(x)
# assert v == x

################################################################################
print('*' * 80)

b = (199583, '0x19d761c6f944eefe91ad70b9aff3d2d76c972e5bb68c443eea7c0eaa144cef9f')
print(geth_client.eth_getBlockByHash(b[1], tx_objects=False))

for x in ['earliest', 'latest', 'pending', b[0]]:
    print(geth_client.eth_getBlockByNumber(x, tx_objects=False))

tx = '0x12cd5d9a82049154c8990214a551479853d1bfe45852688833bc4ef86a29b1a3'
print(geth_client.eth_getTransactionByHash(tx))

################################################################################
print('*' * 80)

code = 'contract Test {}'
print(geth_client.eth_compileSolidity(code))

#code = ''
#print(geth_client.eth_compileSerpent(code)

#code = ''
#print(geth_client.eth_compileLLL(code)

################################################################################
print('*' * 80)

b = (246236, '0xcd43703a1ead33ffa1f317636c7b67453c5cc03a3350cd71dbbdd70fcbe0987a')
index = 2
print(geth_client.eth_getTransactionByBlockHashAndIndex(b[1], index))

for x in ['earliest', 'latest', 'pending', b[0]]:
    print(geth_client.eth_getTransactionByBlockNumberAndIndex(b[0], index))

tx = '0x27191ea9e8228c98bc4418fa60843540937b0c615b2db5e828756800f533f8cd'
print(geth_client.eth_getTransactionReceipt(tx))

b = (246294, '0x3d596ca3c7b344419567957b41b2132bb339d365b6b6b3b6a7645e5444914a16')
index = 0
print(geth_client.eth_getUncleByBlockHashAndIndex(b[1], index))

for x in ['earliest', 'latest', 'pending', b[0]]:
    print(geth_client.eth_getUncleByBlockNumberAndIndex(b[0], index))

################################################################################
print('*' * 80)

addr = '0x1dcb8d1f0fcc8cbc8c2d76528e877f915e299fbe'
for x in ['earliest', 'latest', 'pending', 150000]:
    print(geth_client.eth_getBalance(addr, x))

addr = '0x407d73d8a49eeb85d32cf465507dd71d507100c1'
for x in ['earliest', 'latest', 'pending', 2]:
    print(geth_client.eth_getStorageAt(addr, 0, x))

################################################################################
print('*' * 80)

hash_rate = 1000000
client_id = '0x59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c'
print(geth_client.eth_submitHashrate(hash_rate, client_id))

digest = geth_client.web3_sha3('')
print(digest)
# keccak-256, not sha3-256
assert digest == '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'

################################################################################
if parity:
    print('*' * 80)

    print('personal_listAccounts', parity_client.personal_listAccounts())

    account = parity_client.personal_newAccount('')
    print('personal_newAccount', account)

    parity_client.personal_unlockAccount(account, '') == True
    print('personal_unlockAccount', parity_client.personal_unlockAccount(account, ''))

    # parity_client.personal_signAndSendTransaction()
