@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.
- package/README.md +108 -0
- package/dist/cli/add-casino.d.ts +2 -0
- package/dist/cli/add-casino.js +116 -0
- package/dist/dashboard/assets/index-BtrbrisP.js +360 -0
- package/dist/dashboard/assets/index-tK7EUtyc.css +5 -0
- package/dist/dashboard/index.html +13 -0
- package/dist/src/GraphQLError.d.ts +8 -0
- package/dist/src/GraphQLError.js +79 -0
- package/dist/src/__generated__/fragment-masking.d.ts +19 -0
- package/dist/src/__generated__/fragment-masking.js +16 -0
- package/dist/src/__generated__/gql.d.ts +26 -0
- package/dist/src/__generated__/gql.js +15 -0
- package/dist/src/__generated__/graphql.d.ts +3129 -0
- package/dist/src/__generated__/graphql.js +454 -0
- package/dist/src/__generated__/index.d.ts +2 -0
- package/dist/src/__generated__/index.js +2 -0
- package/dist/src/config.d.ts +14 -0
- package/dist/src/config.js +57 -0
- package/dist/src/db/index.d.ts +89 -0
- package/dist/src/db/index.js +339 -0
- package/dist/src/db/internal.d.ts +7 -0
- package/dist/src/db/internal.js +33 -0
- package/dist/src/db/public.d.ts +7 -0
- package/dist/src/db/public.js +20 -0
- package/dist/src/db/types.d.ts +80 -0
- package/dist/src/db/types.js +1 -0
- package/dist/src/db/util.d.ts +6 -0
- package/dist/src/db/util.js +9 -0
- package/dist/src/express.d.ts +13 -0
- package/dist/src/express.js +1 -0
- package/dist/src/grafast.d.ts +1 -0
- package/dist/src/grafast.js +1 -0
- package/dist/src/graphile.d.ts +1 -0
- package/dist/src/graphile.js +1 -0
- package/dist/src/graphql-client.d.ts +6 -0
- package/dist/src/graphql-client.js +8 -0
- package/dist/src/graphql-queries.d.ts +18 -0
- package/dist/src/graphql-queries.js +123 -0
- package/dist/src/graphql.d.ts +1 -0
- package/dist/src/graphql.js +1 -0
- package/dist/src/index.d.ts +15 -0
- package/dist/src/index.js +65 -0
- package/dist/src/logger.d.ts +9 -0
- package/dist/src/logger.js +21 -0
- package/dist/src/pg-versions/001-schema.sql +456 -0
- package/dist/src/plugins/caas-add-casino.d.ts +1 -0
- package/dist/src/plugins/caas-add-casino.js +150 -0
- package/dist/src/plugins/caas-authenticate.d.ts +1 -0
- package/dist/src/plugins/caas-authenticate.js +175 -0
- package/dist/src/plugins/caas-balance-alert.d.ts +1 -0
- package/dist/src/plugins/caas-balance-alert.js +43 -0
- package/dist/src/plugins/caas-claim-faucet.d.ts +1 -0
- package/dist/src/plugins/caas-claim-faucet.js +85 -0
- package/dist/src/plugins/caas-current-x.d.ts +1 -0
- package/dist/src/plugins/caas-current-x.js +62 -0
- package/dist/src/plugins/caas-schema-prefix.d.ts +1 -0
- package/dist/src/plugins/caas-schema-prefix.js +25 -0
- package/dist/src/plugins/caas-user-balance-by-currency.d.ts +1 -0
- package/dist/src/plugins/caas-user-balance-by-currency.js +55 -0
- package/dist/src/plugins/caas-withdraw.d.ts +1 -0
- package/dist/src/plugins/caas-withdraw.js +133 -0
- package/dist/src/plugins/debug.d.ts +1 -0
- package/dist/src/plugins/debug.js +14 -0
- package/dist/src/plugins/hub-add-casino.d.ts +1 -0
- package/dist/src/plugins/hub-add-casino.js +150 -0
- package/dist/src/plugins/hub-authenticate.d.ts +1 -0
- package/dist/src/plugins/hub-authenticate.js +175 -0
- package/dist/src/plugins/hub-balance-alert.d.ts +1 -0
- package/dist/src/plugins/hub-balance-alert.js +43 -0
- package/dist/src/plugins/hub-claim-faucet.d.ts +1 -0
- package/dist/src/plugins/hub-claim-faucet.js +85 -0
- package/dist/src/plugins/hub-current-x.d.ts +1 -0
- package/dist/src/plugins/hub-current-x.js +62 -0
- package/dist/src/plugins/hub-schema-prefix.d.ts +1 -0
- package/dist/src/plugins/hub-schema-prefix.js +25 -0
- package/dist/src/plugins/hub-user-balance-by-currency.d.ts +1 -0
- package/dist/src/plugins/hub-user-balance-by-currency.js +55 -0
- package/dist/src/plugins/hub-withdraw.d.ts +1 -0
- package/dist/src/plugins/hub-withdraw.js +133 -0
- package/dist/src/plugins/id-to-node-id.d.ts +1 -0
- package/dist/src/plugins/id-to-node-id.js +31 -0
- package/dist/src/plugins/validate-fields.d.ts +1 -0
- package/dist/src/plugins/validate-fields.js +61 -0
- package/dist/src/process-transfers.d.ts +7 -0
- package/dist/src/process-transfers.js +413 -0
- package/dist/src/process-withdrawal-request.d.ts +5 -0
- package/dist/src/process-withdrawal-request.js +129 -0
- package/dist/src/server/graphile.config.d.ts +33 -0
- package/dist/src/server/graphile.config.js +166 -0
- package/dist/src/server/handle-errors.d.ts +10 -0
- package/dist/src/server/handle-errors.js +88 -0
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/index.js +69 -0
- package/dist/src/server/middleware/authentication.d.ts +4 -0
- package/dist/src/server/middleware/authentication.js +55 -0
- package/dist/src/server/middleware/cors.d.ts +3 -0
- package/dist/src/server/middleware/cors.js +14 -0
- package/dist/src/services/jwt-service.d.ts +13 -0
- package/dist/src/services/jwt-service.js +131 -0
- package/dist/src/smart-tags.d.ts +1 -0
- package/dist/src/smart-tags.js +55 -0
- package/dist/src/util.d.ts +12 -0
- package/dist/src/util.js +4 -0
- package/dist/src/validate.d.ts +9 -0
- package/dist/src/validate.js +91 -0
- 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,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
|
+
});
|