我签订了一份智能合约来投票,但我的一个功能(“结果”)遇到了麻烦。这是智能合约:
pragma solidity ^0.5.7;
contract Voting{
struct Option{
string name;
uint256 votes;
}
Option[] private options;
address[] private alreadyVote;
address private owner = msg.sender;
constructor() public {}
function addOption(string memory _name) public{
require(msg.sender==owner,"Permiso denegado");
options.push(Option(_name,0));
}
function vote(string memory _name) public{
require(find(msg.sender,alreadyVote)==-1,"Ya ha ejercido su derecho a voto");
int candidateIndex = findOption(_name);
require(candidateIndex!=-1,"No se ha encontrado el candidato deseado");
options[uint(candidateIndex)].votes++;
alreadyVote.push(msg.sender);
}
function findOption(string memory _name) private view returns (int){
int result = -1;
for(uint i = 0;i<options.length;i++){
if(compareStrings(options[i].name,_name))
result …Run Code Online (Sandbox Code Playgroud) 我想使用 web3.py 和 infura.io 来监听 Uniswap 工厂事件,但是我不太确定在哪里这样做。
这是一个链接:https ://uniswap.org/docs/v2/smart-contracts/factory/
更具体地说,我想监听该PairCreated事件。
我首先尝试用 python 发送交易:
from web3 import Web3
transaction = {
'chainId': 97, # 97: Testnet. 56: main.
'to': '0xmyaddress',
'value': 1,
'gas': 2000000,
'gasPrice': 13,
'nonce': 0,
}
infura_url = "https://mainnet.infura.io/v3/my-api-key"
w3 = Web3(Web3.HTTPProvider(infura_url))
key = '0xmykey'
signed = w3.eth.account.signTransaction(transaction, key)
w3.eth.sendRawTransaction(signed.rawTransaction)
Run Code Online (Sandbox Code Playgroud)
给我以下错误:ValueError: {'code': -32000, 'message': 'invalid sender'}
现在,我正在尝试与合同进行交互 - 调用方法并提供输入,但我不确定如何完成此操作。
我想使用 web3 通过 Pancakeswap 将 BNB 兑换成其他代币,所以我遇到了这个似乎是正确的代码:
import time
import config as config
from web3 import Web3
PancakeABI = open('pancakeABI','r').read().replace('\n','')
bsc="https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
print(web3.isConnected())
#My own address to swap from
sender_address = "YOUR OWN WALLET PUBLIC ADDRESS"
#This is global Pancake V2 Swap router address
router_address = "0x10ED43C718714eb63d5aA57B78B54704E256024E"
#always spend using Wrapped BNB
#I guess you want to use other coins to swap you can do that, but for me I used Wrapped BNB
spend = web3.toChecksumAddress("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c")
#This is …Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法检查令牌是否已被批准用于支出。我不是在问如何批准令牌用于支出,我已经通过参考弄清楚了这一部分:如何批准令牌用于 (Uniswap 路由器合约) 上的支出。我希望我的脚本在出售代币之前先进行检查,然后根据结果决定是先运行批准还是跳过它。这样,每次购买和出售相同的代币时,它就不会批准已经批准的代币。感谢大家的帮助期待。
我正在尝试在项目中使用 AsyncHTTPProvider,这是我的测试代码:
import asyncio
from web3 import Web3
import asyncio
async def main():
w3 = Web3(Web3.AsyncHTTPProvider("http://127.0.0.1:8545"))
coinbase = await w3.eth.coinbase
print(coinbase)
if __name__ == "__main__":
asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)
我使用 ganache-cli 作为本地提供商。
我在网上找不到任何使用 AsyncHTTPProvider 的示例,即使在 web3py github 中我也找不到我能理解的示例(阅读大型库的拉取请求非常乏味)
当我运行此代码时,出现以下错误:
Traceback (most recent call last):
File "test.py", line 11, in <module>
asyncio.run(main())
File "C:\Program Files\Python37\lib\asyncio\runners.py", line 43, in run
return loop.run_until_complete(main)
File "C:\Program Files\Python37\lib\asyncio\base_events.py", line 587, in run_until_complete
return future.result()
File "test.py", line 7, in main
coinbase = await w3.eth.coinbase
File "D:\Name\Ethereum\FirstProject\env\lib\site-packages\web3\eth.py", line 282, …Run Code Online (Sandbox Code Playgroud) 因此,我已成功使用该函数decode_function_input(txn.input)对除合约创建输入数据之外的大多数交易的输入数据进行解码。是否有一种简单直接的方法可以使用 web3.py 创建合约来实现此目的?
“事件”中的哈希值不是合约创建哈希值,但是,它似乎不起作用。任何解决这个问题的见解将不胜感激。
尝试的方法:
event = "0x1113a21473aa89fcce64835d267dd480c9f0339411c676b1f22f11ae8e876b63"
txn = web3.eth.get_transaction(event)
txnInput = routerContract.decode_function_input(txn.input)
Run Code Online (Sandbox Code Playgroud) cytoolz/dicttoolz.c:19:10: fatal error: Python.h: No such file or directory
19 | #include "Python.h"
| ^~~~~~~~~~
compilation terminated.
error: command 'gcc' failed with exit status 1
----------------------------------------
ERROR: Failed building wheel for cytoolz
Run Code Online (Sandbox Code Playgroud)
我正在使用这个:
(venv) root@x-System-Product-Name:~# pypy3 --version
Python 3.6.9 (7.3.1+dfsg-4, Apr 22 2020, 05:15:29)
[PyPy 7.3.1 with GCC 9.3.0]
Run Code Online (Sandbox Code Playgroud)
我在安装 web3 时遇到了问题,能够解决 lru-dict、bitarray 和 cytoolz 包的问题。这些都是最引人注目的...
我尝试了互联网上的这些解决方案,它们对我不起作用:
pip3 install wheel
sudo apt-get install python-dev
sudo apt-get install python3-dev
sudo apt-get install libevent-dev
sudo apt-get install libblas-dev libatlas-base-dev
python -m venv …Run Code Online (Sandbox Code Playgroud) 我正在使用 web3 制作自己的桌面 BSC 钱包。目前我正在使用
private_key = "private key"
account = w3.eth.account.privateKeyToAccount(private_key)
Run Code Online (Sandbox Code Playgroud)
但我想使用助记词短语(如“你好约翰披萨吉他”)创建帐户。我一直在寻找,但无法实现。
如何通过区块哈希获取区块数据?我有兴趣获取每个新块的块时间戳。
from web3 import Web3
avalanche_url = 'https://api.avax.network/ext/bc/C/rpc'
provider = Web3(Web3.HTTPProvider(avalanche_url))
new_block_filter = provider.eth.filter('latest')
while True:
block_hashes = new_block_filter.get_new_entries()
for block_hash in block_hashes:
block = provider.eth.get_block(block_hash.hex())
print(block)
Run Code Online (Sandbox Code Playgroud)
这会导致错误:
web3.exceptions.ExtraDataLengthError: The field extraData is 80 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. The full extraData is: HexBytes('0x0000000000000000000000000001edd400000000000000000000000000000000000000000000000000000000002cb3970000000000000000000000000005902b00000000000000000000000000000000')
Run Code Online (Sandbox Code Playgroud)
同样的查询也适用于以太坊。
web3py ×10
python ×8
ethereum ×2
python-3.x ×2
avalanche ×1
binance ×1
blockchain ×1
gcc ×1
pypy ×1
solidity ×1
transactions ×1