Skip to content

Commit

Permalink
Add api for spice managram to us or from us
Browse files Browse the repository at this point in the history
  • Loading branch information
sipec committed Jun 7, 2024
1 parent 7a1c245 commit 01fbdd1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
4 changes: 2 additions & 2 deletions backend/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import { createportfolio } from './create-portfolio'
import { updateportfolio } from './update-portfolio'
import { searchgiphy } from './search-giphy'
import { manachantweet } from './manachan-tweet'
import { sendMana } from './send-mana'
import { managram } from './managram'
import { leavereview } from './leave-review'
import { getusercontractmetricswithcontracts } from './get-user-contract-metrics-with-contracts'
import { castpollvote } from './cast-poll-vote'
Expand Down Expand Up @@ -265,7 +265,7 @@ const handlers: { [k in APIPath]: APIHandler<k> } = {
markets: getMarkets,
'search-markets': searchMarketsLite,
'search-markets-full': searchMarketsFull,
managram: sendMana,
managram: managram,
managrams: getManagrams,
manalink: createManalink,
donate: donate,
Expand Down
50 changes: 34 additions & 16 deletions backend/api/src/send-mana.ts → backend/api/src/managram.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@

import { isVerified } from 'common/user'
import { canSendMana } from 'common/can-send-mana'
import { APIError, type APIHandler } from './helpers/endpoint'
import { insertTxns } from 'shared/txn/run-txn'
import { createManaPaymentNotification } from 'shared/create-notification'
import * as crypto from 'crypto'
import { isAdminId } from 'common/envs/constants'
import { MAX_COMMENT_LENGTH } from 'common/comment'
import { getUserPortfolioInternal } from 'shared/get-user-portfolio-internal'
import { createSupabaseDirectClient } from 'shared/supabase/init'
import { getUser, getUsers } from 'shared/utils'
import { getUser, getUsers, isProd } from 'shared/utils'
import { bulkIncrementBalances } from 'shared/supabase/users'
import { buildArray } from 'common/util/array'
import {
DEV_HOUSE_LIQUIDITY_PROVIDER_ID,
HOUSE_LIQUIDITY_PROVIDER_ID,
} from 'common/antes'

export const sendMana: APIHandler<'managram'> = async (props, auth) => {
const { amount, toIds, message, groupId: passedGroupId } = props
if (message.length > MAX_COMMENT_LENGTH) {
throw new APIError(
400,
`Message should be less than ${MAX_COMMENT_LENGTH} characters`
)
}
export const managram: APIHandler<'managram'> = async (props, auth) => {
const { amount, toIds, message, token, groupId: passedGroupId } = props
const fromId = auth.uid

if (!isAdminId(fromId) && amount < 10) {
Expand Down Expand Up @@ -54,13 +50,33 @@ export const sendMana: APIHandler<'managram'> = async (props, auth) => {
throw new APIError(403, errorMessage)
}

if (token === 'PP') {
const ManifoldAccount = isProd()
? HOUSE_LIQUIDITY_PROVIDER_ID
: DEV_HOUSE_LIQUIDITY_PROVIDER_ID

if (fromId !== ManifoldAccount) {
if (toIds.length > 1)
throw new APIError(
400,
'You cannot send prize points to multiple users.'
)
if (toIds[0] !== ManifoldAccount)
throw new APIError(
400,
'Do send prize points only to @ManifoldMarkets.'
)
}
}

const total = amount * toIds.length
if (fromUser.balance < total) {
const balance = token === 'M$' ? fromUser.balance : fromUser.spiceBalance
if (balance < total) {
throw new APIError(
403,
`Insufficient balance: ${fromUser.name} needed ${
amount * toIds.length
} but only had ${fromUser.balance} `
} but only had ${balance} `
)
}

Expand All @@ -72,17 +88,19 @@ export const sendMana: APIHandler<'managram'> = async (props, auth) => {
throw new APIError(403, 'All destination users must be verified.')
}

const balanceField = token === 'M$' ? 'balance' : 'spiceBalance'

await bulkIncrementBalances(
tx,
buildArray(
{
id: fromId,
balance: -total,
[balanceField]: -total,
totalDeposits: -total,
},
toIds.map((toId) => ({
id: toId,
balance: amount,
[balanceField]: amount,
totalDeposits: amount,
}))
)
Expand All @@ -101,7 +119,7 @@ export const sendMana: APIHandler<'managram'> = async (props, auth) => {
toId,
toType: 'USER',
amount,
token: 'M$',
token: token === 'M$' ? 'M$' : 'SPICE',
category: 'MANA_PAYMENT',
data: {
message,
Expand Down
5 changes: 3 additions & 2 deletions common/src/api/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
FullMarket,
updateMarketProps,
} from './market-types'
import type { ContractComment } from 'common/comment'
import { MAX_COMMENT_LENGTH, type ContractComment } from 'common/comment'
import { CandidateBet } from 'common/new-bet'
import type { Bet, LimitBet } from 'common/bet'
import { contentSchema } from 'common/api/zod-types'
Expand Down Expand Up @@ -546,8 +546,9 @@ export const API = (_apiTypeCheck = {
.object({
amount: z.number().finite(),
toIds: z.array(z.string()),
message: z.string(),
message: z.string().max(MAX_COMMENT_LENGTH),
groupId: z.string().max(MAX_ID_LENGTH).optional(),
token: z.enum(['M$', 'PP']).default('M$'),
})
.strict(),
},
Expand Down
2 changes: 1 addition & 1 deletion common/src/txn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ type ManaPay = {
category: 'MANA_PAYMENT'
fromType: 'USER'
toType: 'USER'
token: 'M$'
token: 'M$' | 'SPICE'
data: {
visibility: 'public' | 'private'
message: string
Expand Down

0 comments on commit 01fbdd1

Please sign in to comment.