def _merge_account(self, address): server = self._get_horizon_server() account = server.load_account(address) # Increment the sequence number in case the unlock transaction was not processed before the load_account call # account.increment_sequence_number() balances = self._get_free_balances(address) base_fee = server.fetch_base_fee() transaction_builder = TransactionBuilder( source_account=account, network_passphrase=_NETWORK_PASSPHRASES[str(self.network)], base_fee=base_fee) for balance in balances.balances: if balance.is_native(): continue # Step 1: Transfer custom assets transaction_builder.append_payment_op( destination=self.address, amount=balance.balance, asset_code=balance.asset_code, asset_issuer=balance.asset_issuer, ) # Step 2: Delete trustlines transaction_builder.append_change_trust_op( asset_issuer=balance.asset_issuer, asset_code=balance.asset_code, limit="0") # Step 3: Merge account transaction_builder.append_account_merge_op(self.address) transaction_builder.set_timeout(30) transaction = transaction_builder.build() signer_kp = Keypair.from_secret(self.secret) transaction.sign(signer_kp) server.submit_transaction(transaction)
def add_balance(self, code, amount, accounts, dest, src, issuer): tb = TransactionBuilder( self.account_from_json(accounts[src.public_key]), base_fee=settings.MAX_TRANSACTION_FEE_STROOPS or settings.HORIZON_SERVER.fetch_base_fee(), network_passphrase="Test SDF Network ; September 2015", ) balance = self.get_balance(code, accounts[dest.public_key]) if not balance: print(f"\nCreating {code} trustline for {dest.public_key}") if settings.MAX_TRANSACTION_FEE_STROOPS: # halve the base_fee because there are 2 operations tb.base_fee = tb.base_fee // 2 tb.append_change_trust_op( asset_code=code, asset_issuer=issuer.public_key, source=dest.public_key ) payment_amount = amount elif Decimal(balance) < amount: print(f"\nReplenishing {code} balance to {amount} for {dest.public_key}") payment_amount = amount - Decimal(balance) else: print( "Destination account already has more than the amount " "specified, skipping\n" ) return print(f"Sending {code} payment of {payment_amount} to {dest.public_key}") tb.append_payment_op( destination=dest.public_key, amount=payment_amount, asset_code=code, asset_issuer=issuer.public_key, source=src.public_key, ) envelope = tb.set_timeout(30).build() if len(tb.operations) == 2: # add destination's signature if we're adding a trustline envelope.sign(dest) envelope.sign(src) try: self.server.submit_transaction(envelope) except BaseHorizonError as e: print( f"Failed to send {code} payment to {dest.public_key}. " f"Result codes: {e.extras.get('result_codes')}" ) else: print("Success!")
def add_balance(self, code, amount, accounts, dest, src, issuer): tb = TransactionBuilder( self.account_from_json(accounts[src.public_key]), base_fee=500, network_passphrase="Test SDF Network ; September 2015", ) payment_amount = None balance = self.get_balance(code, accounts[dest.public_key]) if not balance: print(f"\nCreating {code} trustline for {dest.public_key}") limit = amount if src == issuer else None tb.append_change_trust_op(code, issuer.public_key, limit, dest.public_key) payment_amount = amount elif Decimal(balance) < amount: print( f"\nReplenishing {code} balance to {amount} for {dest.public_key}" ) payment_amount = amount - Decimal(balance) else: print("Destination account already has more than the amount " "specified, skipping") return print( f"Sending {code} payment of {payment_amount} to {dest.public_key}") tb.append_payment_op(dest.public_key, payment_amount, code, issuer.public_key, src.public_key) envelope = tb.set_timeout(30).build() envelope.sign(src) if len(tb.operations) == 2: # We only need the destination's signature if we're adding a trustline # to the account envelope.sign(dest) try: self.server.submit_transaction(envelope) except BaseHorizonError as e: print(f"Failed to send {code} payment to {dest.public_key}. " f"Result codes: {e.extras.get('result_codes')}") else: print("Success!")
def transfer( self, destination_address, amount, asset="XLM", locked_until=None, memo_text=None, memo_hash=None, fund_transaction=True, from_address=None, ): """Transfer assets to another address :param destination_address: address of the destination. :type destination_address: str :param amount: amount, can be a floating point number with 7 numbers after the decimal point expressed as a Decimal or a string. :type amount: Union[str, Decimal] :param asset: asset to transfer (if none is specified the default 'XLM' is used), if you wish to specify an asset it should be in format 'assetcode:issuer'. Where issuer is the address of the issuer of the asset. :type asset: str :param locked_until: optional epoch timestamp indicating until when the tokens should be locked. :type locked_until: float :param text_memo: optional memo text to add to the transaction, a string encoded using either ASCII or UTF-8, up to 28-bytes long :type: Union[str, bytes] :param memo_hash: optional memo hash to add to the transaction, A 32 byte hash :type: Union[str, bytes] :param fund_transaction: use the threefoldfoundation transaction funding service :type: fund_transaction: bool :param from_address: Use a different address to send the tokens from, useful in multisig use cases. :type from_address: str """ issuer = None self._log_info(f"Sending {amount} {asset} to {destination_address}") if asset != "XLM": assetStr = asset.split(":") if len(assetStr) != 2: raise Exception("Wrong asset format") asset = assetStr[0] issuer = assetStr[1] if locked_until is not None: return self._transfer_locked_tokens( destination_address, amount, asset, issuer, locked_until, memo_text=memo_text, memo_hash=memo_hash, fund_transaction=fund_transaction, from_address=from_address) horizon_server = self._get_horizon_server() base_fee = horizon_server.fetch_base_fee() if from_address: source_account = horizon_server.load_account(from_address) else: source_account = self.load_account() transaction_builder = TransactionBuilder( source_account=source_account, network_passphrase=_NETWORK_PASSPHRASES[str(self.network)], base_fee=base_fee) transaction_builder.append_payment_op( destination=destination_address, amount=amount, asset_code=asset, asset_issuer=issuer, source=source_account.account_id, ) transaction_builder.set_timeout(30) if memo_text is not None: transaction_builder.add_text_memo(memo_text) if memo_hash is not None: transaction_builder.add_hash_memo(memo_hash) transaction = transaction_builder.build() transaction = transaction.to_xdr() if asset in _NETWORK_KNOWN_TRUSTS[str(self.network)]: if fund_transaction: transaction = self._fund_transaction(transaction=transaction) transaction = transaction["transaction_xdr"] transaction = TransactionEnvelope.from_xdr( transaction, _NETWORK_PASSPHRASES[str(self.network)]) my_keypair = Keypair.from_secret(self.secret) transaction.sign(my_keypair) try: response = horizon_server.submit_transaction(transaction) tx_hash = response["hash"] self._log_info("Transaction hash: {}".format(tx_hash)) return tx_hash except BadRequestError as e: result_codes = e.extras.get("result_codes") operations = result_codes.get("operations") if operations is not None: for op in operations: if op == "op_underfunded": raise e # if op_bad_auth is returned then we assume the transaction needs more signatures # so we return the transaction as xdr elif op == "op_bad_auth": self._log_info( "Transaction might need additional signatures in order to send" ) return transaction.to_xdr() raise e
def transfer(self, destination_address, amount, asset="XLM", locked_until=None, memo_text=None, memo_hash=None): """Transfer assets to another address :param destination_address: address of the destination. :type destination_address: str :param amount: amount, can be a floating point number with 7 numbers after the decimal point expressed as a string. :type amount: str :param asset: asset to transfer (if none is specified the default 'XLM' is used), if you wish to specify an asset it should be in format 'assetcode:issuer'. Where issuer is the address of the issuer of the asset. :type asset: str :param locked_until: optional epoch timestamp indicating until when the tokens should be locked. :type locked_until: float :param text_memo: optional memo text to add to the transaction, a string encoded using either ASCII or UTF-8, up to 28-bytes long :type: Union[str, bytes] :param memo_hash: optional memo hash to add to the transaction, A 32 byte hash :type: Union[str, bytes] """ issuer = None self._log_info("Sending {} {} to {}".format(amount, asset, destination_address)) if asset != "XLM": assetStr = asset.split(":") if len(assetStr) != 2: raise Exception("Wrong asset format") asset = assetStr[0] issuer = assetStr[1] if locked_until is not None: return self._transfer_locked_tokens(destination_address, amount, asset, issuer, locked_until) server = self._get_horizon_server() source_keypair = Keypair.from_secret(self.secret) source_public_key = source_keypair.public_key source_account = server.load_account(source_public_key) base_fee = server.fetch_base_fee() transaction_builder = TransactionBuilder( source_account=source_account, network_passphrase=_NETWORK_PASSPHRASES[str(self.network)], base_fee=base_fee) transaction_builder.append_payment_op(destination=destination_address, amount=str(amount), asset_code=asset, asset_issuer=issuer) transaction_builder.set_timeout(30) if memo_text is not None: transaction_builder.add_text_memo(memo_text) if memo_hash is not None: transaction_builder.add_hash_memo(memo_hash) transaction = transaction_builder.build() transaction.sign(source_keypair) try: response = server.submit_transaction(transaction) tx_hash = response["hash"] self._log_info("Transaction hash: {}".format(tx_hash)) return tx_hash except BadRequestError as e: self._log_debug(e) raise e
async def transfer(self, amount, address, memo_id: int = None, asset='XLM'): """ Transfer stellar lumens to the specified address. In the future sending other assets might also be possible. Normally a payment operation is used, but if the account is not created then an account create operation will be done. if you wish to send all of your balance then a merge account operation is used. :param amount: amount of lumens to send, in stroop (0.0000001 XLM) :param address: address to sent lumens to. Should be a normal encoded public key. :param memo_id: memo id for sending lumens to exchanges. :param asset: asset type. only XLM is currently supported. :return: Transaction hash """ balance = await self.get_balance() fee = self.provider.get_base_fee() # fee for one operation if balance['available'] - 1 < int( amount ) + fee: # stellar accounts need to hold a minimum of 1 XLM raise InsufficientFunds('Insufficient funds') self._logger.info( 'Creating Stellar Lumens payment with amount %s to address %s', amount, address) network = Network.PUBLIC_NETWORK_PASSPHRASE if not self.testnet else Network.TESTNET_NETWORK_PASSPHRASE tx_builder = TransactionBuilder( source_account=self.account, base_fee=self.provider.get_base_fee(), network_passphrase=network, ) amount_in_xlm = Decimal( amount / self.STROOP_IN_LUMEN ) # amount in xlm instead of stroop (0.0000001 xlm) if self.provider.check_account_created(address): tx_builder.append_payment_op(address, amount_in_xlm, asset) else: tx_builder.append_create_account_op(address, amount_in_xlm) if memo_id: tx_builder.add_id_memo(memo_id) tx = tx_builder.build() tx.sign(self.keypair) xdr_tx_envelope = tx.to_xdr() tx_hash = self.provider.submit_transaction(xdr_tx_envelope) tx_db = Transaction( hash=tx_hash, source_account=self.get_address(), operation_count=len(tx.transaction.operations), sequence_number=tx.transaction.sequence, succeeded=False, transaction_envelope=xdr_tx_envelope, is_pending=True, fee=tx.transaction.fee, ) self.database.insert_transaction(tx_db) return tx_hash