Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates and tests user referral queries, adds GetUserReferralFees.query #10522

Merged
merged 9 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions libs/adapters/src/rabbitmq/configuration/rascalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ export function getAllRascalConfigs(
arguments: queueOptions,
},
},
[RascalQueues.UserReferrals]: {
...queueConfig,
options: {
arguments: queueOptions,
},
},
[RascalQueues.FarcasterWorkerPolicy]: {
...queueConfig,
options: {
Expand Down Expand Up @@ -224,6 +230,12 @@ export function getAllRascalConfigs(
RascalRoutingKeys.XpProjectionUserMentioned,
],
},
[RascalBindings.UserReferrals]: {
source: RascalExchanges.MessageRelayer,
destination: RascalQueues.UserReferrals,
destinationType: 'queue',
bindingKeys: [RascalRoutingKeys.UserReferralsSignUpFlowCompleted],
},
[RascalBindings.FarcasterWorkerPolicy]: {
source: RascalExchanges.MessageRelayer,
destination: RascalQueues.FarcasterWorkerPolicy,
Expand Down Expand Up @@ -264,6 +276,10 @@ export function getAllRascalConfigs(
queue: RascalQueues.XpProjection,
...subscriptionConfig,
},
[RascalSubscriptions.UserReferrals]: {
queue: RascalQueues.UserReferrals,
...subscriptionConfig,
},
[RascalSubscriptions.FarcasterWorkerPolicy]: {
queue: RascalQueues.FarcasterWorkerPolicy,
...subscriptionConfig,
Expand Down
3 changes: 3 additions & 0 deletions libs/adapters/src/rabbitmq/rabbitMQConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export function getRabbitMQConfig(
RascalQueues.ContestWorkerPolicy,
RascalQueues.ContestProjection,
RascalQueues.XpProjection,
RascalQueues.UserReferrals,
RascalQueues.FarcasterWorkerPolicy,
RascalQueues.DiscordBotPolicy,
]);
Expand All @@ -85,6 +86,7 @@ export function getRabbitMQConfig(
RascalBindings.ContestWorkerPolicy,
RascalBindings.ContestProjection,
RascalBindings.XpProjection,
RascalBindings.UserReferrals,
RascalBindings.FarcasterWorkerPolicy,
RascalBindings.DiscordBotPolicy,
]);
Expand All @@ -98,6 +100,7 @@ export function getRabbitMQConfig(
RascalSubscriptions.ContestWorkerPolicy,
RascalSubscriptions.ContestProjection,
RascalSubscriptions.XpProjection,
RascalSubscriptions.UserReferrals,
RascalSubscriptions.FarcasterWorkerPolicy,
RascalSubscriptions.DiscordBotPolicy,
]);
Expand Down
5 changes: 5 additions & 0 deletions libs/adapters/src/rabbitmq/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum RascalSubscriptions {
ContestWorkerPolicy = BrokerSubscriptions.ContestWorkerPolicy,
ContestProjection = BrokerSubscriptions.ContestProjection,
XpProjection = BrokerSubscriptions.XpProjection,
UserReferrals = BrokerSubscriptions.UserReferrals,
FarcasterWorkerPolicy = BrokerSubscriptions.FarcasterWorkerPolicy,
}

Expand All @@ -35,6 +36,7 @@ export enum RascalQueues {
ContestWorkerPolicy = 'ContestWorkerPolicyQueue',
ContestProjection = 'ContestProjection',
XpProjection = 'XpProjection',
UserReferrals = 'UserReferrals',
FarcasterWorkerPolicy = 'FarcasterWorkerPolicyQueue',
}

Expand All @@ -47,6 +49,7 @@ export enum RascalBindings {
ContestWorkerPolicy = 'ContestWorkerPolicy',
ContestProjection = 'ContestProjection',
XpProjection = 'XpProjection',
UserReferrals = 'UserReferrals',
FarcasterWorkerPolicy = 'FarcasterWorkerPolicy',
}

Expand Down Expand Up @@ -89,6 +92,8 @@ export enum RascalRoutingKeys {
XpProjectionCommentUpvoted = EventNames.CommentUpvoted,
XpProjectionUserMentioned = EventNames.UserMentioned,

UserReferralsSignUpFlowCompleted = EventNames.SignUpFlowCompleted,

FarcasterWorkerPolicyCastCreated = EventNames.FarcasterCastCreated,
FarcasterWorkerPolicyReplyCastCreated = EventNames.FarcasterReplyCastCreated,
FarcasterWorkerPolicyVoteCreated = EventNames.FarcasterVoteCreated,
Expand Down
1 change: 1 addition & 0 deletions libs/core/src/ports/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export enum BrokerSubscriptions {
ContestProjection = 'ContestProjection',
FarcasterWorkerPolicy = 'FarcasterWorkerPolicy',
XpProjection = 'XpProjection',
UserReferrals = 'UserReferrals',
}

/**
Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/models/referral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const Referral = (
defaultValue: 0,
},
created_on_chain_timestamp: {
type: Sequelize.INTEGER,
type: Sequelize.BIGINT,
allowNull: true,
},
created_off_chain_at: {
Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/models/referral_fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const ReferralFee = (
allowNull: false,
},
transaction_timestamp: {
type: Sequelize.INTEGER,
type: Sequelize.BIGINT,
allowNull: false,
},
},
Expand Down
11 changes: 1 addition & 10 deletions libs/model/src/policies/handleReferralFeeDistributed.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { getBlock } from '@hicommonwealth/evm-protocols';
import { models } from '@hicommonwealth/model';
import { chainEvents, events } from '@hicommonwealth/schemas';
import { ZERO_ADDRESS } from '@hicommonwealth/shared';
import { BigNumber } from 'ethers';
import { z } from 'zod';
import { chainNodeMustExist } from './utils';

export async function handleReferralFeeDistributed(
event: z.infer<typeof events.ChainEventCreated>,
Expand All @@ -29,13 +27,6 @@ export async function handleReferralFeeDistributed(
return;
} else if (existingFee) return;

const chainNode = await chainNodeMustExist(event.eventSource.ethChainId);

const { block } = await getBlock({
rpc: chainNode.private_url! || chainNode.url!,
blockHash: event.rawLog.blockHash,
});

const feeAmount =
Number(BigNumber.from(referrerReceivedAmount).toBigInt()) / 1e18;

Expand All @@ -48,7 +39,7 @@ export async function handleReferralFeeDistributed(
distributed_token_address: tokenAddress,
referrer_recipient_address: referrerAddress,
referrer_received_amount: feeAmount,
transaction_timestamp: Number(block.timestamp),
transaction_timestamp: Number(event.block.timestamp),
},
{ transaction },
);
Expand Down
13 changes: 2 additions & 11 deletions libs/model/src/policies/handleReferralSet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { getBlock } from '@hicommonwealth/evm-protocols';
import { models } from '@hicommonwealth/model';
import { chainEvents, events } from '@hicommonwealth/schemas';
import { z } from 'zod';
import { chainNodeMustExist } from './utils';

export async function handleReferralSet(
event: z.infer<typeof events.ChainEventCreated>,
Expand Down Expand Up @@ -34,20 +32,13 @@ export async function handleReferralSet(
return;
}

const chainNode = await chainNodeMustExist(event.eventSource.ethChainId);

const { block } = await getBlock({
rpc: chainNode.private_url! || chainNode.url!,
blockHash: event.rawLog.blockHash,
});

// Triggered when an incomplete Referral (off-chain only) was created during user sign up
if (existingReferral && existingReferral?.eth_chain_id === null) {
await existingReferral.update({
eth_chain_id: event.eventSource.ethChainId,
transaction_hash: event.rawLog.transactionHash,
namespace_address: namespaceAddress,
created_on_chain_timestamp: Number(block.timestamp),
created_on_chain_timestamp: Number(event.block.timestamp),
});
}
// Triggered when the referral was set on-chain only (user didn't sign up i.e. no incomplete Referral)
Expand All @@ -65,7 +56,7 @@ export async function handleReferralSet(
referee_address: event.rawLog.address,
referrer_address: referrerAddress,
referrer_received_eth_amount: 0,
created_on_chain_timestamp: Number(block.timestamp),
created_on_chain_timestamp: Number(event.block.timestamp),
});
}
}
46 changes: 46 additions & 0 deletions libs/model/src/user/GetUserReferralFees.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { type Query } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { QueryTypes } from 'sequelize';
import { z } from 'zod';
import { models } from '../database';

export function GetUserReferralFees(): Query<
typeof schemas.GetUserReferralFees
> {
return {
...schemas.GetUserReferralFees,
auth: [],
secure: true,
body: async ({ actor }) => {
return await models.sequelize.query<
z.infer<typeof schemas.ReferralFeesView>
>(
`
WITH
referrer_addresses AS (
SELECT DISTINCT address
FROM "Addresses"
WHERE user_id = :user_id AND address LIKE '0x%'
)
SELECT
eth_chain_id,
transaction_hash,
namespace_address,
distributed_token_address,
referrer_recipient_address,
referrer_received_amount,
CAST(transaction_timestamp AS DOUBLE PRECISION) as transaction_timestamp
FROM "ReferralFees"
WHERE referrer_recipient_address IN (SELECT * FROM referrer_addresses);
`,
{
type: QueryTypes.SELECT,
raw: true,
replacements: {
user_id: actor.user.id,
},
},
);
},
};
}
45 changes: 31 additions & 14 deletions libs/model/src/user/GetUserReferrals.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,37 @@ export function GetUserReferrals(): Query<typeof schemas.GetUserReferrals> {

return await models.sequelize.query<z.infer<typeof schemas.ReferralView>>(
`
WITH referrer_addresses AS (SELECT DISTINCT address
FROM "Addresses"
WHERE user_id = :user_id
AND address LIKE '0x%'),
referrals AS (SELECT *
FROM "Referrals"
WHERE referrer_address IN (SELECT * FROM referrer_addresses)),
referee_addresses AS (SELECT DISTINCT A.address, A.user_id
FROM "Addresses" A
JOIN referrals ON referee_address = A.address)
SELECT R.*, U.id as referee_user_id, U.profile as referee_profile
FROM referrals R
JOIN referee_addresses RA ON RA.address = R.referee_address
JOIN "Users" U ON U.id = RA.user_id;
WITH
referrer_addresses AS (
SELECT DISTINCT address
FROM "Addresses"
WHERE user_id = :user_id AND address LIKE '0x%'),
referrals AS (
SELECT
id,
eth_chain_id,
transaction_hash,
namespace_address,
referee_address,
referrer_address,
referrer_received_eth_amount,
CAST(created_on_chain_timestamp AS DOUBLE PRECISION) as created_on_chain_timestamp,
created_off_chain_at,
updated_at
FROM "Referrals"
WHERE referrer_address IN (SELECT * FROM referrer_addresses)),
referee_addresses AS (
SELECT DISTINCT A.address, A.user_id
FROM "Addresses" A
JOIN referrals ON referee_address = A.address
)
SELECT
R.*,
U.id as referee_user_id,
U.profile as referee_profile
FROM referrals R
JOIN referee_addresses RA ON RA.address = R.referee_address
JOIN "Users" U ON U.id = RA.user_id;
`,
{
type: QueryTypes.SELECT,
Expand Down
1 change: 1 addition & 0 deletions libs/model/src/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './GetNewContent.query';
export * from './GetUser.query';
export * from './GetUserAddresses.query';
export * from './GetUserProfile.query';
export * from './GetUserReferralFees.query';
export * from './GetUserReferrals.query';
export * from './GetXps.query';
export * from './SearchUserProfiles.query';
Expand Down
Loading
Loading