def rewardUser(uid, amount): """ Reward the user by increasing his karma points via blockchain """ server = Server("https://horizon-testnet.stellar.org") source_key = Keypair.from_secret( "SA6HHDJ5KZKYFZVOOGHENIPVKV2HGKICN4RYQ2UZNZHZP7ZIYSQIQDCI") destination_id = "GA4QPSGBHK7RAJZBBEDCIKFPSYLG3O2UTBT2I56RN4B5KIQF2GFZKSMF" # 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: server.load_account(destination_id) 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. source_account = server.load_account(source_key.public_key) # Let's fetch base_fee from network base_fee = server.fetch_base_fee() # Start building the transaction. transaction = ( TransactionBuilder( source_account=source_account, network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE, base_fee=base_fee, ) # Because Stellar allows transaction in many currencies, you must specify the asset type. # Here we are sending Lumens. .append_payment_op(destination=destination_id, amount="10", asset_code="XLM") # A memo allows you to add your own metadata to a transaction. It's # optional and does not affect how Stellar treats the transaction. .add_text_memo("Test Transaction") # 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. transaction.sign(source_key) 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}") return None
def main(): # server = Server("https://horizon-testnet.stellar.org") server = Server("https://horizon.stellar.org") # source_key = Keypair.from_secret("SARJMVBEUC32ITKBIBYRQZUFEWKYHMR7PET5NDZH6KPREY3CPCUQSBJU") #test1 # destination_id = "GBMHN7DQ7MQTFPUPAYJR6HUGI2WX55LDTJ4AJNBQPIWMHNHSN34A2ENS" #test2 # test2 # Secret: SARJMVBEUC32ITKBIBYRQZUFEWKYHMR7PET5NDZH6KPREY3CPCUQSBJU # Public Key: GDTIZ3P6L33OZE3B437ZPX5KAS7APTGUMUTS34TXX6Z5BHD7IAABZDJZ source_key = Keypair.from_secret( "SARJMVBEUC32ITKBIBYRQZUFEWKYHMR7PET5NDZH6KPREY3CPCUQSBJU") # destination_id = "GC2QRLQCNCIK3FEIPEO7KP64PBOTFREGNCLMUG64QYOQFVQVARQCNPTV" destination_id = "GDKSN4MKI3VCX4ZN6P6WVQR64TGKPOHPKVBCO5ERJABMHI7GJHNAF6PX" #如果目的账户不存在, 则使用 append_create_account_op #如果目的账号存在, 则使用 append_payment_op # 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. is_acc_exits = False try: server.load_account(destination_id) is_acc_exits = True except NotFoundError: # If the account is not found, surface an error message for logging. # raise Exception("The destination account does not exist!") print(f"{destination_id} not found, will create it") is_acc_exits = False # If there was no error, load up-to-date information on your account. source_account = server.load_account(source_key.public_key) # Let's fetch base_fee from network base_fee = server.fetch_base_fee() # Start building the transaction. txbuilder = TransactionBuilder( source_account=source_account, # network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE, network_passphrase=Network.PUBLIC_NETWORK_PASSPHRASE, base_fee=base_fee, ) if is_acc_exits: txbuilder.append_payment_op(destination=destination_id, amount="1.6666", asset_code="XLM") else: txbuilder.append_create_account_op(destination=destination_id, starting_balance="1.001") txbuilder.add_text_memo("101108")\ .set_timeout(1000) transaction = txbuilder.build() # transaction = ( # TransactionBuilder( # source_account=source_account, # # network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE, # network_passphrase=Network.PUBLIC_NETWORK_PASSPHRASE, # base_fee=base_fee, # ) # # Because Stellar allows transaction in many currencies, you must specify the asset type. # # Here we are sending Lumens. # # .append_payment_op(destination=destination_id, amount="1.001", asset_code="XLM") # .append_create_account_op(destination=destination_id, starting_balance="1.001") # # A memo allows you to add your own metadata to a transaction. It's # # optional and does not affect how Stellar treats the transaction. # .add_text_memo("556850") # # 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. transaction.sign(source_key) print(f'xdr trx: {transaction.to_xdr()}') xdr = transaction.to_xdr() print(f'len : {len(xdr)}') print(type(server)) # rsp = server.submit_transaction(xdr) # print(rsp) 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}") pass
source_acc_id = accounts[0]['publicKey'] destination_acc_id = accounts[1]['publicKey'] try: server.load_account(destination_acc_id) except NotFoundError: # If the account is not found, surface an error message for logging. raise Exception("The destination account does not exist!") '''builder = Builder(secret=source_acc_key) bob_address = destination_acc_id builder.append_payment_op(bob_address, '10', 'XLM') builder.add_text_memo('For beers') # string length <= 28 bytes builder.sign() builder.submit()''' base_fee = server.fetch_base_fee() source_account = server.load_account(source_acc_id) # Start building the transaction. transaction = ( TransactionBuilder( source_account=source_account, network_passphrase='Test SDF Network ; September 2015', base_fee=base_fee, ) # Because Stellar allows transaction in many currencies, you must specify the asset type. # Here we are sending Lumens. .append_payment_op(destination=destination_acc_id, amount="220", asset_code="XLM") # A memo allows you to add your own metadata to a transaction. It's # optional and does not affect how Stellar treats the transaction.