#Python

1 messages ยท Page 1 of 1 (latest)

bleak lily
#

The Stellar Python SDK is a library that helps you build and sign transactions, interact with the Stellar network, as well as deploy and invoke Soroban contracts. You can learn more about the project on its GitHub homepage: https://github.com/StellarCN/py-stellar-base

If you have any issues related to this, please feel free to raise them in this post.

knotty berry
#

Hello. I keep getting 5 is not a valid CryptoKeyType when trying to parse a fee bump transaction from an XDR like so FeeBumpTransactionEnvelope.from_xdr(xdr_string)

bleak lily
tropic zephyr
#

Hi Guys. I was just wondering if is it really necessary to learn Soroban or the Python Sdk should be enough?
I need to know if I can do anything Soroban can do with the SDK without actually learning Soroban or Rust?

bleak lily
tropic zephyr
#

@bleak lily
Ohk I get it, What I really wanted to find out is... Is it possible to write smart contracts in Python and achieve the same functionality of Soroban contracts?
I mean is there a way arround learning Soroban or I will have to?
Thanks.

bleak lily
bleak lily
#

To my knowledge, currently there is a lack of comprehensive tools in Python for generating wasm. I will continue to monitor the relevant developments, and if it becomes feasible in the future, I would be happy to add support for it.

tropic zephyr
#

Ohk, Thanks overcat... you're a star.

bleak lily
#

Hi ๐Ÿ˜€, the Stellar Python SDK now supports Soroban Preview 8! This latest update does contain some breaking changes, you can install the latest code and check out related examples through the soroban branch. If you have any questions, simply post them in here and we'll gladly assist. Thank you for your support.

noble oriole
bleak lily
noble oriole
#

Got it, thank you!!

hollow viper
#

Hello! A quick question, maybe you guys can help in saving dev time?

Suppose Iย plan to have a server that is up 24/7 to provide services that are linked to stellar. I also wish to have a on-ramp solution (fiat payment to acquire Stellar-based asset/NFTs on Stellar), I plan to use something like Wyre or MoonPay to do so

In such context : is it better to use django-polaris instead of bare-bones python-sdk? Or would django-polaris be overkill/ redundant?
I haven't studied polaris or payment APIs extensively, yet, but maybe someone here can share insight so I save time. Ty

bleak lily
next nebula
# tropic zephyr Hi Guys. I was just wondering if is it really necessary to learn Soroban or the ...

Using Django Polaris or the Anchor Platform will make your service available as an embedded webview inside Stellar wallets like Lobstr, so if thats the type of product you want then that makes a lot of sense.

If you don't feel like its necessary to have your service available inside wallets, you can just build a standalone website for purchasing the NFTs. Users will have to go directly to your website but it'll probably be easier to implement.

Really, the only Stellar-related actions you'll need to take are issuing the NFTs and sending them to their buyers' accounts, which is fairly simple and can be done using the Python SDK.

knotty berry
#

Is it possible to detect whether the transaction stream is dead in the python SDK?

bleak lily
knotty berry
#

Is there something wrong with the stream interface?

halcyon hawk
#

I've never used streams on python but it seems like it'd be faster than the loop to monitor the ledger?

knotty berry
#

That's what I thought...

bleak lily
# knotty berry That's what I thought...

So, are you experiencing any issues at the moment? In my experience, as long as the network quality is good, there shouldn't be any problems with consuming SSE data. However, if your network connection is particularly poor, the data pushed by SSE may be delayed, which could also happen with other SDKs. Furthermore, this delay may become longer as the pile of unconsumed messages increases.

Let's consider a scenario where you are currently consuming txs using SSE, but Horizon enters maintenance mode. In this case, there will be a large pile of unconsumed messages, and even when Horizon is restored, it may take you a long time to catch up. If the network quality is poor, you may never be able to catch up with the latest Ledger.

On the server side, especially when consuming txs for a long time, I prefer to use a polling mode. If there are 1k unconsumed txs, you can easily consume them quickly using multiple threads/processes.

#

Personally, I recommend using SSE on the client side.

knotty berry
#

Ah I see what you mean. I'm merely consuming txs for an account though

bleak lily
#

Are you only for one account? Or multiple accounts, but each account has enabled an SSE connection?

knotty berry
#

Just one account so basically one thread listening for txs coming to an account

#

I basically don't have a problem I'm just trying to see if there's a way I can write some "self healing logic" i.e. if the stream fails can I automatically restart it

bleak lily
knotty berry
#

Cool thanks

bleak lily
bleak lily
#

Python SDK now supports Soroban Preview 10. This update brings significant changes, so please refer to the code in the examples folder if needed.

I am still brainstorming on how to better support Soroban Auth, so there may still be changes to the API in the next few days.

bleak lily
bleak lily
left skiff
#

@bleak lily horray, I got auth to work.

However, I had to adjust your helper a bit.

bleak lily
left skiff
#

This is how it works for me.

#

So my (somewhat limited) understanding is:

#

Second of all, there might be more than one signer, so i made a list of the signer

#

For me this works now.

#

Here's a full example that you can c&p that uses (and requires) multiple signers against a contract:

bleak lily
#

Thank you, I will consider including it in the next release.

left skiff
#

if you remove one signer, you can now even find a reference to the error number if you dive into the diagnostics:

"data":{
                           "vec":[
                              {
                                 "string":"failing with contract error"
                              },
                              {
                                 "u32":4
                              }
                           ]
                        }
#

The __check_auth logic isn't considerd on prepare tx, so you just have to guess it and bump it.

#

However, since there is currently only on invoke per tx possible, we could change the function definition quite a bit, to simplify things, i guess.

#

how about a

soroban_server.prepare_authorized_transaction(envelope, signers, resources_multiplier=2)
#

if there are at some point multiple invokes in a tx, we could change signers to a list of lists, where the first list must equal the number of invocations.

bleak lily
left skiff
#

Yeah, no hurry. We now have something with which we can progress. Let's grab a beer at meridian!

torpid hill
#

Hey, I have an issue with using horizon streams & python stellar SDK. I get the 406 error, trying to access some endpoint, that marked as "Endpoint with Streaming".
I'm trying to search for all the accounts with the open trustlines for the asset N via this endpoint: https://horizon.stellar.org/accounts/?asset={code}:{issuer}
When I try to open it in the browser (i.g. performing GET request), that works fine, but when I try to use SSE to stream, it fails with 406.
May be I'm doing something wrong? Is there any other possibilities to check for the account ids with a provided trustline for the asset N?

bleak lily
# torpid hill Hey, I have an issue with using horizon streams & python stellar SDK. I get the ...

Sorry, this endpoint is not streamable. I will update the documentation later.

If you don't need real-time data, I recommend you to read this article: https://stellar.org/blog/developers/beyond-the-blockchain-unlocking-the-power-of-analytics-with-hubble

torpid hill
#

Hello, overcat, thank u for ur fast response! No, I don't need the real-time data, I need to obtain this info just once, but a bit afraid of using raw pagination.
Thank u for sharing this resource, I'd check it

bleak lily
#

Hi, 9.0.0b1 is out, is applicable to the upcoming stable Protocol 20 release.

teal oak
#

Hey @bleak lily culd you please assist me with how to get OHLCV data for example for stellar usdt dex?

#

thank you sir in advance for your response

teal oak
teal oak
#

@bleak lily is it possible to use also websocket, as CEX exchanges have? Applyibg perhaps for stream?

teal oak
#

Reason why i am asking is because would like to integrate it into our algorithmic trading platform we are building

#

Hanks for help

teal oak
bleak lily
native tundra
#

Hope this is ok @bleak lily that I share this on this Python thread ๐Ÿ™‚

I forgot to share here that I made a wrapper around the Python SDK to have higher level fonctions for Soroban. Basically I wanted a very simple UI to call contracts similarly to what we have with Rust. So there is a Python API as well as a CLI. Hope this is helpful for anyone ๐Ÿ˜… And feel free to submit issues/PR ๐Ÿค—

https://github.com/tupui/soroban-cli-python

GitHub

API and CLI for Soroban contracts in Python. Contribute to tupui/soroban-cli-python development by creating an account on GitHub.

native tundra
#

Question about the Events, do you plan on automagically converting the topics/values XDR responses? Similarly to what the CLI is doing with soroban events.
Like right now I am doing some poking around and can do this manually. Also there might be a quicker way to infer the types and structure

import stellar_sdk
import binascii

res = stellar_sdk.xdr.sc_val.SCVal.from_xdr('AAAADQAAACCa/N5K2SsdROdFe/OAy7D47x6z81F+57cvQ763w7wCrA==').bytes.sc_bytes
binascii.b2a_hex(res)

res = stellar_sdk.xdr.sc_val.SCVal.from_xdr('AAAADwAAAAZjb21taXQAAA==').sym.sc_symbol
res.decode()
bleak lily
native tundra
#

Awesome ๐Ÿ‘ let me know if you would like some help from me or help review

native tundra
# bleak lily I I will consider adding it. For now, maybe you can take a look at [this](https:...

Looking at what you linked. Seems like the only missing peace is to connect automatically the SCVal.type to a SCVal.from_[type] so that you can get a normal python type. Or am I missing something?

I thought at first pack/unpack were that but then realized these are more private methods. Also would you consider using refactoring str to repr? Would be more usefull than the normal repr and Python would default to it for str.

bleak lily
#

Currently, you need to first determine the type of your SCVal and then call the corresponding method to get the corresponding value.
Do you want to add an API like this?

def from(any_type_scval: stellar_xdr.SCVal) -> Union[str, bytes, int, stellar_xdr.SCVal]:
    pass

Although it can accept any type of SCVal, the returned value is still uncertain, and you need to determine the type yourself. I think this looks a bit unnatural.

I thought at first pack/unpack were that but then realized these are more private methods. Also would you consider using refactoring str to repr? Would be more usefull than the normal repr and Python would default to it for str.
Are you referring to the __str__ and __repl__ in the class? I plan to add __repl__ in the next major version.

native tundra
#

Yes I think the API above would be perfectly fine as a helper to convert a SCVal to it's Python type. Though I would see this more as a class method (and change the name then to something like to_python_type or dump or something else a bit more descriptive). i.e. something similar to what you have in Pandas or NumPy with tolist (but with an underscore).

As it is, it's a bit complicated for users because the names are a bit different than the type.

The context is that from an event I get two topics and a value.

topic_a = 'AAAADwAAAAZjb21taXQAAA=='
topic_b = 'AAAADQAAACCa/N5K2SsdROdFe/OAy7D47x6z81F+57cvQ763w7wCrA=='
value = 'AAAADQAAABTF7kUFjfiSWLEGkJ0tlC0w0SMASA=='

So I do

topic_a_scval = stellar_sdk.xdr.SCVal.from_xdr(topic_a)
topic_a_str = stellar_sdk.scval.from_symbol(topic_a_scval)
# commit

topic_b_scval = stellar_sdk.xdr.SCVal.from_xdr(topic_b)
topic_b_bytes = stellar_sdk.scval.from_bytes(topic_b_scval)
binascii.b2a_hex(topic_b_bytes)
# b'9afcde4ad92b1d44e7457bf380cbb0f8ef1eb3f3517ee7b72f43beb7c3bc02ac'

value_scval = stellar_sdk.xdr.SCVal.from_xdr(value)
value_bytes = stellar_sdk.scval.from_bytes(value_scval)
binascii.b2a_hex(value_bytes)
# b'c5ee45058df89258b106909d2d942d30d1230048'

So yeah for now I can do this

topic_a_scval = stellar_sdk.xdr.SCVal.from_xdr(topic_a)
type_name = topic_a_scval.type.name.split('_')[1].lower()
getattr(stellar_sdk.scval, f"from_{type_name}")(topic_a_scval)
# commit
# the byte to hex part is harder to do automagically I guess

But that's a bit hard on users IMO. I am happy to make a PR to propose this function if you would like.

For __str__, yes I would want to have __repr__. If you have __repr__ Python default to it for __str__ , this is why I was suggesting to just change str to repr as a "quick fix". Happy to read that you will add that to the next version! ๐Ÿ˜ƒ

bleak lily
native tundra
# bleak lily Hi <@818786635482136576>, I have added a draft [here](https://github.com/Stellar...

Nice yes that would do it ๐Ÿ™Œ Just for ref, on my side I have this for now. Which is only working for a limited number of types as some types have slightly different names than their from_....

def parse_xdr(xdr):
    scval = stellar_sdk.xdr.SCVal.from_xdr(xdr)
    type_scval = scval.type.name.split("_")[1].lower()
    parsed_scval = getattr(stellar_sdk.scval, f"from_{type_scval}")(scval)

    if isinstance(parsed_scval, bytes):
        parsed_scval = binascii.b2a_hex(parsed_scval)
    return parsed_scval
bright eagle
#

Hello everybody, does anyone now how to lock an asset's supply forever? I already tried a couple of things but non of them worked.

bleak lily
bright eagle
#

The problem is, I don't know how to do that.

bright eagle
#

Since I do not know python this is pretty confusing to me, is there any way I can find a code and just plug in all the information?

#

Or is there any developer free to help me with this? Sounds like it is not that complicated

native tundra
bright eagle
#

Alright, I will try that. Thank you a lot

young girder
silver thistle
bleak lily
silver thistle
# bleak lily Can you provide a ledgerEntry that you have obtained?

i use this contract id : CCW67TSZV3SSS2HXMBQ5JFGCKJNXKZM7UQUWUZPUTHXSTZLEO7SJMI75
to call getLedgerEntries, and get this xdr:
AAAABgAAAAAAAAABre/OWa7lKWj3YGHUlMJSW3Vln6QpamX0me8p5WR35JYAAAAUAAAAAQAAABMAAAABAAAAAQAAAAMAAAAPAAAACE1FVEFEQVRBAAAAEQAAAAEAAAADAAAADwAAAAdkZWNpbWFsAAAAAAMAAAAHAAAADwAAAARuYW1lAAAADgAAAD1VU0RDOkdBNVpTRUpZQjM3SlJDNUFWQ0lBNU1PUDRSSFRNMzM1WDJLR1gzSUhPSkFQUDVSRTM0SzRLWlZOAAAAAAAADwAAAAZzeW1ib2wAAAAAAA4AAAAEVVNEQwAAABAAAAABAAAAAQAAAA8AAAAFQWRtaW4AAAAAAAASAAAAAAAAAAA7mRE4Dv6Yi6CokA6xz+RPNm99vpRr7QdyQPf2JN8VxQAAABAAAAABAAAAAQAAAA8AAAAJQXNzZXRJbmZvAAAAAAAAEAAAAAEAAAACAAAADwAAAAlBbHBoYU51bTQAAAAAAAARAAAAAQAAAAIAAAAPAAAACmFzc2V0X2NvZGUAAAAAAA4AAAAEVVNEQwAAAA8AAAAGaXNzdWVyAAAAAAANAAAAIDuZETgO/piLoKiQDrHP5E82b32+lGvtB3JA9/Yk3xXF

bleak lily
#

Wait a moment, let me take a look.

bleak lily
silver thistle
bleak lily
#

The most crucial step is how you obtain the ledger entry.

silver thistle
bleak lily
#

The above issue has been resolved in a private conversation.

pine trench
#

Hello @bleak lily how long did it take to get this library into the first production readiness phase? Like how much time elapsed when you started on this framework and when it went to a position where it can really be used in actual projects.

bleak lily
pine trench
bleak lily
earnest lintel
#

Hi @bleak lily , I've had some trouble installing stellar-sdk recently on windows recently.
pip install results in ModuleNotFoundError: No module named 'distutils.msvc9compiler' and Exception: This CFFI feature requires distutils. Please install the distutils or setuptools package.
Those packages are installed though.
This is with python 3.10 and 3.13

bleak lily
earnest lintel
#

I'm trying to figure where in the pyproject.toml might be a reference to setuptools

bleak lily
#

@earnest lintel , would you mind running pip install PyNaCl==1.4.0 and pip install PyNaCl==1.5.0?

earnest lintel
#

pip install PyNaCl==1.4.0 also gives a distutil error

#

and so does pip install PyNaCl==1.5.0 :/

bleak lily
#

Is your machine x86-64 architecture?

earnest lintel
#

Yes

#

I had the problem first on another PC, gave up there thinking I had my install messed up maybe.
But now also on my own desktop

bleak lily
#

Wait a moment, let me try it on a Windows device. Theoretically, it should download the wheels for PyNacl, but it is not; instead, it's trying to build locally.

#

Although it works well on my Windows CI ๐Ÿ˜ฆ

earnest lintel
#

It may well be a <me> issue ๐Ÿ˜ฎ

bleak lily
#

@earnest lintel, It looks like it downloaded the correct wheels for me. Can you run pip install --upgrade setuptools wheel first and then try again?

#

BTW, if you haven't used a virtual environment, please create one and activate it. This can make your environment less chaotic.

earnest lintel
#

Successfully installed wheel-0.44.0 but still ```Collecting stellar-sdk
Using cached stellar_sdk-11.1.0-py3-none-any.whl.metadata (6.8 kB)
Collecting PyNaCl<2.0.0,>=1.4.0 (from stellar-sdk)
Using cached PyNaCl-1.5.0.tar.gz (3.4 MB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... error
error: subprocess-exited-with-error

โ”œรน Preparing metadata (pyproject.toml) did not run successfully.
ร”รถรฉ exit code: 1
ร”รฒโ–‘ร”รถร‡> [46 lines of output]````

bleak lily
bleak lily
earnest lintel
#

Yes that worked

bleak lily
#

Emmmmm, I don't know why it didn't default to getting wheel; now you can try installing stellar-sdk.

earnest lintel
#

Thanks for your help, I'll spend some time with these steps again tonight

#

I have to go now

#

Ok it's not a stellar-sdk issue. Now I'm getting the error with modules that installed OK just ago

bleak lily
#

@earnest lintel, Are you using it to write FCM code? If so, I can provide you with some templates If you need it ๐Ÿ™‚

earnest lintel
#

Yes I am at the moment

#

But also for generic stellar things. Not much Soroban recently, well not after phase 0

native tundra
bleak lily
earnest lintel
#

That's interesting

native tundra
#

Will definitely try it out and see how that plays with my wrapper!!

#

Nice the invoke, a bit like the function I have ๐Ÿ™Œ

bleak lily
bleak lily
next nebula
#

Hey @bleak lily, I've noticed you're building a lot of features present in the JS SDK into the Python SDK (bindings, AssembledTransaction, etc.). Do you have a list of features you're aiming to release with the stable v12 version?

native tundra
bleak lily
native tundra
#

Nice ๐Ÿ˜Š are you planning to make a full UI for the SDK?

bleak lily
bleak lily
west violet
bleak lily
safe mesa
#

hi i'm still new to stellar. i'm currently using python sdk. is there any utils to format balance just like in evm?

whole sail
#

Does the python SDK have any utilities for given a wasm handing back the ScSpecEntry XDR for the contract spec embedded within?

whole sail
#

I was thinking more in code, when scripting, if I've got a wasm file and want to extract out the contract spec XDR. So not so much a CLI.

#

This isn't something in all the SDKs, was just curious if it existed in the python sdk.

#

In the JS SDK there is contract.Spec.fromWasm(wasm).

#

In the RS SDK there is soroban_spec::read::from_wasm(wasm).

bleak lily
# whole sail I was thinking more in code, when scripting, if I've got a wasm file and want to...

I think we don't have to care about the CLI part; it can be used as a library. It returns the raw XDR object, which might be a lot more crude than the JavaScript implementation.

# pip install stellar-contract-bindings
from stellar_contract_bindings import utils

spec = utils.get_specs_by_wasm_hash(bytes.fromhex("9d27138ce9a41f76df25ae1763c3aa8e673636c511c128a91f2f12ef40771271"), "https://soroban-testnet.stellar.org/")
for entry in spec:
    print(entry)
whole sail
#

I'm looking for a function that does 'get specs by wasm file' rather than hash.

earnest lintel
#

I've been trying to extract the auth entries from a prepared transaction with the SDK.
So far, I have gotten it to work via below method. But I wonder if anyone knows of a more elegant way?

<make an assembled transaction>

# Sign the auth entries
assembled_tx.sign_auth_entries(kp)

# Extract the auth entries from the transaon
auth_entries = assembled_tx.built_transaction.transaction.operations[0]._to_operation_body().invoke_host_function_op.auth

# Iterate them into a list
signed_auth_xdr = [auth_entry.to_xdr() for auth_entry in auth_entries]

bleak lily
earnest lintel
#

That results in an <class 'stellar_sdk.operation.invoke_host_function.InvokeHostFunction'> object

bleak lily
#

Sorry, just a moment, I got confused.

earnest lintel
#

For reference/background, I'm looking to create XDR strings to send to OZ Relayer / Launchtube

bleak lily
#

Okay, I checked the source code, and your current method is already the simplest one.
I think your use case should be quite common, and I will consider adding a function to conveniently extra it.

earnest lintel
#

Cool ๐Ÿ˜„

#

Thank you

#

Meanwhile, I do have an example for this method, if anyone want it

lament vine
#

Hey everyone. Iโ€™m currently using the Python SDK (v13.2.1) and preparing for Protocol 26
Do you guys know by any chance if there is a planned SDK release explicitly supporting Protocol 26, or should we just rely on the latest version as updates roll out?
Thanks!