Skip to main content

Overview

This tutorial explains how you can create channel and trigger notifications directly from your smart contract by importing Push core and comm smart contract and using its interface.

We will be creating a simple contract that allows owner to create a channel using ipfshash and send notification for this guide.

Prerequisites

  • Ensure you have know what is a channel (Don't need to create from dApp as we will be creating it from your smart contract) - Creating your channel
  • Ensure you know types of notifications to know what you want to send - Types of notifications

What's going to happen

  • We import interface and function call to create channel function
  • We import interface and function call to trigger notification
  • Watch in 😲 as each payment in contract now notifies the wallet address about tokens received

Tutorial time

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 Push Core to create channel and Push communicator to send notification whenever the contract receives fund.

To learn more about how to import interface and the function call of send notification, please see send notification (via smart contract).

Code

//SPDX-License-Identifier: UNLICENSED
pragma solidity >0.5.0 <0.9.0;

//EPNS Core Contract Interface
interface IEPNSCoreInterface {
enum ChannelType {
ProtocolNonInterest,
ProtocolPromotion,
InterestBearingOpen,
InterestBearingMutual
}

function createChannelWithFees(
ChannelType _channelType,
bytes calldata _identity,
uint256 _amount
) external;


}

//EPNS Comm Contract Interface
interface IEPNSCommInterface {
function sendNotification(address _channel, address _recipient, bytes memory _identity) external;
}

//ERC20 Interface to approve sending push
interface IERC20Interface {
function approve(address spender, uint256 amount) external returns (bool);
}

contract Fund {
address public EPNS_CORE_ADDRESS =0x97D7c5f14B8fe94Ef2b4bA589379f5Ec992197dA;
address public EPNS_COMM_ADDRESS=0x87da9Af1899ad477C67FeA31ce89c1d2435c77DC;
address public PUSH_ADDRESS = 0xf418588522d5dd018b425E472991E52EBBeEEEEE;
address payable public owner;

constructor() {
owner = payable(msg.sender);
}

modifier onlyOwner() {
require(msg.sender == owner, "Only owner can perform the task");
_;
}

//To create channel
function createChannelWithEPNS(string memory _ipfsHash) public onlyOwner {
IERC20Interface(PUSH_ADDRESS).approve(EPNS_CORE_ADDRESS, 50 ether);
IEPNSCoreInterface(EPNS_CORE_ADDRESS).createChannelWithPUSH(
IEPNSCoreInterface.ChannelType.InterestBearingOpen,
bytes(string(
abi.encodePacked(
"1",
"+",
_ipfsHash
)
)),
50 ether
);
}

// To send notification when the contract receives fund
receive() external payable {
IEPNSCommInterface(EPNS_COMM_ADDRESS).sendNotification(address(this), address(this), bytes(string(
// We are passing identity here: https://docs.epns.io/developers/developer-guides/sending-notifications/advanced/notification-payload-types/identity/payload-identity-implementations
abi.encodePacked(
"0", // this is notification identity: https://docs.epns.io/developers/developer-guides/sending-notifications/advanced/notification-payload-types/identity/payload-identity-implementations
"+", // segregator
"1", // this is payload type: https://docs.epns.io/developers/developer-guides/sending-notifications/advanced/notification-payload-types/payload (1, 3 or 4) = (Broadcast, targeted or subset)
"+", // segregator
"Incoming Funds", // this is notification title
"+", // segregator
"Hooray! Someone sent you some funds!", // notification body
)
)));
}

function transferFundToOwner() public payable onlyOwner{
owner.transfer(address(this).balance);
}

function checkAmount() public view returns(uint){
return address(this).balance;
}
}

Note: Make sure you have transferred some $PUSH 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:

  address public EPNS_CORE_ADDRESS =0x97D7c5f14B8fe94Ef2b4bA589379f5Ec992197dA;
address public EPNS_COMM_ADDRESS=0x87da9Af1899ad477C67FeA31ce89c1d2435c77DC;
address public PUSH_ADDRESS = 0xf418588522d5dd018b425E472991E52EBBeEEEEE;

The createChannelWithEPNS takes in IPFS hash which contains the channel details as per protocol standard. Then the function approves Push Core Contract to spend 50 PUSH. At last, it calls the createChannelWithFees of Push Core Contract to create 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 Push 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.

That's it! Your channel should now send notification as soon as anyone transfer any ether to your contract 🎉🎉🎉!