Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
feat: improves sandbox command in repl
Browse files Browse the repository at this point in the history
  • Loading branch information
ant4g0nist committed Jun 6, 2022
1 parent ed7bf16 commit a5d519d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
10 changes: 6 additions & 4 deletions chinstrap/repl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ def sandbox(
if args.stop:
return _sandbox.halt()

_sandbox.initialize()
_sandbox.run()
if not _sandbox.isRunning(args.port):
_sandbox.initialize()
_sandbox.run()

return _sandbox


Expand Down Expand Up @@ -155,7 +157,7 @@ def originate(

@helpers.handleException()
def sandboxAccounts():
sandbox.Sandbox.listAccounts()
Sandbox.Sandbox.listAccounts()


@helpers.handleException()
Expand Down Expand Up @@ -185,7 +187,7 @@ def launchRepl(args):
pretty.install()

if args.network == "development":
_sandbox = sandbox.Sandbox(args)
_sandbox = Sandbox.Sandbox(args)
_sandbox.args.detach = True
_sandbox.initialize()
_sandbox.run()
Expand Down
23 changes: 19 additions & 4 deletions chinstrap/sandbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __str__(self):
class Sandbox:
def __init__(self, args) -> None:
self.args = args
self.loadState()

def initialize(self):
self.state = {"accounts": {}, "port": self.args.port}
Expand All @@ -88,6 +89,9 @@ def download(self):

spinner.succeed("Flextesa sandbox ready to use")

def loadState(self):
self.state = Sandbox.getSandboxState()

@staticmethod
@IsChinstrapProject()
def dumpSandboxState(state):
Expand Down Expand Up @@ -125,6 +129,7 @@ def isRunning(port=None):
f"Sandbox is already {container.status} \
on port: {helpers.RED}{port}{helpers.RST}"
)

else:
print(
f"Sandbox is already {container.status} \
Expand Down Expand Up @@ -190,6 +195,9 @@ def halt(self):
spinner.succeed("Halted the sandbox")

def generateAccount(self, index=0):
if not index:
index = len(self.state["accounts"])

name = giveMeAName(index)
cmd = f"flextesa key {name}"
container = runInFlextesaContainerCli(cmd, detach=False)
Expand All @@ -203,16 +211,21 @@ def generateAccount(self, index=0):

return name, privateKey, line

def generateAccounts(self):
def generateAccounts(self, num_of_accounts=None):
if not num_of_accounts:
num_of_accounts = self.args.num_of_accounts

spinner = halo.Halo(
text=f"Creating {self.args.num_of_accounts} accounts...", spinner="dots"
text=f"Creating {num_of_accounts} accounts...", spinner="dots"
)
spinner.start()

self.accounts = []

for i in range(self.args.num_of_accounts):
name, privateKey, account = self.generateAccount(i)
index = len(self.state["accounts"])

for i in range(num_of_accounts):
name, privateKey, account = self.generateAccount(i + index)
self.accounts.append(f"{account}")

spinner.succeed(text="Accounts created!\n")
Expand Down Expand Up @@ -240,6 +253,8 @@ def generateAccounts(self):
f"{helpers.RED}WARNING:{helpers.RST} Please do not use these accounts on mainnet!"
)

self.dumpSandboxState(self.state)

def launchSandbox(self):
spinner = halo.Halo(text="Starting sandbox", spinner="dots")
spinner.start()
Expand Down
2 changes: 1 addition & 1 deletion chinstrap/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "1.0.9"
version = "1.0.10"

0 comments on commit a5d519d

Please sign in to comment.