• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hicommonwealth / commonwealth / 13203536178

07 Feb 2025 04:05PM UTC coverage: 46.356%. Remained the same
13203536178

push

github

web-flow
Merge pull request #10889 from hicommonwealth/ryan/prevent-zero-balance-votes

Prevent zero balance FC votes + Contest Bot Updates

1365 of 3289 branches covered (41.5%)

Branch coverage included in aggregate %.

5 of 18 new or added lines in 3 files covered. (27.78%)

21 existing lines in 2 files now uncovered.

2617 of 5301 relevant lines covered (49.37%)

36.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.5
/libs/model/src/services/openai/parseBotCommand.ts
1
import { OpenAI } from 'openai';
2
import {
3
  ChatCompletionMessage,
4
  ChatCompletionTool,
5
} from 'openai/resources/index.mjs';
6
import { config } from '../../config';
7

8
export const USDC_BASE_MAINNET_ADDRESS =
9
  '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
29✔
10
export const USDC_BASE_SEPOLIA_ADDRESS =
11
  '0x5dEaC602762362FE5f135FA5904351916053cF70';
29✔
12

13
export const DEFAULT_CONTEST_BOT_PARAMS = {
29✔
14
  payoutStructure: [50, 30, 20],
15
  voterShare: 10,
16
  image_url:
17
    'https://assets.commonwealth.im/42b9d2d9-79b8-473d-b404-b4e819328ded.png',
18
};
19

20
export type ContestMetadataResponse = {
21
  contestName: string;
22
  payoutStructure: number[];
23
  voterShare: number;
24
  image_url: string;
25
  tokenAddress: string;
26
  isUSDC: boolean;
27
};
28

29
const system_prompt: ChatCompletionMessage = {
29✔
30
  role: 'assistant',
31
  content: `
32
    You are a data extraction system that extracts information from the user.
33

34
    The user will ask to "launch", "start", "create", etc. That means they want to launch a contest.
35

36
    Extract the name of the contest as well as the token address from the user prompt.
37
    contestName is the name of the contest.
38
    tokenAddress is the ethereum address of the funding token.
39
    isUSDC should be set to true if the user shows intent to launch with the USDC token.
40

41
    Example: "Hey @contestbot, create a Big Donut with 0xc204af95b0307162118f7bc36a91c9717490ab69"
42
    Expected Output:
43
    {
44
      contestName: "Big Donut",
45
      tokenAddress: "0xc204af95b0307162118f7bc36a91c9717490ab69"
46
    }
47

48
    Example: "@commonbot, ignite the engine! Happy Monday funded via 0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"
49
    Expected Output:
50
    {
51
      contestName: "Happy Monday",
52
      tokenAddress: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"
53
    }
54

55
    Example: "@commonbot, create a Good Dogs Only dog battle. Use 0x0555E30da8f98308EdB960aa94C0Db47230d2B9c."
56
    Expected Output:
57
    {
58
      contestName: "Good Dogs Only",
59
      tokenAddress: "0x0555E30da8f98308EdB960aa94C0Db47230d2B9c"
60
    }
61

62
    Example: "@commonbot, rev up the meme machine! Launch 0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf."
63
    Expected Output:
64
    {
65
      contestName: "meme machine",
66
      tokenAddress: "0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf"
67
    }
68

69
    Example: "@contestbot, let's go! Diamond Handz funded by 0x820c137fa70c8691f0e44dc420a5e53c168921dc"
70
    Expected Output:
71
    {
72
      contestName: "Diamond Handz",
73
      tokenAddress: "0x820c137fa70c8691f0e44dc420a5e53c168921dc"
74
    }
75

76
    Example: "@contestbot, launch Base Bangers on USDC"
77
    Expected Output:
78
    {
79
      contestName: "Diamond Handz",
80
      isUSDC: true
81
    }
82
    `,
83
};
84

85
const tools: ChatCompletionTool[] = [
29✔
86
  {
87
    type: 'function',
88
    function: {
89
      name: 'parse_data',
90
      parameters: {
91
        type: 'object',
92
        properties: {
93
          contestName: { type: 'string' },
94
          tokenAddress: { type: 'string' },
95
          isUSDC: { type: 'boolean' },
96
        },
97
      },
98
    },
99
  },
100
];
101

102
// Custom error type that returns a human-readable error intended for end users
103
export class ParseBotCommandError extends Error {
104
  static ERRORS = {
29✔
105
    InvalidParams:
106
      'Failed to create contest. Specify all contest name and token address.',
107
  } as const;
108

109
  constructor(message: keyof typeof ParseBotCommandError.ERRORS) {
UNCOV
110
    super(message);
×
UNCOV
111
    this.name = this.constructor.name;
×
112

UNCOV
113
    if (Error.captureStackTrace) {
×
UNCOV
114
      Error.captureStackTrace(this, this.constructor);
×
115
    }
116
  }
117

118
  getPrettyError(): string {
UNCOV
119
    return (
×
120
      ParseBotCommandError.ERRORS[
×
121
        this.message as keyof typeof ParseBotCommandError.ERRORS
122
      ] || 'An unknown error occurred.'
123
    );
124
  }
125
}
126

127
export const getContestUSDCAddress = () => {
29✔
128
  if (config.APP_ENV === 'production') {
2!
NEW
129
    return USDC_BASE_MAINNET_ADDRESS;
×
130
  }
131
  return USDC_BASE_SEPOLIA_ADDRESS;
2✔
132
};
133

134
export const parseBotCommand = async (
29✔
135
  command: string,
136
): Promise<ContestMetadataResponse> => {
UNCOV
137
  const openai = new OpenAI({
×
138
    organization: config.OPENAI.ORGANIZATION,
139
    apiKey: config.OPENAI.API_KEY,
140
  });
141

UNCOV
142
  const response = await openai.chat.completions.create({
×
143
    model: 'o3-mini',
144
    messages: [
145
      system_prompt,
146
      {
147
        role: 'user',
148
        content: command,
149
      },
150
    ],
151
    tools,
152
  });
153

UNCOV
154
  let data = null;
×
UNCOV
155
  try {
×
UNCOV
156
    data = JSON.parse(
×
157
      response.choices[0].message.tool_calls![0].function.arguments,
158
    );
159
  } catch (err) {
UNCOV
160
    throw new ParseBotCommandError('InvalidParams');
×
161
  }
162

NEW
163
  if (!data.contestName || !(data.tokenAddress || data.isUSDC)) {
×
164
    throw new ParseBotCommandError('InvalidParams');
×
165
  }
166

NEW
167
  const tokenAddress = data.isUSDC
×
168
    ? getContestUSDCAddress()
169
    : data.tokenAddress;
170

UNCOV
171
  return {
×
172
    contestName: data.contestName,
173
    tokenAddress,
174
    ...DEFAULT_CONTEST_BOT_PARAMS,
175
    isUSDC: data.isUSDC,
176
  };
177
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc