Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3.6
- # -*- coding: utf-8 -*-
- ################################################################################
- #
- ## send_stellar_asset.py v2021-06-18T20:12 prerelease or fu, hackbyte ([email protected])
- #
- # python send stellar asset stuff, barely working 20210618T201924
- # python send stellar asset stuff, finally working 20210620T163500
- # v20210622T131630 added options "--mainnet" and "--testnet", now mandatory, for reasons. ;)
- #
- # usage: send_stellar_asset.py [-h] [-d] [--testnet] [--mainnet]
- # [--secret SECRET] [--asset ASSET]
- # [--issuer ISSUER] [--amount AMOUNT]
- # [--destination DESTINATION] [--memo MEMO]
- #
- # send_stellar_asset.py commandline parameters.
- #
- # optional arguments:
- # -h, --help show this help message and exit
- # -d, --debug show heavy masses(!!) of debug output.
- # --testnet Enable TESTNET operation.
- # --mainnet Enable MAINNET operation.
- # --secret SECRET Secret to use
- # --asset ASSET Asset Code
- # --issuer ISSUER Issuer account id
- # --amount AMOUNT Amount to send
- # --destination DESTINATION
- # Destination account id
- # --memo MEMO Memo
- #
- #
- # Documentation:
- #
- # --mainnet Use mainnet to create a real payment.
- #
- # --testnet Use testnet for testnet purposes.
- #
- # --secret the secret to use to sign transactions (probably your distribution account ;))
- #
- # --asset Asset code of the asset you want to sent.
- #
- # --issuer Issuer ID of the asset you want to sent.
- #
- # --amount Amount of asset you want to send.
- #
- # --destination The destination wallet you want to send to.
- #
- # --memo Memo to add to the transaction.
- #
- ################################################################################
- import os, sys, inspect
- ################################################################################
- # barebone system stuff
- scriptpath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
- scriptname = os.path.basename(inspect.getfile(inspect.currentframe()))
- if 'include' == os.path.basename(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))):
- scriptpath = os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
- else:
- scriptpath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
- includepath = scriptpath + '/include'
- if os.path.isdir(includepath):
- sys.path.insert(0, includepath)
- ################################################################################
- import shutil, time, datetime, locale, logging, logging.handlers, argparse
- from pudb import set_trace
- ################################################################################
- log = logging.getLogger()
- log.setLevel(logging.INFO)
- logformatter_main = logging.Formatter(
- '%(asctime)s.%(msecs)03d ' +
- '%(levelname)-8s ' + '%(filename)-16s ' +
- '%(funcName)-12s ' + '%(lineno)04d ' +
- '%(message)s',
- '%Y%m%d%H%M%S')
- logformatter_syslog = logging.Formatter(
- '%(levelname)-8s ' +
- '%(filename)-16s ' + '%(funcName)-12s ' +
- '%(lineno)04d ' + '%(message)s')
- logcons = logging.StreamHandler()
- logcons.setLevel(logging.DEBUG)
- logcons.setFormatter(logformatter_main)
- log.addHandler(logcons)
- #logfile = logging.FileHandler(scriptpath+'/'+scriptname+'.log')
- #logfile.setLevel(logging.DEBUG)
- #logfile.setFormatter(logformatter_main)
- #log.addHandler(logfile)
- #logsys = logging.handlers.SysLogHandler(address = '/dev/log')
- #logsys.setLevel(logging.INFO)
- #logsys.setFormatter(logformatter_syslog)
- #log.addHandler(logsys)
- ################################################################################
- from stellar_sdk.asset import Asset
- from stellar_sdk.keypair import Keypair
- from stellar_sdk.network import Network
- from stellar_sdk.server import Server
- from stellar_sdk.transaction_builder import TransactionBuilder
- from stellar_sdk.exceptions import NotFoundError, BadResponseError, BadRequestError
- ################################################################################
- parser = argparse.ArgumentParser(description=scriptname + ' commandline parameters.')
- parser.add_argument(
- "-d", "--debug", dest="debug",
- action="store_true", default=False,
- help="show heavy masses(!!) of debug output.")
- parser.add_argument(
- "--testnet", dest="testnet",
- action="store_true", default=False,
- help="Enable TESTNET operation.")
- parser.add_argument(
- "--mainnet", dest="mainnet",
- action="store_true", default=False,
- help="Enable MAINNET operation.")
- parser.add_argument(
- "--secret", dest="secret",
- action="store", default="NONE",
- metavar="SECRET", help="Secret to use")
- parser.add_argument(
- "--asset", dest="asset",
- action="store", default="NONE",
- metavar="ASSET", help="Asset Code")
- parser.add_argument(
- "--issuer", dest="issuer",
- action="store", default="NONE",
- metavar="ISSUER", help="Issuer account id")
- parser.add_argument(
- "--amount", dest="amount",
- action="store", default="NONE",
- metavar="AMOUNT", help="Amount to send")
- parser.add_argument(
- "--destination", dest="destination",
- action="store", default="NONE",
- metavar="DESTINATION", help="Destination account id")
- parser.add_argument(
- "--memo", dest="memo",
- action="store", default="NONE",
- metavar="MEMO", help="Memo")
- cmdline = parser.parse_args()
- ################################################################################
- if cmdline.debug:
- log.setLevel(logging.DEBUG)
- ################################################################################
- log.debug("python version " + str(sys.version).replace('\n', ''))
- log.debug(scriptname+" START " + '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
- log.debug("cmdline.debug = " + str(cmdline.debug))
- ################################################################################
- if cmdline.testnet == False and cmdline.mainnet == False:
- print("No --testnet or --mainnet option given, you need to specify the network.")
- sys.exit(1)
- if cmdline.testnet == True and cmdline.mainnet == True:
- print("--testnet AND --mainnet option given, cannot work on both.")
- sys.exit(1)
- if cmdline.secret == 'NONE':
- print("Error, no secret given!")
- sys.exit(42)
- if cmdline.asset == 'NONE':
- print("Error, no asset given!")
- sys.exit(42)
- if cmdline.issuer == 'NONE':
- print("Error, no issuer given!")
- sys.exit(42)
- if cmdline.amount == 'NONE':
- print("Error, no amount given!")
- sys.exit(42)
- if cmdline.destination == 'NONE':
- print("Error, no destination given!")
- sys.exit(42)
- ##testnet server
- if cmdline.testnet == True:
- server = Server("https://horizon-testnet.stellar.org")
- log.info( "Used Server TESTNET : " + str(server.horizon_url))
- ##mainnet server
- if cmdline.mainnet == True:
- server = Server("https://horizon.stellar.org")
- log.info( "Used Server MAINNET : " + str(server.horizon_url))
- distribution_source_key = Keypair.from_secret(cmdline.secret)
- log.info( "Used Secret: : " + cmdline.secret)
- log.info( "Used asset: : " + cmdline.asset)
- log.info( "Used Issuer: : " + cmdline.issuer)
- log.info( "Used amount: : " + cmdline.amount)
- log.info( "Used Destination : " + cmdline.destination)
- log.debug("distribution source public: " + distribution_source_key.public_key)
- log.debug("distribution source secret: " + distribution_source_key.secret)
- ################################################################################
- ################################################################################
- ### # Create an object to represent the new asset
- our_asset = Asset(str(cmdline.asset), str(cmdline.issuer) )
- log.info("our asset: '" + str(our_asset) + "'")
- # First, check to make sure that the destination account exists.
- # You could skip this, but if the account does not exist, you will be charged
- # the transaction fee when the transaction fails.
- try:
- log.debug("trying 'server.load_account(cmdline.destination)'")
- server.load_account(cmdline.destination)
- except NotFoundError:
- # If the account is not found, surface an error message for logging.
- raise Exception("The destination account does not exist!")
- # If there was no error, load up-to-date information on your account.
- distribution_source_account = server.load_account(distribution_source_key.public_key)
- # Let's fetch base_fee from network
- base_fee = server.fetch_base_fee()
- # Start building the transaction.
- ##testnet server
- if cmdline.testnet == True:
- our_network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE
- ##mainnet server
- if cmdline.mainnet == True:
- our_network_passphrase=Network.PUBLIC_NETWORK_PASSPHRASE
- transaction = (
- TransactionBuilder(
- source_account=distribution_source_account,
- network_passphrase=our_network_passphrase,
- base_fee=base_fee,
- )
- .append_payment_op(
- destination=str(cmdline.destination),
- amount=str(cmdline.amount),
- asset_code=our_asset.code,
- asset_issuer=our_asset.issuer
- )
- .add_text_memo(str(cmdline.memo))
- # Wait a maximum of three minutes for the transaction
- .set_timeout(10)
- .build()
- )
- # Sign the transaction to prove you are actually the person sending it.
- log.debug("SIGNING TRANSACTION WITH: distribution_source_key.secret " + str(distribution_source_key.secret))
- transaction.sign(distribution_source_key.secret)
- try:
- # And finally, send it off to Stellar!
- response = server.submit_transaction(transaction)
- print(f"Response: {response}")
- except (BadRequestError, BadResponseError) as err:
- print(f"Something went wrong!\n{err}")
- log.debug("program end...")
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment