Code Monkey home page Code Monkey logo

starcoin-sdk-python's Introduction

starcoin-sdk-python

Document

The document of starcoin sdk for python: documents site.

Pypi package

https://pypi.org/project/starcoin-sdk-python/

Usage

from starcoin import starcoin_types as types
from starcoin import starcoin_stdlib as stdlib
from starcoin import serde_types as st
from starcoin.sdk import (utils, client, local_account, auth_key)
from starcoin.sdk.receipt_identifier import ReceiptIdentifier
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
    Ed25519PrivateKey, Ed25519PublicKey)
	
# create a client for connecting starcoin node
cli = client.Client("https://barnard-seed.starcoin.org")

# get the node info
print(cli.node_info())

# get the resource of account
account_resource = cli.state_get(
        '0x00000000000000000000000000000001/1/0x00000000000000000000000000000001::Account::Account')
print(account_resource)

# create a account with your private key
private_key = Ed25519PrivateKey.from_private_bytes(bytes.fromhex(
"e424e16db235e3f3b9ef2475516c51d4c15aa5287ceb364213698bd551eab4f2"))
account = local_account.LocalAccount(private_key)

# define a transfer function
def transfer(cli: client.Client, sender: local_account.LocalAccount, receipt: str, amount: st.uint128):
    seq_num = cli.get_account_sequence(
        "0x"+sender.account_address.bcs_serialize().hex())
    receipt = ReceiptIdentifier.decode(receipt)
    script = stdlib.encode_peer_to_peer_v2_script_function(
        token_type=utils.currency_code("STC"),
        payee=receipt.account_address,
        amount=amount,
    )
    node_info = cli.node_info()
    now_seconds = int(node_info.get('now_seconds'))
    # expired after 12 hours
    expiration_timestamp_secs = now_seconds + 43200
    raw_txn = types.RawUserTransaction(
        sender=sender.account_address,
        sequence_number=seq_num,
        payload=script,
        max_gas_amount=10000000,
        gas_unit_price=1,
        gas_token_code="0x1::STC::STC",
        expiration_timestamp_secs=expiration_timestamp_secs,
        chain_id=types.ChainId(st.uint8(251)),
    )
    txn = sender.sign(raw_txn)
    print(cli.submit(txn))

More examples see examples

starcoin-sdk-python's People

Contributors

jolestar avatar sanlee42 avatar wk3368 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

gxke haobtc junasir

starcoin-sdk-python's Issues

Parse payload error in transaction 0x0b93a8f7ac38c986e9c484fbc2140aeae626a8fa12549591d9c83390d03a5f95

image

Traceback (most recent call last): File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1483, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/bobong/Codes/block-chain/starcoin/swap/remedy_star_py/main.py", line 109, in <module> crawl_all_user_options(begin_block_num=4519144, end_block_num=4520500) File "/Users/bobong/Codes/block-chain/starcoin/swap/remedy_star_py/main.py", line 96, in crawl_all_user_options users = get_transactions_between(begin_block_num, end_block_num) File "/Users/bobong/Codes/block-chain/starcoin/swap/remedy_star_py/main.py", line 68, in get_transactions_between users = parse_blocks(start_num, end_num) File "/Users/bobong/Codes/block-chain/starcoin/swap/remedy_star_py/main.py", line 57, in parse_blocks sender, opt = parser_txn(txn, i) File "/Users/bobong/Codes/block-chain/starcoin/swap/remedy_star_py/main.py", line 37, in parser_txn payload = starcoin_types.TransactionPayload.bcs_deserialize(payload) File "/Users/bobong/miniforge3/envs/remedy_star/lib/python3.10/site-packages/starcoin/starcoin_types/__init__.py", line 902, in bcs_deserialize v, buffer = bcs.deserialize(input, TransactionPayload) File "/Users/bobong/miniforge3/envs/remedy_star/lib/python3.10/site-packages/starcoin/bcs/__init__.py", line 117, in deserialize value = deserializer.deserialize_any(obj_type) File "/Users/bobong/miniforge3/envs/remedy_star/lib/python3.10/site-packages/starcoin/serde_binary/__init__.py", line 415, in deserialize_any return self.deserialize_any(new_type) File "/Users/bobong/miniforge3/envs/remedy_star/lib/python3.10/site-packages/starcoin/serde_binary/__init__.py", line 402, in deserialize_any field_value = self.deserialize_any(field_type) File "/Users/bobong/miniforge3/envs/remedy_star/lib/python3.10/site-packages/starcoin/serde_binary/__init__.py", line 402, in deserialize_any field_value = self.deserialize_any(field_type) File "/Users/bobong/miniforge3/envs/remedy_star/lib/python3.10/site-packages/starcoin/serde_binary/__init__.py", line 402, in deserialize_any field_value = self.deserialize_any(field_type) File "/Users/bobong/miniforge3/envs/remedy_star/lib/python3.10/site-packages/starcoin/serde_binary/__init__.py", line 342, in deserialize_any length = self.deserialize_len() File "/Users/bobong/miniforge3/envs/remedy_star/lib/python3.10/site-packages/starcoin/bcs/__init__.py", line 89, in deserialize_len value = self.deserialize_uleb128_as_u32() File "/Users/bobong/miniforge3/envs/remedy_star/lib/python3.10/site-packages/starcoin/bcs/__init__.py", line 70, in deserialize_uleb128_as_u32 byte = int.from_bytes(self.read(1), "little", signed=False) File "/Users/bobong/miniforge3/envs/remedy_star/lib/python3.10/site-packages/starcoin/serde_binary/__init__.py", line 235, in read raise st.DeserializationError("Input is too short") starcoin.serde_types.DeserializationError: Input is too short

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.