def get_user_input(args): print( "Either the pay_address/amount(ADA) is empty. Will try to guide you. OK?" ) p = input( "We are going to use the default payment.addr. Is that ok (Y/N) ? ") if p == "Y" or p == "y": args.target = pc.content(FILES['payment']['addr']) else: c = ("Would you like to continue? Y/N") if c == "Y" or c == "y": get_user_input() else: sys.exit("Quitting!") a = CalcFee() args.amount = a.check_balance_stake() print( Fore.BLUE + f"\nWe are going to withdraw amount {args.amount} from stakepool to pledge address\n" ) p = input( "Are you ok with transferring the rewards to the payment address (Y/N)?" ) if p == "Y" or p == "y": pass else: sys.exit("Quitting!") return args
def draft_transaction(self): """ cardano-cli transaction build-raw \ --tx-in <TxHash>#<TxIx> \ --tx-out $(cat payment.addr)+0 \ --invalid-hereafter 0 \ --fee 0 \ --out-file tx.draft \ --certificate-file pool-deregistration.cert """ try: tx_in_array = build_multiple_tx_in_trans() payment_addr = pc.content(FILES['payment']) tx_out=f"{payment_addr}+0" command=["cardano-cli", "transaction", "build-raw", "--tx-out",tx_out, "--invalid-herafter",0, "--fee", 0, "--out-file", FILES['transaction']['draft'],'--certificate-file',FILES['pool']['deRegCert'], "--tx-in"]+tx_in_array s = subprocess.check_output(command) print(Fore.RED + f"\noutput of command:{command} is:{s}\n\n") except: logging.exception("Could not draft transaction") def main(self): self.generate_cert() self.draft_transaction() return self.calculate_min_fees()
def get_relay_params(): relay_configs = [] #fetch it from topology file fname = SETUP_CONFIGS['topology'] tp_config = json.loads(pc.content(fname))["Producers"] for relay in tp_config: relay_configs.append({"addr": relay["addr"], "port": relay['port']}) return relay_configs
def __init__(self, uuid, amount, policyid, coin_name, output_addr): self.amount = amount self.uuid = uuid self.pid = policyid self.coin_name = coin_name self.dest_addr = output_addr self.payment_addr = pc.content(FILES['payment']['addr']) self.utx0 = pc.get_payment_utx0(self.payment_addr) self.s = nft.Session(self.uuid)
def get_pooldeposit(): try: import json print("inside get pooldeposit") fcontent = pc.content(SETUP_CONFIGS['shelley-genesis']) p = json.loads(fcontent) t = p["protocolParams"]["poolDeposit"] return t except Exception as e: print(e)
def check_payment(self): """ ./cardano-cli query utxo --address `cat pay_bld.addr` --mary-era --mainnet """ try: payment_addr = pc.content(self.s.sdir(FILES['payment']['address'])) total_fund = pc.get_total_fund_in_utx0(payment_addr) print(f"Total funds in the address:{total_fund}") return {'addr':payment_addr, 'amount':total_fund} except: logging.exception("Could not check payment")
def calc_kes_period(self): try: import json fcontent = pc.content(SETUP_CONFIGS['shelley-genesis']) genesis = json.loads(fcontent) slotsPerKESPeriod = genesis['slotsPerKESPeriod'] qtip = int(pc.get_tip()) print(f"qtip:{qtip} and slotsperkes:{slotsPerKESPeriod}") kes_period = int(qtip / slotsPerKESPeriod) print(f'kes_period:{kes_period}') return kes_period except Exception: print("error in level argument", Exception)
def build(self, txArray, remaining_fund, min_fee, withdrawal_amount): """ cardano-cli transaction build-raw \ --tx-in a82f8d2a85cde39118a894306ad7a85ba40af221406064a56bdd9b3c61153527#1 \ --tx-out $(cat payment.addr)+743882981 \ --withdrawal $(cat stake.addr)+550000000 \ --invalid-hereafter 12345678 \ --fee 171089 \ --out-file withdraw_rewards.raw """ try: tx_in_array = [] for val in txArray: print(f"inside build_transaction: val:{val}") tx_in = val[0] + "#" + val[1] print(f"tx_in:{tx_in}") tx_in_array.append('--tx-in') tx_in_array.append(tx_in) payment_addr = pc.content(FILES['payment']['addr']) stake_addr = pc.content(FILES['stake']['addr']) future_tip = int(pc.get_tip()) + TIP_INCREMENT tx_out = "{paddr}+{rfund}".format(paddr=payment_addr, rfund=remaining_fund) command = [ 'cardano-cli', "transaction", "build-raw", "--mary-era", "--tx-out", tx_out, "--withdrawal", stake_addr + "+" + str(withdrawal_amount), "--invalid-hereafter", str(future_tip), "--fee", str(min_fee), "--out-file", FILES['transaction']['raw'] ] + tx_in_array s = subprocess.check_output(command) split_str = s.decode('UTF-8').split(" ") print(Fore.RED + f"\nOutput of command:{command} is {s}\n") except: logging.exception("Could not build transaction")
def calc_remaining_funds(self, from_address=None, transfer_amount=None): try: if from_address == None: from_address = pc.content(FILES['payment']['addr']) tx_array_from_address = pc.get_payment_utx0(from_address) min_fee = self.calculate_min_fees(tx_array_from_address, transfer_amount) print(f"minimum fees: {min_fee}") #remaining funds needs to be transferred to the owner (from_address) remaining_funds = int(pc.get_total_fund_in_utx0( from_address)) - min_fee + transfer_amount print(f"calculated remaining funds is:{remaining_funds}") return remaining_funds, min_fee except: logging.exception("Could not calculate remaining funds")
def check_balance_stake(self): """ cardano-cli query stake-address-info \ --mainnet \ --mary-era \ --address $(cat stake.addr) """ try: stake_addr = pc.content(FILES['stake']['addr']) command = [ "cardano-cli", "query", "stake-address-info", "--mainnet", "--mary-era", "--address", stake_addr ] s = subprocess.check_output(command) print(Fore.RED + f"output for command:{command} is {s}") s_package = json.loads(s) return s_package[0]["rewardAccountBalance"] except: logging.exception("Could not check balance of staking address")
def calculate_min_fees(self, tx_in, withdrawl_amount): """ cardano-cli transaction calculate-min-fee \ --tx-body-file withdraw_rewards.draft \ --tx-in-count 1 \ --tx-out-count 1 \ --witness-count 2 \ --byron-witness-count 0 \ --mainnet \ --protocol-params-file protocol.json """ print(f"Input to calculate min fees:{tx_in} {withdrawl_amount}") try: fname = FILES['transaction']['draft'] tx_in_count = len(tx_in) tx_out = pc.content(FILES['payment']['addr']) + "+" + "0" witness_count = 2 byron_wc = 0 #This is a prerequisite to calculation of min-fee self.draft_transaction(tx_in, tx_out) command = [ 'cardano-cli', 'transaction', 'calculate-min-fee', "--tx-body-file", fname, '--tx-in-count', str(tx_in_count), '--tx-out-count', str(1), "--witness-count", f"{witness_count}", '--byron-witness-count', str(byron_wc), '--mainnet', '--protocol-params-file', FILES['configs']['protocol'] ] s = subprocess.check_output(command, stderr=True, universal_newlines=True) print(Fore.RED + f"\nOutput of command:{command} output is:{s}\n\n") min_fee = float(s.split(" ")[0]) return min_fee except: logging.exception("Could not calculate minimum fees")
def build_raw_transaction(self): """ cardano-cli transaction build-raw \ --tx-in 9db6cf...#0 \ --tx-out $(cat payment.addr)+999999096457 \ --invalid-hereafter 860000 \ --fee 171309 \ --out-file tx.raw \ --certificate-file pool-deregistration.cert """ try: tx_in_array = build_multiple_tx_in_trans() payment_addr = pc.content(FILES['payment']) tx_out_param = payment_addr+self.remaining_fund command=["cardano-cli", "transaction", "build-raw", "--tx-out",tx_out_param, "--invalid-hereafter",self.epoch, "--fee",self._calculate_min_fees(), "--out-file", FILES['transaction']['raw'], '--certificate-file-pool', FILES['pool']['deRegCert'], "--tx-in"]+tx_in_array s = subprocess.check_output(command) print(Fore.RED + f"\noutput of command:{command} is:{s}\n\n") except: logging.exception("Could not build raw transaction")
def build_transaction(self): """ reconstruct: cardano-cli transaction build-raw \ --mary-era \ --tx-in <UTXO>#<TxIx> \ --tx-out $(cat payment.addr)+<CHANGE IN LOVELACE> \ --ttl <TTL> \ --fee <FEE> \ --out-file tx.raw \ --certificate-file pool-registration.cert \ --certificate-file delegation.cert """ try: PFILES = pc.FILES TTL = pc.get_ttl() tx_in_array = self._build_multiple_tx_in_trans() remaining_fund = calc_transaction_amount(self.createPool) fee = _calc_min_fee() payment_addr = pc.content(PFILES['payment']['addr']) tx_out = f"{payment_addr}+{remaining_fund}" print(f"remaining_fund:{remaining_fund}") command = [ CARDANO_CLI, "transaction", "build-raw", "--mary-era", "--tx-out", tx_out, "--ttl", str(TTL), "--fee", fee, "--out-file", FILES['pool']['transaction']['raw'], '--certificate-file', FILES['pool']['cert']['registration'], "--certificate-file", FILES['pool']['cert']['delegation'] ] + tx_in_array print(command) s = subprocess.check_output(command, stderr=True, universal_newlines=True) print(f"output for build transaction:{s}") except: print("Oops!", sys.exc_info()[0], "occurred in build transaction")
def draft_transaction(self, tx_in, tx_out): """ cardano-cli transaction build-raw \ --tx-in a82f8d2a85cde39118a894306ad7a85ba40af221406064a56bdd9b3c61153527#1 \ --tx-out $(cat payment.addr)+0 \ --withdrawal $(cat stake.addr)+0 \ --invalid-hereafter 0 \ --fee 0 \ --out-file withdraw_rewards.draft """ try: print(f"Inside draft transaction") stake_addr = pc.content(FILES['stake']['addr']) tx_in_array = self.create_tx_in(tx_in) command = [ "cardano-cli", "transaction", "build-raw", "--mary-era", "--tx-out", tx_out, "--withdrawal", stake_addr + "+" + '0', "--invalid-hereafter", '0', "--fee", '0', '--out-file', FILES['transaction']['draft'] ] + tx_in_array s = subprocess.check_output(command) print(Fore.RED + f"\noutput of command:{command} is:{s}\n\n") except: logging.exception("Could not draft transactions")
def __init__(self, latest=True, uuid=MUUID): self.payment_addr = pc.content(sdir(FILES['payment']['address'])) self.utx0 = pc.get_payment_utx0(self.payment_addr) self.s = Session(latest, uuid)
def calculate_native_token_count(self): try: payment_addr = pc.content(self.s.sdir(nft.FILES['payment']['address'])) utx0 = pc.get_payment_utx0(payment_addr) except: logging.exception("Unable to calculate native token count in payment address")
def get_pledge_params(): pledgeFile = os.path.join(os.getcwd(), FILES['pool']['pledge']) pcontent = pc.content(pledgeFile) pledgeConfig = json.loads(pcontent) return (pledgeConfig['amount'], pledgeConfig['cost'], pledgeConfig['margin'])
def __init__(self): self.payment_addr = pc.content(FILES['payment']['address']) self.utx0 = pc.get_payment_utx0(self.payment_addr)