[KMS-ETH]- Modules to work with a token

pull/195/head^2
Michael Egorov 2017-11-18 20:40:09 -08:00
parent 8563d9b9e7
commit d54fad14bc
6 changed files with 90 additions and 15 deletions

View File

@ -4,9 +4,14 @@ import nkms_eth
from os.path import dirname, join, abspath
DEFAULT_NETWORK = 'mainnetrpc'
TIMEOUT = 60
_project = threading.local()
# TODO XXX registrar.json is created in a current directory!
# we should instead create all new configs in ~/.local
def project():
# Hardcoded the config for the project
# It will read user-specific configs also which may override it
@ -16,15 +21,24 @@ def project():
return _project.project
def chain(name=DEFAULT_NETWORK):
def get_chain(name=None):
return project().get_chain(name or DEFAULT_NETWORK)
def chain(name=None):
if not hasattr(_project, 'chain'):
_project.chain = project().get_chain(name).__enter__()
_project.chain = get_chain(name).__enter__()
return _project.chain
def disconnect():
_project.chain.__exit__(None, None, None)
delattr(_project, 'chain')
if hasattr(_project, 'project'):
delattr(_project, 'project')
if hasattr(_project, 'chain'):
delattr(_project, 'chain')
if hasattr(_project, 'web3'):
delattr(_project, 'web3')
def web3():

View File

@ -1,11 +0,0 @@
def create_tokens():
pass
def deploy_escrow():
pass
if __name__ == '__main__':
create_tokens()
deploy_escrow()

41
nkms_eth/token.py Normal file
View File

@ -0,0 +1,41 @@
from nkms_eth import blockchain
CONTRACT_NAME = 'HumanStandardToken' # TODO this should be NuCypher's class
SUBDIGITS = 6
M = 10 ** SUBDIGITS
PREMINE = int(1e9) * M
SATURATION = int(1e10) * M
def create():
"""
Creates a contract with tokens and returns it.
If it was already created, just returns the already existing contract
:returns: Token contract object
"""
chain = blockchain.chain()
web3 = chain.web3
creator = web3.eth.accounts[0] # TODO: make it possible to override
token, tx = chain.provider.get_or_deploy_contract(
CONTRACT_NAME, deploy_args=[
PREMINE, SATURATION, 'NuCypher KMS', SUBDIGITS, 'KMS'],
deploy_transaction={
'from': creator})
if tx:
chain.wait.for_receipt(tx, timeout=blockchain.TIMEOUT)
return token
def get(name=CONTRACT_NAME):
"""
Gets an existing contract or returns an error
"""
return blockchain.chain().provider.get_contract(name)
if __name__ == '__main__':
create()
get()

View File

@ -4,5 +4,11 @@ from nkms_eth import blockchain
@pytest.fixture()
def project():
blockchain.DEFAULT_NETWORK = 'tester'
return blockchain.project()
@pytest.fixture()
def chain():
blockchain.DEFAULT_NETWORK = 'tester'
yield blockchain.chain()
blockchain.disconnect()

7
tests/test_blockchain.py Normal file
View File

@ -0,0 +1,7 @@
from nkms_eth import blockchain
def test_chain(chain):
assert blockchain.DEFAULT_NETWORK == 'tester'
web3 = blockchain.web3()
assert web3.eth.blockNumber >= 0

View File

@ -0,0 +1,18 @@
from pytest import raises
from populus.contracts.exceptions import NoKnownAddress
from nkms_eth import token
def test_create(chain):
t = token.create()
assert len(t.address) == 42
def test_get(chain):
with raises(NoKnownAddress):
token.get()
token.create()
t = token.get()
assert len(t.address) == 42