0

Account0.py

from stellar_sdk.keypair import Keypair

pair = Keypair.random()
print(f"Secret: {pair.secret}")
# Secret:
print(f"Public Key: {pair.public_key}")

Account.py

import requests

public_key = "GD5WQZDL7TUYB3ZOYOVPZFDTWZUW54PIDFNTS5YYX5AXWD2ILI5EKPDX"
response = requests.get(f"https://friendbot.stellar.org?addr={public_key}")
if response.status_code == 200:
    print(f"SUCCESS! You have a new account :)\n{response.text}")
else:
    print(f"ERROR! Response: \n{response.text}")

Account3.py

from stellar_sdk.server import Server

server = Server("https://horizon-testnet.stellar.org")
public_key = "GD5WQZDL7TUYB3ZOYOVPZFDTWZUW54PIDFNTS5YYX5AXWD2ILI5EKPDX"
account = server.accounts().account_id(public_key).call()
for balance in account['balances']:
    print(f"Type: {balance['asset_type']}, Balance: {balance['balance']}")

StellaPayment.py

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

server = Server("https://horizon-testnet.stellar.org")
source_key = Keypair.from_secret("SDQTUR3UHH2P7AZAX3GMHEBTOAM74OECYNEHZEUJVDBTM7HSYHWIF24P")
destination_id = "GD5WQZDL7TUYB3ZOYOVPZFDTWZUW54PIDFNTS5YYX5AXWD2ILI5EKPDX"

# 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}")

It worked perfact with Test defalut setting from the website [https://developers.stellar.org/docs/tutorials/create-account/]

But I can not find how to work with the main-net!!! I've spending times to find the example script for the main net of the lumen to use it! and also tried to find the main net settings as well But was not possible to find any~!

Can I get some help to use these example scripts to work with main-net?

0

Creating an account takes 2 steps:

  1. generate a keypair (and save/make a note!)
  2. fund using already existing account

To create an account on mainnet, you need to use mainnet horizon server.

    from stellar_sdk import TransactionBuilder, Server, Network, Keypair

    def create_account():

    source = Keypair.from_secret(source_key)
    destination = Keypair.random()

    source_account = server.load_account(account_id=source.public_key)
    transaction = TransactionBuilder(
        source_account=source_account,
        network_passphrase = network_passphrase,
        base_fee=100) \
        .append_create_account_op(destination=destination.public_key, starting_balance=starting_balance) \
        .build()
    transaction.sign(source)
    response = server.submit_transaction(transaction)

    print("Transaction hash: {}".format(response["hash"]))
    print("New Keypair: \n\taccount id: {account_id}\n\tsecret seed: {secret_seed}".format(
        account_id=destination.public_key, secret_seed=destination.secret))

    if __name__ == '__main__':

    #server = Server(horizon_url = "https://horizon-testnet.stellar.org")
    #network_passphrase = Network.TESTNET_NETWORK_PASSPHRASE
    server = Server(horizon_url = "https://horizon.stellar.org")
    network_passphrase = Network.PUBLIC_NETWORK_PASSPHRASE

    source_key = "SECRET-KEY-OF-FUNDING-ACCOUNT"
    starting_balance = "1.0"

    create_account()

1
  • Thx :) It'd solved my issue!
    – SOO KIM
    Aug 25 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.