odin-connect 1.0.1 → 1.0.3

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 (52) hide show
  1. package/.github/workflows/pr.yml +24 -0
  2. package/demo/package-lock.json +6 -2
  3. package/dist/index.d.ts +4 -2
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +1 -0
  6. package/dist/models/achievement.d.ts +20 -0
  7. package/dist/models/achievement.d.ts.map +1 -0
  8. package/dist/models/achievement.js +1 -0
  9. package/dist/models/activity.d.ts +11 -0
  10. package/dist/models/activity.d.ts.map +1 -0
  11. package/dist/models/activity.js +1 -0
  12. package/dist/models/token.d.ts +5 -0
  13. package/dist/models/token.d.ts.map +1 -1
  14. package/dist/services/api.d.ts +42 -2
  15. package/dist/services/api.d.ts.map +1 -1
  16. package/dist/services/api.js +115 -9
  17. package/dist/services/api.test.d.ts +2 -0
  18. package/dist/services/api.test.d.ts.map +1 -0
  19. package/dist/services/api.test.js +70 -0
  20. package/dist/services/connect.d.ts +26 -4
  21. package/dist/services/connect.d.ts.map +1 -1
  22. package/dist/services/connect.js +93 -28
  23. package/dist/services/connect.test.d.ts +2 -0
  24. package/dist/services/connect.test.d.ts.map +1 -0
  25. package/dist/services/connect.test.js +306 -0
  26. package/dist/services/http.d.ts +6 -2
  27. package/dist/services/http.d.ts.map +1 -1
  28. package/dist/services/http.js +34 -2
  29. package/dist/services/http.test.d.ts +2 -0
  30. package/dist/services/http.test.d.ts.map +1 -0
  31. package/dist/services/http.test.js +37 -0
  32. package/dist/utils/conver-to-odin-amount.test.d.ts +2 -0
  33. package/dist/utils/conver-to-odin-amount.test.d.ts.map +1 -0
  34. package/dist/utils/conver-to-odin-amount.test.js +36 -0
  35. package/dist/utils/convert-to-odin-amount.d.ts +3 -0
  36. package/dist/utils/convert-to-odin-amount.d.ts.map +1 -0
  37. package/dist/utils/convert-to-odin-amount.js +26 -0
  38. package/dist/utils/index.d.ts +6 -0
  39. package/dist/utils/index.d.ts.map +1 -0
  40. package/dist/utils/index.js +134 -0
  41. package/dist/utils/token-field-validators.test.d.ts +2 -0
  42. package/dist/utils/token-field-validators.test.d.ts.map +1 -0
  43. package/dist/utils/token-field-validators.test.js +31 -0
  44. package/package.json +7 -2
  45. package/readme.md +57 -3
  46. package/vitest.config.ts +7 -0
  47. package/dist/api.d.ts +0 -11
  48. package/dist/api.d.ts.map +0 -1
  49. package/dist/api.js +0 -14
  50. package/dist/http.d.ts +0 -6
  51. package/dist/http.d.ts.map +0 -1
  52. package/dist/http.js +0 -11
@@ -0,0 +1,36 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { convertToOdinAmount } from ".";
3
+ describe("convertToOdinAmount", () => {
4
+ it("should convert to Odin amount correctly", () => {
5
+ // divisibility of 8 i.e. ODIN tokens, BTC
6
+ expect(convertToOdinAmount("0.1", { decimals: 3, divisibility: 8 })).toBe(10000000000n);
7
+ expect(convertToOdinAmount("0.00000001", { decimals: 3, divisibility: 8 })).toBe(1000n);
8
+ expect(convertToOdinAmount("0.00000000000000001", { decimals: 3, divisibility: 8 })).toBe(0n);
9
+ // divisibility of 16 i.e. PUPS
10
+ expect(convertToOdinAmount("0.0000000000000005", { decimals: 3, divisibility: 16 })).toBe(5000n);
11
+ expect(convertToOdinAmount("2.00000000002", { decimals: 3, divisibility: 16 })).toBe(20000000000200000000n);
12
+ // divisibility of 0 i.e. whole tokens only
13
+ expect(convertToOdinAmount("120", { decimals: 0, divisibility: 0 })).toBe(120n);
14
+ });
15
+ it("should handle invalid inputs gracefully", () => {
16
+ expect(convertToOdinAmount("abc")).toBe(0n);
17
+ expect(convertToOdinAmount("")).toBe(0n);
18
+ expect(convertToOdinAmount(" ")).toBe(0n);
19
+ expect(convertToOdinAmount(NaN)).toBe(0n);
20
+ });
21
+ });
22
+ describe("convertToOdinAmount edge cases", () => {
23
+ it("should handle very large numbers", () => {
24
+ expect(convertToOdinAmount("12345678901234567890.123456789", { decimals: 3, divisibility: 8 })).toBe(1234567890123456789012345678900n);
25
+ });
26
+ it("should handle numbers with excessive decimal places", () => {
27
+ expect(convertToOdinAmount("1.1234567890123456789012345", {
28
+ decimals: 3,
29
+ divisibility: 8,
30
+ })).toBe(112345678901n); // truncated to 11 decimal places
31
+ });
32
+ it("should handle negative numbers", () => {
33
+ expect(convertToOdinAmount("-0.1", { decimals: 3, divisibility: 8 })).toBe(-10000000000n);
34
+ expect(convertToOdinAmount("-123.456", { decimals: 3, divisibility: 8 })).toBe(-12345600000000n);
35
+ });
36
+ });
@@ -0,0 +1,3 @@
1
+ import { BaseToken } from "../models/token";
2
+ export declare function convertToOdinAmount(numberStr: string | number, token?: Pick<BaseToken, "decimals" | "divisibility">): bigint;
3
+ //# sourceMappingURL=convert-to-odin-amount.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert-to-odin-amount.d.ts","sourceRoot":"","sources":["../../src/utils/convert-to-odin-amount.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,KAAK,GAAE,IAAI,CAAC,SAAS,EAAE,UAAU,GAAG,cAAc,CAGjD,UA6BF"}
@@ -0,0 +1,26 @@
1
+ export function convertToOdinAmount(numberStr, token = {
2
+ decimals: 3,
3
+ divisibility: 8,
4
+ }) {
5
+ const places = token.decimals + token.divisibility;
6
+ // Cast input to String and Remove any whitespace
7
+ numberStr =
8
+ typeof numberStr === "string" ? numberStr.trim() : String(numberStr);
9
+ if (isNaN(+numberStr)) {
10
+ return 0n;
11
+ }
12
+ // Split the number into integer and decimal parts
13
+ let [integerPart, decimalPart = ""] = numberStr.split(".");
14
+ // Truncate decimal part to 11 places
15
+ decimalPart = decimalPart.slice(0, places);
16
+ // Pad with zeros if decimal part is shorter than 11 digits
17
+ decimalPart = decimalPart.padEnd(places, "0");
18
+ // Combine integer and decimal parts, removing any leading zeros from integer part
19
+ // but preserving a single zero if the integer part is only zero
20
+ integerPart = integerPart.replace(/^0+(?=\d)/, "");
21
+ if (integerPart === "")
22
+ integerPart = "0";
23
+ // Combine parts and convert to BigInt
24
+ const result = BigInt(integerPart + decimalPart);
25
+ return result;
26
+ }
@@ -0,0 +1,6 @@
1
+ import { BaseToken, Token } from "../models/token";
2
+ export declare function convertToOdinAmount(numberStr: string | number, token?: Pick<BaseToken, "decimals" | "divisibility">): bigint;
3
+ type TokenValue = string | File | null | bigint;
4
+ export declare const createTokenValidators: Partial<Record<keyof Token, (value: TokenValue) => string | undefined>>;
5
+ export {};
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAEnD,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,KAAK,GAAE,IAAI,CAAC,SAAS,EAAE,UAAU,GAAG,cAAc,CAGjD,UA6BF;AAED,KAAK,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,IAAI,GAAG,MAAM,CAAC;AAEhD,eAAO,MAAM,qBAAqB,EAAE,OAAO,CACzC,MAAM,CAAC,MAAM,KAAK,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,MAAM,GAAG,SAAS,CAAC,CAS/D,CAAC"}
@@ -0,0 +1,134 @@
1
+ export function convertToOdinAmount(numberStr, token = {
2
+ decimals: 3,
3
+ divisibility: 8,
4
+ }) {
5
+ const places = token.decimals + token.divisibility;
6
+ // Cast input to String and Remove any whitespace
7
+ numberStr =
8
+ typeof numberStr === "string" ? numberStr.trim() : String(numberStr);
9
+ if (isNaN(+numberStr)) {
10
+ return 0n;
11
+ }
12
+ // Split the number into integer and decimal parts
13
+ let [integerPart, decimalPart = ""] = numberStr.split(".");
14
+ // Truncate decimal part to 11 places
15
+ decimalPart = decimalPart.slice(0, places);
16
+ // Pad with zeros if decimal part is shorter than 11 digits
17
+ decimalPart = decimalPart.padEnd(places, "0");
18
+ // Combine integer and decimal parts, removing any leading zeros from integer part
19
+ // but preserving a single zero if the integer part is only zero
20
+ integerPart = integerPart.replace(/^0+(?=\d)/, "");
21
+ if (integerPart === "")
22
+ integerPart = "0";
23
+ // Combine parts and convert to BigInt
24
+ const result = BigInt(integerPart + decimalPart);
25
+ return result;
26
+ }
27
+ export const createTokenValidators = {
28
+ name: validateName,
29
+ image: validateImage,
30
+ ticker: validateTicker,
31
+ description: validateDescription,
32
+ twitter: validateTwitter,
33
+ website: validateWebsite,
34
+ telegram: validateTelegram,
35
+ };
36
+ function validateName(name) {
37
+ if (typeof name !== "string") {
38
+ return "Name must be a string.";
39
+ }
40
+ if (!name || name.trim() === "") {
41
+ return "Name is required.";
42
+ }
43
+ if (name.length < 3 || name.length > 30) {
44
+ return "Name must be between 3 and 30 characters.";
45
+ }
46
+ }
47
+ function validateDescription(description) {
48
+ if (!description) {
49
+ return;
50
+ }
51
+ if (typeof description !== "string") {
52
+ return "Description must be a string.";
53
+ }
54
+ if (description && description.length > 100) {
55
+ return "Description must not exceed 100 characters.";
56
+ }
57
+ }
58
+ function validateTicker(ticker) {
59
+ if (typeof ticker !== "string") {
60
+ return "Ticker must be a string.";
61
+ }
62
+ if (!ticker || ticker.trim() === "") {
63
+ return "Ticker is required.";
64
+ }
65
+ if (ticker.length < 3 || ticker.length > 10) {
66
+ return "Ticker must be between 3 and 10 characters.";
67
+ }
68
+ if (!/^[A-Z0-9]+$/.test(ticker)) {
69
+ return "Ticker must be alphanumeric characters in uppercase only.";
70
+ }
71
+ if (!/(?=(.*[A-Z].*[A-Z]))/.test(ticker)) {
72
+ return "Ticker must have at least 2 alpha characters.";
73
+ }
74
+ }
75
+ function validateImage(file) {
76
+ if (!(file instanceof File)) {
77
+ return "Image must be a file.";
78
+ }
79
+ const allowedFileTypes = [
80
+ "image/png",
81
+ "image/jpeg",
82
+ "image/webp",
83
+ "image/svg",
84
+ "image/gif",
85
+ "image/avif",
86
+ ];
87
+ const maxFileSize = 200 * 1024; // 200KB
88
+ if (!file) {
89
+ return "Image file is required.";
90
+ }
91
+ if (!allowedFileTypes.includes(file.type)) {
92
+ return `Invalid file type. Allowed types are: ${allowedFileTypes.join(", ")}`;
93
+ }
94
+ if (file.size > maxFileSize) {
95
+ return `File size exceeds the maximum limit of ${maxFileSize / 1024}KB.`;
96
+ }
97
+ }
98
+ function validateTwitter(value) {
99
+ // it's optional
100
+ if (!value) {
101
+ return;
102
+ }
103
+ if (typeof value !== "string") {
104
+ return "Twitter handle must be a string.";
105
+ }
106
+ if (value &&
107
+ !/^https?:\/\/(www\.)?(twitter\.com|x\.com)\/[A-Za-z0-9_]{1,15}\/?$/.test(value)) {
108
+ return "Twitter must be a valid twitter URL.";
109
+ }
110
+ }
111
+ function validateWebsite(value) {
112
+ if (!value) {
113
+ return;
114
+ }
115
+ if (typeof value !== "string") {
116
+ return "Website must be a string.";
117
+ }
118
+ if (value &&
119
+ !/^(https?:\/\/)?([\w-]+(\.[\w-]+)+)(\/[\w-./?%&=]*)?$/.test(value)) {
120
+ return "Website must be a valid URL.";
121
+ }
122
+ }
123
+ function validateTelegram(value) {
124
+ if (!value) {
125
+ return;
126
+ }
127
+ if (typeof value !== "string") {
128
+ return "Telegram must be a string.";
129
+ }
130
+ if (value &&
131
+ !/^(https?:\/\/)?(t\.me|telegram\.me|telegram\.org)\/[A-Za-z0-9_]{5,32}\/?$/.test(value)) {
132
+ return "Telegram must be a valid telegram URL.";
133
+ }
134
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=token-field-validators.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-field-validators.test.d.ts","sourceRoot":"","sources":["../../src/utils/token-field-validators.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,31 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { createTokenValidators } from ".";
3
+ describe("token-field-validators", () => {
4
+ it("should validate ticker correctly", () => {
5
+ const validator = createTokenValidators.ticker;
6
+ if (validator) {
7
+ expect(validator("TEST")).toBeUndefined();
8
+ expect(validator("T3ST")).toBeUndefined();
9
+ expect(validator("TICKER1")).toBeUndefined();
10
+ expect(validator("tax")).toBe("Ticker must be alphanumeric characters in uppercase only.");
11
+ expect(validator("AB")).toBe("Ticker must be between 3 and 10 characters.");
12
+ expect(validator("A1")).toBe("Ticker must be between 3 and 10 characters.");
13
+ expect(validator("123456")).toBe("Ticker must have at least 2 alpha characters.");
14
+ }
15
+ });
16
+ it("should validate name correctly", () => {
17
+ const validator = createTokenValidators.name;
18
+ if (validator) {
19
+ expect(validator("My Token")).toBeUndefined();
20
+ expect(validator("")).toBe("Name is required.");
21
+ expect(validator("A".repeat(51))).toBe("Name must be between 3 and 30 characters.");
22
+ }
23
+ });
24
+ it("should validate description correctly", () => {
25
+ const validator = createTokenValidators.description;
26
+ if (validator) {
27
+ expect(validator("This is a token description.")).toBeUndefined();
28
+ expect(validator("A".repeat(201))).toBe("Description must not exceed 100 characters.");
29
+ }
30
+ });
31
+ });
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "odin-connect",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
9
  "prepublishOnly": "npm run build",
10
+ "test": "vitest",
10
11
  "demo:update": "npm run build && cd demo && npm install ../ && cd ..",
11
12
  "demo:start": "cd demo && npm run dev",
12
13
  "demo": "npm run demo:update && npm run demo:start",
@@ -26,10 +27,14 @@
26
27
  "homepage": "https://github.com/Toniq-Labs/odin-connect#readme",
27
28
  "devDependencies": {
28
29
  "@types/node": "^24.3.1",
30
+ "@vitest/ui": "^3.2.4",
31
+ "jsdom": "^27.0.0",
29
32
  "release-it": "^19.0.5",
30
- "typescript": "^5.9.2"
33
+ "typescript": "^5.9.2",
34
+ "vitest": "^3.2.4"
31
35
  },
32
36
  "dependencies": {
37
+ "@apimatic/json-bigint": "^1.2.0",
33
38
  "axios": "^1.12.0"
34
39
  },
35
40
  "release-it": {
package/readme.md CHANGED
@@ -1,10 +1,13 @@
1
1
  # OdinConnect
2
2
 
3
- ### Demo
3
+ ## Demo
4
4
 
5
5
  This repository includes a demo. See `demo/` folder
6
+
6
7
  To run it, simply do `npm run demo`
7
8
 
9
+ ## Examples
10
+
8
11
  ### Initializing new instance
9
12
 
10
13
  Instantiate the OdinConnect class with some information about your application
@@ -22,6 +25,7 @@ const user = await odinConnect.connect({
22
25
  target: "_blank",
23
26
  settings: "height=800,width=400",
24
27
  },
28
+ // flag to weather or not api keys are required
25
29
  requires_api: true,
26
30
  });
27
31
  ```
@@ -87,6 +91,56 @@ await odinConnect.removeLiquidity({
87
91
  });
88
92
  ```
89
93
 
94
+ ### Request for creating new token
95
+
96
+ Note: require_api must be set set to true
97
+
98
+ ```typescript
99
+ await odinConnect.createToken({
100
+ image: file, // instance of a file
101
+ principal: "veyov-kjgrf-hke6v-6d63i-sdwae-oldgg-huau6-ke5g3-rllp2-5jhca-uqe",
102
+ name: "Test Token",
103
+ ticker: "TEST",
104
+ description: "Test Description", // optional
105
+ website: "http://test-website.com", // optional
106
+ telegram: "", // optional
107
+ twitter: "", // optional
108
+ buy: 20_000_000n, // 20K sats
109
+ discount: "",
110
+ });
111
+ ```
112
+
113
+ ### API Methods
114
+
115
+ ```typescript
116
+ // Get tokens
117
+ const tokens = await odinConnect.getTokens({
118
+ pagination: { page: 1, limit: 50 },
119
+ sort: { field: "marketcap", direction: "desc" },
120
+ });
121
+
122
+ // Get token info
123
+ const token = await odinConnect.getToken("2jjj");
124
+
125
+ // Get user info
126
+ const user = await odinConnect.getUser(
127
+ "veyov-kjgrf-hke6v-6d63i-sdwae-oldgg-huau6-ke5g3-rllp2-5jhca-uqe"
128
+ );
129
+
130
+ // Get user balance
131
+ const balances = await odinConnect.getBalances({
132
+ principal: user.principal,
133
+ pagination: { page: 1, limit: 20 },
134
+ });
135
+
136
+ // Get user activities
137
+ const activity = await odinConnect.getUserActivity({
138
+ principal: "veyov-kjgrf-hke6v-6d63i-sdwae-oldgg-huau6-ke5g3-rllp2-5jhca-uqe",
139
+ pagination: { page: 1, limit: 10 },
140
+ });
141
+ ```
142
+
90
143
  ### Notes
91
- * The `principal` sent should match the currently logged in user in ODIN.
92
- * BTC amounts are in millisatoshis
144
+
145
+ - The `principal` sent should match the currently logged in user in ODIN.
146
+ - BTC amounts are in millisatoshis
@@ -0,0 +1,7 @@
1
+ import { defineConfig } from "vitest/config";
2
+ export default defineConfig({
3
+ test: {
4
+ globals: true,
5
+ environment: "jsdom", // or 'jsdom' for frontend
6
+ },
7
+ });
package/dist/api.d.ts DELETED
@@ -1,11 +0,0 @@
1
- export declare class OdinApi {
2
- private _apiKey;
3
- private _httpClient;
4
- readonly BASE_URL = "https://api.odin.fun/v1";
5
- constructor();
6
- getUser(id: string): Promise<{
7
- id: string;
8
- }>;
9
- set apiKey(key: string);
10
- }
11
- //# sourceMappingURL=api.d.ts.map
package/dist/api.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAEA,qBAAa,OAAO;IAElB,OAAO,CAAC,OAAO,CAAuB;IACtC,OAAO,CAAC,WAAW,CAAa;IAChC,QAAQ,CAAC,QAAQ,6BAA6B;;IAM9C,OAAO,CAAC,EAAE,EAAC,MAAM;YACiB,MAAM;;IAGxC,IAAI,MAAM,CAAC,GAAG,EAAE,MAAM,EAErB;CAEF"}
package/dist/api.js DELETED
@@ -1,14 +0,0 @@
1
- import { HttpClient } from "./http";
2
- export class OdinApi {
3
- constructor() {
4
- this._apiKey = null;
5
- this.BASE_URL = "https://api.odin.fun/v1";
6
- this._httpClient = new HttpClient();
7
- }
8
- getUser(id) {
9
- return this._httpClient.get(`${this.BASE_URL}/user/${id}`);
10
- }
11
- set apiKey(key) {
12
- this._apiKey = key;
13
- }
14
- }
package/dist/http.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { AxiosRequestConfig } from "axios";
2
- export declare class HttpClient {
3
- get<T>(url: string, options?: AxiosRequestConfig): Promise<T>;
4
- fetch<T>(url: string, options?: AxiosRequestConfig): Promise<T>;
5
- }
6
- //# sourceMappingURL=http.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAElD,qBAAa,UAAU;IAEf,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB;IAIhD,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB;CAOzD"}
package/dist/http.js DELETED
@@ -1,11 +0,0 @@
1
- import axios from "axios";
2
- export class HttpClient {
3
- async get(url, options) {
4
- return this.fetch(url, Object.assign({ method: "GET" }, options));
5
- }
6
- async fetch(url, options) {
7
- const overrides = Object.assign({}, options);
8
- const response = await axios(url, overrides);
9
- return response.data;
10
- }
11
- }