in web3py, any function to call static? - web3py

I want to use Uniswap V3 Quoter function, but it's a write function. I have to call static to avoid gas cost.
I don't see any static call in web3py documentation. Any suggestion? Thanks ahead!

Just use
contractName.functions.xxxfunction(xxx,xxx,xx).call()

Related

Solidity two mint functions or one?

I have an ERC721 contract that has a whitelistEnabled property which, if enabled, requires the user to pass a merkleproof to the minting function. Currently, I have two functions: mint() {...} and mintWhitelist(merkleproof) {...}. Both functions have the same logic and custom implementation. The only difference is that the latter checks the merkleproof and the former doesn't.
Since the bulk of the two functions is the same, I was wondering if 'duplicating' the functions like this is the right, most gas efficient approach.
Would it be better to merge them into one? I think this might be the case, but I don't think there is a way to pass the merkleproof optionally (since there are no optional function parameters in Solidity).
Any help is appreciated!
You can add a boolean argument in the mint function for checks that need to merkelProof or not, if that was true you should check merhleProof and that logic is like WhiteList else you should ignore the merkelProof argument and that can be null or else.
but actually, this code is so dirty and increases the gas fee for anyone use mint (not WhitListMint) and I think your code is good.

Using a state variable from another contract

How I can use a state variable which is in another contract(contract_a) in my contract(contract_b).
That variable is public. I just want to use some special variables not all data that are in contract_1.
First answer here, I wish it'll be helpful.
It looks like every variable of a contract has an implicit getter method, which at first I thought was a little unusual.
When you call this variable from another contract, you are calling its getter method.
So, instead of calling car.color, you've got to call car.color().
I'm still learning, so DYOR.
When you use the import statement in a contract you import all the functions and all the variables of the smart contract you are importing.
In your contractB you need to have an instance of the contractA (or its address) and then call through this instance the variable you want to access. E.g:
import "./ContractA.sol"
contract ContractB {
ContractA instanceOfA;
function callA() public {
instanceOfA.variableYouWantToAccess();
}
}
Note the parentheses () after the name of the variable you want to access, that is because Solidity, for all the varibles, specifies a getter function which is the function you call in order to access these varibles.

Mockito - Is it possible to deep mock a void method call to do nothing?

I wanted to know if it is possible to "deep mock" a void method call without breaking out the call chain, using Mockito.
This is an example for the original call I want to mock:
obj.getSomething().add(3);
where "add"'s return type is void.
I tried:
doNothing().when(obj).getSomething().add(3)
and:
doNothing().when(obj.getSomething()).add(3) //wont work since "when" expects a mock.
I also failed using Mockito.when(...) since it does not work with void methods.
I do not want to break the call up since it will be very cumbersome for fluent API calls that are much longer.
Is there an official solution / workaround for this scenario?
Thanks :)
If the value returned by getSomething is not a mock, it won't work.
Return value of getSomething should be a mock and it will allow to assign mock behavior for that value.
Something someMock = mock(Something.class);
when(obj.getSomething()).thenReturn(someMock);
doNothing().when(someMock).add(3);

Mocking void functions using EasyMock with dynamic behaviour

I need to mock a void function with EasyMock such that the first call returns an Exception, while the next succeeds.
For example:
this.myObject.move((String) EasyMock.anyObject());
EasyMock.expectLastCall().once().andThrow(new RetryableDependencyException());
EasyMock.expectLastCall().once();
But this is not working.
That's not going to work as the second expectLastCall() cannot find any call.
Have you tried this:
this.myObject.move((String) EasyMock.anyObject());
EasyMock.expectLastCall().once().andThrow(new RetryableDependencyException());
this.myObject.move((String) EasyMock.anyObject());
EasyMock.expectLastCall().once();
I know it's a bit verbose but it should get you sorted :)
The shortest is
myObject.move(anyString());
expectLastCall().andThrow(new RetryableDependencyException()).asStub();
which assumes that is doesn't matter if the method is called more than once after the exception. If it matters, it will be
myObject.move(anyString());
expectLastCall().andThrow(new RetryableDependencyException());
myObject.move(anyString());
Interesting facts:
You can chain andReturn
I highly suggest static imports to get a cleaner code
anyString is used to prevent the cast in String
once() is not required as it is the default
expectLastCall isn't required, calling the void method is enough to record a call without any side effect (like a throw)

Convert Wstring to CString

i have a variable of Cstring,need to convert it to wstring.
The MultiByteToWideChar function is the base level Win32 API function to do this. Whatever library or framework you are using may offer a more convenient function that wraps this operation.