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
Beispiel #2
0
# Configure StellarSdk to talk to the horizon instance hosted by Stellar.org
# To use the live network, set the hostname to 'horizon.stellar.org'
server = Server(horizon_url="https://horizon-testnet.stellar.org")

# Keys for accounts to issue and receive the new asset
issuing_keypair = Keypair.from_secret(
    "SCBHQEGSNBTT4S7Y73YAF3M3JSVSTSNBGAVU5M4XVFGUF7664EUXQHFU")
issuing_public = issuing_keypair.public_key

distributor_keypair = Keypair.from_secret(
    "SB6MJ6M3BPJZUGFP2QCODUIKWQWF6AIN4Z6L3J6PWL3QGDW4L6YR3QIU")
distributor_public = distributor_keypair.public_key

# Transactions require a valid sequence number that is specific to this account.
# We can fetch the current sequence number for the source account from Horizon.
distributor_account = server.load_account(distributor_public)

# Create an object to represent the new asset
hello_asset = Asset("Hello", issuing_public)

# First, the receiving account must trust the asset
trust_transaction = (TransactionBuilder(
    source_account=distributor_account,
    network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
    base_fee=100,
).append_change_trust_op(asset_code=hello_asset.code,
                         asset_issuer=hello_asset.issuer).build())

trust_transaction.sign(distributor_keypair)
resp = server.submit_transaction(trust_transaction)
print(f"Change Trust Op Resp:\n{resp}")
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
from stellar_sdk.exceptions import NotFoundError, BadResponseError, BadRequestError
import json
from stellar_base.builder import Builder

server = Server("https://horizon-testnet.stellar.org")

fileName = 'accounts.json'
with open(fileName) as r:
    accounts = json.load(r)

source_acc_key = accounts[0]['secret']
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(