def print_current_token_balance(self, token): if not self.config_check(): return try: token_contract = w3.eth.contract(address=w3.toChecksumAddress(self.get_token_info(token)["address"]), abi=token_abi) token_balance = token_contract.functions.balanceOf(self.get_public_key()).call() token_balance_normalized = w3.fromWei(int(token_balance), 'ether') print("Current Balance: {} {}".format(token_balance_normalized, token)) except: print("Token not supported")
def send(funder_pubkey, funder_privkey): print("Distributor functions:"); print( Distributor.all_functions() ) token_address = '0x4575f41308EC1483f3d399aa9a2826d74Da13Deb' token_main = Token(address = '0x4575f41308EC1483f3d399aa9a2826d74Da13Deb') #distributor_main = Distributor(address = '0x3d971E78e9F5390d4AF4be8EDd88bCCD9040c75E') #funder.privateKey nonce = w3.eth.getTransactionCount(funder_pubkey); print(f"Funder nonce: {nonce}"); k0 = funder_pubkey; k1 = funder_privkey; n = nonce; rec = '0x25A20D9bd3e69a4c20E636F2679F2a19f595dA25'; owed_wei = distributor_main.functions.compute_owed_(rec, 1485279353).call(); owed = w3.fromWei(owed_wei, 'ether'); print(f"owed to {rec} at 1485279353 : {owed}"); owed_wei = distributor_main.functions.compute_owed_(rec, 1585279353).call(); owed = w3.fromWei(owed_wei, 'ether'); print(f"owed to {rec} at 1585279353 : {owed}"); owed_wei = distributor_main.functions.compute_owed_(rec, 1595277426).call(); owed = w3.fromWei(owed_wei, 'ether'); print(f"owed to {rec} at 1595277426 : {owed}"); owed_wei = distributor_main.functions.compute_owed_(rec, 1716813426).call(); owed = w3.fromWei(owed_wei, 'ether'); print(f"owed to {rec} at 1716813426 : {owed}"); unixtime = date(3,27,2020) print(f"3/27/2020: {unixtime}"); send_list(funder_pubkey, funder_privkey, k0,k1,n);
def get_staking_positions(address): """Given an address, returns all the staking positions.""" # abi is the same for all contracts abi_path = os.path.join(MODULE_DIRECTORY, "abi.json") with open(abi_path) as f: abi = json.loads(f.read()) positions = [] for staking_contract, lp_contract in STAKING_POOLS.items(): contract = web3.eth.contract(staking_contract, abi=abi) balance = contract.functions.balanceOf(address).call() if balance > 0: pair_info = get_pair_info(lp_contract) # this is the only missing key compared with the # `get_liquidity_positions()` call balance = web3.fromWei(balance, "ether") pair_info["liquidityTokenBalance"] = balance pair_info["pair"]["staking_contract_address"] = staking_contract positions.append(pair_info) return positions
def api_arbitrage_detector(self, from_token, to_token, quantity): try: tasks = [self.fetch(from_token, to_token, quantity, "", "")] focus, swap_from_result = loop.run_until_complete(asyncio.gather(*tasks))[0] if swap_from_result.status_code != 200: print("Failure getting initial quote") return print(str(w3.fromWei(int(swap_from_result.json()['fromTokenAmount']), 'ether')) + " " + swap_from_result.json()['fromToken']['symbol'] + " to " + str(w3.fromWei(int(swap_from_result.json()['toTokenAmount']), 'ether')) + " " + swap_from_result.json()['toToken']['symbol']) exchanges = requests.get('https://api.1inch.exchange/v1.1/exchanges') hide = "" for exchange in exchanges.json(): hide += exchange["name"] + "," block_splits = hide[0:len(hide) - 2].split(",") blockers = [] blocker_1_copy = block_splits.copy() blocker_1_copy.remove('Uniswap') blockers.append(blocker_1_copy) blocker_2_copy = block_splits.copy() blocker_2_copy.remove('Curve.fi v2') blockers.append(blocker_2_copy) blocker_3_copy = block_splits.copy() blocker_3_copy.remove('Curve.fi') blockers.append(blocker_3_copy) blocker_4_copy = block_splits.copy() blocker_4_copy.remove('MultiSplit') blockers.append(blocker_4_copy) blocker_5_copy = block_splits.copy() blocker_5_copy.remove('Balancer') blockers.append(blocker_5_copy) blocker_6_copy = block_splits.copy() blocker_6_copy.remove('Kyber') blockers.append(blocker_6_copy) tasks = [] for blocker in blockers: tasks.append(self.fetch(to_token, from_token, int(swap_from_result.json()['toTokenAmount']), ",".join(blocker), self.diff(blocker, block_splits))) results = loop.run_until_complete(asyncio.gather(*tasks)) for result in filter(lambda result: result[1].status_code == 200, results): swap_to_json = result[1].json() print(str(w3.fromWei(int(swap_to_json['fromTokenAmount']), 'ether')) + " " + swap_to_json['fromToken']['symbol'] + " to " + str(w3.fromWei(int(swap_to_json['toTokenAmount']), 'ether')) + " " + str(swap_to_json['toToken']['symbol']) + ": " + str(result[0])) if int(swap_to_json['toTokenAmount']) > int(swap_from_result.json()['fromTokenAmount']): print("Arbitrage Detected for: {}".format(result[0])) return print("No Arbitrage Opportunity Detected") except Exception as e: print(e)
def print_current_balance(self): if not self.config_check(): return account = w3.eth.account.privateKeyToAccount(privateKey) print("Current Balance: {}".format(w3.fromWei(w3.eth.getBalance(account.address), 'ether')))
import json from web3.auto.infura import w3 # confirm that the connection succeeded print( w3.isConnected() ) # True # 下面会报错 # coinbase = w3.eth.coinbase # print('coinbase: ', coinbase) balance = w3.eth.getBalance('0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7') balance = w3.fromWei(balance, 'ether') print('balance: ', balance) balance = w3.eth.getBalance('0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb') balance = w3.fromWei(balance, 'ether') print('balance: ', balance) ERC20_ABI = json.loads('[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}]') # noqa: 501 # Address field should be the checksum address at which the ERC20 contract was deployed erc20 = w3.eth.contract(address='0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7', abi=ERC20_ABI) print('erc20: ', erc20) print('address: ', erc20.address) # print('name: ', erc20.name()) print( erc20.functions.name().call() ) # 'Unicorns' try:
from web3.auto.infura import w3 import tqdm import sys K = 51 file = open('./data' + sys.argv[1] + '.csv', 'w') for i in tqdm.tqdm(range(200), unit="block"): n = 8962400 - K * 1000 + i + int(sys.argv[1]) block = w3.eth.getBlock(n) gasUsed = block['gasUsed'] gasLimit = block['gasLimit'] miner_tax = 0 smart_contract = 0 transactions_count = len(block['transactions']) if transactions_count > 0: for tc in range(transactions_count): transaction = w3.eth.getTransactionByBlock(n, tc) if transaction['input'] != '0x': smart_contract += 1 gasPrice = transaction['gasPrice'] gasUsed = w3.eth.getTransactionReceipt( block['transactions'][tc])['gasUsed'] miner_tax = miner_tax + gasUsed * gasPrice text = str(w3.fromWei(miner_tax, 'ether')) + "," + str( transactions_count) + "," + str(smart_contract) + '\n' file.write(text) file.close()
def convert_wei(self, amount, denom): return float(w3.fromWei(amount, denom))
def get_balance(self): return float(w3.fromWei(w3.eth.getBalance(self.address), 'ether'))
def list_unverifiedusers(request, country, admin_name): data = {'country': country, 'admin_name': admin_name} username = None users = UserProfile.objects.filter(country=country, verified='0').order_by('-created') data['users'] = users wallet_users = 0 for user in users: referer = '' wallet = None try: referer = UserProfile.objects.get( member_id=user.referral_id).username except Exception: pass setattr(user, "referer", referer) taker_trades = Offers.objects.filter(taker=user.username) volume = 0 for trade in taker_trades: volume += trade.amount setattr(user, "tradevol", w3.fromWei(volume, 'ether')) maker_trades = Offers.objects.filter(maker=user.username) volume = 0 for trade in maker_trades: volume += trade.amount setattr(user, "makervol", w3.fromWei(volume, 'ether')) try: wallet = Wallet.objects.get(username=user.username) except Exception: pass if wallet: if wallet.tx_hash: setattr(user, "approved", True) wallet_users += 1 timezones = { 'my': 8, 'cn': 8, 'cn-wb': 8, 'id': 7, 'au': 6, 'ng': 1, 'vn': 7, 'gh': 0, 'za': 2, 'tz': 3, 'bw': 2, 'otc': 8, 'ph': 8, } user.created = user.created + timedelta(hours=timezones[country]) data['wallet_users'] = wallet_users data['no_wallet_users'] = len(users) - wallet_users num = len(users) for user in users: setattr(user, 'num', num) num -= 1 data['num'] = num return render(request, 'unverified_user.html', data)
def debug_send(k0, k1, n): #badrec = '0x4a31c1B033dc7209a82e8e60BF5e19DD662705e8'; #owed_wei = distributor_main.functions.calculate(badrec, date(3,5,2020) ).call(); owed = w3.fromWei(owed_wei, 'ether'); #print(f"total to {badrec} at 3/5/2020 : {owed}"); rec = '0x7361c5B033dc7209a81e8e60BF5e19DD66270672' num_funcs = distributor_main.functions.num_limits(rec).call() print(f"{rec} has {num_funcs} funcs") if (num_funcs > 0): temp = distributor_main.functions.get_beg(rec, 0).call() print(f"get_beg({rec},0): {temp} ") temp = distributor_main.functions.get_end(rec, 0).call() print(f"get_end({rec},0): {temp} ") temp = distributor_main.functions.get_amt(rec, 0).call() print(f"get_amt({rec},0): {temp} ") temp = distributor_main.functions.get_beg(rec, 1).call() print(f"get_beg({rec},1): {temp} ") temp = distributor_main.functions.get_end(rec, 1).call() print(f"get_end({rec},1): {temp} ") temp = distributor_main.functions.get_amt(rec, 1).call() print(f"get_amt({rec},1): {temp} ") owed_wei = distributor_main.functions.calculate_at( rec, date(3, 5, 2020), 0).call() owed = w3.fromWei(owed_wei, 'ether') print(f"calculate_at( {rec}, 3/5/2020, 0) : {owed}") owed_wei = distributor_main.functions.calculate_at( rec, date(3, 5, 2020), 1).call() owed = w3.fromWei(owed_wei, 'ether') print(f"calculate_at( {rec}, 3/5/2020, 1) : {owed}") owed_wei = distributor_main.functions.calculate_at( rec, date(3, 5, 2020), 2).call() owed = w3.fromWei(owed_wei, 'ether') print(f"calculate_at( {rec}, 3/5/2020, 2) : {owed}") owed_wei = distributor_main.functions.calculate(rec, date(3, 5, 2020)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"total to {rec} at 3/5/2020 : {owed}") owed_wei = distributor_main.functions.compute_owed_(rec, date(3, 5, 2020)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"owed to {rec} at 3/5/2020 : {owed}") owed_wei = distributor_main.functions.compute_owed_(rec, date(4, 5, 2020)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"owed to {rec} at 4/5/2020 : {owed}") owed_wei = distributor_main.functions.compute_owed_( rec, date(10, 5, 2020)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"owed to {rec} at 10/5/2020 : {owed}") owed_wei = distributor_main.functions.compute_owed_(rec, date(1, 5, 2021)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"owed to {rec} at 1/5/2021 : {owed}") owed_wei = distributor_main.functions.compute_owed_(rec, date(1, 5, 2023)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"owed to {rec} at 1/5/2023 : {owed}") unixtime = date(3, 27, 2020) print(f"3/27/2020: {unixtime}")
def debug_send(a, n): rec = a.addr k0 = a.FUNDER_PUBKEY k1 = a.FUNDER_PRIVKEY #badrec = '0x4a31c1B033dc7209a82e8e60BF5e19DD662705e8'; #owed_wei = distributor_main.functions.calculate(badrec, date(3,5,2020) ).call(); owed = w3.fromWei(owed_wei, 'ether'); #print(f"total to {badrec} at 3/5/2020 : {owed}"); #rec = '0x7361c5B033dc7209a81e8e60BF5e19DD66270672'; num_funcs = distributor_main.functions.num_limits(rec).call() print(f"{rec} has {num_funcs} funcs") if (num_funcs > 0): temp = distributor_main.functions.get_beg(rec, 0).call() #print(f"get_beg({rec},0): {temp} "); temp = distributor_main.functions.get_end(rec, 0).call() #print(f"get_end({rec},0): {temp} "); temp = distributor_main.functions.get_amt(rec, 0).call() #print(f"get_amt({rec},0): {temp} "); temp = distributor_main.functions.get_beg(rec, 1).call() #print(f"get_beg({rec},1): {temp} "); temp = distributor_main.functions.get_end(rec, 1).call() #print(f"get_end({rec},1): {temp} "); temp = distributor_main.functions.get_amt(rec, 1).call() #print(f"get_amt({rec},1): {temp} "); owed_wei = distributor_main.functions.calculate_at( rec, date(3, 5, 2020), 0).call() owed = w3.fromWei(owed_wei, 'ether') print(f"calculate_at( {rec}, 3/5/2020, 0) : {owed}") ValueError: { 'code': -32000, 'message': 'transaction underpriced' } owed_wei = distributor_main.functions.calculate_at( rec, date(3, 5, 2020), 1).call() owed = w3.fromWei(owed_wei, 'ether') print(f"calculate_at( {rec}, 3/5/2020, 1) : {owed}") owed_wei = distributor_main.functions.calculate_at( rec, date(3, 5, 2020), 2).call() owed = w3.fromWei(owed_wei, 'ether') print(f"calculate_at( {rec}, 3/5/2020, 2) : {owed}") owed_wei = distributor_main.functions.calculate(rec, date(3, 5, 2020)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"total to {rec} at 3/5/2020 : {owed}") owed_wei = distributor_main.functions.compute_owed_(rec, date(3, 5, 2020)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"owed to {rec} at 3/5/2020 : {owed}") owed_wei = distributor_main.functions.compute_owed_(rec, date(4, 5, 2020)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"owed to {rec} at 4/5/2020 : {owed}") owed_wei = distributor_main.functions.compute_owed_( rec, date(10, 5, 2020)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"owed to {rec} at 10/5/2020 : {owed}") owed_wei = distributor_main.functions.compute_owed_(rec, date(1, 5, 2021)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"owed to {rec} at 1/5/2021 : {owed}") owed_wei = distributor_main.functions.compute_owed_(rec, date(1, 5, 2023)).call() owed = w3.fromWei(owed_wei, 'ether') print(f"owed to {rec} at 1/5/2023 : {owed}") unixtime = date(3, 27, 2020) print(f"3/27/2020: {unixtime}") return n
from web3.auto.infura import w3 import tqdm import sys K = 55 file_name = open('./data'+sys.argv[1]+'.csv','w') for i in tqdm.tqdm(range(200), unit="block"): n = 8962400 - K*1000 + i + int(sys.argv[1]) block = w3.eth.getBlock(n) used = block.gasUsed limit = block.gasLimit tax_m = 0 contract = 0 transactions_count =len(block.transactions) if transactions_count > 0: for tc in range(transactions_count): transaction = w3.eth.getTransactionByBlock(n,tc) if transaction.input != '0x': contract += 1 price = transaction.gasPrice used = w3.eth.getTransactionReceipt(block.hash).gasUsed tax_m = tax_m + used*price; output = str(w3.fromWei(tax_m,'ether')) +","+str(transactions_count) +","+ str(contract)+'\n' file_name.write(output) file_name.close()