@moneypot/hub 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/README.md +108 -0
  2. package/dist/cli/add-casino.d.ts +2 -0
  3. package/dist/cli/add-casino.js +116 -0
  4. package/dist/dashboard/assets/index-BtrbrisP.js +360 -0
  5. package/dist/dashboard/assets/index-tK7EUtyc.css +5 -0
  6. package/dist/dashboard/index.html +13 -0
  7. package/dist/src/GraphQLError.d.ts +8 -0
  8. package/dist/src/GraphQLError.js +79 -0
  9. package/dist/src/__generated__/fragment-masking.d.ts +19 -0
  10. package/dist/src/__generated__/fragment-masking.js +16 -0
  11. package/dist/src/__generated__/gql.d.ts +26 -0
  12. package/dist/src/__generated__/gql.js +15 -0
  13. package/dist/src/__generated__/graphql.d.ts +3129 -0
  14. package/dist/src/__generated__/graphql.js +454 -0
  15. package/dist/src/__generated__/index.d.ts +2 -0
  16. package/dist/src/__generated__/index.js +2 -0
  17. package/dist/src/config.d.ts +14 -0
  18. package/dist/src/config.js +57 -0
  19. package/dist/src/db/index.d.ts +89 -0
  20. package/dist/src/db/index.js +339 -0
  21. package/dist/src/db/internal.d.ts +7 -0
  22. package/dist/src/db/internal.js +33 -0
  23. package/dist/src/db/public.d.ts +7 -0
  24. package/dist/src/db/public.js +20 -0
  25. package/dist/src/db/types.d.ts +80 -0
  26. package/dist/src/db/types.js +1 -0
  27. package/dist/src/db/util.d.ts +6 -0
  28. package/dist/src/db/util.js +9 -0
  29. package/dist/src/express.d.ts +13 -0
  30. package/dist/src/express.js +1 -0
  31. package/dist/src/grafast.d.ts +1 -0
  32. package/dist/src/grafast.js +1 -0
  33. package/dist/src/graphile.d.ts +1 -0
  34. package/dist/src/graphile.js +1 -0
  35. package/dist/src/graphql-client.d.ts +6 -0
  36. package/dist/src/graphql-client.js +8 -0
  37. package/dist/src/graphql-queries.d.ts +18 -0
  38. package/dist/src/graphql-queries.js +123 -0
  39. package/dist/src/graphql.d.ts +1 -0
  40. package/dist/src/graphql.js +1 -0
  41. package/dist/src/index.d.ts +15 -0
  42. package/dist/src/index.js +65 -0
  43. package/dist/src/logger.d.ts +9 -0
  44. package/dist/src/logger.js +21 -0
  45. package/dist/src/pg-versions/001-schema.sql +456 -0
  46. package/dist/src/plugins/caas-add-casino.d.ts +1 -0
  47. package/dist/src/plugins/caas-add-casino.js +150 -0
  48. package/dist/src/plugins/caas-authenticate.d.ts +1 -0
  49. package/dist/src/plugins/caas-authenticate.js +175 -0
  50. package/dist/src/plugins/caas-balance-alert.d.ts +1 -0
  51. package/dist/src/plugins/caas-balance-alert.js +43 -0
  52. package/dist/src/plugins/caas-claim-faucet.d.ts +1 -0
  53. package/dist/src/plugins/caas-claim-faucet.js +85 -0
  54. package/dist/src/plugins/caas-current-x.d.ts +1 -0
  55. package/dist/src/plugins/caas-current-x.js +62 -0
  56. package/dist/src/plugins/caas-schema-prefix.d.ts +1 -0
  57. package/dist/src/plugins/caas-schema-prefix.js +25 -0
  58. package/dist/src/plugins/caas-user-balance-by-currency.d.ts +1 -0
  59. package/dist/src/plugins/caas-user-balance-by-currency.js +55 -0
  60. package/dist/src/plugins/caas-withdraw.d.ts +1 -0
  61. package/dist/src/plugins/caas-withdraw.js +133 -0
  62. package/dist/src/plugins/debug.d.ts +1 -0
  63. package/dist/src/plugins/debug.js +14 -0
  64. package/dist/src/plugins/hub-add-casino.d.ts +1 -0
  65. package/dist/src/plugins/hub-add-casino.js +150 -0
  66. package/dist/src/plugins/hub-authenticate.d.ts +1 -0
  67. package/dist/src/plugins/hub-authenticate.js +175 -0
  68. package/dist/src/plugins/hub-balance-alert.d.ts +1 -0
  69. package/dist/src/plugins/hub-balance-alert.js +43 -0
  70. package/dist/src/plugins/hub-claim-faucet.d.ts +1 -0
  71. package/dist/src/plugins/hub-claim-faucet.js +85 -0
  72. package/dist/src/plugins/hub-current-x.d.ts +1 -0
  73. package/dist/src/plugins/hub-current-x.js +62 -0
  74. package/dist/src/plugins/hub-schema-prefix.d.ts +1 -0
  75. package/dist/src/plugins/hub-schema-prefix.js +25 -0
  76. package/dist/src/plugins/hub-user-balance-by-currency.d.ts +1 -0
  77. package/dist/src/plugins/hub-user-balance-by-currency.js +55 -0
  78. package/dist/src/plugins/hub-withdraw.d.ts +1 -0
  79. package/dist/src/plugins/hub-withdraw.js +133 -0
  80. package/dist/src/plugins/id-to-node-id.d.ts +1 -0
  81. package/dist/src/plugins/id-to-node-id.js +31 -0
  82. package/dist/src/plugins/validate-fields.d.ts +1 -0
  83. package/dist/src/plugins/validate-fields.js +61 -0
  84. package/dist/src/process-transfers.d.ts +7 -0
  85. package/dist/src/process-transfers.js +413 -0
  86. package/dist/src/process-withdrawal-request.d.ts +5 -0
  87. package/dist/src/process-withdrawal-request.js +129 -0
  88. package/dist/src/server/graphile.config.d.ts +33 -0
  89. package/dist/src/server/graphile.config.js +166 -0
  90. package/dist/src/server/handle-errors.d.ts +10 -0
  91. package/dist/src/server/handle-errors.js +88 -0
  92. package/dist/src/server/index.d.ts +2 -0
  93. package/dist/src/server/index.js +69 -0
  94. package/dist/src/server/middleware/authentication.d.ts +4 -0
  95. package/dist/src/server/middleware/authentication.js +55 -0
  96. package/dist/src/server/middleware/cors.d.ts +3 -0
  97. package/dist/src/server/middleware/cors.js +14 -0
  98. package/dist/src/services/jwt-service.d.ts +13 -0
  99. package/dist/src/services/jwt-service.js +131 -0
  100. package/dist/src/smart-tags.d.ts +1 -0
  101. package/dist/src/smart-tags.js +55 -0
  102. package/dist/src/util.d.ts +12 -0
  103. package/dist/src/util.js +4 -0
  104. package/dist/src/validate.d.ts +9 -0
  105. package/dist/src/validate.js +91 -0
  106. package/package.json +69 -0
package/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # @moneypot/hub
2
+
3
+ This library implements a controller server that manages users and balances across any number of Moneypot casinos.
4
+
5
+ You can use it to quickly create your own controller.
6
+
7
+ Example implementations:
8
+
9
+ 1. <https://github.com/moneypot/wheel-controller/>: A wheel of fortune game ([frontend](https://github.com/moneypot/wheel-experience/))
10
+ 2. <https://github.com/moneypot/dice-controller/>: A dice game ([frontend](https://github.com/moneypot/dice-experience/))
11
+
12
+ ## Manual
13
+
14
+ View our docs: https://moneypot.com/docs/hub
15
+
16
+ ## Install
17
+
18
+ ```sh
19
+ $ npm install @moneypot/hub
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```typescript
25
+ import { defaultPlugins, ServerOptions, startAndListen } from "@moneypot/hub";
26
+ import path from "path";
27
+
28
+ const options: ServerOptions = {
29
+ // Name of the postgres schemas you're exposing to the GraphQL API (if any)
30
+ extraPgSchemas: ["app"],
31
+ plugins: [
32
+ ...defaultPlugins,
33
+ // And any custom plugins you've written
34
+ ],
35
+ // Where to save the GraphQL schema (derived from your database + plugins)
36
+ exportSchemaSDLPath: path.join(import.meta.dirname, "../schema.graphql"),
37
+ // Where your database migration files are located (if any)
38
+ userDatabaseMigrationsPath: path.join(import.meta.dirname, "../pg-versions"),
39
+ };
40
+
41
+ startAndListen(options)
42
+ .then(({ port }) => {
43
+ console.log(`Listening on ${port}`);
44
+ })
45
+ .catch(console.error);
46
+ ```
47
+
48
+ ## Development
49
+
50
+ To work on this library , there's a mini project in the `./demo` directory that uses `@moneypot/hub` as a lib.
51
+
52
+ ```sh
53
+ createdb hub_demo
54
+ ```
55
+
56
+ Ensure this role exists:
57
+
58
+ ```sql
59
+ CREATE ROLE app_postgraphile LOGIN PASSWORD 'pass';
60
+ ```
61
+
62
+ Create `.env`:
63
+
64
+ ```ini
65
+ DATABASE_URL="postgres://app_postgraphile:pass@localhost/hub_demo"
66
+ SUPERUSER_DATABASE_URL="postgres://app_superuser:pass@localhost/hub_demo"
67
+ GRAPHILE_ENV=development
68
+ NODE_ENV=development
69
+ ```
70
+
71
+ Run the demo:
72
+
73
+ ```sh
74
+ cd demo
75
+ npm install
76
+ npm run dev
77
+ ```
78
+
79
+ The hub-demo server should be running at <http://localhost:8888/graphql>.
80
+
81
+ ### Dashboard development
82
+
83
+ To work on the `./dashboard` react app, ensure that hub-demo is running.
84
+
85
+ Terminal 1: Run demo controller inside this repo's `demo` dir:
86
+
87
+ ```sh
88
+ cd demo
89
+ npm install
90
+ npm run dev
91
+ # Listening on :8888
92
+ ```
93
+
94
+ Terminal 2: Run dashboard project pointed at demo controller
95
+
96
+ ```
97
+ cd dashboard
98
+ npm install
99
+ VITE_GRAPHQL_URL=http://localhost:8888/graphql npm run dev
100
+ ```
101
+
102
+ Now visit the localhost url that it prints out.
103
+
104
+ (The `VITE_GRAPHQL_URL` override is necessary since the dashboard defaults to `window.location.origin + "/graphql"` as its api endpoint since it usually runs bundled with the hub server.)
105
+
106
+ ## Change log
107
+
108
+ - 1.0.0: Initial release.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env node
2
+ import { z } from "zod";
3
+ import { dbInsertCasino } from "../src/db/internal.js";
4
+ import { superuserPool, upsertCurrencies } from "../src/db/index.js";
5
+ import { createGraphqlClient } from "../src/graphql-client.js";
6
+ import { GET_CURRENCIES } from "../src/graphql-queries.js";
7
+ import { gql } from "../src/__generated__/gql.js";
8
+ const GET_CURRENT_CONTROLLER = gql(`
9
+ query AddCasino_GetCurrentController {
10
+ currentController {
11
+ id
12
+ }
13
+ }
14
+ `);
15
+ function httpUrl() {
16
+ return z
17
+ .string()
18
+ .url("Invalid URL format")
19
+ .refine((url) => url.startsWith("http://") || url.startsWith("https://"), {
20
+ message: "URL must start with http:// or https://",
21
+ });
22
+ }
23
+ const AddCasinoSchema = z
24
+ .object({
25
+ name: z.string().min(1, "Name is required"),
26
+ base_url: httpUrl().refine((url) => new URL(url).pathname === "/", {
27
+ message: "URL should not have a path",
28
+ }),
29
+ graphql_url: httpUrl().refine((url) => new URL(url).pathname === "/graphql", {
30
+ message: "URL should have /graphql path",
31
+ }),
32
+ api_key: z.string().uuid("API key must be a UUID"),
33
+ })
34
+ .strict();
35
+ function parseArgs(args) {
36
+ const parsedArgs = {};
37
+ let i = 0;
38
+ while (i < args.length - 1) {
39
+ const arg = args[i];
40
+ const nextArg = args[i + 1];
41
+ if (arg.startsWith("--")) {
42
+ const key = arg.slice(2);
43
+ if (!nextArg.startsWith("--")) {
44
+ parsedArgs[key] = nextArg;
45
+ i += 2;
46
+ }
47
+ else {
48
+ console.warn(`Warning: No value provided for argument: ${arg}`);
49
+ i += 1;
50
+ }
51
+ }
52
+ else {
53
+ console.warn(`Warning: Ignoring unexpected argument: ${arg}`);
54
+ i += 1;
55
+ }
56
+ }
57
+ if (i === args.length - 1) {
58
+ const lastArg = args[i];
59
+ if (lastArg.startsWith("--")) {
60
+ console.warn(`Warning: No value provided for last argument: ${lastArg}`);
61
+ }
62
+ else {
63
+ console.warn(`Warning: Ignoring unexpected last argument: ${lastArg}`);
64
+ }
65
+ }
66
+ return parsedArgs;
67
+ }
68
+ async function addCasino(input) {
69
+ const graphqlClient = createGraphqlClient({
70
+ graphqlUrl: input.graphql_url,
71
+ apiKey: input.api_key,
72
+ });
73
+ console.log(`Verifying controller creds with ${input.graphql_url}...`);
74
+ const result1 = await graphqlClient.request(GET_CURRENT_CONTROLLER);
75
+ if (!result1.currentController) {
76
+ throw new Error("Bad controller ID or API key");
77
+ }
78
+ const result2 = await graphqlClient.request(GET_CURRENCIES);
79
+ const currencies = result2.allCurrencies?.nodes.flatMap((x) => (x ? [x] : [])) || [];
80
+ console.log(`Found ${currencies.length} currencies:`, currencies.map((x) => x.id).join(", "));
81
+ const fields = { ...input, controller_id: result1.currentController.id };
82
+ const casino = await dbInsertCasino(superuserPool, fields);
83
+ if (currencies.length > 0) {
84
+ await upsertCurrencies(superuserPool, { casinoId: casino.id, currencies });
85
+ }
86
+ return casino;
87
+ }
88
+ function printUsageExample() {
89
+ console.error("\nUsage example:");
90
+ console.error("add-casino \\");
91
+ console.error(` --name "moneypot.dev" \\`);
92
+ console.error(" --base_url https://moneypot.dev \\");
93
+ console.error(" --graphql_url https://api.moneypot.dev/graphql \\");
94
+ console.error(" --api_key <uuid>");
95
+ }
96
+ async function main() {
97
+ const args = process.argv.slice(2);
98
+ const params = parseArgs(args);
99
+ console.log("params", params);
100
+ const validated = AddCasinoSchema.safeParse(params);
101
+ if (!validated.success) {
102
+ console.error("Validation error:");
103
+ validated.error.errors.forEach((err) => {
104
+ console.error(`--${err.path.join(".")}: ${err.message}`);
105
+ });
106
+ printUsageExample();
107
+ process.exit(1);
108
+ }
109
+ const casino = await addCasino(validated.data);
110
+ console.log(`Added casino with ID: "${casino.id}"`);
111
+ }
112
+ main().catch((err) => {
113
+ console.error("An unexpected error occurred:", err);
114
+ printUsageExample();
115
+ process.exit(1);
116
+ });