giacomomaraglino
giacomomaraglino

Reputation: 345

How to get ownership of smart contract using web3 Api Python?

I am practicing with web3 APIs and Python. I would like to extract the ownership of a specific smart contract giving the contract address (or smart contract) as input and getting as output the ownership of the contract.

For example if I check this contract 0x2A9718defF471f3Bb91FA0ECEAB14154F150a385 on BscScan reading the contract on point n.11 I can see the ownership is owned by this address 0x42997cf4fc165ebb8269cffc54a3198984367f02. How to automatize this process with web3 APIs on python? Or do you know any other method to get it except of selenium webdriver?

Upvotes: 0

Views: 2694

Answers (1)

furas
furas

Reputation: 142641

I don't using web3 and bitcoin, but I found a similar question on the Ethereum Stack Exchange site written in JavaScript.

Digging into Python examples on the internet, I created a version in Python:

from web3 import Web3

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

print("connected:", web3.isConnected())

abi = '''[
    {
      "constant": true,
      "inputs": [],
      "name": "owner",
      "outputs": [
        {
          "name": "",
          "type": "address"
        }
      ],
      "payable": false,
      "type": "function"
    },
    {
      "inputs": [],
      "payable": false,
      "type": "constructor"
    }
]'''

contract = web3.eth.contract(address="0x2A9718defF471f3Bb91FA0ECEAB14154F150a385", abi=abi)
owner = contract.functions.owner().call()

print("owner:", owner)

And this gives me 0x42997cf4fc165ebb8269cffc54a3198984367f02

Upvotes: 1

Related Questions