-
Notifications
You must be signed in to change notification settings - Fork 4
Tutorial | Transactions
A simple transaction can be posted to the network using only a few lines.
Transaction can be used inside with-statement:
#submit payment transaction to stellar
with stellar.new_transaction('SBEA5I3H4ZTOVKIEPDPVEVY4DKKFHBTYFPY2BN56NLAPAWTUKEGJMEW3', memo='test-pay') as t:
t.pay('GBWF6NTCPGBROJIPF54XXYRLTUGBDLLORPFDK4FGQQ3IRI4T5PHCGVXV', '42')
if t.is_success():
print t.result()
else:
print t.errors()
In above example, API will take care of retrieving current sequence for given account, incrementing it by one and then posting the transaction to network.
Transaction also can be submitted without with-statement:
t = stellar.new_transaction('SBEA5I3H4ZTOVKIEPDPVEVY4DKKFHBTYFPY2BN56NLAPAWTUKEGJMEW3', memo='test-pay')
.pay('GBWF6NTCPGBROJIPF54XXYRLTUGBDLLORPFDK4FGQQ3IRI4T5PHCGVXV', '42')
.submit()
if t.is_success():
print t.result()
else:
print t.errors()
If you want to specify multiple signers then, pass account-id in 'account' field and pass list of signers in 'signers' field.
account_id = 'GBWF6NTCPGBROJIPF54XXYRLTUGBDLLORPFDK4FGQQ3IRI4T5PHCGVXV'
signers = ['SBEA5I3H4ZTOVKIEPDPVEVY4DKKFHBTYFPY2BN56NLAPAWTUKEGJMEW3', 'SALCB22A3PL2JFI3GE62BM4S2TE64NJZP4GF2DBGPBC6QIUQ7GI7BRBN']
with stellar.new_transaction(account_id, signers, memo='test-pay') as t:
t.pay('GBWF6NTCPGBROJIPF54XXYRLTUGBDLLORPFDK4FGQQ3IRI4T5PHCGVXV', '42')
You can submit more than one operations in single transactions:
account_seed = "SCVLSUGYEAUC4MVWJORB63JBMY2CEX6ATTJ5MXTENGD3IELUQF4F6HUB"
to_account1 = "GDKGNWAE7N2KH6MLY5GGZUZWK775TH2M7YFIYEYRXDEGZ2OEATD3QKB4"
to_account2 = "GDZ4R34MNVITLNZ4KVKBEANJU3UZZFZZLOX7ZVU5AWI7FEL5A6JWDM24"
usd_issuer = "GDUWG5CZ6YJNWOPQB33DOKWVWSNHJAWPWOUNAEBVTM7QRJ66NGFYEFAJ"
trx = stellar.new_transaction(account_seed, memo='test-multi-pay')
.pay(to_account1, "10.01") #by default, payment is done in native asset
.pay(to_account2, "42.42", asset=('USD', usd_issuer)) #or you can specify asset
.submit()
or using with-statement:
with stellar.new_transaction(account_seed, memo='test-multi-pay') as trx:
trx.pay(to_account1, "10.01") #by default, payment is done in native asset
trx.pay(to_account2, "42.42", asset=('USD', usd_issuer)) #or you can specify asset
When you submit the transaction, Sirius API automatically retrieves current sequence, increments it by one and submits the transaction. In case you want to manually set sequence, say to create a complex contract which needs to be executed in particular order, you can pass sequence manually to API.
seq = int(stellar.account('GDKG__QKB4').fetch().sequence)
with stellar.new_transaction(account_seed, seq=seq+1) as t:
t.pay('GBWF6NTCPGBROJIPF54XXYRLTUGBDLLORPFDK4FGQQ3IRI4T5PHCGVXV', '42')
To submit text memo, you can just pass string message. For 'id', 'hash', 'ret_hash', type of memo, pass tuple, whose first element is type and second element is memo value.
with stellar.new_transaction(account_seed, memo=('id', 78127881)) as t:
t.pay('GBWF6NTCPGBROJIPF54XXYRLTUGBDLLORPFDK4FGQQ3IRI4T5PHCGVXV', '42')
Each operation in transaction costs at least a base fee (which is 100 stroops). So if you submit 5 different operations in a single transaction it will cost you at-least 500 stroops. You can optionally pass, different fee than 100 stroops/operation (i.e. 0.00001 XLM/operation).
In Sirius, every post API will cost 100 stroops, except set_options operation when submitted in a single transaction. If you set inflation destination and auth flags in a single transaction then it will only cost you 100 stroops.
You can also specify the custom fee for transaction you are submitting. If too many transactions are submitted to stellar network, nodes propose the transactions with the highest fees for the ledger’s transaction set. Transactions that aren’t included are held for a future ledger, when fewer transactions are waiting. So providing higher fee may cause your transaction to be preferred over other transactions and can get confirmed early.
#submit transaction with fee 500 stroops (instead of default 100 stroops)
with stellar.new_transaction(account_seed, memo='high priority', fee=500) as t:
t.pay('GBWF6NTCPGBROJIPF54XXYRLTUGBDLLORPFDK4FGQQ3IRI4T5PHCGVXV', '42')