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

Publish contest bot cast *after* contest projection created #11008

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions libs/model/src/bot/CreateBotContest.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export function CreateBotContest(): Command<typeof schemas.CreateBotContest> {
ticker: tokenMetadata.ticker,
decimals: tokenMetadata.decimals,
contest_address: contestAddress,
farcaster_author_cast_hash: payload.castHash,
},
{ transaction },
);
Expand Down
32 changes: 31 additions & 1 deletion libs/model/src/contest/Contests.projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import {
} from '@hicommonwealth/evm-protocols';
import { config } from '@hicommonwealth/model';
import { events } from '@hicommonwealth/schemas';
import { buildContestLeaderboardUrl, getBaseUrl } from '@hicommonwealth/shared';
import {
buildContestLeaderboardUrl,
buildFarcasterContestFrameUrl,
getBaseUrl,
} from '@hicommonwealth/shared';
import { QueryTypes } from 'sequelize';
import { models } from '../database';
import { mustExist } from '../middleware/guards';
import { EvmEventSourceAttributes } from '../models';
import { DEFAULT_CONTEST_BOT_PARAMS } from '../services/openai/parseBotCommand';
import { getWeightedNumTokens } from '../services/stakeHelper';
import {
decodeThreadContentUrl,
Expand Down Expand Up @@ -258,6 +263,31 @@ export function Contests(): Projection<typeof inputs> {
true,
payload.block_number,
);

// if bot-created farcaster contest, notify author
const contestManager = await models.ContestManager.findOne({
where: {
contest_address: payload.contest_address,
},
});
mustExist('Contest Manager', contestManager);

if (contestManager.farcaster_author_cast_hash) {
await publishCast(
contestManager.farcaster_author_cast_hash,
({ username }) => {
const {
payoutStructure: [winner1, winner2, winner3],
voterShare,
} = DEFAULT_CONTEST_BOT_PARAMS;
return `Hey @${username}, your contest has been created. The prize distribution is ${winner1}% to winner, ${winner2}% to second place, ${winner3}% to third , and ${voterShare}% going to voters. The contest will run for 7 days. Anyone who replies to a cast containing the frame enters the contest.`;
},
{
// eslint-disable-next-line max-len
embed: `${getBaseUrl(config.APP_ENV, config.CONTESTS.FARCASTER_NGROK_DOMAIN!)}${buildFarcasterContestFrameUrl(payload.contest_address)}`,
},
);
}
},

// This happens for each recurring contest _after_ the initial contest
Expand Down
1 change: 1 addition & 0 deletions libs/model/src/models/contest_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default (
topic_id: { type: Sequelize.INTEGER, allowNull: true },
is_farcaster_contest: { type: Sequelize.BOOLEAN, allowNull: false },
vote_weight_multiplier: { type: Sequelize.FLOAT, allowNull: true },
farcaster_author_cast_hash: { type: Sequelize.STRING, allowNull: true },
},
{
tableName: 'ContestManagers',
Expand Down
29 changes: 2 additions & 27 deletions libs/model/src/policies/FarcasterWorker.policy.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import { command, logger, Policy } from '@hicommonwealth/core';
import { events } from '@hicommonwealth/schemas';
import {
buildFarcasterContestFrameUrl,
getBaseUrl,
} from '@hicommonwealth/shared';
import { NeynarAPIClient } from '@neynar/nodejs-sdk';
import { Op } from 'sequelize';
import { config, models } from '..';
import { CreateBotContest } from '../bot/CreateBotContest.command';
import { systemActor } from '../middleware';
import { mustExist } from '../middleware/guards';
import { DEFAULT_CONTEST_BOT_PARAMS } from '../services/openai/parseBotCommand';
import {
buildFarcasterContentUrl,
buildFarcasterWebhookName,
publishCast,
} from '../utils';
import { buildFarcasterContentUrl, buildFarcasterWebhookName } from '../utils';
import {
createOnchainContestContent,
createOnchainContestVote,
Expand Down Expand Up @@ -210,29 +201,13 @@ export function FarcasterWorker(): Policy<typeof inputs> {
});
},
FarcasterContestBotMentioned: async ({ payload }) => {
const contestAddress = await command(CreateBotContest(), {
await command(CreateBotContest(), {
actor: systemActor({}),
payload: {
castHash: payload.hash!,
prompt: payload.text,
},
});
if (contestAddress) {
await publishCast(
payload.hash,
({ username }) => {
const {
payoutStructure: [winner1, winner2, winner3],
voterShare,
} = DEFAULT_CONTEST_BOT_PARAMS;
return `Hey @${username}, your contest has been created. The prize distribution is ${winner1}% to winner, ${winner2}% to second place, ${winner3}% to third , and ${voterShare}% going to voters. The contest will run for 7 days. Anyone who replies to a cast containing the frame enters the contest.`;
},
{
// eslint-disable-next-line max-len
embed: `${getBaseUrl(config.APP_ENV, config.CONTESTS.FARCASTER_NGROK_DOMAIN!)}${buildFarcasterContestFrameUrl(contestAddress)}`,
},
);
}
},
},
};
Expand Down
6 changes: 6 additions & 0 deletions libs/schemas/src/entities/contest-manager.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,11 @@ export const ContestManager = z
.gt(0)
.nullish()
.describe('Vote weight multiplier'),
farcaster_author_cast_hash: z
.string()
.optional()
.describe(
"For bot-created contests, the hash of the farcaster author's cast that created the contest",
),
})
.describe('On-Chain Contest Manager');
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn(
'ContestManagers',
'farcaster_author_cast_hash',
{
type: Sequelize.STRING,
allowNull: true,
},
);
},

async down(queryInterface, Sequelize) {
await queryInterface.removeColumn(
'ContestManagers',
'farcaster_author_cast_hash',
);
},
};
Loading