Overview
This tutorial explains how you can integrate notifications directly in your smart contract by importing Push comm smart contract and using its interface. We will be creating a simple token transfer notification for this guide.
Prerequisites
- Ensure you have created your channel - Creating your channel
- Ensure you know what are channel delegates - Adding channel delegates
- Ensure you know types of notifications to know what you want to send - Types of notifications
What's going to happen
- We create a channel and copy it's address
- We import interface and function call to trigger notification
- We deploy the contract and add the contract address as delegate
- Watch in 😲 as each transfer now notifies the wallet address about tokens received
Tutorial time
Push protocol also supports sending notifications to user wallets directly from your smart contract and that too by only adding 2 lines of extra code to your smart contract 🎉.
The best way to do it is by creating your channel first > tweaking your smart contract with notification logic and finally adding the deployed contract address as delegate to your channel.
To learn more about how to import interface and the function call of send notification, please see send notification (via smart contract).
Code
pragma solidity ^0.8.20;
// SPDX-License-Identifier: UNLICENSED
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.5.0/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/v4.9.0/contracts/utils/StringsUpgradeable.sol";
// PUSH Comm Contract Interface
interface IPUSHCommInterface {
function sendNotification(address _channel, address _recipient, bytes calldata _identity) external;
}
contract Push is ERC20 {
using StringsUpgradeable for uint256;
using StringsUpgradeable for address;
// EPNS COMM ADDRESS ON ETHEREUM SEPOLIA, CHECK THIS: https://docs.epns.io/developers/developer-tooling/epns-smart-contracts/epns-contract-addresses
address public EPNS_COMM_ADDRESS = 0x0C34d54a09CFe75BCcd878A469206Ae77E0fe6e7;
constructor ()
ERC20("Push Token", "PUSH")
{
_mint(msg.sender, 1000 * 10 ** uint(decimals()));
}
function transfer(address to, uint amount) override public returns (bool success) {
address owner = _msgSender();
_transfer(owner, to, amount);
address sender = msg.sender;
uint256 readableAmount = amount / 10 ** uint(decimals());
//"0+3+Hooray! ", msg.sender, " sent ", token amount, " PUSH to you!"
IPUSHCommInterface(EPNS_COMM_ADDRESS).sendNotification(
0x554d29160f779Adf0a4328597cD33Ea1Df4D9Ee9, // from channel
to, // to recipient, put address(this) in case you want Broadcast or Subset. For Targeted put the address to which you want to send
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
"3", // 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
"Tranfer Alert", // this is notification title
"+", // segregator
"Hooray! ", // notification body
sender.toHexString(), // notification body
" sent ", // notification body
readableAmount.toString(), // notification body
" PUSH to you!" // notification body
)
)
)
);
return true;
}
}
Add delegate
The only remaining step is to deploy your contract and then go to your channel and add the smart contract address as delegate to your channel which will allow that smart contract address to send notification on behalf of your channel.
That's it! Your channel should now send notification as soon as anyone transfer your tokens from one address to the other 🎉🎉🎉!