Skip to main content

Use subscriptions in your contract

tip

You can also view this tutorial on Dev.to. ✨

A few steps are needed to enable subscriptions for your contract.

Step 1

Create an account that will manage the subscription for the contract using Arthera Wallet UI, we call it the subscription owner. The subscription owner account can whitelist its users so they can interact with the app without paying any gas fees.


Step 2

Create the ISubscriptionOwner.sol file:

pragma solidity ^0.8.0;

interface ISubscriptionOwner {
function getSubscriptionOwner() external view returns (address);
}

Step 3

Import ISubscriptionOwner.sol and @openzeppelin/contracts/utils/introspection/ERC165.sol:

import "./ISubscriptionOwner.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";

Step 4

Your contract must inherit from ISubscriptionOwner and ERC165:

contract YourContract is ISubscriptionOwner, ERC165 {
/// ... contract code ....
}

Step 5

At the end of your contract code, you must add the following methods:

/// ... contract code ....

function getSubscriptionOwner() external view returns (address) {
// the owner of the subscription must be an EOA
// Replace this with the account created in Step 1
return <ADDRESS_OF_THE_SUBSCRIPTION_OWNER>;
}

function supportsInterface(bytes4 interfaceId) public view override(ERC165) returns (bool) {
return interfaceId == type(ISubscriptionOwner).interfaceId || super.supportsInterface(interfaceId);
}

The ISubscriptionOwner interface allows Arthera to determine the owner of the subscription, which must be an EOA account (it can't be a contract).

If your contract is ownable, your getSubscriptionOwner function would look like this:

function getSubscriptionOwner() external view returns (address) {
return owner();
}

If your contracts inherits from other contracts, you want to make sure the order of inheritances is correct (the ERC165 often goes first).

Step 6

Deploy your contract to Arthera Testnet.

  • RPC Endpoint: https://rpc-test.arthera.net
  • Chain ID: 10243

Step 7

  • Go to the Arthera SMP (Subscription Management Platform)
  • In the left menu, click on "On-Chain"
  • Connect with the subscription owner wallet
  • Click on "Subscribe" (DApp Plan)
  • Paste your contract address in the field

At this stage, you're ready to whitelist your users so they can interact with your service without paying any gas fees.

There are 3 different ways to do this:

  • Let your users checkout in the SMP which will whitelist them automatically after a successful checkout as explained at Checkout
  • Whitelist your user in the SMP API as explained at Whitelisting without checkout
  • In your app admin interface, you can whitelist users by calling the whitelistAccount function from the subscription owner wallet:
await subscribers.whitelistAccount(subId, userAddress);

The ABI of the Subscribers contract is on GitHub, the address is:

0x000000000000000000000000000000000000aa07
info

Latest update: January 22, 2024