2022-03-15 22:27:51 +00:00
|
|
|
import * as _ from 'lodash'
|
|
|
|
|
2022-03-09 02:43:30 +00:00
|
|
|
import { DOMAIN, PROJECT_ID } from '../../common/envs/constants'
|
2022-02-23 06:05:04 +00:00
|
|
|
import { Answer } from '../../common/answer'
|
2022-02-24 00:52:19 +00:00
|
|
|
import { Bet } from '../../common/bet'
|
2022-01-30 21:51:30 +00:00
|
|
|
import { getProbability } from '../../common/calculate'
|
2022-02-23 01:35:25 +00:00
|
|
|
import { Comment } from '../../common/comment'
|
2022-04-18 23:02:40 +00:00
|
|
|
import { Contract, FreeResponseContract } from '../../common/contract'
|
2022-02-17 18:18:02 +00:00
|
|
|
import { CREATOR_FEE } from '../../common/fees'
|
2022-02-08 11:26:33 +00:00
|
|
|
import { PrivateUser, User } from '../../common/user'
|
2022-02-17 18:18:02 +00:00
|
|
|
import { formatMoney, formatPercent } from '../../common/util/format'
|
2022-02-08 11:26:33 +00:00
|
|
|
import { sendTemplateEmail, sendTextEmail } from './send-email'
|
2022-03-09 02:43:30 +00:00
|
|
|
import { getPrivateUser, getUser } from './utils'
|
2022-01-02 00:08:52 +00:00
|
|
|
|
|
|
|
export const sendMarketResolutionEmail = async (
|
|
|
|
userId: string,
|
2022-02-24 07:04:24 +00:00
|
|
|
investment: number,
|
2022-01-02 00:08:52 +00:00
|
|
|
payout: number,
|
|
|
|
creator: User,
|
|
|
|
contract: Contract,
|
2022-02-20 07:26:26 +00:00
|
|
|
resolution: string,
|
|
|
|
resolutionProbability?: number,
|
|
|
|
resolutions?: { [outcome: string]: number }
|
2022-01-02 00:08:52 +00:00
|
|
|
) => {
|
2022-01-19 03:36:46 +00:00
|
|
|
const privateUser = await getPrivateUser(userId)
|
|
|
|
if (
|
|
|
|
!privateUser ||
|
|
|
|
privateUser.unsubscribedFromResolutionEmails ||
|
|
|
|
!privateUser.email
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2022-01-02 00:08:52 +00:00
|
|
|
const user = await getUser(userId)
|
|
|
|
if (!user) return
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const outcome = toDisplayResolution(
|
|
|
|
contract,
|
|
|
|
resolution,
|
|
|
|
resolutionProbability,
|
|
|
|
resolutions
|
|
|
|
)
|
2022-01-02 00:08:52 +00:00
|
|
|
|
2022-01-10 22:07:44 +00:00
|
|
|
const subject = `Resolved ${outcome}: ${contract.question}`
|
2022-01-02 00:08:52 +00:00
|
|
|
|
2022-01-10 22:07:44 +00:00
|
|
|
const templateData: market_resolved_template = {
|
2022-01-19 18:43:12 +00:00
|
|
|
userId: user.id,
|
2022-01-10 22:07:44 +00:00
|
|
|
name: user.name,
|
|
|
|
creatorName: creator.name,
|
|
|
|
question: contract.question,
|
|
|
|
outcome,
|
2022-04-28 02:18:52 +00:00
|
|
|
investment: `${Math.floor(investment)}`,
|
|
|
|
payout: `${Math.floor(payout)}`,
|
2022-03-09 02:43:30 +00:00
|
|
|
url: `https://${DOMAIN}/${creator.username}/${contract.slug}`,
|
2022-01-10 22:07:44 +00:00
|
|
|
}
|
2022-01-02 00:08:52 +00:00
|
|
|
|
2022-01-10 22:07:44 +00:00
|
|
|
// Modify template here:
|
|
|
|
// https://app.mailgun.com/app/sending/domains/mg.manifold.markets/templates/edit/market-resolved/initial
|
|
|
|
// Mailgun username: james@mantic.markets
|
2022-01-02 00:08:52 +00:00
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
await sendTemplateEmail(
|
|
|
|
privateUser.email,
|
|
|
|
subject,
|
|
|
|
'market-resolved',
|
|
|
|
templateData
|
|
|
|
)
|
2022-01-02 00:08:52 +00:00
|
|
|
}
|
2022-02-08 11:26:33 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
type market_resolved_template = {
|
|
|
|
userId: string
|
|
|
|
name: string
|
|
|
|
creatorName: string
|
|
|
|
question: string
|
|
|
|
outcome: string
|
|
|
|
investment: string
|
|
|
|
payout: string
|
|
|
|
url: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const toDisplayResolution = (
|
|
|
|
contract: Contract,
|
|
|
|
resolution: string,
|
|
|
|
resolutionProbability?: number,
|
|
|
|
resolutions?: { [outcome: string]: number }
|
|
|
|
) => {
|
|
|
|
if (contract.outcomeType === 'BINARY') {
|
|
|
|
const prob = resolutionProbability ?? getProbability(contract)
|
|
|
|
|
|
|
|
const display = {
|
|
|
|
YES: 'YES',
|
|
|
|
NO: 'NO',
|
|
|
|
CANCEL: 'N/A',
|
|
|
|
MKT: formatPercent(prob ?? 0),
|
|
|
|
}[resolution]
|
|
|
|
|
|
|
|
return display || resolution
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolution === 'MKT' && resolutions) return 'MULTI'
|
2022-03-19 02:11:47 +00:00
|
|
|
if (resolution === 'CANCEL') return 'N/A'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
const answer = (contract as FreeResponseContract).answers?.find(
|
|
|
|
(a) => a.id === resolution
|
|
|
|
)
|
|
|
|
if (answer) return answer.text
|
2022-03-15 22:27:51 +00:00
|
|
|
return `#${resolution}`
|
|
|
|
}
|
|
|
|
|
2022-02-08 11:26:33 +00:00
|
|
|
export const sendWelcomeEmail = async (
|
|
|
|
user: User,
|
|
|
|
privateUser: PrivateUser
|
|
|
|
) => {
|
|
|
|
const firstName = user.name.split(' ')[0]
|
|
|
|
|
|
|
|
await sendTextEmail(
|
|
|
|
privateUser.email || '',
|
|
|
|
'Welcome to Manifold Markets!',
|
|
|
|
`Hi ${firstName},
|
|
|
|
|
|
|
|
Thanks for joining us! We can't wait to see what markets you create.
|
2022-02-22 22:35:53 +00:00
|
|
|
|
2022-02-08 11:26:33 +00:00
|
|
|
Questions? Feedback? I'd love to hear from you - just reply to this email!
|
2022-02-22 22:35:53 +00:00
|
|
|
|
2022-02-08 11:26:33 +00:00
|
|
|
Or come chat with us on Discord: https://discord.gg/eHQBNBqXuh
|
|
|
|
|
|
|
|
Best,
|
2022-02-17 04:42:52 +00:00
|
|
|
Austin from Manifold
|
2022-03-09 02:43:30 +00:00
|
|
|
https://${DOMAIN}/`
|
2022-02-08 11:26:33 +00:00
|
|
|
)
|
|
|
|
}
|
2022-02-17 18:18:02 +00:00
|
|
|
|
|
|
|
export const sendMarketCloseEmail = async (
|
|
|
|
user: User,
|
|
|
|
privateUser: PrivateUser,
|
|
|
|
contract: Contract
|
|
|
|
) => {
|
|
|
|
if (
|
|
|
|
!privateUser ||
|
|
|
|
privateUser.unsubscribedFromResolutionEmails ||
|
|
|
|
!privateUser.email
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
const { username, name, id: userId } = user
|
|
|
|
const firstName = name.split(' ')[0]
|
|
|
|
|
|
|
|
const { question, pool: pools, slug } = contract
|
|
|
|
const pool = formatMoney(_.sum(_.values(pools)))
|
2022-03-09 02:43:30 +00:00
|
|
|
const url = `https://${DOMAIN}/${username}/${slug}`
|
2022-02-17 18:18:02 +00:00
|
|
|
|
|
|
|
await sendTemplateEmail(
|
|
|
|
privateUser.email,
|
|
|
|
'Your market has closed',
|
|
|
|
'market-close',
|
|
|
|
{
|
|
|
|
name: firstName,
|
|
|
|
question,
|
|
|
|
pool,
|
|
|
|
url,
|
|
|
|
userId,
|
|
|
|
creatorFee: (CREATOR_FEE * 100).toString(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2022-02-23 01:35:25 +00:00
|
|
|
|
|
|
|
export const sendNewCommentEmail = async (
|
|
|
|
userId: string,
|
|
|
|
commentCreator: User,
|
2022-02-24 00:52:19 +00:00
|
|
|
contract: Contract,
|
2022-02-23 01:35:25 +00:00
|
|
|
comment: Comment,
|
2022-04-21 17:09:06 +00:00
|
|
|
bet?: Bet,
|
2022-02-24 06:36:27 +00:00
|
|
|
answer?: Answer
|
2022-02-23 01:35:25 +00:00
|
|
|
) => {
|
|
|
|
const privateUser = await getPrivateUser(userId)
|
|
|
|
if (
|
|
|
|
!privateUser ||
|
2022-02-23 06:05:04 +00:00
|
|
|
!privateUser.email ||
|
|
|
|
privateUser.unsubscribedFromCommentEmails
|
2022-02-23 01:35:25 +00:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
const { question, creatorUsername, slug } = contract
|
2022-03-09 02:43:30 +00:00
|
|
|
const marketUrl = `https://${DOMAIN}/${creatorUsername}/${slug}`
|
2022-02-23 01:35:25 +00:00
|
|
|
|
2022-03-09 02:43:30 +00:00
|
|
|
const unsubscribeUrl = `https://us-central1-${PROJECT_ID}.cloudfunctions.net/unsubscribe?id=${userId}&type=market-comment`
|
2022-02-23 01:35:25 +00:00
|
|
|
|
|
|
|
const { name: commentorName, avatarUrl: commentorAvatarUrl } = commentCreator
|
|
|
|
const { text } = comment
|
|
|
|
|
2022-04-21 17:09:06 +00:00
|
|
|
let betDescription = ''
|
|
|
|
if (bet) {
|
|
|
|
const { amount, sale } = bet
|
2022-04-28 02:18:52 +00:00
|
|
|
betDescription = `${sale || amount < 0 ? 'sold' : 'bought'} ${formatMoney(
|
|
|
|
amount
|
|
|
|
)}`
|
2022-04-21 17:09:06 +00:00
|
|
|
}
|
2022-02-24 00:52:19 +00:00
|
|
|
|
2022-02-23 01:35:25 +00:00
|
|
|
const subject = `Comment on ${question}`
|
|
|
|
const from = `${commentorName} <info@manifold.markets>`
|
|
|
|
|
2022-02-24 06:36:27 +00:00
|
|
|
if (contract.outcomeType === 'FREE_RESPONSE') {
|
|
|
|
const answerText = answer?.text ?? ''
|
|
|
|
const answerNumber = `#${answer?.id ?? ''}`
|
|
|
|
|
|
|
|
await sendTemplateEmail(
|
|
|
|
privateUser.email,
|
|
|
|
subject,
|
|
|
|
'market-answer-comment',
|
|
|
|
{
|
|
|
|
answer: answerText,
|
|
|
|
answerNumber,
|
|
|
|
commentorName,
|
|
|
|
commentorAvatarUrl: commentorAvatarUrl ?? '',
|
|
|
|
comment: text,
|
|
|
|
marketUrl,
|
|
|
|
unsubscribeUrl,
|
|
|
|
betDescription,
|
|
|
|
},
|
|
|
|
{ from }
|
|
|
|
)
|
|
|
|
} else {
|
2022-04-21 17:09:06 +00:00
|
|
|
if (bet) {
|
|
|
|
betDescription = `${betDescription} of ${toDisplayResolution(
|
|
|
|
contract,
|
|
|
|
bet.outcome
|
|
|
|
)}`
|
|
|
|
}
|
2022-02-24 06:36:27 +00:00
|
|
|
await sendTemplateEmail(
|
|
|
|
privateUser.email,
|
|
|
|
subject,
|
|
|
|
'market-comment',
|
|
|
|
{
|
|
|
|
commentorName,
|
|
|
|
commentorAvatarUrl: commentorAvatarUrl ?? '',
|
|
|
|
comment: text,
|
|
|
|
marketUrl,
|
|
|
|
unsubscribeUrl,
|
|
|
|
betDescription,
|
|
|
|
},
|
|
|
|
{ from }
|
|
|
|
)
|
|
|
|
}
|
2022-02-23 01:35:25 +00:00
|
|
|
}
|
2022-02-23 06:05:04 +00:00
|
|
|
|
|
|
|
export const sendNewAnswerEmail = async (
|
|
|
|
answer: Answer,
|
|
|
|
contract: Contract
|
|
|
|
) => {
|
|
|
|
// Send to just the creator for now.
|
|
|
|
const { creatorId: userId } = contract
|
2022-03-02 01:25:19 +00:00
|
|
|
|
|
|
|
// Don't send the creator's own answers.
|
|
|
|
if (answer.userId === userId) return
|
|
|
|
|
2022-02-23 06:05:04 +00:00
|
|
|
const privateUser = await getPrivateUser(userId)
|
|
|
|
if (
|
|
|
|
!privateUser ||
|
|
|
|
!privateUser.email ||
|
|
|
|
privateUser.unsubscribedFromAnswerEmails
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
const { question, creatorUsername, slug } = contract
|
|
|
|
const { name, avatarUrl, text } = answer
|
|
|
|
|
2022-03-09 02:43:30 +00:00
|
|
|
const marketUrl = `https://${DOMAIN}/${creatorUsername}/${slug}`
|
|
|
|
const unsubscribeUrl = `https://us-central1-${PROJECT_ID}.cloudfunctions.net/unsubscribe?id=${userId}&type=market-answer`
|
2022-02-23 06:05:04 +00:00
|
|
|
|
|
|
|
const subject = `New answer on ${question}`
|
|
|
|
const from = `${name} <info@manifold.markets>`
|
|
|
|
|
|
|
|
await sendTemplateEmail(
|
|
|
|
privateUser.email,
|
|
|
|
subject,
|
|
|
|
'market-answer',
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
avatarUrl: avatarUrl ?? '',
|
|
|
|
answer: text,
|
|
|
|
marketUrl,
|
|
|
|
unsubscribeUrl,
|
|
|
|
},
|
|
|
|
{ from }
|
|
|
|
)
|
|
|
|
}
|