From 93322e894dde0affc54d739fd7f634e31e885a7a Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Thu, 21 Apr 2022 04:41:02 -0700 Subject: [PATCH] More plumbing for txns --- common/txn.ts | 3 ++- functions/README.md | 2 +- functions/package.json | 3 ++- functions/src/backup-db.ts | 1 + functions/src/index.ts | 1 + web/components/charity/charity-card.tsx | 2 +- web/lib/firebase/api-call.ts | 2 ++ web/pages/charity/[...slugs]/index.tsx | 10 +++++++++- 8 files changed, 19 insertions(+), 5 deletions(-) diff --git a/common/txn.ts b/common/txn.ts index c81bce08..29ae08ac 100644 --- a/common/txn.ts +++ b/common/txn.ts @@ -5,6 +5,7 @@ export type Txn = { createdTime: number fromId: string + // TODO: Do we really want to denormalize name/username/avatar here? fromName: string fromUsername: string fromAvatarUrl?: string @@ -27,5 +28,5 @@ export type TxnCategory = 'TO_CHARITY' // | 'TIP' | 'BET' | ... export type TxnData = CharityData // | TipData | BetData | ... export type CharityData = { - // TODO: Could fill this in + charityId: string } diff --git a/functions/README.md b/functions/README.md index 2bce0c92..a6e1b4d1 100644 --- a/functions/README.md +++ b/functions/README.md @@ -19,7 +19,7 @@ Adapted from https://firebase.google.com/docs/functions/get-started 2. `$ yarn` to install JS dependencies 3. `$ firebase login` to authenticate the CLI tools to Firebase 4. `$ firebase use dev` to choose the dev project -5. `$ firebase functions:config:get > .runtimeconfig.json` to cache secrets for local dev (TODO: maybe not for Manifold) +5. `$ firebase functions:config:get > .runtimeconfig.json` to cache secrets for local dev ## Developing locally diff --git a/functions/package.json b/functions/package.json index 144d3d54..a4676876 100644 --- a/functions/package.json +++ b/functions/package.json @@ -8,7 +8,8 @@ "shell": "yarn build && firebase functions:shell", "start": "yarn shell", "deploy": "firebase deploy --only functions", - "logs": "firebase functions:log" + "logs": "firebase functions:log", + "dev": "yarn serve" }, "main": "lib/functions/src/index.js", "dependencies": { diff --git a/functions/src/backup-db.ts b/functions/src/backup-db.ts index bdafbf98..e840b71a 100644 --- a/functions/src/backup-db.ts +++ b/functions/src/backup-db.ts @@ -46,6 +46,7 @@ export const backupDb = functions.pubsub 'comments', 'followers', 'answers', + 'txns', ], }) .then((responses) => { diff --git a/functions/src/index.ts b/functions/src/index.ts index dedf42a1..bb2d6f5f 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -3,6 +3,7 @@ import * as admin from 'firebase-admin' admin.initializeApp() // export * from './keep-awake' +export * from './transact' export * from './place-bet' export * from './resolve-market' export * from './stripe' diff --git a/web/components/charity/charity-card.tsx b/web/components/charity/charity-card.tsx index e8f7a5cb..36581655 100644 --- a/web/components/charity/charity-card.tsx +++ b/web/components/charity/charity-card.tsx @@ -3,8 +3,8 @@ import { Row } from '../layout/row' // TODO: type probably belongs elsewhere export interface Charity { + slug: string // Note, slugs double as charity IDs name: string - slug: string website: string ein: string photo?: string diff --git a/web/lib/firebase/api-call.ts b/web/lib/firebase/api-call.ts index 6236cd64..e1c9bb5e 100644 --- a/web/lib/firebase/api-call.ts +++ b/web/lib/firebase/api-call.ts @@ -16,6 +16,8 @@ export const createFold = cloudFunction< { status: 'error' | 'success'; message?: string; fold?: Fold } >('createFold') +export const transact = cloudFunction('transact') + export const placeBet = cloudFunction('placeBet') export const sellBet = cloudFunction('sellBet') diff --git a/web/pages/charity/[...slugs]/index.tsx b/web/pages/charity/[...slugs]/index.tsx index 392a525f..e51890fb 100644 --- a/web/pages/charity/[...slugs]/index.tsx +++ b/web/pages/charity/[...slugs]/index.tsx @@ -10,6 +10,7 @@ import { Spacer } from '../../../components/layout/spacer' import { User } from '../../../../common/user' import { useUser } from '../../../hooks/use-user' import { Linkify } from '../../../components/linkify' +import { transact } from '../../../lib/firebase/api-call' const manaToUSD = (mana: number) => (mana / 100).toLocaleString('en-US', { style: 'currency', currency: 'USD' }) @@ -117,7 +118,14 @@ function DonationBox(props: { user?: User | null }) { setIsSubmitting(true) setError(undefined) // TODO await sending to db - await new Promise((resolve) => setTimeout(resolve, 1000)) + await transact({ + amount, + toId: 'asdfsasdf', // TODO hardcode in Manifold Markets official account + category: 'TO_CHARITY', + data: { + charityId: 'fjdkslasdf', // TODO fill in + }, + }) setIsSubmitting(false) setAmount(undefined) }