Smart Contracts Example
Along with the UI one can also use contract to contract interaction to create channel as well as send notification.
Here is an example of an contract that uses EPNS Core to create channel and EPNS communicator to send notification whenever the contract receives fund
1
//SPDX-License-Identifier: UNLICENSED
2
pragma solidity >0.5.0 <0.9.0;
3
​
4
//EPNS Core Contract Interface
5
interface IEPNSCoreInterface {
6
enum ChannelType {
7
ProtocolNonInterest,
8
ProtocolPromotion,
9
InterestBearingOpen,
10
InterestBearingMutual
11
}
12
​
13
function createChannelWithFees(
14
ChannelType _channelType,
15
bytes calldata _identity,
16
uint256 _amount
17
)external;
18
​
19
​
20
}
21
​
22
//EPNS Comm Contract Interface
23
interface IEPNSCommInterface {
24
function sendNotification(address _channel,
25
address _recipient,
26
bytes memory _identity)
27
external;
28
}
29
​
30
//ERC20 Interface to approve sending dai
31
interface IERC20Interface {
32
function approve(address spender, uint256 amount) external returns (bool);
33
}
34
​
35
contract Fund {
36
address public EPNS_CORE_ADDRESS =0x97D7c5f14B8fe94Ef2b4bA589379f5Ec992197dA;
37
address public EPNS_COMM_ADDRESS=0x87da9Af1899ad477C67FeA31ce89c1d2435c77DC;
38
address public DAI_ADDRESS = 0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD;
39
address payable public owner;
40
​
41
constructor() {
42
owner = payable(msg.sender);
43
}
44
​
45
modifier onlyOwner() {
46
require(msg.sender == owner, "Only owner can perform the task");
47
_;
48
}
49
50
//To create channel
51
function createChannelWithEPNS(string memory _ipfsHash) public onlyOwner {
52
IERC20Interface(DAI_ADDRESS).approve(EPNS_CORE_ADDRESS, 50 ether);
53
IEPNSCoreInterface(EPNS_CORE_ADDRESS).createChannelWithFees(
54
IEPNSCoreInterface.ChannelType.InterestBearingOpen,
55
bytes(string(
56
abi.encodePacked(
57
"2",
58
"+",
59
_ipfsHash
60
)
61
)),
62
50 ether
63
);
64
}
65
​
66
// To send notification when the contract receives fund
67
receive() external payable {
68
IEPNSCommInterface(EPNS_COMM_ADDRESS).sendNotification(address(this), address(this), bytes(string(
69
abi.encodePacked(
70
"1",
71
"+",
72
"QmSyKMiRvpQpiaUyXR3BmCXjSme6xFNxypD5Jn8GTBAELM"
73
)
74
)));
75
}
76
77
78
79
function transferFundToOwner() public payable onlyOwner{
80
owner.transfer(address(this).balance);
81
}
82
83
function checkAmount() public view returns(uint){
84
return address(this).balance;
85
}
86
}
87
​
88
​
Copied!
​
Note: Make sure you have transferred some DAI to the contract address so that later it will used while creating the channel.
Some of the constants that would be used throughout the code:
1
address public EPNS_CORE_ADDRESS =0x97D7c5f14B8fe94Ef2b4bA589379f5Ec992197dA;
2
address public EPNS_COMM_ADDRESS=0x87da9Af1899ad477C67FeA31ce89c1d2435c77DC;
3
address public DAI_ADDRESS = 0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD;
Copied!
The createChannelWithEPNS takes in IPFS hash which contains the channel details as per EPNS standard. Then the function approves EPNS Core Contract to spend 50 DAI. At last, it calls the createChannelWithFees of EPNS Core Contract to crate the channel.
After the channel is created, we want to notify all the subscribers whenever funds are sent to the contract. This is where we will use the EPNS Communicator contract to send notification. The receive function is a special function that gets executed whenever ether is sent to the contract. So, here we are calling the sendNotification of the communicator contract. The parameters include channel, recipient and the message body in the form of identity bytes.
This example might be outdated, We will be updating the example very soon ;)
Copy link