Skip to content

Commit

Permalink
✨ feat: 新版本完成
Browse files Browse the repository at this point in the history
  • Loading branch information
B1ue1nWh1te committed Oct 31, 2024
1 parent 18019b3 commit de565eb
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__pycache__
.env
.vscode
dist
logs
Expand Down
91 changes: 88 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,91 @@ 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)
Expand All @@ -63,12 +148,12 @@ python main.py

# 注意事项

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

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

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

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

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

0 comments on commit de565eb

Please sign in to comment.