Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Invoke from address selection #329

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project are documented in this file.

[0.6.2-dev] in progress
-----------------------
- ...
- Added support for using ``--from-addr=`` to specify the address to use for ``testinvoke`` in ``prompt.py``. (`PR #329 <https://github.com/CityOfZion/neo-python/pull/329>`_)


[0.6.1] 2018-03-16
Expand Down
13 changes: 8 additions & 5 deletions neo/Prompt/Commands/BuildNRun.py
@@ -1,4 +1,4 @@
from neo.Prompt.Utils import get_arg
from neo.Prompt.Utils import get_arg, get_from_addr
from neo.Prompt.Commands.LoadSmartContract import GatherLoadedContractParams, generate_deploy_script
from neo.SmartContract.ContractParameterType import ContractParameterType
from neo.SmartContract.ContractParameter import ContractParameter
Expand All @@ -13,6 +13,8 @@

def LoadAndRun(arguments, wallet):

arguments, from_addr = get_from_addr(arguments)

path = get_arg(arguments)

try:
Expand All @@ -29,24 +31,25 @@ def LoadAndRun(arguments, wallet):
script = content

print("arguments.... %s " % arguments)
DoRun(script, arguments, wallet, path)
DoRun(script, arguments, wallet, path, from_addr=from_addr)

except Exception as e:
print("Could not load script %s " % e)


def BuildAndRun(arguments, wallet, verbose=True, min_fee=DEFAULT_MIN_FEE):
arguments, from_addr = get_from_addr(arguments)
path = get_arg(arguments)

contract_script = Compiler.instance().load_and_save(path)

newpath = path.replace('.py', '.avm')
print("Saved output to %s " % newpath)

return DoRun(contract_script, arguments, wallet, path, verbose, min_fee=min_fee)
return DoRun(contract_script, arguments, wallet, path, verbose, from_addr, min_fee)


def DoRun(contract_script, arguments, wallet, path, verbose=True, min_fee=DEFAULT_MIN_FEE):
def DoRun(contract_script, arguments, wallet, path, verbose=True, from_addr=None, min_fee=DEFAULT_MIN_FEE):

test = get_arg(arguments, 1)

Expand All @@ -59,7 +62,7 @@ def DoRun(contract_script, arguments, wallet, path, verbose=True, min_fee=DEFAUL

script = GatherLoadedContractParams(f_args, contract_script)

tx, result, total_ops, engine = test_deploy_and_invoke(script, i_args, wallet, min_fee)
tx, result, total_ops, engine = test_deploy_and_invoke(script, i_args, wallet, from_addr, min_fee)
i_args.reverse()

return_type_results = []
Expand Down
23 changes: 16 additions & 7 deletions neo/Prompt/Commands/Invoke.py
Expand Up @@ -2,7 +2,7 @@
from neo.VM.ScriptBuilder import ScriptBuilder
from neo.VM.InteropService import InteropInterface
from neo.Network.NodeLeader import NodeLeader
from neo.Prompt.Utils import parse_param, get_asset_attachments
from neo.Prompt.Utils import parse_param, get_asset_attachments, lookup_addr_str


from neo.Implementations.Blockchains.LevelDB.DBCollection import DBCollection
Expand Down Expand Up @@ -39,9 +39,12 @@
DEFAULT_MIN_FEE = Fixed8.FromDecimal(.0001)


def InvokeContract(wallet, tx, fee=Fixed8.Zero()):
def InvokeContract(wallet, tx, fee=Fixed8.Zero(), from_addr=None):

wallet_tx = wallet.MakeTransaction(tx=tx, fee=fee, use_standard=True)
if from_addr is not None:
from_addr = lookup_addr_str(wallet, from_addr)

wallet_tx = wallet.MakeTransaction(tx=tx, fee=fee, use_standard=True, from_addr=from_addr)

# pdb.set_trace()

Expand Down Expand Up @@ -204,7 +207,7 @@ def TestInvokeContract(wallet, args, withdrawal_tx=None, parse_params=True, from

outputs.append(output)

return test_invoke(out, wallet, outputs, withdrawal_tx, min_fee=min_fee)
return test_invoke(out, wallet, outputs, withdrawal_tx, from_addr, min_fee)

else:

Expand All @@ -217,6 +220,9 @@ def test_invoke(script, wallet, outputs, withdrawal_tx=None, from_addr=None, min

# print("invoke script %s " % script)

if from_addr is not None:
from_addr = lookup_addr_str(wallet, from_addr)

bc = GetBlockchain()

sn = bc._db.snapshot()
Expand Down Expand Up @@ -319,7 +325,7 @@ def test_invoke(script, wallet, outputs, withdrawal_tx=None, from_addr=None, min
return None, None, None, None


def test_deploy_and_invoke(deploy_script, invoke_args, wallet, min_fee=DEFAULT_MIN_FEE):
def test_deploy_and_invoke(deploy_script, invoke_args, wallet, from_addr=None, min_fee=DEFAULT_MIN_FEE):

bc = GetBlockchain()

Expand All @@ -344,7 +350,10 @@ def test_deploy_and_invoke(deploy_script, invoke_args, wallet, min_fee=DEFAULT_M
dtx.scripts = []
dtx.Script = binascii.unhexlify(deploy_script)

dtx = wallet.MakeTransaction(tx=dtx)
if from_addr is not None:
from_addr = lookup_addr_str(wallet, from_addr)

dtx = wallet.MakeTransaction(tx=dtx, from_addr=from_addr)
context = ContractParametersContext(dtx)
wallet.Sign(context)
dtx.scripts = context.GetScripts()
Expand Down Expand Up @@ -445,7 +454,7 @@ def test_deploy_and_invoke(deploy_script, invoke_args, wallet, min_fee=DEFAULT_M
itx.Attributes = [TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=Crypto.ToScriptHash(contract.Script, unhex=False).Data)]

itx = wallet.MakeTransaction(tx=itx)
itx = wallet.MakeTransaction(tx=itx, from_addr=from_addr)
context = ContractParametersContext(itx)
wallet.Sign(context)
itx.scripts = context.GetScripts()
Expand Down
6 changes: 4 additions & 2 deletions neo/Prompt/Commands/Tokens.py
@@ -1,5 +1,5 @@
from neo.Prompt.Commands.Invoke import InvokeContract, InvokeWithTokenVerificationScript
from neo.Prompt.Utils import get_asset_id, get_asset_attachments
from neo.Prompt.Utils import get_asset_id, get_from_addr
from neocore.Fixed8 import Fixed8
from prompt_toolkit import prompt
from decimal import Decimal
Expand Down Expand Up @@ -156,6 +156,8 @@ def token_mint(wallet, args, prompt_passwd=True):
def token_crowdsale_register(wallet, args, prompt_passwd=True):
token = get_asset_id(wallet, args[0])

args, from_addr = get_from_addr(args)

if len(args) < 2:
raise Exception("Specify addr to register for crowdsale")
register_addr = args[1:]
Expand All @@ -175,7 +177,7 @@ def token_crowdsale_register(wallet, args, prompt_passwd=True):
print("incorrect password")
return

return InvokeContract(wallet, tx, fee)
return InvokeContract(wallet, tx, fee, from_addr)

else:
print("Could not register addresses: %s " % str(results[0]))
Expand Down
12 changes: 6 additions & 6 deletions neo/SmartContract/tests/test_gas_costs.py
Expand Up @@ -44,7 +44,7 @@ def test_build_contract(self):

wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", True, False, "put", "key1", "b'ab'"]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put", "key1", "b'ab'", "--from-addr=" + self.wallet_1_addr]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False, min_fee=Fixed8.FromDecimal(.0004))

Expand All @@ -64,7 +64,7 @@ def test_build_contract_2(self):

wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", True, False, "put_5", "key1", "b'abababababab'"]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put_5", "key1", "b'abababababab'"]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False)

Expand All @@ -84,7 +84,7 @@ def test_build_contract_3(self):
"""
wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", True, False, "put_and_get", "key1", "b'abababababab'"]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put_and_get", "key1", "b'abababababab'"]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False)

Expand All @@ -104,7 +104,7 @@ def test_build_contract_4(self):
"""
wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", True, False, "put_and_get", "key1", self.big_str]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put_and_get", "key1", self.big_str]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False)

Expand Down Expand Up @@ -132,7 +132,7 @@ def test_build_contract_5(self):
"""
wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", True, False, "put_5", "key1", self.big_str]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put_5", "key1", self.big_str]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False)

Expand All @@ -149,7 +149,7 @@ def test_build_contract_6(self):
"""
wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", True, False, "put_9", "key1", "b'ababababab'"]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put_9", "key1", "b'ababababab'"]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False)

Expand Down
4 changes: 2 additions & 2 deletions neo/Wallets/NEP5Token.py
Expand Up @@ -268,7 +268,7 @@ def Mint(self, wallet, mint_to_addr, attachment_args):

return tx, fee, results

def CrowdsaleRegister(self, wallet, register_addresses):
def CrowdsaleRegister(self, wallet, register_addresses, from_addr=None):
"""
Register for a crowd sale.

Expand All @@ -285,7 +285,7 @@ def CrowdsaleRegister(self, wallet, register_addresses):
invoke_args = [self.ScriptHash.ToString(), 'crowdsale_register',
[parse_param(p, wallet) for p in register_addresses]]

tx, fee, results, num_ops = TestInvokeContract(wallet, invoke_args, None, True)
tx, fee, results, num_ops = TestInvokeContract(wallet, invoke_args, None, True, from_addr)

return tx, fee, results

Expand Down
20 changes: 13 additions & 7 deletions neo/bin/prompt.py
Expand Up @@ -38,7 +38,7 @@
token_mint, token_crowdsale_register
from neo.Prompt.Commands.Wallet import DeleteAddress, ImportWatchAddr, ImportToken, ClaimGas, DeleteToken, AddAlias, \
ShowUnspentCoins
from neo.Prompt.Utils import get_arg
from neo.Prompt.Utils import get_arg, get_from_addr
from neo.Prompt.InputParser import InputParser
from neo.Settings import settings, PrivnetConnectionError, PATH_USER_DATA
from neo.UserPreferences import preferences
Expand Down Expand Up @@ -97,12 +97,14 @@ class PromptInterface(object):
'wallet migrate',
'wallet rebuild {start block}',
'wallet delete_addr {addr}',
'wallet delete_token {token_contract_hash}',
'wallet alias {addr} {title}',
'wallet tkn_send {token symbol} {address_from} {address to} {amount} ',
'wallet tkn_send_from {token symbol} {address_from} {address to} {amount}',
'wallet tkn_approve {token symbol} {address_from} {address to} {amount}',
'wallet tkn_allowance {token symbol} {address_from} {address to}',
'wallet tkn_mint {token symbol} {mint_to_addr} (--attach-neo={amount}, --attach-gas={amount})',
'wallet tkn_register {addr} ({addr}...) (--from-addr={addr})',
'wallet unspent',
'wallet close',
'withdraw_request {asset_name} {contract_hash} {to_addr} {amount}',
Expand All @@ -114,7 +116,7 @@ class PromptInterface(object):
'withdraw all # withdraw all holds available',
'send {assetId or name} {address} {amount} (--from-addr={addr})',
'sign {transaction in JSON format}',
'testinvoke {contract hash} {params} (--attach-neo={amount}, --attach-gas={amount})',
'testinvoke {contract hash} {params} (--attach-neo={amount}, --attach-gas={amount}) (--from-addr={addr})',
'debugstorage {on/off/reset}'
]

Expand Down Expand Up @@ -156,7 +158,7 @@ def get_completer(self):
'wallet', 'contract', 'asset', 'wif',
'watch_addr', 'contract_addr', 'testinvoke', 'tkn_send',
'tkn_mint', 'tkn_send_from', 'tkn_approve', 'tkn_allowance',
'build', 'notifications', ]
'tkn_register', 'build', 'notifications', ]

if self.Wallet:
for addr in self.Wallet.Addresses:
Expand Down Expand Up @@ -700,8 +702,10 @@ def test_invoke_contract(self, args):
print("Please open a wallet")
return

args, from_addr = get_from_addr(args)

if args and len(args) > 0:
tx, fee, results, num_ops = TestInvokeContract(self.Wallet, args)
tx, fee, results, num_ops = TestInvokeContract(self.Wallet, args, from_addr=from_addr)

if tx is not None and results is not None:
print(
Expand All @@ -719,7 +723,7 @@ def test_invoke_contract(self, args):
if not self.Wallet.ValidatePassword(passwd):
return print("Incorrect password")

result = InvokeContract(self.Wallet, tx, fee)
result = InvokeContract(self.Wallet, tx, fee, from_addr=from_addr)

return
else:
Expand All @@ -733,6 +737,8 @@ def load_smart_contract(self, args):
print("Please open a wallet")
return

args, from_addr = get_from_addr(args)

function_code = LoadContract(args[1:])

if function_code:
Expand All @@ -741,7 +747,7 @@ def load_smart_contract(self, args):

if contract_script is not None:

tx, fee, results, num_ops = test_invoke(contract_script, self.Wallet, [])
tx, fee, results, num_ops = test_invoke(contract_script, self.Wallet, [], from_addr=from_addr)

if tx is not None and results is not None:
print(
Expand All @@ -760,7 +766,7 @@ def load_smart_contract(self, args):
if not self.Wallet.ValidatePassword(passwd):
return print("Incorrect password")

result = InvokeContract(self.Wallet, tx, Fixed8.Zero())
result = InvokeContract(self.Wallet, tx, Fixed8.Zero(), from_addr=from_addr)

return
else:
Expand Down