-
Notifications
You must be signed in to change notification settings - Fork 1
/
035-interfaces-in-solidity.sol
45 lines (36 loc) · 1.11 KB
/
035-interfaces-in-solidity.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Interfaces
* @notice Interfaces are similar to abstract contracts
* and are created using interface keyword.
*
* Interfaces can not have any function with implementation
* Functions of an interface can be only of type external
* Interfaces can not have a constructor
* They can not have a state variable
* Interface can have enum, structs which can be accessed
* using the interface name dot notation.
*/
contract Counter {
uint256 public count;
function increment() external {
count += 1;
}
}
// creating interface
interface ICounter {
function count() external view returns (uint256);
function increment() external;
}
// to execute copy the address of the Counter contract
// and feed into the parameters of the functions defined
// at MyContract
contract MyContract {
function incrementCounter(address _counter) external {
ICounter(_counter).increment();
}
function getCount(address _counter) external view returns (uint256) {
return ICounter(_counter).count();
}
}