Skip to content

Commit

Permalink
Testnet integration (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Lee authored Apr 28, 2021
1 parent 4dfc88d commit 0c8c948
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 89 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,9 @@ cython_debug/

# Project data files
data/

# Keys
*.addr
*.skey
*.vkey
*.json
4 changes: 3 additions & 1 deletion automint/account/Account.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def set_lovelace(self, quantity):

return new_account


def set_ada(self, quantity):
assert quantity >= 0

Expand Down Expand Up @@ -128,6 +127,9 @@ def get_ada(self):
def get_native_token(self, token_id):
return self.native_tokens.get(token_id, 0)

def get_native_tokens(self):
return self.native_tokens

def duplicate(self):
new_account = copy.deepcopy(self)
return new_account
Expand Down
1 change: 1 addition & 0 deletions automint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# executable or (2) a valid command in the shell (ie `cardano-cli` is
# working for most people)
CARDANO_CLI = 'cardano-cli'
TESTNET_MAGIC_DEFAULT = '1097911063'
141 changes: 92 additions & 49 deletions automint/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@
import logging
import json
import requests
from automint.config import CARDANO_CLI
from automint.config import CARDANO_CLI, TESTNET_MAGIC_DEFAULT


logger = logging.getLogger(__name__)


def get_protocol_params(working_dir):
def get_protocol_params(working_dir, use_testnet=False, testnet_magic=TESTNET_MAGIC_DEFAULT):
"""Query protocol parameters and write to file"""

protocol_json_path = os.path.join(working_dir, 'protocol.json')

proc = subprocess.run([CARDANO_CLI,
'query',
'protocol-parameters',
'--mainnet',
'--out-file',
protocol_json_path], capture_output=True, text=True)
cmd_builder = [CARDANO_CLI.replace(' ', '\ '),
'query',
'protocol-parameters',
'--out-file',
protocol_json_path]

if proc.stderr != "":
if use_testnet:
cmd_builder.append('--testnet-magic')
cmd_builder.append(str(testnet_magic))
else:
cmd_builder.append('--mainnet')

cmd = ' '.join(cmd_builder)

proc = subprocess.run(cmd, capture_output=True, text=True, shell=True)

if proc.stderr != '':
logger.error(f'Failed to fetch protocol parameters...')
return ""
return ''

return protocol_json_path

Expand Down Expand Up @@ -145,13 +154,13 @@ def build_raw_transaction(working_dir, input_utxos, output_accounts, policy_id=N

proc = subprocess.run(cmd, capture_output=True, text=True, shell=True)

if proc.stderr != "":
if proc.stderr != '':
logger.error(f'Error encountered when building transaction\n{cmd_builder}\n{proc.stderr}')

return raw_matx_path


def calculate_tx_fee(raw_matx_path, protocol_json_path, input_utxos, output_accounts):
def calculate_tx_fee(raw_matx_path, protocol_json_path, input_utxos, output_accounts, witness_count=2, use_testnet=False, testnet_magic=TESTNET_MAGIC_DEFAULT):
"""Calculate transaction fees"""

if type(input_utxos) != list:
Expand All @@ -160,30 +169,41 @@ def calculate_tx_fee(raw_matx_path, protocol_json_path, input_utxos, output_acco
if type(output_accounts) != list:
output_accounts = [output_accounts]

proc = subprocess.run([CARDANO_CLI,
'transaction',
'calculate-min-fee',
'--tx-body-file',
raw_matx_path,
'--tx-in-count',
f'{len(input_utxos)}',
'--tx-out-count',
f'{len(output_accounts)}',
'--witness-count',
'2',
'--mainnet',
'--protocol-params-file',
protocol_json_path], capture_output=True, text=True)
assert witness_count >= len(input_utxos)

cmd_builder = [CARDANO_CLI.replace(' ', '\ '),
'transaction',
'calculate-min-fee',
'--tx-body-file',
raw_matx_path,
'--tx-in-count',
f'{len(input_utxos)}',
'--tx-out-count',
f'{len(output_accounts)}',
'--witness-count',
f'{witness_count}',
'--protocol-params-file',
protocol_json_path]

if use_testnet:
cmd_builder.append('--testnet-magic')
cmd_builder.append(str(testnet_magic))
else:
cmd_builder.append('--mainnet')

cmd = ' '.join(cmd_builder)

proc = subprocess.run(cmd, capture_output=True, text=True, shell=True)

if proc.stderr != '':
logger.error(f'Error encountered when calculating transcation fee...\n{proc.stdout}')
logger.debug(f'{proc.stderr}')
return ""
return ''

return int(proc.stdout.split()[0])


def sign_tx(nft_dir, signing_wallets, raw_matx_path, force=False):
def sign_tx(nft_dir, signing_wallets, raw_matx_path, force=False, use_testnet=False, testnet_magic=TESTNET_MAGIC_DEFAULT):
"""Generate and write signed transaction file"""
if type(signing_wallets) != list:
signing_wallets = [signing_wallets]
Expand All @@ -192,18 +212,23 @@ def sign_tx(nft_dir, signing_wallets, raw_matx_path, force=False):

# Only generate/overwrite the keys if they do not exist or force=True
cmd_builder = [CARDANO_CLI.replace(' ', '\ '),
'transaction',
'sign',
'--mainnet',
'--tx-body-file',
raw_matx_path,
'--out-file',
signed_matx_path]
'transaction',
'sign',
'--tx-body-file',
raw_matx_path,
'--out-file',
signed_matx_path]

for wallet in signing_wallets:
cmd_builder.append('--signing-key-file')
cmd_builder.append(wallet.get_skey_path())

if use_testnet:
cmd_builder.append('--testnet-magic')
cmd_builder.append(str(testnet_magic))
else:
cmd_builder.append('--mainnet')

cmd = ' '.join(cmd_builder)

proc = subprocess.run(cmd, capture_output=True, text=True, shell=True)
Expand All @@ -215,15 +240,24 @@ def sign_tx(nft_dir, signing_wallets, raw_matx_path, force=False):
return signed_matx_path


def submit_transaction(signed_matx_path):
def submit_transaction(signed_matx_path, use_testnet=False, testnet_magic=TESTNET_MAGIC_DEFAULT):
"""Submit signed transaction"""

proc = subprocess.run([CARDANO_CLI,
'transaction',
'submit',
'--tx-file',
signed_matx_path,
'--mainnet'], capture_output=True, text=True)
cmd_builder = [CARDANO_CLI.replace(' ', '\ '),
'transaction',
'submit',
'--tx-file',
signed_matx_path]

if use_testnet:
cmd_builder.append('--testnet-magic')
cmd_builder.append(str(testnet_magic))
else:
cmd_builder.append('--mainnet')

cmd = ' '.join(cmd_builder)

proc = subprocess.run(cmd, capture_output=True, text=True, shell=True)

if proc.stderr != '':
logger.error(f'Error encountered when submitting transaction\n{proc.stderr}')
Expand All @@ -246,7 +280,7 @@ def get_return_address_from_utxo(utxo):
address = address.replace("<a href=/address/", "").replace("><", "")
return address
except requests.exceptions.RequestException as e:
return ""
return ''


def get_stake_key(address):
Expand All @@ -266,16 +300,25 @@ def get_cli_version():

if proc.stderr != '':
logger.error('Unable to get version')
return ""
return ''

return proc.stdout.split()[1]


def query_tip():
proc = subprocess.run([CARDANO_CLI,
'query',
'tip',
'--mainnet'], capture_output=True, text=True)
def query_tip(use_testnet=False, testnet_magic=TESTNET_MAGIC_DEFAULT):
cmd_builder = [CARDANO_CLI,
'query',
'tip']

if use_testnet:
cmd_builder.append('--testnet-magic')
cmd_builder.append(str(testnet_magic))
else:
cmd_builder.append('--mainnet')

cmd = ' '.join(cmd_builder)

proc = subprocess.run(cmd, capture_output=True, text=True, shell=True)

if proc.stderr != '':
logger.error('Unable to query tip information')
Expand Down
Loading

0 comments on commit 0c8c948

Please sign in to comment.