Skip to main content

sdk for asimov chain

Project description

py-asimov

Introduction

This document describes high level Asimov Python API.

Note that these high level APIs are a wrapper of Asimov Restful API to provide a more easy to use interfaces. A python developer can choose to use restful api directly if willing to.

To use Asimov Python API, one must have python3.7+ installed. With this library, you can do contract test/deploy script. However, you are free to utilize these APIs for other developing purposes.

We extract following high level modules in python API:

  • Template
  • Contract
  • Transaction

all of which are built on top of the RPC and Setting modules.

Read more in the documentation on ReadTheDocs. View the change log

Quickstart

pip install py-asimov

Node Object

Node object is a virtual asimov node. It is almost equivalent to restful api in python language. You can initialize a node object with rpc server url and private key.

from asimov import Node
node = Node("http://localhost:8545", "0x3197eb7cd1538b26cf2398caa5986f1744934fb43e0b7f2a71c947bba0da3b48")
# change rpc server url and private key when ever you want
node.set_rpc_server("http://you-rpc-server")
node.set_private_key("0x8dd839d5b978f113047ac9d08035ebf1b58ecd5ae6e92049f411e9d659be31f6")
node.call(method, arguments)
  • method rpc method to call.
  • arguments arguments to call the given rpc method.

This function execute an rpc call directly agianst the set rpc server. More information on rpc methods, please refer to Asimov Restful API.

node.send(address, asset_value, asset_type, tx_fee_value, tx_fee_type)
  • address the recepient address.
  • assetvalue the asset value to transfer.
  • asset_type (OPTIONAL) asset type to transfer. If not set, it defaults to 0 which represents Asim.
  • tx_fee_value (OPTIONAL) transaction fee asset value. If not set, it defaults to 0.
  • tx_fee_type (OPTIONAL) transaction fee asset type. If not set, it defaults to 0 which represents Asim.

This function sends a normal transaction on asimov blockchain and returns the transaction object.

Asimov Smart Contract Object

Setting module provides methods to config default value for common settings. There are following functions in this module.

from asimov import AsimovSolc
AsimovSolc.set_solidity_compiler(compiler)
  • compiler solidity compiler bin file location.

This function sets the default solidity complier. You can install asimov compiler refer to Asimov Compiler

AsimovSolc.compile(source)
  • source contract source file path.

Template Object

As introduced in Contract Development Guide, developers need to submit templates before deploying contracts. The precedure goes like this:

Contract Source File --submit--> Asimov Template --deploy--> Contract Instance

for the design purpose of asimov template, please read Tech White Paper.

How to initialize a template module:

from asimov import Template
Template = Template(node)
  • node Node object.

There are two key functions of template module.

submit(source, template_name, chosen_contract, tx_fee_value, tx_fee_type)
  • source contract source file path.
  • template_name template name.
  • chosen_contract there could be more than 1 contract instance in the source file, you need to choose one when submitting a template. Input the contract name of the chosen contact.
  • tx_fee_value (OPTIONAL) transaction fee asset value. If not set, it defaults to 0.
  • tx_fee_type (OPTIONAL) transaction fee asset type. If not set, it defaults to 0 which represents Asim.

This function submits a new template to asimov blockchain and returns the template id of the newly submitted template. We use the corresponding transaction id as the template id.

tx, contract_address = deploy_contract(template_id, constructor_arguments, asset_value, asset_type, tx_fee_value, tx_fee_type)
  • template_id template id.
  • constructor_arguments arguments for the constructor.
  • asset_value (OPTIONAL) asset value to transfer to the contract. If not set, it defaults to 0.
  • asset_type (OPTIONAL) asset type to transfer to the contract. If not set, it defaults to 0 which represents Asim.
  • tx_fee_value (OPTIONAL) transaction fee asset value. If not set, it defaults to 0.
  • tx_fee_type (OPTIONAL) transaction fee asset type. If not set, it defaults to 0 which represents Asim.

This function deploys a contract based on a given template id and returns the address of the newly deployed contract on asimov blockchain and transaction object.

Contract Object

Web dapp development usually start with deployed contracts on Asimov chain. It can be done by either using Web IDE or Cmd Tool or the above Template Module API.

How to initialize a Contract module:

from asimov import Contract
contract = Contract(node, contract_address)
  • node Node object.
  • contract_address contract address.

or even more simply

from asimov import AsimovSolc, Contract
compiled_contract = AsimovSolc.compile("./contracts/tutorial.sol")['Tutorial']
contract = Contract(node, c=compiled_contract)
  • c complied contract object

There are four key functions of contract module.

contract.execute(method, arguments, asset_value, asset_type, tx_fee_value, tx_fee_type)
  • method method to call.
  • arguments arguments for the method.
  • asset_value (OPTIONAL) asset value to transfer to the contract. If not set, it defaults to 0.
  • asset_type (OPTIONAL) asset type to transfer to the contract. If not set, it defaults to 0 which represents Asim.
  • tx_fee_value (OPTIONAL) transaction fee asset value. If not set, it defaults to 0.
  • tx_fee_type (OPTIONAL) transaction fee asset type. If not set, it defaults to 0 which represents Asim.

This function sends a transaction to execute a method in the contract and returns the transaction object.

contract.vote(method, argument, asset_value, asset_type, tx_fee_value, tx_fee_type)
  • method method to call.
  • arguments arguments for the method.
  • votevalue asset value to vote. If you want to vote all, set to 0.
  • asset_type (OPTIONAL) asset type to transfer to the contract. If not set, it defaults to 0 which represents Asim.
  • tx_fee_value (OPTIONAL) transaction fee asset value. If not set, it defaults to 0.
  • tx_fee_type (OPTIONAL) transaction fee asset type. If not set, it defaults to 0 which represents Asim.

This function sends a transaction to vote on a contract and returns the transaction object. Note the vote function may not vote the accurate value you set in the function since the policy is to consume whole vote value of chosen utxos.

contract.read(method, arguments)
  • method method to call.
  • arguments arguments for the method.

This function calls a view method in the contract and returns the execution result.

contract.fetch(tx_id)
  • tx_id transaction id.

This function fetchs the logs during the contract execution.

Transaction Object

It is relatively complicated to send a transaction and check transaction status on asimov blockchain using restful api direclty. As a result, we also provide a Transaction object to deal with these actions.

There is only one key functions of transaction object.

tx_object.check()

If the transaction is a normal transaction, this function checks wheter a transaction is confirmed on chain and returns 1/0. If the transaction is a contract transaction, this function checks wheter a transaction is confirmed on chain and return contract execution status.


Developer Setup

If you would like to hack on py-asimov, please follow these steps:

  • Testing
  • Pull Requests
  • Code Style
  • Documentation

Development Environment Setup

You can set up your dev environment with:

git clone git@github.com:ethereum/eth-account.git
cd pyasimov
pyenv virtualenv 3.7.3 env-asimov
pyenv activate env-asimov
pip install -e .[dev]

# If you're using zsh you need to escape square brackets: 
pip install -e .\[extra\]

Testing Setup

During development, you might like to have tests run on every file save.

Show flake8 errors on file change:

# Test flake8
when-changed -v -s -r -1 eth_account/ tests/ -c "clear; flake8 eth_account tests && echo 'flake8 success' || echo 'error'"

Run multi-process tests in one command, but without color:

# in the project root:
pytest --numprocesses=4 --looponfail --maxfail=1
# the same thing, succinctly:
pytest -n 4 -f --maxfail=1

Release setup

For Debian-like systems:

apt install pandoc

To release a new version:

make release bump=$$VERSION_PART_TO_BUMP$$

How to bumpversion

The version format for this repo is {major}.{minor}.{patch} for stable, and {major}.{minor}.{patch}-{stage}.{devnum} for unstable (stage can be alpha or beta).

To issue the next version in line, specify which part to bump, like make release bump=minor or make release bump=devnum. This is typically done from the master branch, except when releasing a beta (in which case the beta is released from master, and the previous stable branch is released from said branch). To include changes made with each release, update "docs/releases.rst" with the changes, and apply commit directly to master before release.

If you are in a beta version, make release bump=stage will switch to a stable.

To issue an unstable version when the current version is stable, specify the new version explicitly, like make release bump="--new-version 4.0.0-alpha.1 devnum"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for py-asimov, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size py_asimov-0.0.2-py3-none-any.whl (21.7 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size py-asimov-0.0.2.tar.gz (21.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page