2020-11-18 15:15:57 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2021-08-25 17:08:54 +00:00
|
|
|
import os
|
2020-11-18 15:15:57 +00:00
|
|
|
import pexpect
|
|
|
|
import re
|
|
|
|
|
2020-11-30 10:33:05 +00:00
|
|
|
from robot.api.deco import keyword
|
|
|
|
from robot.api import logger
|
2021-08-25 17:08:54 +00:00
|
|
|
from neo3 import wallet
|
2020-11-18 15:15:57 +00:00
|
|
|
|
2021-04-20 09:51:53 +00:00
|
|
|
from common import *
|
2021-08-25 17:08:54 +00:00
|
|
|
import rpc_client
|
2021-09-30 15:25:16 +00:00
|
|
|
import contract
|
2020-11-18 15:15:57 +00:00
|
|
|
|
2021-04-20 09:51:53 +00:00
|
|
|
ROBOT_AUTO_KEYWORDS = False
|
2020-11-18 15:15:57 +00:00
|
|
|
|
2021-09-30 15:25:16 +00:00
|
|
|
NNS_CONTRACT = contract.get_nns_contract_hash(NEOFS_NEO_API_ENDPOINT)
|
|
|
|
BALANCE_CONTRACT_HASH = contract.get_morph_contract_hash(
|
|
|
|
'balance.neofs', NNS_CONTRACT, NEOFS_NEO_API_ENDPOINT
|
|
|
|
)
|
2021-08-25 17:08:54 +00:00
|
|
|
MORPH_TOKEN_POWER = 12
|
|
|
|
|
|
|
|
morph_rpc_cli = rpc_client.RPCClient(NEOFS_NEO_API_ENDPOINT)
|
|
|
|
mainnet_rpc_cli = rpc_client.RPCClient(NEO_MAINNET_ENDPOINT)
|
2020-11-18 15:15:57 +00:00
|
|
|
|
|
|
|
|
2020-11-30 10:33:05 +00:00
|
|
|
@keyword('Withdraw Mainnet Gas')
|
2020-11-18 15:15:57 +00:00
|
|
|
def withdraw_mainnet_gas(wallet: str, address: str, scripthash: str, amount: int):
|
2021-01-17 11:55:10 +00:00
|
|
|
cmd = (
|
2021-02-01 16:43:35 +00:00
|
|
|
f"{NEOGO_CLI_EXEC} contract invokefunction -w {wallet} -a {address} "
|
2021-01-17 11:55:10 +00:00
|
|
|
f"-r {NEO_MAINNET_ENDPOINT} {NEOFS_CONTRACT} withdraw {scripthash} "
|
2021-05-26 14:14:46 +00:00
|
|
|
f"int:{amount} -- {scripthash}:Global"
|
2021-01-17 11:55:10 +00:00
|
|
|
)
|
2020-11-18 15:15:57 +00:00
|
|
|
|
|
|
|
logger.info(f"Executing command: {cmd}")
|
2020-11-28 03:41:35 +00:00
|
|
|
out = _run_sh_with_passwd('', cmd)
|
2020-11-18 15:15:57 +00:00
|
|
|
logger.info(f"Command completed with output: {out}")
|
2020-11-29 00:46:53 +00:00
|
|
|
m = re.match(r'^Sent invocation transaction (\w{64})$', out)
|
|
|
|
if m is None:
|
2020-11-30 10:43:19 +00:00
|
|
|
raise Exception("Can not get Tx.")
|
2020-11-29 00:46:53 +00:00
|
|
|
tx = m.group(1)
|
|
|
|
return tx
|
2020-11-18 15:15:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword('Transaction accepted in block')
|
2021-08-25 17:08:54 +00:00
|
|
|
def transaction_accepted_in_block(tx_id: str):
|
2020-11-18 15:15:57 +00:00
|
|
|
"""
|
|
|
|
This function return True in case of accepted TX.
|
|
|
|
Parameters:
|
2021-08-25 17:08:54 +00:00
|
|
|
:param tx_id: transaction ID
|
2020-11-18 15:15:57 +00:00
|
|
|
"""
|
|
|
|
|
2021-08-25 17:08:54 +00:00
|
|
|
try:
|
|
|
|
resp = mainnet_rpc_cli.get_transaction_height(tx_id)
|
|
|
|
if resp is not None:
|
|
|
|
logger.info(f"got block height: {resp}")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
logger.info(f"request failed with error: {e}")
|
|
|
|
raise e
|
2020-11-18 15:15:57 +00:00
|
|
|
|
|
|
|
|
2021-05-07 12:14:37 +00:00
|
|
|
@keyword('Get NeoFS Balance')
|
2021-08-25 17:08:54 +00:00
|
|
|
def get_balance(wif: str):
|
2020-11-18 15:15:57 +00:00
|
|
|
"""
|
2021-08-25 17:08:54 +00:00
|
|
|
This function returns NeoFS balance for given WIF.
|
2020-11-18 15:15:57 +00:00
|
|
|
"""
|
|
|
|
|
2021-08-25 17:08:54 +00:00
|
|
|
acc = wallet.Account.from_wif(wif, '')
|
|
|
|
payload = [
|
|
|
|
{
|
|
|
|
'type': 'Hash160',
|
|
|
|
'value': str(acc.script_hash)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
try:
|
|
|
|
resp = morph_rpc_cli.invoke_function(
|
|
|
|
BALANCE_CONTRACT_HASH, 'balanceOf', payload
|
|
|
|
)
|
|
|
|
logger.info(resp)
|
|
|
|
value = int(resp['stack'][0]['value'])
|
|
|
|
return value/(10**MORPH_TOKEN_POWER)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"failed to get {wif} balance: {e}")
|
|
|
|
raise e
|
2020-11-18 15:15:57 +00:00
|
|
|
|
|
|
|
|
2020-11-28 03:41:35 +00:00
|
|
|
def _run_sh_with_passwd(passwd, cmd):
|
|
|
|
p = pexpect.spawn(cmd)
|
|
|
|
p.expect(".*")
|
2021-02-16 11:56:52 +00:00
|
|
|
p.sendline(passwd + '\r')
|
2020-11-28 03:41:35 +00:00
|
|
|
p.wait()
|
|
|
|
# throw a string with password prompt
|
|
|
|
# take a string with tx hash
|
|
|
|
tx_hash = p.read().splitlines()[-1]
|
|
|
|
return tx_hash.decode()
|