I like to automatically transfer funds from all my Metamask wallets into one central wallet automatically on the Polygon chain. How exactly do I do this? Currently I don't how to exactly approach this as the token I'd like to transact is on the polygon chain and I've only seen implementations for the Ethereum chain. This is the token: https://polygonscan.com/token/0x3a9A81d576d83FF21f26f325066054540720fC34
Also don't see an ABI there. It is still an ERC20 token, but I don't know how the implementation differs from a regular token on the Ethereum chain. Currently this is my code for just checking balance, but that doesn't work either as the contract address is not recognized. The error says: "Could not transact with/call contract function, is contract deployed correctly and chain synced?
from ethtoken.abi import EIP20_ABI
w3 = Web3(HTTPProvider("https://mainnet.infura.io/v3/..."))
contract_address = '0x3a9A81d576d83FF21f26f325066054540720fC34'
contract = w3.eth.contract(address=contract_address, abi=EIP20_ABI)
print(contract.address)
n1 = '0x...'
raw_balance = contract.functions.balanceOf(n1).call()
You are using wrong RPC url for Polygon Mainnet.
if you are using infura, then it should be like this:
w3 = Web3(HTTPProvider("https://polygon.infura.io/v3/YOUR_INFURA_PROJECT_ID"))
or you can use public RPC url:
w3 = Web3(HTTPProvider("https://polygon-rpc.com/"))
Related
I am trying to do a simple trade using uniswap-python and it doesn't work.
Sample code:
from uniswap import Uniswap
provider = "https://polygon-mainnet.infura.io/v3/"+INFURA_API_KEY
uniswap = Uniswap(address, private_key, version = 3, provider)
result = uniswap.make_trade(USDT, WMATIC, 10)
Result:
raise ExtraDataLengthError(web3.exceptions.ExtraDataLengthError:
The field extraData is 97 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('0xd3...d0')
I've checked PoA docs and tested all options without success. I always get the same message.
There are enough funds in my wallet to do the trade.
Any clues?
This is a problem with the web3 provider used internally by the uniswap-python module. If you're running on Polygon or another Proof of Authority chain (BNB for instance), you need to tell that to the web3 provider:
from web3.middleware import geth_poa_middleware
uniswap.w3.middleware_onion.inject(geth_poa_middleware, layer=0)
I'm trying to use the getAmountOut() function from the Uniswap Router-02 which should tell you how much of output token you should receive for a given input token amount.
Problem is I am getting the following error:
web3.exceptions.ContractLogicError: execution reverted: UniswapV2Library: INSUFFICIENT_LIQUIDITY.
Now does this function require you to use some ETH because it has to pay gas fees in order to execute or is there an error in my code ?
def checkSubTrade(exchange, in_token_address, out_token_address, amount):
# Should be general cross exchange but would have to check if each routers contain the same methods
router_address = w3.toChecksumAddress(router_dict[str(exchange)])
router_abi = abi_dict[str(exchange)]
router_contract = w3.eth.contract(address = router_address, abi = router_abi)
swap_path = [in_token_address, out_token_address]
output = router_contract.functions.getAmountsOut(amount, swap_path).call()
return output
output = checkSubTrade('uniswap', token_dict['WETH'], token_dict['UNI'], 100000000)
print(output())
token_dict, router_dict contain the addresses and abi_dict contains the ABI for the DEX.
i think you need to check two things.
router_address : maybe it is different then you think
when you go scope page that chain providing, you can see transaction
detail. and there is from and to, for example this link you
might see the to, in swap session to is route_address. so like that, you need to search in scope page to get route_address
router_abi : i think you using uniswapv2library, but in uniswap2library there is many other abi. for example factoryabi. so you need to check abi is correct or not. i highly recommend that you need to get or make solidity file of abi and change abi via web ide
I was tasked with fiding way how to add approved vacations to calendar from database. So I made this simple code:
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application")
def sendMeeting():
appt = outlook.CreateItem(1) # AppointmentItem
appt.Start = "2021-12-09 10:10" # yyyy-MM-dd hh:mm
appt.Subject = "Test test"
appt.Duration = 60
appt.Location = "Out of office"
appt.MeetingStatus = 1 # 1 - olMeeting; Changing the appointment to meeting. Only after changing the meeting status recipients can be added
appt.Organizer = "michal.liska#havelpartners.cz"
appt.Recipients.Add("michal.liska#havelpartners.cz") # Don't end ; as delimiter
appt.Save()
appt.Send()
print("sent")
sendMeeting()
This is all well, but problem is that if I sent the meeting It also appear in my calendar. So If I was to create it for 100 users my callendar would be spammed.
Also I can't connect directly to server and creating something more complex like this: Sending Meeting Invitations With Python. This is not desirable, because I presume, that I would need get login and password for all the users.
So is there some simple way to do it? Meaby just delete it afterwards or outlook parameter, that I can disable it with?
There is no way to get this working by automating Outlook locally.
Everything you do with the Outlook object model is related to the local accounts only. If you need to modify the remote calendar by adding appointments or meeting without customizing yours, consider using the EWS, Outlook REST API or Graph API if you use Exchange accounts or O365. See Explore the EWS Managed API, EWS, and web services in Exchange and One Outlook REST API - your favorite platform - 400+ million users for more information. Graph API is most reliable way because other technologies are going to die in near future.
Also you may consider delegating access to other accounts and use the NameSpace.GetSharedDefaultFolder method which returns a Folder object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Calendar folder). In that case you will be able to access calendar folders of users and add appointments directly there without sending anything. Here is the sample VBA code which illustrates the sequence of OOM calls:
Sub ResolveName()
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.Folder
Set myNamespace = Application.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev")
myRecipient.Resolve
If myRecipient.Resolved Then
Call ShowCalendar(myNamespace, myRecipient)
End If
End Sub
Sub ShowCalendar(myNamespace, myRecipient)
Dim CalendarFolder As Outlook.Folder
Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
CalendarFolder.Display
End Sub
I know the question is in Python, but the Outlook object model is common for all kind of programming languages, so hopefully it will not a problem to understand the OOM calls.
I'm writing a script that uses the requests library to obtain API data from a website requiring authentication. From my understanding and testing, the site doesn't seem to use auth-saving cookies, so authentication is required with each request, such as this:
page = requests.get('http://a_url.com', auth=(uname, pword))
This is fine for what I need except, that I have specific tasks split up into separate functions that handle different data in different ways. The only commonality is that they all require the username and password (obtained at the command line), so I've ended up with just short of every function taking "uname" and "pword" as their first parameters, followed by any other necessary params:
def get_all_projects(uname, pword):
# function
def get_single_project(uname, pword, project_name):
# function
def get_select_projects(uname, pword, projects):
for project in projects:
get_single_project(uname, pword, project)
# Etc...
Aside from turning the script into a class, is there any other way to deliver the credentials to the functions that need them without having to parameterize them? Requests.Session() won't work, since as stated above I need to include the auth with each call to get. I feel like a decorator would fit the situation well, but I don't understand them well enough yet to be able to confirm that belief.
As #barny pointed out, I missed the fact that you can indeed pass authentication to a requests.Session() via the following (from the docs and barny's comment):
s = requests.Session()
s.auth = (uname, pword)
so my url request from the op can now just be:
page = s.get('http://a_url.com')
with the auth automatically included as long as the session is active.
Along with that, my functions can now be condensed to just using the session object as a param instead of the username and password each time:
def get_all_projects(s):
# function
def get_single_project(s, project_name):
# function
def get_select_projects(s, projects):
for project in projects:
get_single_project(s, project)
# Etc...
I'm using Pyramid with Cornice to create an API for a Backbone.js application to consume. My current code is working perfectly for GET and POST requests, but it is returning 404 errors when it receives PUT requests. I believe that this is because Backbone sends them as http://example.com/api/clients/ID, where ID is the id number of the object in question.
My Cornice setup code is:
clients = Service(name='clients', path='/api/clients', description="Clients")
#clients.get()
def get_clients(request):
...
#clients.post()
def create_client(request):
...
#clients.put()
def update_client(request):
...
It seems that Cornice only registers the path /api/clients and not /api/clients/{id}. How can I make it match both?
The documentation gives an example of a service that has both an individual path (/users/{id}) and an object path (/users). Would this work for you ?
#resource(collection_path='/users', path='/users/{id}')
A quick glance at the code for the resource decorator shows that it mainly creates two Service : one for the object and one for the collection. Your problem can probably be solved by adding another Service :
client = Service(name='client', path='/api/clients/{id}', description="Client")