Create a Custom Token on Stellar Network in Python

Posted By : Jatin Gupta | 31-Dec-2019

This blog is comprised of four python scripts which will help you to create a token on stellar network and send that token to some address on stellar.
 

The steps will be,

1. Host a stellar testnet on local system ( can connect to stellar testnet url also)

2. Create 2 
addresses on stellar network.

3.  Issue a asset ( token ) on stellar network.

4. Send the token to another address.
 

I) To create a custom tokenon

stellar network, first we have to setup the network on our system using below command.

docker run --rm -it -p "8000:8000" -v "/stellar:/opt/stellar" --name stellar stellar/quickstart --testnet

 

II) Tocreate address on stellar network and add some testnet funds in the address.

from stellar_base.keypair import Keypair

def gen_address():
    kp = Keypair.random()
    publickey = kp.address().decode()
    seed = kp.seed().decode()
    return publickey, seed

if __name__ == '__main__':
    address, password = gen_address()
    print(address, password)

This will give you an accounts public key ( wallet address ) and a private key ( wallet password )
Example output : GBT6XB6Q7PTUSVR5YESFNTSAQVCSATXF6WISFIK4BZ57MDABYDSMUYS3,  SCAZIRCS3QAXRP3HV7Z3JO7X3BCKNEEMDBGHV5PIXZC3CECUGY7IMFCP

 

Add this address in the below program and fund 10000 testnet XLM in the account
 


import requests

def fund_account(address):
    r = requests.get('https://horizon-testnet.stellar.org/friendbot?addr=' + address)
    return r.text

if __name__ == '__main__':
    ISSUER_ADDRESS = 'GBT6XB6Q7PTUSVR5YESFNTSAQVCSATXF6WISFIK4BZ57MDABYDSMUYS3'
    ISSUER_SEED = 'SCAZIRCS3QAXRP3HV7Z3JO7X3BCKNEEMDBGHV5PIXZC3CECUGY7IMFCP'
    result = fund_account(ISSUER_ADDRESS)
    print(result)

 

Check the balance in the address using the command 


curl http://192.168.9.69:8000/accounts/GBT6XB6Q7PTUSVR5YESFNTSAQVCSATXF6WISFIK4BZ57MDABYDSMUYS3

 

Now repeat the process again to create a new address and add 10000 XLM.

 

Once the address are generated and funded, we can create the token

from stellar_base.asset import Asset

def create_asset(code, issuer_address):
    asset = Asset(code, issuer_address)
    return asset

if __name__ == '__main__':
    ISSUER_ADDRESS = 'GBT6XB6Q7PTUSVR5YESFNTSAQVCSATXF6WISFIK4BZ57MDABYDSMUYS3'
    ISSUER_SEED = 'SCAZIRCS3QAXRP3HV7Z3JO7X3BCKNEEMDBGHV5PIXZC3CECUGY7IMFCP'
    ASSET_CODE = 'token_name'
    new_asset = create_asset(ASSET_CODE,ISSUER_ADDRESS)
    print(new_asset)

 

Now we have to issue this asset (token) on the stellar network

 

from stellar_base.keypair import Keypair
from stellar_base.asset import Asset
from stellar_base.builder import Builder

issuing_secret = 'SCAZIRCS3QAXRP3HV7Z3JO7X3BCKNEEMDBGHV5PIXZC3CECUGY7IMFCP'
issuing_public = Keypair.from_seed(issuing_secret).address().decode()

receiving_secret = 'SCAZIRCS3QAXRP3HV7Z3JO7X3BCKNEEMDBDHACHBQANWKCEHGFQAKQW'
receiving_public = Keypair.from_seed(receiving_secret).address().decode()

my_asset = Asset('token_name', issuing_public)

builder = Builder(
    receiving_secret,horizon_uri='http://localhost:8000', network='TESTNET').append_trust_op(
        destination=my_asset.issuer, code=my_asset.code)
builder.sign()
resp = builder.submit()
print(resp)

builder = Builder(
    issuing_secret,horizon_uri='http://localhost:8000', network='TESTNET').append_payment_op(
        destination=receiving_public,
        amount='10000',
        asset_code=my_asset.code,
        asset_issuer=my_asset.issuer)
builder.sign()
resp = builder.submit()
print(resp)

 

This will issue the token on stellar network and you can check by using the command to check balance, now other than XLM your 10000 token of yout token_name will also be present.
 

Do you want a web app that performs fast and has an interactive user interface? Python is the number one programming language to date. It supports the development of feature-rich apps including video streaming apps, 3D web apps, data science apps and more. Oodles Technologies is a Python development company with more than 10 years of experience. Contact our expert for more details!  

Related Tags

About Author

Author Image
Jatin Gupta

Jatin is a DevOps trainee. He ha deep interest in python and cloud technologies. He likes to read about science/history and fiction, listening to music and explore new places.

Request for Proposal

Name is required

Comment is required

Sending message..