2021-10-10 14:49:22 +02:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
from .base import (
|
|
|
|
Data,
|
|
|
|
data_dir,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Token(Data):
|
2021-11-29 13:18:28 +01:00
|
|
|
"""Encapsulates the token data used by the extension to deploy and/or register token and token related applications on chain.
|
|
|
|
|
|
|
|
Token details (name, symbol etc) will be used to initialize the token settings when start is called. If load is called instead, any token detail parameters passed to the constructor will be overwritten by data stored in the settings.
|
|
|
|
|
|
|
|
:param path: Settings directory path
|
|
|
|
:type path: str
|
|
|
|
:param name: Token name
|
|
|
|
:type name: str
|
|
|
|
:param symbol: Token symbol
|
|
|
|
:type symbol: str
|
|
|
|
:param precision: Token value precision (number of decimals)
|
|
|
|
:type precision: int
|
|
|
|
:param supply: Token supply (in smallest precision units)
|
|
|
|
:type supply: int
|
|
|
|
:param code: Bytecode for token chain application
|
|
|
|
:type code: str (hex)
|
|
|
|
"""
|
2021-10-14 15:43:23 +02:00
|
|
|
def __init__(self, path='.', name=None, symbol=None, precision=1, supply=0, code=None):
|
2021-10-10 14:49:22 +02:00
|
|
|
super(Token, self).__init__()
|
|
|
|
self.name = name
|
|
|
|
self.symbol = symbol
|
2021-10-14 15:43:23 +02:00
|
|
|
self.supply = supply
|
2021-10-10 14:49:22 +02:00
|
|
|
self.precision = precision
|
|
|
|
self.code = code
|
|
|
|
self.extra_args = None
|
|
|
|
self.path = path
|
|
|
|
self.token_path = os.path.join(self.path, 'token.json')
|
|
|
|
|
|
|
|
|
|
|
|
def load(self):
|
2021-11-29 13:18:28 +01:00
|
|
|
"""Load token data from settings.
|
|
|
|
"""
|
2021-10-10 14:49:22 +02:00
|
|
|
super(Token, self).load()
|
|
|
|
|
|
|
|
f = open(self.token_path, 'r')
|
|
|
|
o = json.load(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
self.name = o['name']
|
|
|
|
self.symbol = o['symbol']
|
|
|
|
self.precision = o['precision']
|
|
|
|
self.code = o['code']
|
2021-10-12 19:39:18 +02:00
|
|
|
self.supply = o['supply']
|
2021-10-10 14:49:22 +02:00
|
|
|
self.extra_args = o['extra']
|
|
|
|
|
|
|
|
self.inited = True
|
|
|
|
|
|
|
|
|
|
|
|
def start(self):
|
2021-11-29 13:18:28 +01:00
|
|
|
"""Initialize token settings from arguments passed to the constructor and/or template.
|
|
|
|
"""
|
2021-10-10 14:49:22 +02:00
|
|
|
super(Token, self).load()
|
|
|
|
|
|
|
|
token_template_file_path = os.path.join(data_dir, 'token_template_v{}.json'.format(self.version()))
|
|
|
|
|
|
|
|
f = open(token_template_file_path)
|
|
|
|
o = json.load(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
o['name'] = self.name
|
|
|
|
o['symbol'] = self.symbol
|
|
|
|
o['precision'] = self.precision
|
|
|
|
o['code'] = self.code
|
2021-10-12 19:39:18 +02:00
|
|
|
o['supply'] = self.supply
|
2021-10-10 14:49:22 +02:00
|
|
|
|
|
|
|
f = open(self.token_path, 'w')
|
2021-10-18 10:47:48 +02:00
|
|
|
json.dump(o, f, sort_keys=True, indent="\t")
|
2021-10-10 14:49:22 +02:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
s = """name = {}
|
|
|
|
symbol = {}
|
|
|
|
precision = {}
|
|
|
|
""".format(self.name, self.symbol, self.precision)
|
|
|
|
return s
|