PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label web3py. Show all posts
Showing posts with label web3py. Show all posts

Sunday, September 25, 2022

[FIXED] How to call a Smart Contract function using Python and web3.py

 September 25, 2022     blockchain, ethereum, python, web3py     No comments   

Issue

I have a contract deployed on Ethereum test network which has some functions in it and they all happen to work while using the Remix interface. When trying to call those functions using web3.py in Python, I'm able to call only for public functions and that part works fine. The problem is calling a function with a "restriction" such as having an "owner requirement", meaning only the address which created the contract can call that specific function. I've Googled it but no luck. I'm guessing that I am supposed to use both the "address" and the "password" for that Ethereum account as parameters when calling the function but I have no idea how to do it. Function is called "set()" and it takes only 2 string values.

Here is the part of Solidity code which makes the function "set()" accessible only by the owner of this contract.

constructor() public {
    owner = msg.sender;
}

modifier onlyOwner() {
    require(msg.sender == owner);
    _;
}

function set(string memory _lastHash,
             string memory _fullHash) public onlyOwner {
    lastHash = _lastHash;
    fullHash = _fullHash;
}

Here is the python function which i'm using to get the return values from the other 2 functions which i've not included:

data = contract.functions.getFullHash().call()

Function is called "getFullHash()". Given Python code doesn't work with the function "set()".


Solution

Since my original comment got deleted I'll post it one last time.

I've managed to do it with the instructions provided on this link. Here is the code that worked for me:

transaction = contract.functions.set(
    'string1',
    'string2' ).buildTransaction({
    'gas': 70000,
    'gasPrice': web3.toWei('1', 'gwei'),
    'from': adress,
    'nonce': nonce
    }) 
private_key = "enter_your_key_here" 
signed_txn = web3.eth.account.signTransaction(transaction, private_key=private_key)
web3.eth.sendRawTransaction(signed_txn.rawTransaction)

I read somewhere that Infura only accepts raw signed transactions, not sure if its true but it worked this way.



Answered By - Moltenpowa
Answer Checked By - David Marino (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, July 28, 2022

[FIXED] How to get all transaction data from the entire Ethereum network using web3py

 July 28, 2022     blockchain, ethereum, python-3.x, web3py     No comments   

Issue

I'm trying to run some analysis on cryptocurrency(e.g. Bitcoin, Ethereum) data but having trouble finding data sources. For example, I'd like to collect transaction data such as input address, output address, transaction time, transaction amount, etc. for Ethereum.

I've found that I can access Ethereum data with web3py but is it possible to get data for "ALL" transactions that have made recently in the entire Ethereum network, not just the transactions connected to my own wallet(address)? For example, I'd like to get data on all Ethereum transaction occurred today.

Also, do I must have my own Ethereum wallet(address) in order to access their data with web3py? I wonder whether I need a specific address as a starting point or I can just scrape the data without creating a wallet.

Thanks.


Solution

For example, I'd like to collect transaction data such as input address, output address, transaction time, transaction amount, etc. for Ethereum.

You can iterate over all blocks and transactions using web3.eth.get_block call. You need, however, parse the transaction content yourself.

To access all the data, it is recommended that you run your own node to have the maximum network bandwidth for JSON-RPC calls.

Also, do I must have my own Ethereum wallet(address) in order to access their data with web3py?

Address is just a derived from a random number and you do not need to generate one.



Answered By - Mikko Ohtamaa
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing