Skip to content

Commit

Permalink
feat: move getInvitations into circles data
Browse files Browse the repository at this point in the history
  • Loading branch information
Wagalidoom committed Dec 4, 2024
1 parent 2382383 commit 2ccd20f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 72 deletions.
112 changes: 68 additions & 44 deletions packages/data/src/circlesData.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {CirclesQuery} from './pagedQuery/circlesQuery';
import {TransactionHistoryRow} from './rows/transactionHistoryRow';
import {TrustListRow} from './rows/trustListRow';
import {TokenBalanceRow} from './rows/tokenBalanceRow';
import {CirclesRpc} from './circlesRpc';
import {AvatarRow} from './rows/avatarRow';
import { CirclesQuery } from './pagedQuery/circlesQuery';
import { TransactionHistoryRow } from './rows/transactionHistoryRow';
import { TrustListRow } from './rows/trustListRow';
import { TokenBalanceRow } from './rows/tokenBalanceRow';
import { CirclesRpc } from './circlesRpc';
import { AvatarRow } from './rows/avatarRow';
import {
attoCirclesToCircles,
attoCirclesToStaticAttoCircles, circlesToAttoCircles,
Expand All @@ -12,19 +12,19 @@ import {
tcToCrc,
uint8ArrayToCidV0
} from '@circles-sdk/utils';
import {TrustRelation, TrustRelationRow} from './rows/trustRelationRow';
import {CirclesDataInterface, GroupQueryParams} from './circlesDataInterface';
import {Observable} from './observable';
import {CirclesEvent} from './events/events';
import {InvitationRow} from './rows/invitationRow';
import {PagedQueryParams} from './pagedQuery/pagedQueryParams';
import {Filter} from './rpcSchema/filter';
import {GroupMembershipRow} from './rows/groupMembershipRow';
import {GroupRow} from './rows/groupRow';
import {TokenInfoRow} from './rows/tokenInfoRow';
import {parseRpcSubscriptionMessage, RcpSubscriptionEvent} from './events/parser';
import {FilterPredicate} from "./rpcSchema/filterPredicate";
import {EventRow} from "./pagedQuery/eventRow";
import { TrustRelation, TrustRelationRow } from './rows/trustRelationRow';
import { CirclesDataInterface, GroupQueryParams } from './circlesDataInterface';
import { Observable } from './observable';
import { CirclesEvent } from './events/events';
import { InvitationRow } from './rows/invitationRow';
import { PagedQueryParams } from './pagedQuery/pagedQueryParams';
import { Filter } from './rpcSchema/filter';
import { GroupMembershipRow } from './rows/groupMembershipRow';
import { GroupRow } from './rows/groupRow';
import { TokenInfoRow } from './rows/tokenInfoRow';
import { parseRpcSubscriptionMessage, RcpSubscriptionEvent } from './events/parser';
import { FilterPredicate } from "./rpcSchema/filterPredicate";
import { EventRow } from "./pagedQuery/eventRow";

export type TrustEvent = {
blockNumber: number;
Expand Down Expand Up @@ -393,7 +393,7 @@ export class CirclesData implements CirclesDataInterface {
trustListRows.forEach(row => {
const addToBucket = (key: string) => {
if (!trustBucket[key]) {
trustBucket[key] = {rows: [], version: new Set()};
trustBucket[key] = { rows: [], version: new Set() };
}
trustBucket[key].rows.push(row);
trustBucket[key].version.add(row.version);
Expand All @@ -410,7 +410,7 @@ export class CirclesData implements CirclesDataInterface {
// Determine trust relations
return Object.entries(trustBucket)
.filter(([avatar]) => avatar !== avatarAddress)
.map(([avatar, {rows, version}]) => {
.map(([avatar, { rows, version }]) => {
const versionRelations: { [key: number]: TrustRelation } = {};
const maxTimestamp = Math.max(...rows.map(o => o.timestamp));

Expand Down Expand Up @@ -604,36 +604,60 @@ export class CirclesData implements CirclesDataInterface {
}

/**
* TODO: update this comment if implemented
* Gets the invitations sent by an avatar.
* @param avatar The avatar to get the invitations for.
* @param pageSize The maximum number of invitations per page.
* @returns A CirclesQuery object to fetch the invitations.
*/
getInvitations(avatar: string, pageSize: number): CirclesQuery<InvitationRow> {
return new CirclesQuery<InvitationRow>(this.rpc, {
namespace: 'CrcV2',
table: 'InviteHuman',
columns: [
'blockNumber',
'transactionIndex',
'logIndex',
'timestamp',
'transactionHash',
'inviter',
'invited'
],
filter: [
{
Type: 'FilterPredicate',
FilterType: 'Equals',
Column: 'inviter',
Value: avatar.toLowerCase()
async getInvitations(avatar: string): Promise<AvatarRow[]> {
const MIN_TOKENS_REQUIRED = 96;
const avatarInfo = await this.getAvatarInfo(avatar);
if (avatarInfo?.version == 2) return [];

const v2Relations = await this.getAggregatedTrustRelations(avatar, 2);

const humanInviters: AvatarRow[] = [];

for (const relation of v2Relations) {
const inviterInfo = await this.getAvatarInfo(relation.subjectAvatar);

if (inviterInfo?.isHuman &&
(relation.relation === 'trusts' || relation.relation === 'mutuallyTrusts')) {
const balance = await this.getTotalBalanceV2(relation.subjectAvatar);
if (parseFloat(balance) >= MIN_TOKENS_REQUIRED) {
humanInviters.push(inviterInfo);
}
],
sortOrder: 'DESC',
limit: pageSize
});
}
}

return humanInviters;
}
// getInvitations(avatar: string, pageSize: number): CirclesQuery<InvitationRow> {
// return new CirclesQuery<InvitationRow>(this.rpc, {
// namespace: 'CrcV2',
// table: 'InviteHuman',
// columns: [
// 'blockNumber',
// 'transactionIndex',
// 'logIndex',
// 'timestamp',
// 'transactionHash',
// 'inviter',
// 'invited'
// ],
// filter: [
// {
// Type: 'FilterPredicate',
// FilterType: 'Equals',
// Column: 'inviter',
// Value: avatar.toLowerCase()
// }
// ],
// sortOrder: 'DESC',
// limit: pageSize
// });
// }

/**
* Gets the avatar that invited the given avatar.
Expand Down
3 changes: 2 additions & 1 deletion packages/data/src/circlesDataInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export interface CirclesDataInterface {
* @param avatar The address to get the invitations for.
* @param pageSize The maximum number of invitations per page.
*/
getInvitations(avatar: string, pageSize: number): CirclesQuery<InvitationRow>;
// getInvitations(avatar: string, pageSize: number): CirclesQuery<InvitationRow>;
getInvitations(avatar: string, pageSize: number): Promise<AvatarRow[]>;

/**
* Gets the avatar that invited the given avatar.
Expand Down
5 changes: 0 additions & 5 deletions packages/sdk/src/AvatarInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ export interface AvatarInterface {
* Gets the total supply of either this avatar's Personal or Group Circles.
*/
getTotalSupply(): Promise<bigint>;

/**
* Gets all inviters of the avatar (for v1).
*/
getInviters(): Promise<TrustRelationRow[]>;
}

/**
Expand Down
17 changes: 0 additions & 17 deletions packages/sdk/src/v1/v1Avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,21 +292,4 @@ export class V1Avatar implements AvatarInterface {
throw new Error('Pathfinder is not available');
}
}

async getInviters(): Promise<TrustRelationRow[]> {
const v2Relations = await this.sdk.data.getAggregatedTrustRelations(this.address, 2);

const humanInviters: TrustRelationRow[] = [];

for (const relation of v2Relations) {
const inviterInfo = await this.sdk.data.getAvatarInfo(relation.subjectAvatar);

if (inviterInfo?.isHuman &&
(relation.relation === 'trusts' || relation.relation === 'mutuallyTrusts')) {
humanInviters.push(relation);
}
}

return humanInviters;
}
}
5 changes: 0 additions & 5 deletions packages/sdk/src/v2/v2Avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,4 @@ export class V2Avatar implements AvatarInterfaceV2 {
throw new Error('Name registry is not available');
}
}

// We don't return any inviter if avatar is already in v2
async getInviters(): Promise<TrustRelationRow[]> {
return [];
}
}

0 comments on commit 2ccd20f

Please sign in to comment.