how to get to know where is interface's link between implementation and declaration on ethereum blockchain - interface

I don't get that how to connect interface declaration to implementation part in solidity
Here is pseudo code that leaves only important part which is related.
ApproveAndCall.sol
contract ApproveAndCall {
function receiveApproval(address _sender, uint256 _amount, address _addressOfToken, bytes _extraData) external {
emit ReceiveApproval(_sender, _amount, _addressOfToken, _extraData);
}
}
TokenERC20.sol
interface tokenRecipient {
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external;
}
contract TokenERC20 is Pausable {
function approveAndCall(address _spender, uint256 _value, bytes _extraData) public noReentrancy returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
spender.receiveApproval(msg.sender, _value, this, _extraData);
}
}
just like you see, interface "tokenRecipient" was declared in TokenERC20.sol
and the tokenRecipient named "spender" would call function "receiveApproval".
but How does TokenERC20 smart contract know that real "receiveApproval" which is called by "spender"?
I think it seems not like any connection with addresses or some other thing.
Both smart contract was already deployed on rinkeby testnet. and it still seems to work well.

This interface is just for convenience, so you can easily call the interface methods. You can cast any smart contract address to this interface, even if they are not an instance of this interface.
For instance, if I would make the following changes to your example:
ApproveAndCall.sol
contract ApproveAndCall {
function receiveApproval(address _sender, uint256 _amount, address _addressOfToken, bytes _extraData) external {
emit ReceiveApproval(_sender, _amount, _addressOfToken, _extraData);
}
}
contract ApproveAndCall2 {
function() public {
emit Fallback(msg.data);
}
}
TokenERC20.sol
interface tokenRecipient {
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external;
}
contract TokenERC20 is Pausable {
function approveAndCall(address _spender, uint256 _value, bytes _extraData) public noReentrancy returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
spender.receiveApproval(msg.sender, _value, this, _extraData);
}
}
If the address of an ApproveAndCall contract would be used as the _spender parameter, it would work as expected, because the corresponding function is in fact defined in the smart contract, so receiveApproval gets called.
However, if the address of an ApproveAndCall2 contract would be used as the _spender parameter, instead the 'fallback function' would be called, because the receiveApproval function does not exist on the ApproveAndCall2 contract. The msg.data variable contains the encoded calldata for this function call (function name, parameter values etc.).

Related

Add .aar file to Flutter project [duplicate]

I am writing a native plugin that, in some cases, has to call functions in the Flutter portion of the app, written in Dart.
How it's achieved, is explained here:
https://flutter.io/platform-channels/
Furthermore, an example of invoking a method from the native/platform part towards the Dart/non-native is here:
https://github.com/flutter/plugins/tree/master/packages/quick_actions
Now, this example is really nice in case the platform only needs to invoke a method, i.e. that call returns nothing/void, but in case it needs to invoke a function, i.e. needs a return value from the non-native/Dart part, I could not have found an example or documentation on the internet. I believe it can be implemented though, because in the native Java part, there is a method:
public void invokeMethod(String method, Object arguments, MethodChannel.Result callback)
So, there is a callback object that could have a return value from the non-native part - or, I am mistaken here, and there is currently no way of returning a value from the non-native Dart portion of the app?
The signature is void setMethodCallHandler(Future<dynamic> handler(MethodCall call)), so we need to provide a function at the Dart end that returns Future<dynamic>, for example _channel.setMethodCallHandler(myUtilsHandler);
Then implement the handler. This one handles two methods foo and bar returning respectively String and double.
Future<dynamic> myUtilsHandler(MethodCall methodCall) async {
switch (methodCall.method) {
case 'foo':
return 'some string';
case 'bar':
return 123.0;
default:
throw MissingPluginException('notImplemented');
}
}
At the Java end the return value is passed to the success method of the Result callback.
channel.invokeMethod("foo", arguments, new Result() {
#Override
public void success(Object o) {
// this will be called with o = "some string"
}
#Override
public void error(String s, String s1, Object o) {}
#Override
public void notImplemented() {}
});
In Swift, the return value is an Any? passed to the result closure. (Not implemented is signaled by the any parameter being the const NSObject value FlutterMethodNotImplemented.)
channel.invokeMethod("foo", arguments: args, result: {(r:Any?) -> () in
// this will be called with r = "some string" (or FlutterMethodNotImplemented)
})

Flutter: how to call dart code from native (swift, kotlin)? [duplicate]

I am writing a native plugin that, in some cases, has to call functions in the Flutter portion of the app, written in Dart.
How it's achieved, is explained here:
https://flutter.io/platform-channels/
Furthermore, an example of invoking a method from the native/platform part towards the Dart/non-native is here:
https://github.com/flutter/plugins/tree/master/packages/quick_actions
Now, this example is really nice in case the platform only needs to invoke a method, i.e. that call returns nothing/void, but in case it needs to invoke a function, i.e. needs a return value from the non-native/Dart part, I could not have found an example or documentation on the internet. I believe it can be implemented though, because in the native Java part, there is a method:
public void invokeMethod(String method, Object arguments, MethodChannel.Result callback)
So, there is a callback object that could have a return value from the non-native part - or, I am mistaken here, and there is currently no way of returning a value from the non-native Dart portion of the app?
The signature is void setMethodCallHandler(Future<dynamic> handler(MethodCall call)), so we need to provide a function at the Dart end that returns Future<dynamic>, for example _channel.setMethodCallHandler(myUtilsHandler);
Then implement the handler. This one handles two methods foo and bar returning respectively String and double.
Future<dynamic> myUtilsHandler(MethodCall methodCall) async {
switch (methodCall.method) {
case 'foo':
return 'some string';
case 'bar':
return 123.0;
default:
throw MissingPluginException('notImplemented');
}
}
At the Java end the return value is passed to the success method of the Result callback.
channel.invokeMethod("foo", arguments, new Result() {
#Override
public void success(Object o) {
// this will be called with o = "some string"
}
#Override
public void error(String s, String s1, Object o) {}
#Override
public void notImplemented() {}
});
In Swift, the return value is an Any? passed to the result closure. (Not implemented is signaled by the any parameter being the const NSObject value FlutterMethodNotImplemented.)
channel.invokeMethod("foo", arguments: args, result: {(r:Any?) -> () in
// this will be called with r = "some string" (or FlutterMethodNotImplemented)
})

How to use an asynchronous validation function with the flutter_login package

I'm trying to use the flutter_login package. The FlutterLogin widget it provides accepts a userValidator field, which accepts a custom function that should have a return type of String? (null is returned if the username is valid, if not, then an error message e.g. 'invalid username').
The problem is that my validation process involves asynchronous steps (as I have to check with my backend database). So assuming a function body for the userValidator field like so:
static Future<String?> usernameValidator(username) async {
bool validated = await myAPI.validateUsername(username);
if (validated) {
return null;
}
return 'invalid username';
}
This throws an error as the function expects String? and not Future<String?>:
"The argument type 'Future<String?> Function(dynamic)' can't be assigned to the parameter type 'String? Function(String?)?'."
So I tried to workaround the issue of returning a Future by using .then():
static String? usernameValidator(username) {
myAPI.validateUsername(username).then((val) {
if (val) {
return null;
}
return 'invalid username';
});
}
However, it seems that anything returned inside the body of .then() doesn't return from the outside function, i.e. userValidator doesn't actually return 'invalid username'.
I'm sure this must have a trivial solution I'm not seeing, since this is a really popular package and validating usernames and emails must surely require asynchronous processes as long as a backend is involved. What's the solution?

In Solidity Language, what happens when a function with given parameters on different contracts but different types of data stored for Interface?

For Solidity Programing Language,
While defining Interface, we name it as per our needs and then call it as needed with the same types of inputs.
What happens when there are the same function names with the same parameter names but different types of data stored?
Eg. Here are two different contracts stored on Ethereum Blockchain separately. What will happen if while writing a 3rd smart contract, I create an interface and call function with name "favoriteNumber". Which value will it return? 10 or 15?
contract A {
uint randomnumber = 10;
function favoriteNumber (uint _number) public view returns(uint randomnumber) {
}
}
contract B {
uint randomnumber = 15;
function favoriteNumber (uint _number) public view returns(uint randomnumber) {
}
}
It will depends on what contract you call, each contract have it unique address with each own storage

PowerRegisterSuspendResumeNotification - provided callback function doesn't work as expected

I register my application to receive notification when the system is suspended or resumed.
MSDN documentation
Function I'd like to be executed after application receives notification (I tried both void and void CALLBACK and both work same way):
void isConnectedStandby()
{
printf( "ConnectedStandby Request");
}
1st case - I provide pointer to the isConnectedStandby function, but system treats as a double pointer to the function - it calls an address which is under this callback pointer.
HPOWERNOTIFY RegistrationHandle;
PowerRegisterSuspendResumeNotification(
DEVICE_NOTIFY_CALLBACK,
&isConnectedStandby,
&RegistrationHandle
);
2nd case - here I provide as follows (this way my function code is executed):
typedef void (*StatusFunction_t)();
StatusFunction_t StatusFunction = isConnectedStandby;
HPOWERNOTIFY RegistrationHandle;
PowerRegisterSuspendResumeNotification(
DEVICE_NOTIFY_CALLBACK,
&isConnectedStandby,
&RegistrationHandle
);
System calls not only mine function, but all addresses after the first one (if I provide an array of functions, it executes one after another to crash when there is no valid code available)
What is the correct way to use this function?
Function declaration (must be static ULONG with 3 parameters as you can see below):
static ULONG isConnectedStandby(PVOID Context, ULONG Type, PVOID Setting);
ULONG isConnectedStandby(PVOID Context, ULONG Type, PVOID Setting)
{
printf( "ConnectedStandby Request");
return 0;
}
Istead of providing callback function directly to PowerRegisterSuspendResumeNotification we have to provide struct _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS filled with our functions address :
static _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS testCallback = {
isConnectedStandby,
nullptr
};
HPOWERNOTIFY RegistrationHandle;
PowerRegisterSuspendResumeNotification(
DEVICE_NOTIFY_CALLBACK,
&testCallback,
&RegistrationHandle
);
MSDN documentation did not mention any of those information.