GithubHelp home page GithubHelp logo

autonity.py's Introduction

autonity.py

Autonity is a protocol that provides smart contract and settlement infrastructure specialized for developing new risk markets. It is a fork of the Ethereum protocol. See the Autonity documentation for further information.

This package provides typed wrappers around the Autonity-specific extensions of Ethereum, using the Web3.py framework, for convenient and statically checked interactions with the Autonity network.

Installation

pip install autonity

Usage

The primary utility of this library is the typed wrappers around the Autonity protocol contract, which provides access to Autonity-specific functionality.

from autonity.utils.web3 import create_web3_for_endpoint
from autonity import Autonity, Validator

w3 = create_web3_for_endpoint("<RPC_ENDPOINT>")

# Create the typed wrapper around the Autonity contract.
autonity = Autonity(w3)

# Get total supply of Newton
ntn_supply = autonity.total_supply()

# Get the current validator list
validator_ids = autonity.get_validators()

# Get descriptor for the 0-th validator.  Print LNTN contract address.
validator_desc_0 = autonity.get_validator(validator_ids[0])
print(f"LNTN contract addr: {validator_desc_0['liquid_contract']}")

# Typed validator Liquid Newton contract.  Query unclaimed fees for <ADDRESS>.
validator_0 = Validator(w3, validator_desc_0)
unclaimed_atn, unclaimed_ntn = validator_0.unclaimed_rewards("<ADDRESS>")
print(f"unclaimed rewards: {unclaimed_atn} ATN, {unclaimed_ntn} NTN")

Where<RPC_ENDPOINT> is the name of the Autonity network being connected to. See https://docs.autonity.org/networks/ for information about specific networks.

Development

The project uses hatch as the build tool. To launch the tests, run:

hatch run test:all

For linting use the command:

hatch run lint:check

Updating the Contract ABIs

The script script/update_abi.sh [AUTONITY_COMMIT] builds the contract ABIs using AGC at the specified Git commit ID or tag. Keys are ordered via the jq tool, in order to produce deterministic output, and the results written to the autonity/abi directory. Further, it normalizes the commit ID that was used.

After executing the script against a new version of the code, the diffs can be reviewed to determine which methods have been modified, removed or added.

Reporting a Vulnerability

Please do not file a public ticket mentioning the vulnerability.

Instead, please send an email to [email protected] to report a security issue.

The following PGP key may be used to communicate sensitive information to developers:

Fingerprint: 6006 CCC3 DD11 7885 1A23 4290 7486 F832 6320 219E

-----BEGIN PGP PUBLIC KEY BLOCK-----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b48uugJAMmhCm3ALehcx0RuaIkSF5jP57eTLAo83/AJ2dikZvYZmh5OHdirTo
iZnjRt3uIL3SshrFz44poKrfHYr7X+ePAUEIAQeM9lDngdxemVEF0pI9uMcqqhdB
uA9h+hmjldAcdsvpBV8AEQEAAcLBfAQYAQgAJhYhBGAGzMPdEXiFGiNCkHSG+DJj
ICGeBQJi+3qfBQkFo5qAAhsMAAoJEHSG+DJjICGe17MQAKjw0EJar0BTEwTYraKq
ed2m6fhbSmyhV+UXtxtoinkEU2cxVe6IoK+x/uP0nfmCoH7ZlWapIOgKSDKsb/Ze
czVTmHt23O9/Tq7C2aCvK3UFcAWNEQFR6pWGgiPonxSaTN4Cw2f1vKekhxAYXrbm
7sqEKZl+59D8uzHA0QSORP8FKpextccCtiL2L5b3ttGmrjGiXeL1wm1iWHxuOksm
OpGFz6WgVZS1MYuomyBb/tm8MOsPabODmW3kJDUd1DcxO99ZFP72IERBTKqonKLW
VCTV8Evv2agpTwTiP7TxGnl9ep5ZxkXAnQUXMwfVBYg0uGmmMhdcQ2n8wh6f1aR2
GksOuLSMQTC/RNNHOnS0xTKrlh0uQ5fF0WZJaUpUXjHxCjiBAXUdlwXJET+S2t7H
jLXA1MdBmJp7ymBVRqQQguaH5G2dciSEG/iqMLH76u7c+L1w+esGpwbSu1OH+wd7
7ys9vJxxJIqch8mzKlRTun+M/CCXWX5uvxeVGrwmvrARfnyOpyR9W0MzJ5xi7n5I
B1LUp7ycX/NeWHviWALjz1ObHeipvErh2n2iD/8swWez6eho1BDJ9sf8hz/gVJbR
dNvOgvIvgW1Bcibq3uqiigQnFYo15bmfIDRCJCBCmqf4Xb8Ip+m/QrLf92KIcDRc
VtiVUMzKBEpmz4LdeSy73Qfr
=12PL
-----END PGP PUBLIC KEY BLOCK-----

autonity.py's People

Contributors

dtebbs avatar aiman avatar szemate avatar noandrea avatar cmjc avatar col4c avatar

Stargazers

 avatar Mikko Ohtamaa avatar magnitis avatar

Watchers

Robert Sams avatar  avatar Jay avatar  avatar Geekdom avatar Edward Chen avatar

autonity.py's Issues

Fix `ModuleNotFoundError` triggered by eth-rlp

After working for some time, importing autonity.py is unexpectedly failing due to a ModuleNotFoundError that is raised by eth-rlp. The problem seems to be an undeclared dependency of eth-rlp on typing-extensions.

>>> import autonity
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/test/src/autonity.py/autonity/__init__.py", line 10, in <module>
    from autonity.autonity import (
  File "/home/test/src/autonity.py/autonity/autonity.py", line 14, in <module>
    from web3 import Web3
  File "/home/test/.local/share/hatch/env/virtual/autonity/H65cO8gT/autonity/lib/python3.11/site-packages/web3/__init__.py", line 1, in <module>
    from eth_account import Account  # noqa: E402,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/test/.local/share/hatch/env/virtual/autonity/H65cO8gT/autonity/lib/python3.11/site-packages/eth_account/__init__.py", line 1, in <module>
    from eth_account.account import (
  File "/home/test/.local/share/hatch/env/virtual/autonity/H65cO8gT/autonity/lib/python3.11/site-packages/eth_account/account.py", line 49, in <module>
    from eth_account._utils.legacy_transactions import (
  File "/home/test/.local/share/hatch/env/virtual/autonity/H65cO8gT/autonity/lib/python3.11/site-packages/eth_account/_utils/legacy_transactions.py", line 13, in <module>
    from eth_rlp import (
  File "/home/test/.local/share/hatch/env/virtual/autonity/H65cO8gT/autonity/lib/python3.11/site-packages/eth_rlp/__init__.py", line 5, in <module>
    from .main import (
  File "/home/test/.local/share/hatch/env/virtual/autonity/H65cO8gT/autonity/lib/python3.11/site-packages/eth_rlp/main.py", line 18, in <module>
    from typing_extensions import Self  # move to typing when >= py311
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'typing_extensions'

See: ethereum/eth-rlp#17

Better API to expose Tx creation

The web3.py contract objects are convenient, but internally fill out all required fields, potentially contacting the node to estimate gas, compute nonces etc. The internal calls are not yet well exposed by web3.py, and so the only way to prevent web3.py from computing everything itself is to fill out a TxParams object and pass that in to the contract call.

For now, we can expose this as a prepare-call or prepare-finalize process, (prepare the TxParam with any available data, such as the nonce, then use the web3.py contract wrapper to fill in the "to" and "data" fields, and then finalize by adding any fields not already added. In general, finalization is not required for contract calls (since missing fields are filled out internall).

This is rather inconvenient for callers who want to specify this information, and we need a better solution. Maybe a tx builder class, potentially with flags to prevent the builder from connecting to a node.

Running tests against new versions of the node

Currently executing tests against the deployed testnet. In order to prepare for new release, we need to be able to run tests against development versions of the node / protocol contracts.

Improve toolchain

Propose a dev lifecycle and tooling to simplify dev and release process:

  • Adopt hatch as py management tool
  • Adopt convential commits (?)
  • Automatically generate CHANGELOG
  • Publish package on pypi

Fix version for autonity contract during bulid

Change the process of retrieving the Autonity contract so that the version is pre-defined. Currently the version of the contract is determined by the commit of a local clone of the autonity node

There are a few minor bug issues in the game Autonity

There are a few minor bug issues in the game Autonity:
Error Handling: Ensure that error handling mechanisms are in place. For example, if validator_ids is an empty list, accessing validator_ids[0] will result in an IndexError. Make sure to check for such cases and handle them appropriately.

RPC Endpoint Placeholder: <RPC_ENDPOINT> is a placeholder in the create_web3_for_endpoint function. Make sure to replace it with the actual RPC endpoint before running the code.

Unclaimed Rewards Function: Ensure that the unclaimed_rewards function in the Validator class is implemented correctly. Check for potential errors or exceptions that might occur during its execution.

Import Statements: Verify that the import statements are correct, and the required modules are available

Drop temporary dependency on `typing-extensions`

Installing autonity.py was unexpectedly failing due to a ModuleNotFoundError that was raised by eth-rlp. Temporary workaround implemented in 6f9f60a was to just add typing-extensions as a temporary dependency in this project until the eth-rlp issue is resolved.

This issue is a reminder to remove the typing-extensions dependency from this project once the issue is resolved in eth-rlp.

See: ethereum/eth-rlp#17

`AssertionError` on returning tuple

Context

Solidity compilation automatically generates getters for calling public variables. Attempting to call such a getter to return data from the Autonity Stabilization Contract, an error is thrown. The function can be called and data correctly returned using a simple web3.py script using eth.contract.

The getter is for this data structure in https://github.com/autonity/autonity/blob/8f10cceba9e47a014000a9de78b4ab85a91ec0ec/autonity/solidity/contracts/asm/Stabilization.sol#L65C1-L66C41:

    /// A mapping to retrieve the CDP for an account address.
    mapping(address => CDP) public cdps;

Bug

autonity.py is not handling the Solidity datatype correctly and is throwing an error.

Example

Calling the cdps getter function, this error is thrown:

ubuntu@vps-c7c3e8c7:~/TEST/autcli$ aut -v contract call --abi Stabilization.abi --address 0x29b2440db4A256BE6d3B4CDcaA68E20c1E6d3B4CDcaA68E2440A08f cdps 0x1f790c60D974F5A8f88558CA90F743a71F009641                                  

method: cdps
parameters: ['0x1f790c60D974F5A8f88558CA90F743a71F009641']
fn_params (parsed): ['0x1f790c60D974F5A8f88558CA90F743a71F009641']
found config file: /home/ubuntu/TEST/autcli/.autrc
endpoint from config file: http://127.0.0.1:8545/
Traceback (most recent call last):
  File "/home/ubuntu/.local/bin/aut", line 8, in <module>
    sys.exit(aut())
  File "/home/ubuntu/.local/pipx/venvs/aut/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/ubuntu/.local/pipx/venvs/aut/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/ubuntu/.local/pipx/venvs/aut/lib/python3.10/site-packages/click/core.py", line 1657, in invokl__e
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/ubuntu/.local/pipx/venvs/aut/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/ubuntu/.local/pipx/venvs/aut/lib/python3.10/site-packages/click/core.py", line 1404, in invokee
    return ctx.invoke(self.callbackclient_loop: send disconnect: Broken pipe
  File "/home/ubuntu/.local/pipx/venvs/aut/lib/python3.10/site-packages/click/core.py", line 760, in invoke
[Process completed]    return __callback(*args, **kwargs)
  File "/home/ubuntu/.local/pipx/venvs/aut/lib/python3.10/site-packages/aut/commands/contract.py", line 188, in call_cmd
    parsed_result = parse_return_value(abi_fn, result)
  File "/home/ubuntu/.local/pipx/venvs/aut/lib/python3.10/site-packages/autonity/abi_parser.py", line 75, in parse_return_n parse_return_value
    assert isinstance(return_value, tuple)
AssertionError

The error is thrown in the abi-pasrser.py file here:

https://github.com/autonity/autonity.py/blob/master/autonity/abi_parser.py#L73-L74

    assert isinstance(return_value, tuple)
    return _parse_return_value_tuple(outputs, return_value)

Returning the data successfully with web3.py

A simple python script using w3.eth.contract works fine:

#aut_cdps.py

from web3 import Web3, HTTPProvider
import json

with open("Stabilization.abi") as f:
	abi = info_json = json.load(f)


w3 = Web3(HTTPProvider('https://localhost:8545'))

stabilizationContractAddress = "0x29b2440db4A256B0c1E6d3B4CDcaA68E2440A08f"
contract_instance = w3.eth.contract(address=stabilizationContractAddress, abi=abi)

# read state: call the cdps function passing in <CDP_ACCOUNT_ADDRESS>:

print(contract_instance.functions.cdps("0x1f790c60D974F5A8f88558CA90F743a71F009641").call())

This successfully returns the data as expected:

% python3 aut_cdps.py
[1695737259, 410000024269118276, 300010815663118114, 0]

return of `autonity.config()` makes no sense

This

from autonity.utils.web3 import create_web3_for_endpoint
from autonity import Autonity, Validator
import pprint

w3 = create_web3_for_endpoint("https://rpc1.piccadilly.autonity.org")

# Create the typed wrapper around the Autonity contract.
autonity = Autonity(w3)

pp = pprint.PrettyPrinter(indent=4)
pp.pprint(autonity.config())

Returns for me:

{   'block_period': 238044082548333950705483927632573396321407377551,
    'committee_size': 798656913401135627232192857470168543466665104586,
    'contract_version': 409751456048444136304209391525758347269861709698,
    'delegation_rate': 1205578988788339067396526300728542657047263191959,
    'epoch_period': 515330412878651974478734746372204808145222434547,
    'min_basefee': 21600,
    'operator_account': '0x000000000000000000000000002386F26fc10000',
    'treasury_account': '0x000000000000000000000000000000001dcd6500',
    'treasury_fee': 1000,
    'unbonding_period': 410556365057780192082296870566657146538054515293}

which makes no sense?

Export contract version as py variable

The update-abi scripts writes the git version of the contract on a text file,
and the build process bundles the abi files in the python package.

Expose these info so they can be used by third party app.

It will introduce 2 new exported functions:

  • def get_autonity_contract_version() -> str:
  • def get_autonity_contract_abi_path() -> str:

Use case

The aut client introduces a new sub-command group utils or info with the following commands

  • autonity-contract-version the permalink to the contract github url using the contract_git_commit_hash
  • autonity-contract-abi the path to the abi file embedded in the autonity.py library
  • autonity-contract-address the contract address (since it doesn't change)

It should also add the exact sequence of commands that are used to generate the abi since the abi keys are sorted before the checksum is calculated

Wrapper methods `get_oracle` and `config` return inconsistent values?

Shouldn't these should return the same value?

from autonity.utils.web3 import create_web3_for_endpoint                                                                      
from autonity import Autonity                                                                                                 
w3 = create_web3_for_endpoint("https://rpc1.piccadilly.autonity.org")                                                         
autonity = Autonity(w3)                                                                                                       
config = autonity.config()                                                                                                    
print(config['contracts']['oracle_contract'])                                                                                 
print(autonity.get_oracle())                                                                                                                                                   
0x5a443704dd4B594B382c22a083e2BD3090A6feF3
0x47e9Fbef8C83A1714F1951F142132E6e90F5fa5D

Support for Python v3.12

Currently, importing autonity.py on Python 3.12 fails due to ModuleNotFoundError triggered by Web3.py:

Traceback (most recent call last):
  File "/home/test/test.py", line 1, in <module>
    import autonity
  File "/home/test/.local/share/virtualenvs/test-hFzLK45U/lib/python3.12/site-packages/autonity/__init__.py", line 10, in <module>
    from autonity.autonity import (
  File "/home/test/.local/share/virtualenvs/test-hFzLK45U/lib/python3.12/site-packages/autonity/autonity.py", line 14, in <module>
    from web3 import Web3
  File "/home/test/.local/share/virtualenvs/test-hFzLK45U/lib/python3.12/site-packages/web3/__init__.py", line 2, in <module>
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'

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.