cic-cli/cic/token.py

115 lines
3.5 KiB
Python

# standard imports
import json
import os
# local imports
from .base import Data, data_dir
class Token(Data):
"""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)
"""
def __init__(
self,
path=".",
name=None,
symbol=None,
precision=1,
supply=0,
code=None,
extra_args=[],
extra_args_types=[],
):
super(Token, self).__init__()
self.name = name
self.symbol = symbol
self.supply = supply
self.precision = precision
self.code = code
self.extra_args = extra_args
self.extra_args_types = extra_args_types
self.path = path
self.token_path = os.path.join(self.path, "token.json")
def load(self):
"""Load token data from settings."""
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"]
self.supply = o["supply"]
extras = []
extra_types = []
token_extras: list = o["extra"]
if token_extras:
for token_extra in token_extras:
arg = token_extra.get("arg")
arg_type = token_extra.get("arg_type")
if arg:
extras.append(arg)
if arg_type:
extra_types.append(arg_type)
self.extra_args = extras
self.extra_args_types = extra_types
self.inited = True
def start(self):
"""Initialize token settings from arguments passed to the constructor and/or template."""
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
o["supply"] = self.supply
extra = []
for i in range(len(self.extra_args)):
extra.append(
{"arg": self.extra_args[i], "arg_type": self.extra_args_types[i]}
)
if len(extra):
o["extra"] = extra
print(extra)
f = open(self.token_path, "w")
json.dump(o, f, sort_keys=True, indent="\t")
f.close()
def __str__(self):
s = f"name = {self.name}\n"
s += f"symbol = {self.symbol}\n"
s += f"precision = {self.precision}\n"
s += f"supply = {self.supply}\n"
for idx, extra in enumerate(self.extra_args):
s += f"extra_args[{idx}]({self.extra_args_types[idx]}) = {extra}\n"
return s