odin-connect 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.
- package/demo/README.md +69 -0
- package/demo/eslint.config.js +23 -0
- package/demo/index.html +13 -0
- package/demo/package-lock.json +3395 -0
- package/demo/package.json +30 -0
- package/demo/public/icon.svg +6 -0
- package/demo/tsconfig.app.json +27 -0
- package/demo/tsconfig.node.json +25 -0
- package/demo/vite.config.ts +7 -0
- package/dist/api.d.ts +11 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +14 -0
- package/dist/http.d.ts +6 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +11 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/models/balance.d.ts +6 -0
- package/dist/models/balance.d.ts.map +1 -0
- package/dist/models/balance.js +1 -0
- package/dist/models/token.d.ts +10 -0
- package/dist/models/token.d.ts.map +1 -0
- package/dist/models/token.js +1 -0
- package/dist/models/user.d.ts +10 -0
- package/dist/models/user.d.ts.map +1 -0
- package/dist/models/user.js +1 -0
- package/dist/services/api.d.ts +18 -0
- package/dist/services/api.d.ts.map +1 -0
- package/dist/services/api.js +37 -0
- package/dist/services/connect.d.ts +83 -0
- package/dist/services/connect.d.ts.map +1 -0
- package/dist/services/connect.js +204 -0
- package/dist/services/http.d.ts +6 -0
- package/dist/services/http.d.ts.map +1 -0
- package/dist/services/http.js +11 -0
- package/package.json +33 -0
- package/readme.md +85 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "demo",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "tsc -b && vite build",
|
|
9
|
+
"lint": "eslint .",
|
|
10
|
+
"preview": "vite preview"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"odin-connect": "file:..",
|
|
14
|
+
"react": "^19.1.1",
|
|
15
|
+
"react-dom": "^19.1.1"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@eslint/js": "^9.33.0",
|
|
19
|
+
"@types/react": "^19.1.10",
|
|
20
|
+
"@types/react-dom": "^19.1.7",
|
|
21
|
+
"@vitejs/plugin-react": "^5.0.0",
|
|
22
|
+
"eslint": "^9.33.0",
|
|
23
|
+
"eslint-plugin-react-hooks": "^5.2.0",
|
|
24
|
+
"eslint-plugin-react-refresh": "^0.4.20",
|
|
25
|
+
"globals": "^16.3.0",
|
|
26
|
+
"typescript": "~5.8.3",
|
|
27
|
+
"typescript-eslint": "^8.39.1",
|
|
28
|
+
"vite": "^7.1.2"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
|
2
|
+
<rect width="100" height="100" fill="#0b0b0b" />
|
|
3
|
+
<line x1="40" y1="20" x2="40" y2="80" stroke="#e0e0e0" stroke-width="6" stroke-linecap="round" />
|
|
4
|
+
<line x1="40" y1="40" x2="70" y2="20" stroke="#e0e0e0" stroke-width="6" stroke-linecap="round" />
|
|
5
|
+
<line x1="40" y1="60" x2="70" y2="40" stroke="#e0e0e0" stroke-width="6" stroke-linecap="round" />
|
|
6
|
+
</svg>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
|
4
|
+
"target": "ES2022",
|
|
5
|
+
"useDefineForClassFields": true,
|
|
6
|
+
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
7
|
+
"module": "ESNext",
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
|
|
10
|
+
/* Bundler mode */
|
|
11
|
+
"moduleResolution": "bundler",
|
|
12
|
+
"allowImportingTsExtensions": true,
|
|
13
|
+
"verbatimModuleSyntax": true,
|
|
14
|
+
"moduleDetection": "force",
|
|
15
|
+
"noEmit": true,
|
|
16
|
+
"jsx": "react-jsx",
|
|
17
|
+
|
|
18
|
+
/* Linting */
|
|
19
|
+
"strict": true,
|
|
20
|
+
"noUnusedLocals": true,
|
|
21
|
+
"noUnusedParameters": true,
|
|
22
|
+
"erasableSyntaxOnly": true,
|
|
23
|
+
"noFallthroughCasesInSwitch": true,
|
|
24
|
+
"noUncheckedSideEffectImports": true
|
|
25
|
+
},
|
|
26
|
+
"include": ["src"]
|
|
27
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
|
4
|
+
"target": "ES2023",
|
|
5
|
+
"lib": ["ES2023"],
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
|
|
9
|
+
/* Bundler mode */
|
|
10
|
+
"moduleResolution": "bundler",
|
|
11
|
+
"allowImportingTsExtensions": true,
|
|
12
|
+
"verbatimModuleSyntax": true,
|
|
13
|
+
"moduleDetection": "force",
|
|
14
|
+
"noEmit": true,
|
|
15
|
+
|
|
16
|
+
/* Linting */
|
|
17
|
+
"strict": true,
|
|
18
|
+
"noUnusedLocals": true,
|
|
19
|
+
"noUnusedParameters": true,
|
|
20
|
+
"erasableSyntaxOnly": true,
|
|
21
|
+
"noFallthroughCasesInSwitch": true,
|
|
22
|
+
"noUncheckedSideEffectImports": true
|
|
23
|
+
},
|
|
24
|
+
"include": ["vite.config.ts"]
|
|
25
|
+
}
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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
ADDED
|
@@ -0,0 +1 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAC5D,YAAY,EAAE,IAAI,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AACtD,YAAY,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Connect as OdinConnect } from "./services/connect";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balance.d.ts","sourceRoot":"","sources":["../../src/models/balance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,MAAM,WAAW,OAAQ,SAAQ,KAAK;IACpC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/models/token.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,KAAK;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,OAAO,CAAC;IACrB,QAAQ,EAAE,OAAO,CAAC;CACnB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../../src/models/user.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,IAAI;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,oBAAoB,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,KAAK,EAAE,MAAM,CAAA;CACd"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Balance } from "../models/balance";
|
|
2
|
+
import { Token } from "../models/token";
|
|
3
|
+
import { User } from "../models/user";
|
|
4
|
+
export type Pagination = {
|
|
5
|
+
page: number;
|
|
6
|
+
limit: number;
|
|
7
|
+
};
|
|
8
|
+
export declare class OdinApi {
|
|
9
|
+
private _apiKey;
|
|
10
|
+
private _httpClient;
|
|
11
|
+
readonly BASE_URL: string;
|
|
12
|
+
constructor(env?: "prod" | "dev");
|
|
13
|
+
getUser(id: string): Promise<User>;
|
|
14
|
+
getBalances(principal: string, pagination: Pagination): Promise<ReadonlyArray<Balance>>;
|
|
15
|
+
getTokens(pagination: Pagination): Promise<readonly Token[]>;
|
|
16
|
+
set apiKey(key: string);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/services/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAStC,MAAM,MAAM,UAAU,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,qBAAa,OAAO;IAClB,OAAO,CAAC,OAAO,CAAuB;IACtC,OAAO,CAAC,WAAW,CAAa;IAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;gBAEd,GAAG,GAAE,MAAM,GAAG,KAAc;IAKxC,OAAO,CAAC,EAAE,EAAE,MAAM;IAIZ,WAAW,CACf,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,UAAU,GACrB,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAc5B,SAAS,CAAC,UAAU,EAAE,UAAU;IActC,IAAI,MAAM,CAAC,GAAG,EAAE,MAAM,EAErB;CACF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { HttpClient } from "./http";
|
|
2
|
+
const BASE_URL_ENV = {
|
|
3
|
+
dev: "https://api.odin.fun/dev",
|
|
4
|
+
prod: "https://api.odin.fun/v1",
|
|
5
|
+
local: "https://api.odin.fun/dev",
|
|
6
|
+
};
|
|
7
|
+
export class OdinApi {
|
|
8
|
+
constructor(env = "prod") {
|
|
9
|
+
this._apiKey = null;
|
|
10
|
+
this._httpClient = new HttpClient();
|
|
11
|
+
this.BASE_URL = BASE_URL_ENV[env];
|
|
12
|
+
}
|
|
13
|
+
getUser(id) {
|
|
14
|
+
return this._httpClient.get(`${this.BASE_URL}/user/${id}`);
|
|
15
|
+
}
|
|
16
|
+
async getBalances(principal, pagination) {
|
|
17
|
+
const response = await this._httpClient.get(`${this.BASE_URL}/user/${principal}/balances`, {
|
|
18
|
+
headers: {
|
|
19
|
+
Authorization: `Bearer ${this._apiKey}`,
|
|
20
|
+
},
|
|
21
|
+
params: Object.assign({}, pagination),
|
|
22
|
+
});
|
|
23
|
+
return response.data;
|
|
24
|
+
}
|
|
25
|
+
async getTokens(pagination) {
|
|
26
|
+
const response = await this._httpClient.get(`${this.BASE_URL}/tokens`, {
|
|
27
|
+
headers: {
|
|
28
|
+
Authorization: `Bearer ${this._apiKey}`,
|
|
29
|
+
},
|
|
30
|
+
params: Object.assign({}, pagination),
|
|
31
|
+
});
|
|
32
|
+
return response.data;
|
|
33
|
+
}
|
|
34
|
+
set apiKey(key) {
|
|
35
|
+
this._apiKey = key;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { User } from "../models/user";
|
|
2
|
+
declare const ORIGINS: {
|
|
3
|
+
local: string;
|
|
4
|
+
prod: string;
|
|
5
|
+
dev: string;
|
|
6
|
+
_deployment_preview: string;
|
|
7
|
+
};
|
|
8
|
+
interface AppInitOptions {
|
|
9
|
+
name: string;
|
|
10
|
+
icon?: string;
|
|
11
|
+
env?: keyof typeof ORIGINS;
|
|
12
|
+
}
|
|
13
|
+
interface ConnectOptions {
|
|
14
|
+
open?: {
|
|
15
|
+
target: string;
|
|
16
|
+
settings: string;
|
|
17
|
+
};
|
|
18
|
+
requires_api: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface BuyOptions {
|
|
21
|
+
principal: string;
|
|
22
|
+
token: string;
|
|
23
|
+
btcAmount: bigint;
|
|
24
|
+
}
|
|
25
|
+
interface SellOptions {
|
|
26
|
+
principal: string;
|
|
27
|
+
token: string;
|
|
28
|
+
tokenAmount: bigint;
|
|
29
|
+
}
|
|
30
|
+
interface TransferOptions {
|
|
31
|
+
principal: string;
|
|
32
|
+
token: string;
|
|
33
|
+
amount: bigint;
|
|
34
|
+
destination: string;
|
|
35
|
+
}
|
|
36
|
+
interface GetBalanceOptions {
|
|
37
|
+
principal: string;
|
|
38
|
+
pagination: {
|
|
39
|
+
page: number;
|
|
40
|
+
limit: number;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
interface AddLiquidityOptions {
|
|
44
|
+
principal: string;
|
|
45
|
+
btcAmount: bigint;
|
|
46
|
+
token: string;
|
|
47
|
+
}
|
|
48
|
+
interface RemoveLiquidityOptions {
|
|
49
|
+
principal: string;
|
|
50
|
+
lpAmount: bigint;
|
|
51
|
+
token: string;
|
|
52
|
+
}
|
|
53
|
+
interface SwapOptions {
|
|
54
|
+
principal: string;
|
|
55
|
+
fromToken: string;
|
|
56
|
+
toToken: string;
|
|
57
|
+
fromAmount: bigint;
|
|
58
|
+
}
|
|
59
|
+
export declare class Connect {
|
|
60
|
+
private _appInfo;
|
|
61
|
+
private _api;
|
|
62
|
+
private _windowSettings;
|
|
63
|
+
constructor(appInfo: Partial<AppInitOptions>);
|
|
64
|
+
private createUrl;
|
|
65
|
+
private openWindow;
|
|
66
|
+
get origin(): string;
|
|
67
|
+
connect({ open, requires_api }: Partial<ConnectOptions>): Promise<User>;
|
|
68
|
+
getBalances({ principal, pagination }: GetBalanceOptions): Promise<readonly import("..").OdinBalance[]>;
|
|
69
|
+
getTokens(pagination: {
|
|
70
|
+
page: number;
|
|
71
|
+
limit: number;
|
|
72
|
+
}): Promise<readonly import("../models/token").Token[]>;
|
|
73
|
+
sell({ token, tokenAmount, principal }: SellOptions): Promise<boolean>;
|
|
74
|
+
buy({ principal, token, btcAmount }: BuyOptions): Promise<boolean>;
|
|
75
|
+
transfer({ principal, token, amount, destination }: TransferOptions): Promise<boolean>;
|
|
76
|
+
addLiquidity({ principal, btcAmount, token }: AddLiquidityOptions): Promise<boolean>;
|
|
77
|
+
removeLiquidity({ principal, lpAmount, token }: RemoveLiquidityOptions): Promise<boolean>;
|
|
78
|
+
swap({ principal, fromToken: from, toToken: to, fromAmount }: SwapOptions): Promise<boolean>;
|
|
79
|
+
hello(): void;
|
|
80
|
+
private baseAction;
|
|
81
|
+
}
|
|
82
|
+
export {};
|
|
83
|
+
//# sourceMappingURL=connect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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local: "http://localhost:5173",
|
|
4
|
+
prod: "https://odin.fun",
|
|
5
|
+
dev: "https://dev.odin.fun",
|
|
6
|
+
_deployment_preview: "https://deploy-preview-1047--dev-odin-toniq.netlify.app"
|
|
7
|
+
};
|
|
8
|
+
export class Connect {
|
|
9
|
+
constructor(appInfo) {
|
|
10
|
+
this._appInfo = null;
|
|
11
|
+
this._appInfo = Object.assign({ env: "prod", name: "app_name" }, appInfo);
|
|
12
|
+
this._api = new OdinApi(this._appInfo.env === "prod" ? "prod" : "dev");
|
|
13
|
+
this._windowSettings = {
|
|
14
|
+
target: "_blank",
|
|
15
|
+
settings: "",
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
createUrl(path) {
|
|
19
|
+
var _a;
|
|
20
|
+
const url = new URL(`${this.origin}/${path}`);
|
|
21
|
+
if ((_a = this._appInfo) === null || _a === void 0 ? void 0 : _a.name) {
|
|
22
|
+
url.searchParams.append("app_name", this._appInfo.name);
|
|
23
|
+
}
|
|
24
|
+
return url;
|
|
25
|
+
}
|
|
26
|
+
openWindow(url, onClose) {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
return window.open(url, ((_a = this._windowSettings) === null || _a === void 0 ? void 0 : _a.target) || "_blank", (_b = this._windowSettings) === null || _b === void 0 ? void 0 : _b.settings);
|
|
29
|
+
}
|
|
30
|
+
get origin() {
|
|
31
|
+
var _a;
|
|
32
|
+
return ORIGINS[((_a = this._appInfo) === null || _a === void 0 ? void 0 : _a.env) || "prod"];
|
|
33
|
+
}
|
|
34
|
+
connect({ open, requires_api }) {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
if (open) {
|
|
37
|
+
this._windowSettings = open;
|
|
38
|
+
}
|
|
39
|
+
const handleMessage = async (event) => {
|
|
40
|
+
if (event.origin === this.origin) {
|
|
41
|
+
window.removeEventListener("message", handleMessage);
|
|
42
|
+
if (event.data != "rejected") {
|
|
43
|
+
// the user accepted the connection
|
|
44
|
+
if (requires_api) {
|
|
45
|
+
// issue a api key
|
|
46
|
+
this._api.apiKey = "some-api-key";
|
|
47
|
+
}
|
|
48
|
+
const userId = event.data;
|
|
49
|
+
try {
|
|
50
|
+
// we need to fetch user data from the api to get the full user object
|
|
51
|
+
const user = await this._api.getUser(userId);
|
|
52
|
+
resolve(user);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
reject(new Error("Failed to fetch user data"));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
reject(new Error("User rejected the connection"));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
this.openWindow(this.createUrl("authorize/connect"), () => {
|
|
64
|
+
window.removeEventListener("message", handleMessage);
|
|
65
|
+
reject("User closed the window");
|
|
66
|
+
});
|
|
67
|
+
window.addEventListener("message", handleMessage);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async getBalances({ principal, pagination }) {
|
|
71
|
+
if (!principal) {
|
|
72
|
+
throw new Error("Principal is required to fetch balances");
|
|
73
|
+
}
|
|
74
|
+
return this._api.getBalances(principal, pagination);
|
|
75
|
+
}
|
|
76
|
+
async getTokens(pagination) {
|
|
77
|
+
return this._api.getTokens(pagination);
|
|
78
|
+
}
|
|
79
|
+
sell({ token, tokenAmount, principal }) {
|
|
80
|
+
return this.baseAction({
|
|
81
|
+
params: {
|
|
82
|
+
principal,
|
|
83
|
+
token,
|
|
84
|
+
amount: tokenAmount.toString(),
|
|
85
|
+
},
|
|
86
|
+
odinPath: "authorize/sell",
|
|
87
|
+
receivedMessageFromOrigin: "sold",
|
|
88
|
+
resolve: {
|
|
89
|
+
success: true,
|
|
90
|
+
failure: "Sell failed or was cancelled",
|
|
91
|
+
close: "User closed the window",
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
buy({ principal, token, btcAmount }) {
|
|
96
|
+
return this.baseAction({
|
|
97
|
+
params: {
|
|
98
|
+
principal,
|
|
99
|
+
token,
|
|
100
|
+
amount: btcAmount.toString(),
|
|
101
|
+
},
|
|
102
|
+
odinPath: "authorize/buy",
|
|
103
|
+
receivedMessageFromOrigin: "purchased",
|
|
104
|
+
resolve: {
|
|
105
|
+
success: true,
|
|
106
|
+
failure: "Purchase failed or was cancelled",
|
|
107
|
+
close: "User closed the window",
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
transfer({ principal, token, amount, destination }) {
|
|
112
|
+
return this.baseAction({
|
|
113
|
+
params: {
|
|
114
|
+
principal,
|
|
115
|
+
token,
|
|
116
|
+
amount: amount.toString(),
|
|
117
|
+
destination,
|
|
118
|
+
},
|
|
119
|
+
odinPath: "authorize/transfer",
|
|
120
|
+
receivedMessageFromOrigin: "transferred",
|
|
121
|
+
resolve: {
|
|
122
|
+
success: true,
|
|
123
|
+
failure: "Transfer failed or was cancelled",
|
|
124
|
+
close: "User closed the window",
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
addLiquidity({ principal, btcAmount, token }) {
|
|
129
|
+
return this.baseAction({
|
|
130
|
+
params: {
|
|
131
|
+
principal,
|
|
132
|
+
amount: btcAmount.toString(),
|
|
133
|
+
token,
|
|
134
|
+
},
|
|
135
|
+
odinPath: "authorize/add_liquidity",
|
|
136
|
+
receivedMessageFromOrigin: "addedLiquidity",
|
|
137
|
+
resolve: {
|
|
138
|
+
success: true,
|
|
139
|
+
failure: "Add liquidity failed or was cancelled",
|
|
140
|
+
close: "User closed the window",
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
removeLiquidity({ principal, lpAmount, token }) {
|
|
145
|
+
return this.baseAction({
|
|
146
|
+
params: {
|
|
147
|
+
principal,
|
|
148
|
+
amount: lpAmount.toString(),
|
|
149
|
+
token,
|
|
150
|
+
},
|
|
151
|
+
odinPath: "authorize/remove_liquidity",
|
|
152
|
+
receivedMessageFromOrigin: "removedLiquidity",
|
|
153
|
+
resolve: {
|
|
154
|
+
success: true,
|
|
155
|
+
failure: "Remove liquidity failed or was cancelled",
|
|
156
|
+
close: "User closed the window",
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
swap({ principal, fromToken: from, toToken: to, fromAmount }) {
|
|
161
|
+
return this.baseAction({
|
|
162
|
+
params: {
|
|
163
|
+
principal,
|
|
164
|
+
from,
|
|
165
|
+
to,
|
|
166
|
+
amount: fromAmount.toString(),
|
|
167
|
+
},
|
|
168
|
+
odinPath: "authorize/swap",
|
|
169
|
+
receivedMessageFromOrigin: "swapped",
|
|
170
|
+
resolve: {
|
|
171
|
+
success: true,
|
|
172
|
+
failure: "Swap failed or was cancelled",
|
|
173
|
+
close: "User closed the window",
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
hello() {
|
|
178
|
+
console.log("Hello from Odin Connect!");
|
|
179
|
+
}
|
|
180
|
+
baseAction({ params, odinPath, receivedMessageFromOrigin, resolve: resolveMessages, }) {
|
|
181
|
+
return new Promise((resolve, reject) => {
|
|
182
|
+
const handleMessage = async (event) => {
|
|
183
|
+
if (event.origin === this.origin) {
|
|
184
|
+
window.removeEventListener("message", handleMessage);
|
|
185
|
+
if (event.data === receivedMessageFromOrigin) {
|
|
186
|
+
resolve(resolveMessages.success);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
reject(new Error(resolveMessages.failure));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const url = this.createUrl(odinPath);
|
|
194
|
+
for (const key in params) {
|
|
195
|
+
url.searchParams.append(key, params[key]);
|
|
196
|
+
}
|
|
197
|
+
this.openWindow(url, () => {
|
|
198
|
+
window.removeEventListener("message", handleMessage);
|
|
199
|
+
reject(new Error(resolveMessages.close));
|
|
200
|
+
});
|
|
201
|
+
window.addEventListener("message", handleMessage);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/services/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"}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "odin-connect",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepublishOnly": "npm run build",
|
|
10
|
+
"demo:update": "npm run build && cd demo && npm install ../ && cd ..",
|
|
11
|
+
"demo:start": "cd demo && npm run dev",
|
|
12
|
+
"demo": "npm run demo:update && npm run demo:start"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/Toniq-Labs/odin-connect.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [],
|
|
19
|
+
"author": "",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/Toniq-Labs/odin-connect/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/Toniq-Labs/odin-connect#readme",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^24.3.1",
|
|
28
|
+
"typescript": "^5.9.2"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"axios": "^1.12.0"
|
|
32
|
+
}
|
|
33
|
+
}
|