odin-connect 1.0.2 → 1.1.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.
- package/.github/workflows/pr.yml +24 -0
- package/demo/package-lock.json +101 -2
- package/demo/package.json +2 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/models/achievement.d.ts +20 -0
- package/dist/models/achievement.d.ts.map +1 -0
- package/dist/models/achievement.js +1 -0
- package/dist/models/token.d.ts +5 -0
- package/dist/models/token.d.ts.map +1 -1
- package/dist/services/api.d.ts +26 -0
- package/dist/services/api.d.ts.map +1 -1
- package/dist/services/api.js +93 -0
- package/dist/services/api.test.d.ts +2 -0
- package/dist/services/api.test.d.ts.map +1 -0
- package/dist/services/api.test.js +70 -0
- package/dist/services/connect.d.ts +36 -4
- package/dist/services/connect.d.ts.map +1 -1
- package/dist/services/connect.js +82 -21
- package/dist/services/connect.test.d.ts +2 -0
- package/dist/services/connect.test.d.ts.map +1 -0
- package/dist/services/connect.test.js +323 -0
- package/dist/services/http.d.ts +6 -2
- package/dist/services/http.d.ts.map +1 -1
- package/dist/services/http.js +30 -0
- package/dist/services/http.test.d.ts +2 -0
- package/dist/services/http.test.d.ts.map +1 -0
- package/dist/services/http.test.js +37 -0
- package/dist/utils/conver-to-odin-amount.test.d.ts +2 -0
- package/dist/utils/conver-to-odin-amount.test.d.ts.map +1 -0
- package/dist/utils/conver-to-odin-amount.test.js +36 -0
- package/dist/utils/index.d.ts +4 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +108 -0
- package/dist/utils/token-field-validators.test.d.ts +2 -0
- package/dist/utils/token-field-validators.test.d.ts.map +1 -0
- package/dist/utils/token-field-validators.test.js +31 -0
- package/package.json +9 -2
- package/readme.md +26 -3
- package/vitest.config.ts +7 -0
|
@@ -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
|
+
});
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import { BaseToken } from "../models/token";
|
|
1
|
+
import { BaseToken, Token } from "../models/token";
|
|
2
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 {};
|
|
3
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;
|
|
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"}
|
package/dist/utils/index.js
CHANGED
|
@@ -24,3 +24,111 @@ export function convertToOdinAmount(numberStr, token = {
|
|
|
24
24
|
const result = BigInt(integerPart + decimalPart);
|
|
25
25
|
return result;
|
|
26
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 @@
|
|
|
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,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "odin-connect",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
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",
|
|
13
|
+
"demo:build": "npm run demo:update && cd demo && npm run build",
|
|
12
14
|
"demo": "npm run demo:update && npm run demo:start",
|
|
13
15
|
"release": "release-it"
|
|
14
16
|
},
|
|
@@ -26,10 +28,15 @@
|
|
|
26
28
|
"homepage": "https://github.com/Toniq-Labs/odin-connect#readme",
|
|
27
29
|
"devDependencies": {
|
|
28
30
|
"@types/node": "^24.3.1",
|
|
31
|
+
"@vitest/ui": "^3.2.4",
|
|
32
|
+
"jsdom": "^27.0.0",
|
|
29
33
|
"release-it": "^19.0.5",
|
|
30
|
-
"typescript": "^5.9.2"
|
|
34
|
+
"typescript": "^5.9.2",
|
|
35
|
+
"vitest": "^3.2.4"
|
|
31
36
|
},
|
|
32
37
|
"dependencies": {
|
|
38
|
+
"@apimatic/json-bigint": "^1.2.0",
|
|
39
|
+
"@dfinity/identity": "^3.3.0",
|
|
33
40
|
"axios": "^1.12.0"
|
|
34
41
|
},
|
|
35
42
|
"release-it": {
|
package/readme.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# OdinConnect
|
|
2
2
|
|
|
3
|
-
|
|
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,25 @@ 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
|
+
|
|
90
113
|
### API Methods
|
|
91
114
|
|
|
92
115
|
```typescript
|
|
@@ -104,13 +127,13 @@ const user = await odinConnect.getUser(
|
|
|
104
127
|
"veyov-kjgrf-hke6v-6d63i-sdwae-oldgg-huau6-ke5g3-rllp2-5jhca-uqe"
|
|
105
128
|
);
|
|
106
129
|
|
|
107
|
-
// Get balance
|
|
130
|
+
// Get user balance
|
|
108
131
|
const balances = await odinConnect.getBalances({
|
|
109
132
|
principal: user.principal,
|
|
110
133
|
pagination: { page: 1, limit: 20 },
|
|
111
134
|
});
|
|
112
135
|
|
|
113
|
-
// Get activities
|
|
136
|
+
// Get user activities
|
|
114
137
|
const activity = await odinConnect.getUserActivity({
|
|
115
138
|
principal: "veyov-kjgrf-hke6v-6d63i-sdwae-oldgg-huau6-ke5g3-rllp2-5jhca-uqe",
|
|
116
139
|
pagination: { page: 1, limit: 10 },
|