-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(billing): add billing module with trial wallet creation
As a part of this add: - DI container - Drizzle ORM - logging refs #247
- Loading branch information
1 parent
4c2c88b
commit ddcc130
Showing
59 changed files
with
2,796 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
MASTER_WALLET_MNEMONIC="south super just human picture mesh garlic carbon witness drill river design" | ||
NETWORK=sandbox | ||
POSTGRES_DB_URI=postgres://postgres:[email protected]:5432/cloudmos-users | ||
RPC_NODE_ENDPOINT=https://rpc.sandbox-01.aksh.pw:443 | ||
TRIAL_DEPLOYMENT_ALLOWANCE_AMOUNT=100000 | ||
TRIAL_FEES_ALLOWANCE_AMOUNT=100000 | ||
TRIAL_ALLOWANCE_DENOM=uakt | ||
LOG_LEVEL=info | ||
BILLING_ENABLED=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineConfig } from "drizzle-kit"; | ||
|
||
import { env } from "./src/utils/env"; | ||
|
||
export default defineConfig({ | ||
schema: "./src/billing/model-schemas", | ||
out: "./drizzle", | ||
dialect: "postgresql", | ||
dbCredentials: { | ||
url: env.UserDatabaseCS | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE IF NOT EXISTS "user_wallets" ( | ||
"id" serial PRIMARY KEY NOT NULL, | ||
"user_id" varchar, | ||
"address" varchar, | ||
"stripe_customer_id" varchar, | ||
"credit_amount" numeric(10, 2) DEFAULT '0.00' NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"id": "777d9ff9-333b-42b3-a5f4-759b3774909b", | ||
"prevId": "00000000-0000-0000-0000-000000000000", | ||
"version": "7", | ||
"dialect": "postgresql", | ||
"tables": { | ||
"public.user_wallets": { | ||
"name": "user_wallets", | ||
"schema": "", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "serial", | ||
"primaryKey": true, | ||
"notNull": true | ||
}, | ||
"user_id": { | ||
"name": "user_id", | ||
"type": "varchar", | ||
"primaryKey": false, | ||
"notNull": false | ||
}, | ||
"address": { | ||
"name": "address", | ||
"type": "varchar", | ||
"primaryKey": false, | ||
"notNull": false | ||
}, | ||
"stripe_customer_id": { | ||
"name": "stripe_customer_id", | ||
"type": "varchar", | ||
"primaryKey": false, | ||
"notNull": false | ||
}, | ||
"credit_amount": { | ||
"name": "credit_amount", | ||
"type": "numeric(10, 2)", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"default": "'0.00'" | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {} | ||
} | ||
}, | ||
"enums": {}, | ||
"schemas": {}, | ||
"_meta": { | ||
"columns": {}, | ||
"schemas": {}, | ||
"tables": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"version": "7", | ||
"dialect": "postgresql", | ||
"entries": [ | ||
{ | ||
"idx": 0, | ||
"version": "7", | ||
"when": 1720022370732, | ||
"tag": "0000_chilly_mastermind", | ||
"breakpoints": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import dotenv from "dotenv"; | ||
import { z } from "zod"; | ||
|
||
dotenv.config({ path: ".env.local" }); | ||
dotenv.config(); | ||
|
||
const envSchema = z.object({ | ||
MASTER_WALLET_MNEMONIC: z.string(), | ||
NETWORK: z.enum(["mainnet", "testnet", "sandbox"]), | ||
RPC_NODE_ENDPOINT: z.string(), | ||
TRIAL_ALLOWANCE_EXPIRATION_DAYS: z.number({ coerce: true }).default(14), | ||
TRIAL_DEPLOYMENT_ALLOWANCE_AMOUNT: z.number({ coerce: true }), | ||
TRIAL_FEES_ALLOWANCE_AMOUNT: z.number({ coerce: true }), | ||
TRIAL_ALLOWANCE_DENOM: z.string(), | ||
GAS_SAFETY_MULTIPLIER: z.number({ coerce: true }).default(1.5), | ||
BILLING_ENABLED: z.enum(["true", "false"]).transform(value => value === "true") | ||
}); | ||
|
||
export const envConfig = envSchema.parse(process.env); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { envConfig } from "./env.config"; | ||
import { USDC_IBC_DENOMS } from "./network.config"; | ||
|
||
export const config = { | ||
...envConfig, | ||
USDC_IBC_DENOMS | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const USDC_IBC_DENOMS = { | ||
mainnetId: "ibc/170C677610AC31DF0904FFE09CD3B5C657492170E7E52372E48756B71E56F2F1", | ||
sandboxId: "ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84" | ||
}; |
38 changes: 38 additions & 0 deletions
38
apps/api/src/billing/controllers/wallet/wallet.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { PromisePool } from "@supercharge/promise-pool"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { UserWalletRepository } from "@src/billing/repositories"; | ||
import { CreateWalletInput, CreateWalletOutput } from "@src/billing/routes"; | ||
import { WalletInitializerService, WalletService } from "@src/billing/services"; | ||
import { WithTransaction } from "@src/core/services"; | ||
|
||
@singleton() | ||
export class WalletController { | ||
constructor( | ||
private readonly walletManager: WalletService, | ||
private readonly userWalletRepository: UserWalletRepository, | ||
private readonly walletInitializer: WalletInitializerService | ||
) {} | ||
|
||
@WithTransaction() | ||
async create({ userId }: CreateWalletInput): Promise<CreateWalletOutput> { | ||
await this.walletInitializer.initialize(userId); | ||
} | ||
|
||
async refillAll() { | ||
const wallets = await this.userWalletRepository.find(); | ||
const { results, errors } = await PromisePool.withConcurrency(2) | ||
.for(wallets) | ||
.process(async wallet => { | ||
const refilled = await this.walletManager.refill(wallet); | ||
console.log("DEBUG refilled", refilled); | ||
return refilled; | ||
}); | ||
|
||
if (errors) { | ||
console.log("DEBUG errors", errors); | ||
} | ||
|
||
console.log("DEBUG results", results); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import "./providers"; | ||
|
||
export * from "./routes"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./user-wallet/user-wallet.schema"; |
14 changes: 14 additions & 0 deletions
14
apps/api/src/billing/model-schemas/user-wallet/user-wallet.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { numeric, pgTable, serial, varchar } from "drizzle-orm/pg-core"; | ||
|
||
export const userWalletSchema = pgTable("user_wallets", { | ||
id: serial("id").primaryKey(), | ||
userId: varchar("user_id"), | ||
address: varchar("address"), | ||
stripeCustomerId: varchar("stripe_customer_id"), | ||
creditAmount: numeric("credit_amount", { | ||
precision: 10, | ||
scale: 2 | ||
}) | ||
.notNull() | ||
.default("0.00") | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./user-wallet/user-wallet.model"; |
Oops, something went wrong.