poseidon-python


Nameposeidon-python JSON
Version 2.0.0 PyPI version JSON
download
home_pagehttps://github.com/B1ue1nWh1te/Poseidon
Summary海神波塞冬,本工具库对常用的链上交互操作进行了模块化抽象与简洁式封装,让开发者能够轻松快速地与主流区块链网络进行交互。目前支持任意 EVM 链。
upload_time2024-10-31 05:38:24
maintainerNone
docs_urlNone
authorB1ue1nWh1te
requires_python<4.0,>=3.9
licenseGPL-3.0
keywords blockchain web3 ethereum evm tooling
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">

[![data](https://socialify.git.ci/B1ue1nWh1te/Poseidon/image?font=Bitter&forks=1&issues=1&language=1&logo=https%3A%2F%2Fimg.seaeye.cn%2Fimg%2Fposeidon%2Flogo.png&name=1&owner=1&pattern=Circuit%20Board&pulls=1&stargazers=1&theme=Auto)](https://github.com/B1ue1nWh1te/Poseidon)

**Poseidon 海神波塞冬**,本工具库对常用的链上交互操作进行了模块化抽象与简洁式封装,

让开发者能够轻松快速地与主流区块链网络进行交互。目前支持任意 EVM 链。

[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)
[![Python](https://img.shields.io/badge/python-3.9+-blue)](https://www.python.org/)
[![Release](https://img.shields.io/github/v/release/B1ue1nWh1te/Poseidon)](https://github.com/B1ue1nWh1te/Poseidon/releases/)
[![Downloads](https://img.shields.io/pypi/dm/poseidon-python?color=%23008BE1)](https://pypi.org/project/poseidon-python/)

</div>

# 安装

## 最简方式

直接使用 pip 安装,但有可能由于本地 python 环境依赖库紊乱而导致脚本运行出错。

```bash
pip install -U poseidon-python
```

## 推荐方式

基于 [模板库](https://github.com/B1ue1nWh1te/PoseidonTemplate) 使用 poetry 创建虚拟环境,这样可以保证脚本运行环境干净,减少出现意外错误的可能。

安装 poetry 虚拟环境管理工具(如果之前未安装):

```bash
pip install -U poetry
```

克隆 [模板库](https://github.com/B1ue1nWh1te/PoseidonTemplate) 至本地(也可先使用该模板库创建一个副本至你自己的 Github 仓库中再克隆):

```bash
git clone git@github.com:B1ue1nWh1te/PoseidonTemplate.git
```

切换至模板仓库目录并安装虚拟环境:

```bash
cd PoseidonTemplate
poetry install
```

之后假设你编写了一个名为 main.py 的脚本要运行:

```bash
poetry shell
python main.py
```

# 示例

以下通过对比 Poseidon 与 web3.py 的使用,展示 Poseidon 的简洁性优势。

## 使用 Poseidon

```python
from poseidon.evm import Chain, Account, Contract, Utils

rpc_url = "https://<RPC_URL>"
chain = Chain(rpc_url)

address, private_key = Utils.generate_new_account()
account = Account(chain, private_key)
signature_data = account.sign_message_string("test")
signed_message_data = Utils.recover_message_string("test", signature_data.signature_data.signature)
account.send_transaction(to=ZERO_ADDRESS, data="0x", value=1)

Utils.set_solidity_version("0.8.28")
abi, bytecode = Utils.compile_solidity_contract("./Contract.sol", "Contract")
tx_receipt = account.deploy_contract(abi, bytecode)

contract: Contract = tx_receipt.contract
contract.call_function("anyWriteFunction", "(param1)", "(param2)")
contract.read_only_call_function("anyReadOnlyFunction", "(param1)", "(param2)")
```

## 使用 web3.py

```python
from web3 import Web3
from eth_account import Account as Web3Account
from eth_account.messages import encode_defunct
from solcx import compile_source, install_solc
import json

w3 = Web3(Web3.HTTPProvider("https://<RPC_URL>"))

account = Web3Account.create()
address = account.address
private_key = account.key.hex()
message = encode_defunct(text="test")
signed_message = w3.eth.account.sign_message(message, private_key=private_key)
recovered_address = w3.eth.account.recover_message(message, signature=signed_message.signature)
transaction = {
    'nonce': w3.eth.get_transaction_count(address),
    'to': ZERO_ADDRESS,
    'value': 1,
    'gas': 21000,
    'gasPrice': w3.eth.gas_price,
    'data': '0x'
}
signed_txn = w3.eth.account.sign_transaction(transaction, private_key)
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

install_solc('0.8.28')
with open('./Contract.sol', 'r') as file:
    source = file.read()
compiled_sol = compile_source(source)
contract_interface = compiled_sol['<stdin>:Contract']
bytecode = contract_interface['bin']
abi = contract_interface['abi']
contract = w3.eth.contract(abi=abi, bytecode=bytecode)
transaction = contract.constructor().build_transaction({
    'from': address,
    'nonce': w3.eth.get_transaction_count(address),
    'gas': 2000000,
    'gasPrice': w3.eth.gas_price
})
signed_txn = w3.eth.account.sign_transaction(transaction, private_key)
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

contract_instance = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)
write_txn = contract_instance.functions.anyWriteFunction("(param1)", "(param2)").build_transaction({
    'from': address,
    'nonce': w3.eth.get_transaction_count(address),
    'gas': 200000,
    'gasPrice': w3.eth.gas_price
})
signed_txn = w3.eth.account.sign_transaction(write_txn, private_key)
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
result = contract_instance.functions.anyReadOnlyFunction("(param1)", "(param2)").call()
```

# 文档

主要文档:[**Poseidon Docs**](https://seaverse.gitbook.io/poseidon)

其他文档:[**web3.py(v6) Docs**](https://web3py.readthedocs.io/en/v6.20.2/)

# 注意事项

1. **EVM** 模块的所有功能在 `Ethereum Sepolia`, `Arbitrum Sepolia`, `Optimism Sepolia`, `BSC Testnet`, `Polygon Amoy` **测试网络**中均正常通过测试。

2. 建议始终使用**全新生成的**账户进行导入,以避免意外情况下隐私数据泄露。

3. 关于安全性,代码完全开源并且基于常用的第三方库进行封装,可以自行进行审阅。

4. 如果你在使用过程中遇到了问题或者有任何好的想法和建议,欢迎提 [**Issues**](https://github.com/B1ue1nWh1te/Poseidon/issues) 或 [**PRs**](https://github.com/B1ue1nWh1te/Poseidon/pulls) 进行反馈和贡献。

5. 本工具库**开源的目的是进行技术开发上的交流与分享**,不涉及任何其他方面的内容。原则上该工具只应该在开发测试环境下与区块链测试网进行交互调试,作者并不提倡在其他情况下使用。若开发者自行选择在具有经济价值的区块链主网中使用,所造成的任何影响由其个人负责,与作者本人无关。

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/B1ue1nWh1te/Poseidon",
    "name": "poseidon-python",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "blockchain, web3, ethereum, evm, tooling",
    "author": "B1ue1nWh1te",
    "author_email": "themagicdeepsea@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/92/e1/63bdcc78b191f7d0028c4298701e40ceb0a63133f10e967725952ca0aa83/poseidon_python-2.0.0.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n\n[![data](https://socialify.git.ci/B1ue1nWh1te/Poseidon/image?font=Bitter&forks=1&issues=1&language=1&logo=https%3A%2F%2Fimg.seaeye.cn%2Fimg%2Fposeidon%2Flogo.png&name=1&owner=1&pattern=Circuit%20Board&pulls=1&stargazers=1&theme=Auto)](https://github.com/B1ue1nWh1te/Poseidon)\n\n**Poseidon \u6d77\u795e\u6ce2\u585e\u51ac**\uff0c\u672c\u5de5\u5177\u5e93\u5bf9\u5e38\u7528\u7684\u94fe\u4e0a\u4ea4\u4e92\u64cd\u4f5c\u8fdb\u884c\u4e86\u6a21\u5757\u5316\u62bd\u8c61\u4e0e\u7b80\u6d01\u5f0f\u5c01\u88c5\uff0c\n\n\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u5feb\u901f\u5730\u4e0e\u4e3b\u6d41\u533a\u5757\u94fe\u7f51\u7edc\u8fdb\u884c\u4ea4\u4e92\u3002\u76ee\u524d\u652f\u6301\u4efb\u610f EVM \u94fe\u3002\n\n[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)\n[![Python](https://img.shields.io/badge/python-3.9+-blue)](https://www.python.org/)\n[![Release](https://img.shields.io/github/v/release/B1ue1nWh1te/Poseidon)](https://github.com/B1ue1nWh1te/Poseidon/releases/)\n[![Downloads](https://img.shields.io/pypi/dm/poseidon-python?color=%23008BE1)](https://pypi.org/project/poseidon-python/)\n\n</div>\n\n# \u5b89\u88c5\n\n## \u6700\u7b80\u65b9\u5f0f\n\n\u76f4\u63a5\u4f7f\u7528 pip \u5b89\u88c5\uff0c\u4f46\u6709\u53ef\u80fd\u7531\u4e8e\u672c\u5730 python \u73af\u5883\u4f9d\u8d56\u5e93\u7d0a\u4e71\u800c\u5bfc\u81f4\u811a\u672c\u8fd0\u884c\u51fa\u9519\u3002\n\n```bash\npip install -U poseidon-python\n```\n\n## \u63a8\u8350\u65b9\u5f0f\n\n\u57fa\u4e8e [\u6a21\u677f\u5e93](https://github.com/B1ue1nWh1te/PoseidonTemplate) \u4f7f\u7528 poetry \u521b\u5efa\u865a\u62df\u73af\u5883\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u811a\u672c\u8fd0\u884c\u73af\u5883\u5e72\u51c0\uff0c\u51cf\u5c11\u51fa\u73b0\u610f\u5916\u9519\u8bef\u7684\u53ef\u80fd\u3002\n\n\u5b89\u88c5 poetry \u865a\u62df\u73af\u5883\u7ba1\u7406\u5de5\u5177\uff08\u5982\u679c\u4e4b\u524d\u672a\u5b89\u88c5\uff09\uff1a\n\n```bash\npip install -U poetry\n```\n\n\u514b\u9686 [\u6a21\u677f\u5e93](https://github.com/B1ue1nWh1te/PoseidonTemplate) \u81f3\u672c\u5730\uff08\u4e5f\u53ef\u5148\u4f7f\u7528\u8be5\u6a21\u677f\u5e93\u521b\u5efa\u4e00\u4e2a\u526f\u672c\u81f3\u4f60\u81ea\u5df1\u7684 Github \u4ed3\u5e93\u4e2d\u518d\u514b\u9686\uff09\uff1a\n\n```bash\ngit clone git@github.com:B1ue1nWh1te/PoseidonTemplate.git\n```\n\n\u5207\u6362\u81f3\u6a21\u677f\u4ed3\u5e93\u76ee\u5f55\u5e76\u5b89\u88c5\u865a\u62df\u73af\u5883\uff1a\n\n```bash\ncd PoseidonTemplate\npoetry install\n```\n\n\u4e4b\u540e\u5047\u8bbe\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u540d\u4e3a main.py \u7684\u811a\u672c\u8981\u8fd0\u884c\uff1a\n\n```bash\npoetry shell\npython main.py\n```\n\n# \u793a\u4f8b\n\n\u4ee5\u4e0b\u901a\u8fc7\u5bf9\u6bd4 Poseidon \u4e0e web3.py \u7684\u4f7f\u7528\uff0c\u5c55\u793a Poseidon \u7684\u7b80\u6d01\u6027\u4f18\u52bf\u3002\n\n## \u4f7f\u7528 Poseidon\n\n```python\nfrom poseidon.evm import Chain, Account, Contract, Utils\n\nrpc_url = \"https://<RPC_URL>\"\nchain = Chain(rpc_url)\n\naddress, private_key = Utils.generate_new_account()\naccount = Account(chain, private_key)\nsignature_data = account.sign_message_string(\"test\")\nsigned_message_data = Utils.recover_message_string(\"test\", signature_data.signature_data.signature)\naccount.send_transaction(to=ZERO_ADDRESS, data=\"0x\", value=1)\n\nUtils.set_solidity_version(\"0.8.28\")\nabi, bytecode = Utils.compile_solidity_contract(\"./Contract.sol\", \"Contract\")\ntx_receipt = account.deploy_contract(abi, bytecode)\n\ncontract: Contract = tx_receipt.contract\ncontract.call_function(\"anyWriteFunction\", \"(param1)\", \"(param2)\")\ncontract.read_only_call_function(\"anyReadOnlyFunction\", \"(param1)\", \"(param2)\")\n```\n\n## \u4f7f\u7528 web3.py\n\n```python\nfrom web3 import Web3\nfrom eth_account import Account as Web3Account\nfrom eth_account.messages import encode_defunct\nfrom solcx import compile_source, install_solc\nimport json\n\nw3 = Web3(Web3.HTTPProvider(\"https://<RPC_URL>\"))\n\naccount = Web3Account.create()\naddress = account.address\nprivate_key = account.key.hex()\nmessage = encode_defunct(text=\"test\")\nsigned_message = w3.eth.account.sign_message(message, private_key=private_key)\nrecovered_address = w3.eth.account.recover_message(message, signature=signed_message.signature)\ntransaction = {\n    'nonce': w3.eth.get_transaction_count(address),\n    'to': ZERO_ADDRESS,\n    'value': 1,\n    'gas': 21000,\n    'gasPrice': w3.eth.gas_price,\n    'data': '0x'\n}\nsigned_txn = w3.eth.account.sign_transaction(transaction, private_key)\ntx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)\ntx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)\n\ninstall_solc('0.8.28')\nwith open('./Contract.sol', 'r') as file:\n    source = file.read()\ncompiled_sol = compile_source(source)\ncontract_interface = compiled_sol['<stdin>:Contract']\nbytecode = contract_interface['bin']\nabi = contract_interface['abi']\ncontract = w3.eth.contract(abi=abi, bytecode=bytecode)\ntransaction = contract.constructor().build_transaction({\n    'from': address,\n    'nonce': w3.eth.get_transaction_count(address),\n    'gas': 2000000,\n    'gasPrice': w3.eth.gas_price\n})\nsigned_txn = w3.eth.account.sign_transaction(transaction, private_key)\ntx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)\ntx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)\n\ncontract_instance = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)\nwrite_txn = contract_instance.functions.anyWriteFunction(\"(param1)\", \"(param2)\").build_transaction({\n    'from': address,\n    'nonce': w3.eth.get_transaction_count(address),\n    'gas': 200000,\n    'gasPrice': w3.eth.gas_price\n})\nsigned_txn = w3.eth.account.sign_transaction(write_txn, private_key)\ntx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)\ntx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)\nresult = contract_instance.functions.anyReadOnlyFunction(\"(param1)\", \"(param2)\").call()\n```\n\n# \u6587\u6863\n\n\u4e3b\u8981\u6587\u6863\uff1a[**Poseidon Docs**](https://seaverse.gitbook.io/poseidon)\n\n\u5176\u4ed6\u6587\u6863\uff1a[**web3.py(v6) Docs**](https://web3py.readthedocs.io/en/v6.20.2/)\n\n# \u6ce8\u610f\u4e8b\u9879\n\n1. **EVM** \u6a21\u5757\u7684\u6240\u6709\u529f\u80fd\u5728 `Ethereum Sepolia`, `Arbitrum Sepolia`, `Optimism Sepolia`, `BSC Testnet`, `Polygon Amoy` **\u6d4b\u8bd5\u7f51\u7edc**\u4e2d\u5747\u6b63\u5e38\u901a\u8fc7\u6d4b\u8bd5\u3002\n\n2. \u5efa\u8bae\u59cb\u7ec8\u4f7f\u7528**\u5168\u65b0\u751f\u6210\u7684**\u8d26\u6237\u8fdb\u884c\u5bfc\u5165\uff0c\u4ee5\u907f\u514d\u610f\u5916\u60c5\u51b5\u4e0b\u9690\u79c1\u6570\u636e\u6cc4\u9732\u3002\n\n3. \u5173\u4e8e\u5b89\u5168\u6027\uff0c\u4ee3\u7801\u5b8c\u5168\u5f00\u6e90\u5e76\u4e14\u57fa\u4e8e\u5e38\u7528\u7684\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u5c01\u88c5\uff0c\u53ef\u4ee5\u81ea\u884c\u8fdb\u884c\u5ba1\u9605\u3002\n\n4. \u5982\u679c\u4f60\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u9047\u5230\u4e86\u95ee\u9898\u6216\u8005\u6709\u4efb\u4f55\u597d\u7684\u60f3\u6cd5\u548c\u5efa\u8bae\uff0c\u6b22\u8fce\u63d0 [**Issues**](https://github.com/B1ue1nWh1te/Poseidon/issues) \u6216 [**PRs**](https://github.com/B1ue1nWh1te/Poseidon/pulls) \u8fdb\u884c\u53cd\u9988\u548c\u8d21\u732e\u3002\n\n5. \u672c\u5de5\u5177\u5e93**\u5f00\u6e90\u7684\u76ee\u7684\u662f\u8fdb\u884c\u6280\u672f\u5f00\u53d1\u4e0a\u7684\u4ea4\u6d41\u4e0e\u5206\u4eab**\uff0c\u4e0d\u6d89\u53ca\u4efb\u4f55\u5176\u4ed6\u65b9\u9762\u7684\u5185\u5bb9\u3002\u539f\u5219\u4e0a\u8be5\u5de5\u5177\u53ea\u5e94\u8be5\u5728\u5f00\u53d1\u6d4b\u8bd5\u73af\u5883\u4e0b\u4e0e\u533a\u5757\u94fe\u6d4b\u8bd5\u7f51\u8fdb\u884c\u4ea4\u4e92\u8c03\u8bd5\uff0c\u4f5c\u8005\u5e76\u4e0d\u63d0\u5021\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u82e5\u5f00\u53d1\u8005\u81ea\u884c\u9009\u62e9\u5728\u5177\u6709\u7ecf\u6d4e\u4ef7\u503c\u7684\u533a\u5757\u94fe\u4e3b\u7f51\u4e2d\u4f7f\u7528\uff0c\u6240\u9020\u6210\u7684\u4efb\u4f55\u5f71\u54cd\u7531\u5176\u4e2a\u4eba\u8d1f\u8d23\uff0c\u4e0e\u4f5c\u8005\u672c\u4eba\u65e0\u5173\u3002\n",
    "bugtrack_url": null,
    "license": "GPL-3.0",
    "summary": "\u6d77\u795e\u6ce2\u585e\u51ac\uff0c\u672c\u5de5\u5177\u5e93\u5bf9\u5e38\u7528\u7684\u94fe\u4e0a\u4ea4\u4e92\u64cd\u4f5c\u8fdb\u884c\u4e86\u6a21\u5757\u5316\u62bd\u8c61\u4e0e\u7b80\u6d01\u5f0f\u5c01\u88c5\uff0c\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u5feb\u901f\u5730\u4e0e\u4e3b\u6d41\u533a\u5757\u94fe\u7f51\u7edc\u8fdb\u884c\u4ea4\u4e92\u3002\u76ee\u524d\u652f\u6301\u4efb\u610f EVM \u94fe\u3002",
    "version": "2.0.0",
    "project_urls": {
        "Documentation": "https://seaverse.gitbook.io/poseidon",
        "Homepage": "https://github.com/B1ue1nWh1te/Poseidon",
        "Repository": "https://github.com/B1ue1nWh1te/Poseidon"
    },
    "split_keywords": [
        "blockchain",
        " web3",
        " ethereum",
        " evm",
        " tooling"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e9a795e31ac0641d420ec3c6b379fc4fe0001ea9dab64f840f87cf07bb3101fb",
                "md5": "fd48f4cc655d0a405d38f4a0f688ce17",
                "sha256": "0838918038f09e96f0d667dc09a0fb1da6342a82dc82c49d007028ebfaa80927"
            },
            "downloads": -1,
            "filename": "poseidon_python-2.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fd48f4cc655d0a405d38f4a0f688ce17",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 28145,
            "upload_time": "2024-10-31T05:38:23",
            "upload_time_iso_8601": "2024-10-31T05:38:23.182588Z",
            "url": "https://files.pythonhosted.org/packages/e9/a7/95e31ac0641d420ec3c6b379fc4fe0001ea9dab64f840f87cf07bb3101fb/poseidon_python-2.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "92e163bdcc78b191f7d0028c4298701e40ceb0a63133f10e967725952ca0aa83",
                "md5": "95b5da6821db3c02b74904ef9083a93d",
                "sha256": "d9a344da4f300276476cfdfd3b8b19684e393b20a76ea2361a795cc89c521f55"
            },
            "downloads": -1,
            "filename": "poseidon_python-2.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "95b5da6821db3c02b74904ef9083a93d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 29921,
            "upload_time": "2024-10-31T05:38:24",
            "upload_time_iso_8601": "2024-10-31T05:38:24.730075Z",
            "url": "https://files.pythonhosted.org/packages/92/e1/63bdcc78b191f7d0028c4298701e40ceb0a63133f10e967725952ca0aa83/poseidon_python-2.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-31 05:38:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "B1ue1nWh1te",
    "github_project": "Poseidon",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "poseidon-python"
}
        
Elapsed time: 0.70897s