Get list of recent added/deployed smart contracts - ethereum
Is there a way to retrieve the most recent added/deployed smart contracts to the ethereum chain?
Thanks a lot for any advice.
Regards,
JR
Newly deployed contract addresses are not directly available through the node RPC API (and its wrappers such as ethersjs and web3js), but you can build a script that
Subscribes to newly published blocks
Loops through transaction receipts on the block
And searches for contractAddress property of the transaction receipt
The contractAddress property returns null if the transaction does not deploy a contract (most transactions) or an address if the transaction deploys a contract.
The above mentioned approach only retrieves contracts that were deployed directly - by sending a transaction with empty to field and data field containing the contract bytecode.
It does not retrieve contracts that were deployed using an internal transaction. For example it would not catch the following Hello contract deployment (that is deployed by executing the deployHello() function).
pragma solidity ^0.8;
contract Hello {}
contract HelloFactory {
event Deployed(address);
function deployHello() external {
// deploys a new instance of the `Hello` contract to a new address
address deployedTo = address(
new Hello()
);
emit Deployed(deployedTo);
}
}
If you want to retrieve deployments of these contracts as well, then you'd need to debug each newly produced block and search for the create and create2 opcodes (each of them deploys a new contract, they take different input arguments).
So overall, it's not a simple task but it's doable.
It's generally discouraged to recommend any specific 3rd party APIs and tools here at StackOverflow. But I'm guessing that there are already some existing services that do all of this on their backend, and are able to return the aggregated list of newly deployed contracts as a result.
Thanks to the help of Petr Hejda I created the following Python code:
import sys
import time
from web3 import Web3
def connect():
# infura API key
API_KEY = "INFURA_API_KEY"
# change endpoint to mainnet or ropsten or any other of your account
url = f"https://mainnet.infura.io/v3/{API_KEY}"
w3 = Web3(Web3.HTTPProvider(url))
# res = w3.isConnected()
# print(res)
return w3
def get_latest_block(connection):
return connection.eth.get_block('latest')
def get_latest_block_id(block_information):
return block_information['number']
def search_new_contracts(connection, block_information):
block_transactions = block_information['transactions']
# print(block_transactions)
for transaction in block_transactions:
transaction_data = connection.eth.getTransaction(transaction)
if transaction_data['to'] == "":
print(f"Contract creation transaction: {transaction_data}")
print("Block searching done")
if __name__ == "__main__":
current_block_number = sys.maxsize
connection = connect()
while True:
latest_block = get_latest_block(connection)
latest_block_id = get_latest_block_id(latest_block)
if current_block_number is not latest_block_id:
print(f'New block detected: {latest_block_id}')
search_new_contracts(connection, latest_block)
current_block_number = latest_block_id
Problem is that it is slow so I have to see if I can speed things up.
Related
How to generate arbitrary wallet seeded with eth in hardhat tests using ethers.js?
I'm currently trying to run a test in hardhat/waffle that requires hundreds of unique wallets to call a contract, using new ethers.Wallet.createRandom(). However, none of these wallets are supplied with eth, so I can't call/send transactions with them. What would be the simplest/most effective way to supply an arbitrary amount of randomly generated wallets with eth? Thanks!
I think you'll probably want to use the hardhat network method hardhat_setBalance, the docs use an example like this: await network.provider.send("hardhat_setBalance", [ "<ACCOUNT ADDRESS>", "0x1000", # 4096 wei ]); I'm not developing in javascript though, but I've been doing something similar in python using web3.py and eth-account with code like this: from web3 import Web3 from eth_account import Account chain = Web3(HTTPProvider("http://127.0.0.1:8545")) acct = Account.create('<RANDOM VALUE>') address = Web3.toChecksumAddress(acct.address) print(chain.eth.get_balance(address)) # 0 wei # add a balance of eth tokens to the address chain.provider.make_request("hardhat_setBalance", [address, "0x1000"]) print(chain.eth.get_balance(address)) # 4096 wei
Hardhat allows to seed a custom amount of prefunded addresses using the accounts.count config option of the built-in hardhat network. Example: module.exports = { networks: { hardhat: { accounts: { count: 1000 } } } } Docs: https://hardhat.org/hardhat-network/reference/#config The new ethers.Wallet.createRandom() function only creates an account from a random private key in the context of the JS application, but it doesn't communicate with the provider (Hardhat in your case).
saw this answer, and i think it solves the problem: https://github.com/ethers-io/ethers.js/issues/686 const wallet = ethers.Wallet.createRandom().connect(provider);
Smartcontract/Lottery : Errors during execution of running scripts
While running the deploy_lottery.py I am getting the following error: The transactions executes until def start_lottery(). Once it reaches the def enter_lottery the programs stops execution with the following error: 1."sender doesn't have enough funds to send tx." I am running the script in local development chain with a "id" account.
Can you share your code please? mine looks like this and it works python def startLotter(): account = get_account() lottery = Lottery[-1] starting_tx = lottery.startLottery({"from": account}) starting_tx.wait(1) print("The lottery has started!!") solidity function startLottery() public onlyOwner { require(lottery_state == LOTTERY_STATE.CLOSED, "lottery already open"); lottery_state = LOTTERY_STATE.OPEN; } the function only has to change the state of the lottery but that does require a transaction so you should have the funds.
How do I solve the Smart Contract error "returned values aren't valid"?
I am trying to read basic information from a smart contract using web3.js (GRAPH Token): https://etherscan.io/address/0xc944e90c64b2c07662a292be6244bdf05cda44a7#code This is my super simple react web3.js setup: import Web3 from 'web3'; ... useEffect(() => { const start = async () => { const web3 = new Web3('https://bsc-dataseed1.binance.org:443') const abi = [{"inputs":[{"internalType":"uint256","name":"_initialSupply","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"}],"name":"MinterAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"}],"name":"MinterRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"}],"name":"NewOwnership","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"}],"name":"NewPendingOwnership","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"acceptOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"addMinter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burn","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burnFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"governor","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"isMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"nonces","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingGovernor","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"},{"internalType":"uint8","name":"_v","type":"uint8"},{"internalType":"bytes32","name":"_r","type":"bytes32"},{"internalType":"bytes32","name":"_s","type":"bytes32"}],"name":"permit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"removeMinter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceMinter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newGovernor","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}] const address = '0xc944E90C64B2c07662A292be6244BDf05Cda44a7' const contract = new web3.eth.Contract(abi, address) console.log(contract); const i = await contract.methods.getName().call() console.log(i); // => ERROR } start() }, []) And it throws error: Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced. This is a known and misleading error which usually means that the address or the contract abi is invalid but I verified it is correct. I researched this error for two days but didn't manage to solve this issue... any suggestions?
You're trying to interact with an Ethereum contract, using a BSC (Binance Smart Chain) Web3 provider (in your case https://bsc-dataseed1.binance.org:443). This results in trying to call the getName() function on the BSC address (which doesn't contain any contract). Ethereum and BSC are different networks, unrelated to each other. Solution: Use an Ethereum mainnet provider (for example Infura is widely used).
multisig wallet submitTransaction
I have a smart contract MyContract which is owned by a multisig wallet (Gnosis). When I submitTransaction from web3/testrpc on the multisig wallet for executing a function from MyContract with its parameters, and confirm the transaction, it is the fallback function from MyContract which is executed in the end. So it seems like the function call which is passed as raw data to the Gnosis submitTransaction method is not well interpreted. I have tried 2 ways to get the transaction data and both give the same result: function from MyContract that I want to execute on mycontract2 instance through Multisig: function setTransferOperator(address newOperator) data generated using ethereumjs-abi lib: var data = abi.methodID('setTransferOperator', ['address']).toString('hex') + abi.rawEncode(['address'], [accounts[8]]).toString('hex'); return gnosiswallet.submitTransaction(mycontract2.address, 0, data, {from: accounts[0]}); the same data is generated using truffle: var data2 = mycontract2.setTransferOperator.request(accounts[8]); The transaction with data succeeds in a simple scenario when mycontract2 is owned by a standard account
Is it possible to export Xpub Key from Ledger Nano S Ethereum Wallet
I need to give payment ETH address to my customers for deposit ETH their accounts. I want to use a HD ETH wallet for this and I am using Ledger Nano S now. But Ledger showing me only 1 receive address so I need ETH wallet's XPub for generate many address from it for distribute to users. If Ledger support HD, How can I export XPub? If ledger does not support, Which wallet can usable for this purpose. Any suggestions.
You can't export the xpub directly from a ledger nano s with the default bitcoin or ethereum apps. But you can construct an xpub using the data you can extract from the ledger. What you need is the public key and chaincode for the bip32 path you're interested in and the public key of the parent of that path. In the ethereum app you would use getaddress (currently there is a restriction to m/44/60', m/44'/61', and m/44'/1' and below for this app (I have an issue here to hopefully remove that restriction). In the bitcoin app you would use getwalletpublickey. There is no bip32 path restrictions in the bitcoin app. Once you have the two public keys and the chain code, you can construct the xpub. Here is a chunk of python that can accomplish this. It uses the un-maintained pybitcointools, so user-beware. #!/usr/bin/env python import sys import binascii from bitcoin import bin_hash160 from bitcoin import bip32_serialize from bitcoin import compress def main(parent_pubkey, pubkey, chaincode, depth, index, network): if (network.lower() == "main"): network_bytes = b'\x04\x88\xb2\x1e' elif (network.lower() == "test"): network_bytes = b'\x04\x35\x87\xcf' else: sys.exit("network must be either main or test") compressed_pubkey_bytes = binascii.unhexlify(compress(pubkey)) fingerprint_bytes = bin_hash160(binascii.unhexlify(compress(parent_pubkey)))[:4] chaincode_bytes = binascii.unhexlify(chaincode) deserialized = (network_bytes, int(depth), fingerprint_bytes, int(index), chaincode_bytes, compressed_pubkey_bytes) xpub = bip32_serialize(deserialized) print(xpub) if __name__ == '__main__': if (len(sys.argv) < 7): sys.exit("USAGE: generate_xpub PARENT_PUBLICKEY PUBLICKEY CHAINCODE DEPTH INDEX (MAIN|TEST)") main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6])