gamepay-server 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ declare const _default: (urlPrefix?: string) => {
2
+ registerUser: string;
3
+ placeBetCryptoInUsd: string;
4
+ winBetCryptoInUsd: string;
5
+ withdrawCryptoInUsd: string;
6
+ };
7
+ export default _default;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (urlPrefix) => ({
4
+ registerUser: `${urlPrefix}/user-create`,
5
+ placeBetCryptoInUsd: `${urlPrefix}/user-placeBetCryptoInUsd`,
6
+ winBetCryptoInUsd: `${urlPrefix}/user-winBetCryptoInUsd`,
7
+ withdrawCryptoInUsd: `${urlPrefix}/user-withdrawCryptoInUsd`,
8
+ fetchCryptoWithdrawalHistory: `${urlPrefix}/user-fetchCryptoWithdrawalHistory`,
9
+ fetchCryptoDepositHistory: `${urlPrefix}/user-fetchCryptoDepositHistory`,
10
+ });
11
+ //# sourceMappingURL=url.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"url.js","sourceRoot":"","sources":["../../src/config/url.ts"],"names":[],"mappings":";;AAAA,kBAAe,CAAC,SAAkB,EAAE,EAAE,CAAC,CAAC;IACtC,YAAY,EAAE,GAAG,SAAS,cAAc;IACxC,mBAAmB,EAAE,GAAG,SAAS,2BAA2B;IAC5D,iBAAiB,EAAE,GAAG,SAAS,yBAAyB;IACxD,mBAAmB,EAAE,GAAG,SAAS,2BAA2B;IAC5D,4BAA4B,EAAE,GAAG,SAAS,oCAAoC;IAC9E,yBAAyB,EAAE,GAAG,SAAS,iCAAiC;CACzE,CAAC,CAAC"}
@@ -0,0 +1,39 @@
1
+ import { BlockchainName, DevType, DollarsAsString } from "./typings";
2
+ import Backend from "./utils/Backend";
3
+ export default class GamePay {
4
+ projectEmail: string;
5
+ secretKey: string;
6
+ backend: Backend;
7
+ type: DevType;
8
+ constructor(email: string, secretKey: string);
9
+ /**
10
+ * Registers a new user with your website.
11
+ *
12
+ * New user will be created, and you will need to store the User ID for future queries.
13
+ * @param userEmail
14
+ * @returns Promise<{ userEmail: string; userId: string }>
15
+ */
16
+ userRegister(userEmail: string): Promise<{
17
+ userEmail: string;
18
+ userId: string;
19
+ }>;
20
+ /**
21
+ * Deducts a User balance right after they have placed a bet
22
+ * @param userId
23
+ * @param betAmountUsd
24
+ * @param blockchainName
25
+ * @returns
26
+ */
27
+ userPlaceBet(userId: string, betAmountUsd: DollarsAsString, blockchainName: BlockchainName): Promise<{
28
+ error: boolean;
29
+ msg: string;
30
+ }>;
31
+ userWinBet(userId: string, winAmountUsd: DollarsAsString, blockchainName: BlockchainName): Promise<{
32
+ error: boolean;
33
+ msg: string;
34
+ }>;
35
+ userWithdraw(userId: string, withdrawAmountUsd: string, blockchainName: BlockchainName, receiverAddress: string, extraData: any): Promise<{
36
+ error: boolean;
37
+ msg: string;
38
+ }>;
39
+ }
package/dist/index.js ADDED
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const Backend_1 = require("./utils/Backend");
4
+ class GamePay {
5
+ constructor(email, secretKey) {
6
+ const keyType = secretKey.split("-")[0];
7
+ if (keyType !== "sk") {
8
+ throw "Invalid key type, key type needs to begin with sk";
9
+ }
10
+ this.type = secretKey.split("-")[1];
11
+ this.backend = new Backend_1.default("https://us-central1-payment-gateway-7bee8.cloudfunctions.net");
12
+ this.projectEmail = email;
13
+ this.secretKey = secretKey;
14
+ }
15
+ /**
16
+ * Registers a new user with your website.
17
+ *
18
+ * New user will be created, and you will need to store the User ID for future queries.
19
+ * @param userEmail
20
+ * @returns Promise<{ userEmail: string; userId: string }>
21
+ */
22
+ userRegister(userEmail) {
23
+ return this.backend.user.create(this.secretKey, this.projectEmail, userEmail.toLowerCase());
24
+ }
25
+ /**
26
+ * Deducts a User balance right after they have placed a bet
27
+ * @param userId
28
+ * @param betAmountUsd
29
+ * @param blockchainName
30
+ * @returns
31
+ */
32
+ userPlaceBet(userId, betAmountUsd, blockchainName) {
33
+ return this.backend.user
34
+ .placeBetCryptoInUsd(this.secretKey, this.projectEmail, userId, betAmountUsd, blockchainName)
35
+ .catch((err) => {
36
+ console.error("Error with placeBetCryptoInUsd", err);
37
+ return {
38
+ error: true,
39
+ err,
40
+ msg: "Something went wrong placing user bet",
41
+ };
42
+ });
43
+ }
44
+ userWinBet(userId, winAmountUsd, blockchainName) {
45
+ return this.backend.user
46
+ .winBetCryptoInUsd(this.secretKey, this.projectEmail, userId, winAmountUsd, blockchainName)
47
+ .catch((err) => {
48
+ console.error("Error with userWinBet", err);
49
+ return { error: true, msg: "Something went wrong placing user bet" };
50
+ });
51
+ }
52
+ userWithdraw(userId, withdrawAmountUsd, blockchainName, receiverAddress, extraData) {
53
+ return this.backend.user
54
+ .withdraw(this.secretKey, this.projectEmail, userId, withdrawAmountUsd, blockchainName, receiverAddress, extraData)
55
+ .catch((err) => {
56
+ console.error("Error with userWithdraw", err);
57
+ return {
58
+ error: true,
59
+ msg: "Something went wrong withdrawing user funds",
60
+ };
61
+ });
62
+ }
63
+ fetchUserWithdrawalHistory(userId, orderBy) {
64
+ return this.backend.user.fetchCryptoWithdrawalHistory(this.secretKey, this.projectEmail, userId, orderBy, {});
65
+ }
66
+ fetchUserDepositHistory(userId, orderBy) {
67
+ return this.backend.user.fetchCryptoDepositHistory(this.secretKey, this.projectEmail, userId, orderBy, {});
68
+ }
69
+ }
70
+ exports.default = GamePay;
71
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AACA,6CAAsC;AAEtC,MAAqB,OAAO;IAM1B,YAAY,KAAa,EAAE,SAAiB;QAC1C,MAAM,OAAO,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAExC,IAAI,OAAO,K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AAC;YAC5C,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,uCAAuC,EAAE,CAAC;QACvE,CAAC,CAAC,CAAC;IACP,CAAC;IAED,YAAY,CACV,MAAc,EACd,iBAAyB,EACzB,cAA8B,EAC9B,eAAuB,EACvB,SAAc;QAEd,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI;aACrB,QAAQ,CACP,IAAI,CAAC,SAAS,EACd,IAAI,CAAC,YAAY,EACjB,MAAM,EACN,iBAAiB,EACjB,cAAc,EACd,eAAe,EACf,SAAS,CACV;aACA,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACb,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,GAAG,CAAC,CAAC;YAC9C,OAAO;gBACL,KAAK,EAAE,IAAI;gBACX,GAAG,EAAE,6CAA6C;aACnD,CAAC;QACJ,CAAC,CAAC,CAAC;IACP,CAAC;IAED,0BAA0B,CAAC,MAAc,EAAE,OAAuB;QAChE,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,4BAA4B,CACnD,IAAI,CAAC,SAAS,EACd,IAAI,CAAC,YAAY,EACjB,MAAM,EACN,OAAO,EACP,EAAE,CACH,CAAC;IACJ,CAAC;IAED,uBAAuB,CAAC,MAAc,EAAE,OAAuB;QAC7D,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,yBAAyB,CAChD,IAAI,CAAC,SAAS,EACd,IAAI,CAAC,YAAY,EACjB,MAAM,EACN,OAAO,EACP,EAAE,CACH,CAAC;IACJ,CAAC;CACF;AApID,0BAoIC"}
@@ -0,0 +1,30 @@
1
+ import { BlockchainName, DollarsAsString } from "../typings";
2
+ export default class {
3
+ urlPrefix: string;
4
+ mappedFunctions: {
5
+ registerUser: string;
6
+ placeBetCryptoInUsd: string;
7
+ winBetCryptoInUsd: string;
8
+ withdrawCryptoInUsd: string;
9
+ };
10
+ constructor(urlPrefix: string);
11
+ private fetch;
12
+ user: {
13
+ create: (secretKey: string, projectEmail: string, userEmail: string) => Promise<{
14
+ userEmail: string;
15
+ userId: string;
16
+ }>;
17
+ placeBetCryptoInUsd: (secretKey: string, projectEmail: string, userId: string, betAmountUsd: DollarsAsString, blockchainName: BlockchainName) => Promise<{
18
+ error: boolean;
19
+ msg: string;
20
+ }>;
21
+ winBetCryptoInUsd: (secretKey: string, projectEmail: string, userId: string, winAmountUsd: DollarsAsString, blockchainName: BlockchainName) => Promise<{
22
+ error: boolean;
23
+ msg: string;
24
+ }>;
25
+ withdraw: (secretKey: string, projectEmail: string, userId: string, withdrawAmountUsd: string, blockchainName: BlockchainName, receiverAddress: string, extraData: any) => Promise<{
26
+ error: boolean;
27
+ msg: string;
28
+ }>;
29
+ };
30
+ }
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const node_fetch_1 = require("node-fetch");
4
+ const url_1 = require("../config/url");
5
+ class default_1 {
6
+ constructor(urlPrefix) {
7
+ this.user = {
8
+ create: (secretKey, projectEmail, userEmail) => {
9
+ return this.fetch(this.mappedFunctions.registerUser, {
10
+ secretKey,
11
+ projectEmail,
12
+ userEmail,
13
+ })
14
+ .then((result) => result.data)
15
+ .catch((err) => {
16
+ console.error("Error with registerUser", err);
17
+ return { error: true, err };
18
+ });
19
+ },
20
+ placeBetCryptoInUsd: (secretKey, projectEmail, userId, betAmountUsd, blockchainName) => {
21
+ return this.fetch(this.mappedFunctions.placeBetCryptoInUsd, {
22
+ secretKey,
23
+ projectEmail,
24
+ userId,
25
+ betAmountUsd,
26
+ blockchainName,
27
+ }).then((result) => result);
28
+ },
29
+ winBetCryptoInUsd: (secretKey, projectEmail, userId, winAmountUsd, blockchainName) => {
30
+ return this.fetch(this.mappedFunctions.winBetCryptoInUsd, {
31
+ secretKey,
32
+ projectEmail,
33
+ userId,
34
+ winAmountUsd,
35
+ blockchainName,
36
+ }).then((result) => result.data);
37
+ },
38
+ withdraw: (secretKey, projectEmail, userId, withdrawAmountUsd, blockchainName, receiverAddress, extraData) => {
39
+ return this.fetch(this.mappedFunctions.withdrawCryptoInUsd, {
40
+ secretKey,
41
+ projectEmail,
42
+ userId,
43
+ withdrawAmountUsd,
44
+ blockchainName,
45
+ receiverAddress,
46
+ extraData: extraData || {},
47
+ }).then((result) => result.data);
48
+ },
49
+ fetchCryptoWithdrawalHistory: (secretKey, projectEmail, userId, orderBy, query) => {
50
+ return this.fetch(this.mappedFunctions.fetchCryptoWithdrawalHistory, {
51
+ secretKey,
52
+ projectEmail,
53
+ userId,
54
+ orderBy,
55
+ query,
56
+ })
57
+ .then((result) => result.data)
58
+ .catch((err) => {
59
+ console.error("Error with fetchCryptoWithdrawalHistory", err);
60
+ return { error: true, err, withdrawals: [] };
61
+ });
62
+ },
63
+ fetchCryptoDepositHistory: (secretKey, projectEmail, userId, orderBy, query) => {
64
+ return this.fetch(this.mappedFunctions.fetchCryptoDepositHistory, {
65
+ secretKey,
66
+ projectEmail,
67
+ userId,
68
+ orderBy,
69
+ query,
70
+ })
71
+ .then((result) => result.data)
72
+ .catch((err) => {
73
+ console.error("Error with fetchCryptoDepositHistory", err);
74
+ return { error: true, err, deposits: [] };
75
+ });
76
+ },
77
+ };
78
+ this.urlPrefix = urlPrefix;
79
+ this.mappedFunctions = (0, url_1.default)(urlPrefix);
80
+ }
81
+ async fetch(url, data) {
82
+ const result = await (0, node_fetch_1.default)(url, {
83
+ method: "POST",
84
+ headers: {
85
+ "Content-Type": "application/json",
86
+ },
87
+ body: JSON.stringify(data),
88
+ }).then((response) => {
89
+ return response.json();
90
+ });
91
+ return result;
92
+ }
93
+ }
94
+ exports.default = default_1;
95
+ //# sourceMappingURL=Backend.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Backend.js","sourceRoot":"","sources":["../../src/utils/Backend.ts"],"names":[],"mappings":";;AAAA,2CAA+B;AAE/B,uCAA6C;AAE7C;IAWE,YAAY,SAAiB;QAmB7B,SAAI,GAAG;YACL,MAAM,EAAE,CACN,S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,CAAC,sCAAsC,EAAE,GAAG,CAAC,CAAC;oBAC3D,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;gBAC5C,CAAC,CAAC,CAAC;YACP,CAAC;SACF,CAAC;QA5HA,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,eAAe,GAAG,IAAA,aAAgB,EAAC,SAAS,CAAC,CAAC;IACrD,CAAC;IAEO,KAAK,CAAC,KAAK,CAAC,GAAW,EAAE,IAAS;QACxC,MAAM,MAAM,GAAG,MAAM,IAAA,oBAAK,EAAC,GAAG,EAAE;YAC9B,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;YACnB,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;QACzB,CAAC,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC;IAChB,CAAC;CA6GF;AAzID,4BAyIC"}
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "gamepay-server",
3
+ "version": "1.0.0",
4
+ "description": "GamePay Javascript library to be used in a secure environment like your server.",
5
+ "main": "index.js",
6
+
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1",
9
+ "build": "tsc"
10
+ },
11
+ "author": "GatewayPayment137",
12
+ "license": "MIT",
13
+ "devDependencies": {
14
+ "@types/node": "^18.0.1",
15
+ "@types/node-fetch": "^2.6.2",
16
+ "typescript": "^4.7.4"
17
+ },
18
+ "dependencies": {
19
+ "node-fetch": "^2.6.1"
20
+ }
21
+ }
@@ -0,0 +1,8 @@
1
+ export default (urlPrefix?: string) => ({
2
+ registerUser: `${urlPrefix}/user-create`,
3
+ placeBetCryptoInUsd: `${urlPrefix}/user-placeBetCryptoInUsd`,
4
+ winBetCryptoInUsd: `${urlPrefix}/user-winBetCryptoInUsd`,
5
+ withdrawCryptoInUsd: `${urlPrefix}/user-withdrawCryptoInUsd`,
6
+ fetchCryptoWithdrawalHistory: `${urlPrefix}/user-fetchCryptoWithdrawalHistory`,
7
+ fetchCryptoDepositHistory: `${urlPrefix}/user-fetchCryptoDepositHistory`,
8
+ });
package/src/index.ts ADDED
@@ -0,0 +1,136 @@
1
+ import { BlockchainName, DevType, DollarsAsString } from "./typings";
2
+ import Backend from "./utils/Backend";
3
+
4
+ export default class GamePay {
5
+ projectEmail: string;
6
+ private secretKey: string;
7
+ private backend: Backend;
8
+ type: DevType;
9
+
10
+ constructor(email: string, secretKey: string) {
11
+ const keyType = secretKey.split("-")[0];
12
+
13
+ if (keyType !== "sk") {
14
+ throw "Invalid key type, key type needs to begin with sk";
15
+ }
16
+ this.type = secretKey.split("-")[1] as DevType;
17
+ this.backend = new Backend(
18
+ "https://us-central1-payment-gateway-7bee8.cloudfunctions.net"
19
+ );
20
+
21
+ this.projectEmail = email;
22
+ this.secretKey = secretKey;
23
+ }
24
+
25
+ /**
26
+ * Registers a new user with your website.
27
+ *
28
+ * New user will be created, and you will need to store the User ID for future queries.
29
+ * @param userEmail
30
+ * @returns Promise<{ userEmail: string; userId: string }>
31
+ */
32
+ userRegister(
33
+ userEmail: string
34
+ ): Promise<{ userEmail: string; userId: string }> {
35
+ return this.backend.user.create(
36
+ this.secretKey,
37
+ this.projectEmail,
38
+ userEmail.toLowerCase()
39
+ );
40
+ }
41
+
42
+ /**
43
+ * Deducts a User balance right after they have placed a bet
44
+ * @param userId
45
+ * @param betAmountUsd
46
+ * @param blockchainName
47
+ * @returns
48
+ */
49
+ userPlaceBet(
50
+ userId: string,
51
+ betAmountUsd: DollarsAsString,
52
+ blockchainName: BlockchainName
53
+ ): Promise<{ error: boolean; msg: string }> {
54
+ return this.backend.user
55
+ .placeBetCryptoInUsd(
56
+ this.secretKey,
57
+ this.projectEmail,
58
+ userId,
59
+ betAmountUsd,
60
+ blockchainName
61
+ )
62
+ .catch((err) => {
63
+ console.error("Error with placeBetCryptoInUsd", err);
64
+ return {
65
+ error: true,
66
+ err,
67
+ msg: "Something went wrong placing user bet",
68
+ };
69
+ });
70
+ }
71
+
72
+ userWinBet(
73
+ userId: string,
74
+ winAmountUsd: DollarsAsString,
75
+ blockchainName: BlockchainName
76
+ ): Promise<{ error: boolean; msg: string }> {
77
+ return this.backend.user
78
+ .winBetCryptoInUsd(
79
+ this.secretKey,
80
+ this.projectEmail,
81
+ userId,
82
+ winAmountUsd,
83
+ blockchainName
84
+ )
85
+ .catch((err) => {
86
+ console.error("Error with userWinBet", err);
87
+ return { error: true, msg: "Something went wrong placing user bet" };
88
+ });
89
+ }
90
+
91
+ userWithdraw(
92
+ userId: string,
93
+ withdrawAmountUsd: string,
94
+ blockchainName: BlockchainName,
95
+ receiverAddress: string,
96
+ extraData: any
97
+ ): Promise<{ error: boolean; msg: string }> {
98
+ return this.backend.user
99
+ .withdraw(
100
+ this.secretKey,
101
+ this.projectEmail,
102
+ userId,
103
+ withdrawAmountUsd,
104
+ blockchainName,
105
+ receiverAddress,
106
+ extraData
107
+ )
108
+ .catch((err) => {
109
+ console.error("Error with userWithdraw", err);
110
+ return {
111
+ error: true,
112
+ msg: "Something went wrong withdrawing user funds",
113
+ };
114
+ });
115
+ }
116
+
117
+ fetchUserWithdrawalHistory(userId: string, orderBy: "asc" | "desc") {
118
+ return this.backend.user.fetchCryptoWithdrawalHistory(
119
+ this.secretKey,
120
+ this.projectEmail,
121
+ userId,
122
+ orderBy,
123
+ {}
124
+ );
125
+ }
126
+
127
+ fetchUserDepositHistory(userId: string, orderBy: "asc" | "desc") {
128
+ return this.backend.user.fetchCryptoDepositHistory(
129
+ this.secretKey,
130
+ this.projectEmail,
131
+ userId,
132
+ orderBy,
133
+ {}
134
+ );
135
+ }
136
+ }
@@ -0,0 +1,27 @@
1
+ export type DevType = "test" | "live";
2
+ /**
3
+ * Dollars as string.
4
+ *
5
+ * Ex:
6
+ * 50.66,
7
+ * 25,
8
+ * 12.5,
9
+ * 200.05
10
+ */
11
+ export type DollarsAsString = string;
12
+
13
+ export type BlockchainType = "evm" | "btc";
14
+ export type BlockchainName =
15
+ | "bitcoin"
16
+ | "ethereum"
17
+ | "avalanche"
18
+ | "matic"
19
+ | "kovan";
20
+ export type BlockchainSymbol = "btc" | "eth" | "avax" | "matic";
21
+
22
+ export type Blockchain = {
23
+ type: BlockchainType;
24
+ name: BlockchainName;
25
+ symbol: BlockchainSymbol;
26
+ decimals: number;
27
+ };
@@ -0,0 +1,142 @@
1
+ import fetch from "node-fetch";
2
+ import { BlockchainName, DollarsAsString } from "../typings";
3
+ import backendFunctions from "../config/url";
4
+
5
+ export default class {
6
+ urlPrefix: string;
7
+ mappedFunctions: {
8
+ registerUser: string;
9
+ placeBetCryptoInUsd: string;
10
+ winBetCryptoInUsd: string;
11
+ withdrawCryptoInUsd: string;
12
+ fetchCryptoWithdrawalHistory: string;
13
+ fetchCryptoDepositHistory: string;
14
+ };
15
+
16
+ constructor(urlPrefix: string) {
17
+ this.urlPrefix = urlPrefix;
18
+ this.mappedFunctions = backendFunctions(urlPrefix);
19
+ }
20
+
21
+ private async fetch(url: string, data: any) {
22
+ const result = await fetch(url, {
23
+ method: "POST", // or 'PUT'
24
+ headers: {
25
+ "Content-Type": "application/json",
26
+ },
27
+ body: JSON.stringify(data),
28
+ }).then((response) => {
29
+ return response.json();
30
+ });
31
+
32
+ return result;
33
+ }
34
+
35
+ user = {
36
+ create: (
37
+ secretKey: string,
38
+ projectEmail: string,
39
+ userEmail: string
40
+ ): Promise<{ userEmail: string; userId: string }> => {
41
+ return this.fetch(this.mappedFunctions.registerUser, {
42
+ secretKey,
43
+ projectEmail,
44
+ userEmail,
45
+ })
46
+ .then((result: any) => result.data)
47
+ .catch((err: any) => {
48
+ console.error("Error with registerUser", err);
49
+ return { error: true, err };
50
+ });
51
+ },
52
+ placeBetCryptoInUsd: (
53
+ secretKey: string,
54
+ projectEmail: string,
55
+ userId: string,
56
+ betAmountUsd: DollarsAsString,
57
+ blockchainName: BlockchainName
58
+ ): Promise<{ error: boolean; msg: string }> => {
59
+ return this.fetch(this.mappedFunctions.placeBetCryptoInUsd, {
60
+ secretKey,
61
+ projectEmail,
62
+ userId,
63
+ betAmountUsd,
64
+ blockchainName,
65
+ }).then((result: any) => result);
66
+ },
67
+ winBetCryptoInUsd: (
68
+ secretKey: string,
69
+ projectEmail: string,
70
+ userId: string,
71
+ winAmountUsd: DollarsAsString,
72
+ blockchainName: BlockchainName
73
+ ): Promise<{ error: boolean; msg: string }> => {
74
+ return this.fetch(this.mappedFunctions.winBetCryptoInUsd, {
75
+ secretKey,
76
+ projectEmail,
77
+ userId,
78
+ winAmountUsd,
79
+ blockchainName,
80
+ }).then((result: any) => result.data);
81
+ },
82
+ withdraw: (
83
+ secretKey: string,
84
+ projectEmail: string,
85
+ userId: string,
86
+ withdrawAmountUsd: string,
87
+ blockchainName: BlockchainName,
88
+ receiverAddress: string,
89
+ extraData: any
90
+ ): Promise<{ error: boolean; msg: string }> => {
91
+ return this.fetch(this.mappedFunctions.withdrawCryptoInUsd, {
92
+ secretKey,
93
+ projectEmail,
94
+ userId,
95
+ withdrawAmountUsd,
96
+ blockchainName,
97
+ receiverAddress,
98
+ extraData: extraData || {},
99
+ }).then((result: any) => result.data);
100
+ },
101
+ fetchCryptoWithdrawalHistory: (
102
+ secretKey: string,
103
+ projectEmail: string,
104
+ userId: string,
105
+ orderBy: "asc" | "desc",
106
+ query: any
107
+ ) => {
108
+ return this.fetch(this.mappedFunctions.fetchCryptoWithdrawalHistory, {
109
+ secretKey,
110
+ projectEmail,
111
+ userId,
112
+ orderBy,
113
+ query,
114
+ })
115
+ .then((result: any) => result.data)
116
+ .catch((err: any) => {
117
+ console.error("Error with fetchCryptoWithdrawalHistory", err);
118
+ return { error: true, err, withdrawals: [] };
119
+ });
120
+ },
121
+ fetchCryptoDepositHistory: (
122
+ secretKey: string,
123
+ projectEmail: string,
124
+ userId: string,
125
+ orderBy: "asc" | "desc",
126
+ query: any
127
+ ) => {
128
+ return this.fetch(this.mappedFunctions.fetchCryptoDepositHistory, {
129
+ secretKey,
130
+ projectEmail,
131
+ userId,
132
+ orderBy,
133
+ query,
134
+ })
135
+ .then((result: any) => result.data)
136
+ .catch((err: any) => {
137
+ console.error("Error with fetchCryptoDepositHistory", err);
138
+ return { error: true, err, deposits: [] };
139
+ });
140
+ },
141
+ };
142
+ }
package/test/index.js ADDED
@@ -0,0 +1,29 @@
1
+ const GamePay = require("../dist/index.js");
2
+ async function main() {
3
+ const SECRET_KEY =
4
+ "sk-test-coyMQR5lHP67HWRGhkykcS78ztHFdvPTTBrioj45QUNxJm0dAWmH4Z1yNkcdYnud";
5
+ const PROJECT_EMAIL = "gamecoin@example.com";
6
+ // const USER_EMAIL = "helloWORLD@gmail.com";
7
+ const USER_ID =
8
+ "7z5LIMfkkEajT7kWieMPMHWVVKXg-efbc9209-f39d-4fe8-ba7f-903e50c01c30";
9
+
10
+ const gamePay = new GamePay.default(PROJECT_EMAIL, SECRET_KEY);
11
+ // const registerUserResult = await gamePay.userRegister(USER_EMAIL);
12
+ // console.log("registerUserResult", registerUserResult);
13
+
14
+ // const userPlaceBet = await gamePay.userPlaceBet(USER_ID, 10.5, "kovan");
15
+ // console.log("userPlaceBet", userPlaceBet);
16
+
17
+ // const userWinBet = await gamePay.userWinBet(USER_ID, 25, "kovan");
18
+ // console.log("userWinBet", userWinBet);
19
+
20
+ const userWithdraw = await gamePay.userWithdraw(
21
+ USER_ID,
22
+ 25,
23
+ "kovan",
24
+ "0x84e571A93B66709C72f5e6919612E986239EE163"
25
+ );
26
+ console.log("userWithdraw", userWithdraw);
27
+ }
28
+
29
+ main();
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "noImplicitReturns": true,
5
+ "noUnusedLocals": true,
6
+ "outDir": "dist",
7
+ "sourceMap": true,
8
+ "strict": true,
9
+ "target": "es2017",
10
+ "types": ["node"]
11
+ },
12
+ "compileOnSave": true,
13
+ "include": ["src"],
14
+ "resolveJsonModuletrue": true
15
+ }