rituals-sdk v0.1.14
Rituals SDK
Rituals SDK is a Typescript SDK that makes it easy to build applications on top of Rituals smart contracts. You can use Rituals contracts to host challenges and token gated communities and tournaments.
Architecture
Installation
Install the package with npm in your terminal:
npm install rituals-sdk
Once it's installed, import the module into your project as shown below.
import Rituals from 'rituals-sdk';
// or
const Rituals = require('rituals-sdk');
Quick examples
Get Rituals By Creator
This code fetches all published.
// you provide these values
const infuraKey = 'your-infura-api-key';
const privateKey = '0xf00...';
const ritualService = await Rituals.initialize('https', {
url: `https://mainnet.infura.io/v3/${infuraKey}`,
privateKey,
chainId: 1,
});
const result = ritualService.getPublishedRituals();
console.log(result);
Rituals.initialize
You can configure the behavior of Rituals SDK by passing different arguments to Rituals.initialize. The first argument is the name of a preset, and the second is an options object.
Presets
'browser'
Use this preset when using the library in a browser environment. It will attempt to connect using window.ethereum or window.web3. Make sure you pass in the connected browser provider instance'https'
Connect to a JSON-RPC node. Requires url to be set in the options.'custom'
Connect to a custom transport.
// browser
const ritualService = await Rituals.initialize('browser');
// https
const ritualService = await Rituals.initialize('https');
// custom transport, for example, privy
const ritualService = await Rituals.initialize('custom', {
chainId: currentChain.id,
customProvider: provider,
account: wallet.account?.address,
});
Options
privateKey
- Optional. The private key used to sign transactions. If this is omitted, the first account available from the Ethereum provider will be used. Only used with the 'https' preset.
- If this is omitted and the provider does not have an unlocked account, the Rituals object will start in read-only mode.
url
- The URL of the node to connect to. Only used with the 'http' preset.
account
- An account object containing an address, 0xaddress.
chainId
- The chainId to connect to. Must match a supported chain.
API Reference
RitualService
This class encapsulates interactions with the Ritual Service contract with maintains and index of all rituals that have been created and tracks them by creator and membership.
createAndInitializeRitual
creates a new ritual contract and returns the address of the new contract.
const ritualAddress = await rs.createAndInitializeRitual({
title: 'Hot Girl Summer Walk',
description: 'Coming in hottt 🔥',
isPrivate: false,
startDate: BigInt(17402846583920),
endDate: BigInt(17402846584819),
joinCutoff: BigInt(0),
capacity: BigInt(250),
tags: ['hot', 'girl', 'summer', 'walk'],
schedule: [
{ day: Day.MON.valueOf(), timeInSeconds: BigInt(0) },
{ day: Day.TUE.valueOf(), timeInSeconds: BigInt(27000) },
{ day: Day.WED.valueOf(), timeInSeconds: BigInt(0) },
{ day: Day.THU.valueOf(), timeInSeconds: BigInt(27000) },
{ day: Day.FRI.valueOf(), timeInSeconds: BigInt(0) },
{ day: Day.SAT.valueOf(), timeInSeconds: BigInt(27000) },
{ day: Day.SUN.valueOf(), timeInSeconds: BigInt(0) },
],
});
getRItualsByCreator
const publishedRituals = await ritualService.getRItualsByCreator(`0x{address}`);
getRitualsWhereMember
const publishedRituals = await ritualService.getRitualsWhereMember(`0x{address}`);
getPublishedRituals
const rituals = await ritualService.getPublishedRituals();
getActivities
returns a list of available activities to attest to
const activities = await ritualService.getActivities();
getActivity
const activity = await ritualService.getActivity('running');
console.log(activity.label, ' has ', activity.availableMetrics.length, ' trackable metrics');
deleteDraft
const tx_hash = await ritualService.deleteDraft(`0xcontractAddress`);
if (tx_hash) console.log('draft deleted');
getRitualInstance
// connect to a ritual contract
const ritual = ritualService.getRitualInstance(`0x{address}`);
// get the ritual's details
const ritualInfo = await ritual.get();
console.log(ritualInfo.title, ' starts on ', ritualInfo.startTime);
Ritual
This class supoorts interactions with an individual Ritual contract and encapsulates admin interactions like initializing, setting fields and publishing as well as membership interactions like joining, leaving and claiming rewards.
publish
// publish a ritual to make it joinable
const tx_hash = await ritual.publish();
if (tx_hash) console.log('publish successful');
join
// try joining a ritual
const tx_hash = await ritual.join();
if (tx_hash) console.log('join successful');
leave
// leave the ritual
const tx_hash = await ritual.leave();
if (tx_hash) console.log('leave successful');
get
// get the ritual's details
const ritualInfo = await ritual.get();
console.log(
ritualInfo.title,
' starts on ',
ritualInfo.startTime,
' and lives at ',
ritualInfo.ritualAddress,
' and has ',
ritualInfo.members.length,
' members.',
);
console.log('Memeber 1 is ' ritualInfo.members[0].account);
addGoal
const tx_hash = await ritual.addGoal('running', 'distance', 500000, 'to a bagel shop each day');
addTags
const tx_hash = await ritual.addTags('hotgirlsummer', 'walking', 'nyc');
setStartDate
const offsetInDays = 10;
const timeInSeconds = Math.floor((Date.now() + offsetInDays * 86400000) / 1000);
const tx_hash = await ritual.setStartDate(BigInt(timeInSeconds));
if (tx_hash) console.log('set start date successful');
setEndDate
const offsetInDays = 40;
const timeInSeconds = Math.floor((Date.now() + offsetInDays * 86400000) / 1000);
const tx_hash = await ritual.setEndDate(BigInt(timeInSeconds));
if (tx_hash) console.log('set end date successful');
setMetadata
const tx_hash = await ritual.setMetadata('coverPhoto', 'https://picsum.photos/500/200');