标签: web3py

使用 python 在 Binance 智能链上出现 Web3 ExtraDataLength 错误

我试图提取特定区块上发生的交易,但我被困在这里:

from web3 import Web3

bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))

block = web3.eth.get_block('latest')

web3.exceptions.ExtraDataLengthError: The field extraData is 97 bytes, but should be 32. It is quite likely that you are connected to a POA chain. Refer to http://web3py.readthedocs.io/en/stable/middleware.html#geth-style-proof-of-authority for more details.
Run Code Online (Sandbox Code Playgroud)

我想获取某个钱包地址涉及的交易,但我不知道为什么 web3 不允许我从 bsc 节点提取该交易。

python blockchain web3py binance-smart-chain

4
推荐指数
1
解决办法
3206
查看次数

web3.contract.functions.getAmount() 中的差异

我正在使用 Web3.py,但遇到了一些奇怪的事情。

对于以下代码(使用 Pancake Router V2):

from web3 import Web3
from web3.middleware import geth_poa_middleware

web3 = Web3(Web3.HTTPProvider('https://bsc-dataseed1.binance.org:443'))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)

ABI = {"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsOut","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"}

CAKE_ROUTER_V2 = web3.toChecksumAddress('0x10ed43c718714eb63d5aa57b78b54704e256024e')
router_contract = web3.eth.contract(address=CAKE_ROUTER_V2, abi=ABI),

WBNB = web3.toChecksumAddress('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c')
CAKE = web3.toChecksumAddress('0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82')
KONGSHIBA = web3.toChecksumAddress('0x126f5f2a88451d24544f79d11f869116351d46e1')

print(router_contract.functions.getAmountsOut(1, [WBNB, CAKE]).call())
print(router_contract.functions.getAmountsOut(1, [WBNB, KONGSHIBA]).call())
Run Code Online (Sandbox Code Playgroud)

我得到以下信息:

[1, 19]
[1, 160]
Run Code Online (Sandbox Code Playgroud)

WBNB 和 CAKE 有 18 位小数,KONGSHIBA 有 17 位
。CAKE 目前的价值约为 27.7 美元,WBNB 为 545.41291093 美元
,KONGSHIBA 为 0.00000000000000000332 美元。
所以我应该回来:

[1, 19]
[1, 16000000000000000000]
Run Code Online (Sandbox Code Playgroud)

请指教。

python blockchain smartcontracts web3js web3py

3
推荐指数
1
解决办法
5259
查看次数

bsc 通过钱包地址获取交易 Web3.py

如何跟踪 bsc 网络中钱包列表的代币交易?

我考虑使用websocket和过滤功能。我认为可以使用作为topics过滤器参数的一部分,并仅反映发送Transfer至/来自监视地址的事件,以便我的应用程序不必处理不必要的数据。

但我做错了,不知道如何正确地将钱包列表(或至少只有一个钱包)作为我的过滤函数的参数。怎么做?

我在从Transfer事件获取数据时遇到问题,因为我不知道如何解码HexBytes类型。我看到了 web3.js 的功能,但没有看到 web3.py 的功能。

address_list = ['0x67fdE6D04a82689a59E5188f9B572CBeF53D4763', '...', '...']

web3 = Web3(Web3.WebsocketProvider('wss://bsc.getblock.io/mainnet/?api_key=your_api_key'))
web3_filter = web3.eth.filter({'topics': address_list}) 
while True:
    for event in web3_filter.get_new_entries():
        print(web3.toJSON(web3.eth.wait_for_transaction_receipt(event).logs))
Run Code Online (Sandbox Code Playgroud)

python blockchain smartcontracts web3py

3
推荐指数
1
解决办法
1万
查看次数

使用Web3和Python计算BSC代币的价格

我正在使用 web3 和 python 构建一个工具,需要通过 PancakeSwap 快速准确地获取币安智能链上的代币价格。

该工具收集有关 BSC 代币、价格、流动性等信息,以便我可以进一步分析 rugpulls。

在下面的代码中,它提供了一个合约地址,并且需要提供 BNB 中每个代币的当前价格。然而,它出现了很多故障,并且没有给我正确的价格,我不知道出了什么问题。代码如下。

from web3 import Web3

web3 = Web3(Web3.WebsocketProvider('wss://speedy-nodes-nyc.moralis.io/b51e035eb24e1e81cc144788/bsc/mainnet/ws'))

tokenPriceABI = 'Token Price ABI'
   
def getTokenPrice(tokenAddress):
    BNBTokenAddress = Web3.toChecksumAddress("0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c")  # BNB
    amountOut = None#
    #tokenAddress = Web3.toChecksumAddress(tokenAddress)

    tokenRouter = web3_sell.eth.contract(address=tokenAddress, abi=tokenPriceABI)
    
    router = web3_sell.eth.contract(address=Web3.toChecksumAddress("0x10ed43c718714eb63d5aa57b78b54704e256024e"), abi=pancakeABI)
    amountIn = web3_sell.toWei(1, 'ether')
    amountOut = router.functions.getAmountsOut(amountIn, [tokenAddress, BNBTokenAddress]).call()
    amountOut = web3_sell.fromWei(amountOut[1], 'ether')

    return amountOut


tokenAddress = input("Enter token address: ")
tokenAddress = Web3.toChecksumAddress(tokenAddress)

priceInBnb = getTokenPrice(tokenAddress)

print(priceInBnb)

Run Code Online (Sandbox Code Playgroud)

有人能帮忙吗?谢谢。

python cryptocurrency binance web3py binance-smart-chain

3
推荐指数
1
解决办法
3674
查看次数

使用 web3.py 计算令牌对的 LP 地址

经过几个小时的搜索,我设法运行了这段代码,但不幸的是,这并没有产生我想要的输出,即获取(TOKEN/BNB LP)中的 LP 池地址。

给定令牌地址:0xe56842ed550ff2794f010738554db45e60730371

我想获取 BIN/BNB 池地址:0xe432afB7283A08Be24E9038C30CA6336A7cC8218

有什么想法可能是什么问题吗?

from web3 import Web3
from eth_abi.packed import encode_abi_packed
from eth_abi import encode_abi
import eth_abi

"""
Contract: 0xe56842ed550ff2794f010738554db45e60730371
BIN/BNB Address: 0xe432afB7283A08Be24E9038C30CA6336A7cC8218
BIN/BNB LP URL: https://bscscan.com/token/0xe432afB7283A08Be24E9038C30CA6336A7cC8218#balances
"""

CONTRACTS = {"CONTRACT": "0xe56842ed550ff2794f010738554db45e60730371",}

PANCAKE_SWAP_FACTORY = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73"
PANCAKE_SWAP_ROUTER  = "0x10ED43C718714eb63d5aA57B78B54704E256024E"
WBNB_ADDRESS = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"

hexadem_= '0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
factory = PANCAKE_SWAP_FACTORY
abiEncoded_1 = encode_abi_packed(['address', 'address'], (CONTRACTS['CONTRACT'], WBNB_ADDRESS))
salt_ = Web3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed([ 'address', 'bytes32'], ( factory, salt_))
resPair = Web3.solidityKeccak(['bytes','bytes'], ['0xff' + abiEncoded_2.hex(), …
Run Code Online (Sandbox Code Playgroud)

python solidity web3py binance-smart-chain

3
推荐指数
1
解决办法
3423
查看次数

Python Web3 Ganache - BuildTransaction 上的回溯错误

我正在 YouTube 上学习freeCodeCamp课程,在运行该程序时发现了一个错误。当我试图在 Ganache 中建立一个交易时。我在 Ganache 日志中看到也有活动。第一次发帖,请告诉我还应该提供哪些信息!

\n

课程:Solidity、区块链和智能合约课程 \xe2\x80\x93 初学者到专家 Python 教程

\n
transaction = SimpleStorage.constructor().buildTransaction(\n    {"chainId": chain_id, "from": my_address, "nonce": nonce}\n)\n
Run Code Online (Sandbox Code Playgroud)\n

终端返回的错误:

\n
transaction = SimpleStorage.constructor().buildTransaction(\n    {"chainId": chain_id, "from": my_address, "nonce": nonce}\n)\n
Run Code Online (Sandbox Code Playgroud)\n

我使用的完整代码如下:

\n
from solcx import compile_standard, install_solc\nimport json\nfrom web3 import Web3\n\nwith open("./SimpleStorage.sol", "r") as file:\n    simple_storage_file = file.read()\n\n# Compile Our Solidity\nprint("Installing...")\ninstall_solc("0.6.0")\n\ncompiled_sol = compile_standard(\n    {\n        "language": "Solidity",\n        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},\n        "settings": {\n            "outputSelection": {\n                "*": {\n                    "*": [\n                        "abi",\n                        "metadata",\n                        "evm.bytecode",\n …
Run Code Online (Sandbox Code Playgroud)

python blockchain solidity ganache web3py

3
推荐指数
1
解决办法
1509
查看次数

“Web3”对象没有属性“isConnected”

我正在尝试使用 Web3 连接到以太坊区块链。当我使用 jupyter Notebook 安装 web3 时,我不断收到 Web3 没有属性的错误。有人可以建议如何连接到以太坊网络吗?

我的代码

pip install web3

from web3 import Web3, EthereumTesterProvider
w3 = Web3(EthereumTesterProvider())
w3.isConnected() 
Run Code Online (Sandbox Code Playgroud)

错误

AttributeError Traceback (most recent call last)
Input In [29], in <cell line: 3>()
  1 from web3 import EthereumTesterProvider
  2 w3 = Web3(EthereumTesterProvider())
----> 3 w3.isConnected()

AttributeError: 'Web3' object has no attribute 'isConnected'
Run Code Online (Sandbox Code Playgroud)

我已经尝试了 web3 和 Capital Web3,但仍然收到相同的错误。我也尝试过

w3 = Web3(Web3.EthereumTesterProvider()) 
Run Code Online (Sandbox Code Playgroud)

但同样的问题。

python ethereum jupyter-notebook web3py

3
推荐指数
1
解决办法
3563
查看次数

通过python计算uniswap对地址

我正在尝试基于此Solidity 示例,使用 python、web3 和 eth-abi 库计算(离线,即没有 http 请求)Uniswap 对的地址。

address factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
address token0 = 0xCAFE000000000000000000000000000000000000; // change me!
address token1 = 0xF00D000000000000000000000000000000000000; // change me!

address pair = address(uint(keccak256(abi.encodePacked(
  hex'ff',
  factory,
  keccak256(abi.encodePacked(token0, token1)),
  hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
))));
Run Code Online (Sandbox Code Playgroud)

有一些想法:

hexadem_ ='0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
hexadem_1 = 0xff
abiEncoded_1 = encode_abi_packed(['address', 'address'], (  token_0, token_1 ))
salt_ = web3.Web3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed(['bytes', 'address', 'bytes32'], (bytes(hexadem_1), factory, salt_))
resPair = web3.Web3.solidityKeccak(['bytes','bytes'], ['0x' +abiEncoded_2.hex(), hexadem_])
Run Code Online (Sandbox Code Playgroud)

有人可以建议我,出了什么问题,应该考虑哪种方式?

python hex solidity web3py uniswap

2
推荐指数
1
解决办法
4138
查看次数

执行恢复:SafeERC20:低级调用失败

我正在尝试使用 web3.py 获得闪贷。我能够成功部署闪贷合约,但是当我调用闪贷功能时,它给了我

执行恢复:SafeERC20:低级调用失败'失败错误。我的账户里有足够的以太币。

我需要向我的闪贷合约发送一些以太币吗?但我不认为这个错误是因为缺乏以太币来支付汽油费。(如果是这种情况请告诉我!)

下面是我的闪贷代码

pragma solidity ^0.6.6;

import "./aave/FlashLoanReceiverBaseV2.sol";
import "../../interfaces/v2/ILendingPoolAddressesProviderV2.sol";
import "../../interfaces/v2/ILendingPoolV2.sol";

contract FlashloanV2 is FlashLoanReceiverBaseV2, Withdrawable {
    constructor(address _addressProvider) FlashLoanReceiverBaseV2(_addressProvider) public {}

    function executeOperation(
        address[] calldata assets,
        uint256[] calldata amounts,
        uint256[] calldata premiums,
        address initiator,
        bytes calldata params
    )
        external
        override
        returns (bool)
    {

        // Approve the LendingPool contract allowance to *pull* the owed amount
        for (uint i = 0; i < assets.length; i++) {
            uint amountOwing = amounts[i].add(premiums[i]);
            IERC20(assets[i]).approve(address(LENDING_POOL), amountOwing);
        }
        return true;
    }

    function …
Run Code Online (Sandbox Code Playgroud)

python solidity web3js web3py

2
推荐指数
1
解决办法
4563
查看次数

从 ERC-20 代币地址获取有关代币的详细信息

我对 Web3 开发真的很陌生,想知道是否有一种方法可以从智能合约中找到的代币地址获取有关代币的更多信息(即我有一个代币地址,但不知道它是什么类型的代币,直到我在 etherscan 上查找交易哈希)。我可以直接从区块链/Web3 模块中提取这些信息,还是需要使用外部 API 来收集这些信息?

python cryptocurrency web3py

2
推荐指数
1
解决办法
1585
查看次数