@swapkit/helpers 3.0.0-beta.11 → 3.0.0-beta.13
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/dist/api/index.cjs +2 -2
- package/dist/api/index.cjs.map +5 -5
- package/dist/api/index.js +2 -2
- package/dist/api/index.js.map +5 -5
- package/dist/{chunk-6vvdbb6s.js → chunk-7shcm26h.js} +3 -3
- package/dist/{chunk-6vvdbb6s.js.map → chunk-7shcm26h.js.map} +1 -1
- package/dist/chunk-8fdws4se.js +4 -0
- package/dist/chunk-8fdws4se.js.map +10 -0
- package/dist/{chunk-dbsrwnw2.js → chunk-b8hashv0.js} +3 -3
- package/dist/{chunk-dbsrwnw2.js.map → chunk-b8hashv0.js.map} +1 -1
- package/dist/{chunk-kdcsgh3q.js → chunk-bf0ph1bk.js} +3 -3
- package/dist/{chunk-kdcsgh3q.js.map → chunk-bf0ph1bk.js.map} +1 -1
- package/dist/{chunk-mmwvr2y8.js → chunk-bh986mnf.js} +3 -3
- package/dist/{chunk-mmwvr2y8.js.map → chunk-bh986mnf.js.map} +1 -1
- package/dist/{chunk-ahpfxkx0.js → chunk-dzypg9sp.js} +3 -3
- package/dist/{chunk-ahpfxkx0.js.map → chunk-dzypg9sp.js.map} +1 -1
- package/dist/{chunk-wmxwvv8c.js → chunk-ghdhtvtt.js} +3 -3
- package/dist/{chunk-wmxwvv8c.js.map → chunk-ghdhtvtt.js.map} +1 -1
- package/dist/{chunk-fr86y3rx.js → chunk-jhmhh7ax.js} +3 -3
- package/dist/{chunk-fr86y3rx.js.map → chunk-jhmhh7ax.js.map} +1 -1
- package/dist/{chunk-ekd1k975.js → chunk-nm1av1e6.js} +3 -3
- package/dist/{chunk-ekd1k975.js.map → chunk-nm1av1e6.js.map} +1 -1
- package/dist/{chunk-wrjh857m.js → chunk-rarep8vg.js} +3 -3
- package/dist/{chunk-wrjh857m.js.map → chunk-rarep8vg.js.map} +1 -1
- package/dist/{chunk-kr69v1tm.js → chunk-rr043vwc.js} +3 -3
- package/dist/{chunk-kr69v1tm.js.map → chunk-rr043vwc.js.map} +1 -1
- package/dist/{chunk-3wnfcm30.js → chunk-s82pm7xc.js} +2 -2
- package/dist/{chunk-3wnfcm30.js.map → chunk-s82pm7xc.js.map} +1 -1
- package/dist/chunk-sb9hymk1.js +3 -0
- package/dist/chunk-sb9hymk1.js.map +10 -0
- package/dist/{chunk-tq87xn7m.js → chunk-tn4e7zrk.js} +3 -3
- package/dist/{chunk-tq87xn7m.js.map → chunk-tn4e7zrk.js.map} +1 -1
- package/dist/{chunk-z8emzpyc.js → chunk-vadspc46.js} +3 -3
- package/dist/{chunk-z8emzpyc.js.map → chunk-vadspc46.js.map} +1 -1
- package/dist/{chunk-xyczdrny.js → chunk-vgg88vt8.js} +3 -3
- package/dist/{chunk-xyczdrny.js.map → chunk-vgg88vt8.js.map} +1 -1
- package/dist/{chunk-q8xy3739.js → chunk-wssz50sc.js} +3 -3
- package/dist/{chunk-q8xy3739.js.map → chunk-wssz50sc.js.map} +1 -1
- package/dist/contracts/index.js +1 -1
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +15 -16
- package/dist/index.js +3 -3
- package/dist/index.js.map +15 -16
- package/dist/tokens/index.js +2 -2
- package/dist/tokens/index.js.map +2 -2
- package/package.json +2 -2
- package/src/api/microgard/types.ts +0 -3
- package/src/api/midgard/endpoints.ts +1 -65
- package/src/api/midgard/types.ts +0 -49
- package/src/api/swapkitApi/endpoints.ts +1 -2
- package/src/api/swapkitApi/types.ts +1 -1
- package/src/modules/__tests__/assetValue.test.ts +42 -0
- package/src/modules/__tests__/swapKitNumber.test.ts +4 -5
- package/src/modules/assetValue.ts +12 -28
- package/src/modules/bigIntArithmetics.ts +12 -1
- package/src/modules/requestClient.ts +36 -22
- package/src/modules/swapKitError.ts +294 -105
- package/src/types/chains.ts +45 -37
- package/src/types/derivationPath.ts +1 -0
- package/src/types/quotes.ts +0 -2
- package/src/types/sdk.ts +0 -2
- package/src/types/wallet.ts +2 -0
- package/src/utils/__tests__/memo.test.ts +0 -20
- package/src/utils/asset.ts +3 -2
- package/src/utils/memo.ts +5 -42
- package/src/utils/others.ts +1 -2
- package/src/utils/wallets.ts +4 -1
package/dist/tokens/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{e as w}from"../chunk-dzypg9sp.js";import{f as C}from"../chunk-vgg88vt8.js";import{g as p}from"../chunk-wssz50sc.js";import{h as m}from"../chunk-vadspc46.js";import{i as x}from"../chunk-jhmhh7ax.js";import{j as f}from"../chunk-tn4e7zrk.js";import{k as l}from"../chunk-ghdhtvtt.js";import{l as L}from"../chunk-rarep8vg.js";import{m as e}from"../chunk-bf0ph1bk.js";import{n as V}from"../chunk-nm1av1e6.js";import{o as t}from"../chunk-bh986mnf.js";import{p as a}from"../chunk-b8hashv0.js";import{q as i}from"../chunk-rr043vwc.js";import{r as o}from"../chunk-7shcm26h.js";import{t as T}from"../chunk-s82pm7xc.js";var s={};T(s,{UniswapV3List:()=>C,UniswapV2List:()=>w,TraderjoeV2List:()=>V,ThorchainList:()=>e,SushiswapList:()=>L,PangolinList:()=>l,PancakeswapList:()=>f,OpenOceanV2List:()=>x,OneInchList:()=>m,MayaList:()=>p,JupiterList:()=>o,ChainflipList:()=>i,CaviarV1List:()=>a,CamelotV3List:()=>t});function B(O){for(let c of Object.values(s)){let r=c.tokens.find((P)=>P.identifier===O);if(r?.logoURI)return r.logoURI}return}export{s as tokenLists,B as getTokenIcon};
|
|
2
2
|
|
|
3
|
-
//# debugId=
|
|
3
|
+
//# debugId=3A5B936F2C5A6EC964756E2164756E21
|
package/dist/tokens/index.js.map
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"import * as tokenLists from \"./lists\";\n\nexport function getTokenIcon(identifier: string): string | undefined {\n // Search through all lists for a matching token\n for (const list of Object.values(tokenLists)) {\n const token = list.tokens.find((token) => token.identifier === identifier);\n if (token?.logoURI) {\n return token.logoURI;\n }\n }\n\n return undefined;\n}\n\nexport { tokenLists };\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": "g9BAEO,IAAS,JAAY,JAAC,HAAwC,JAEnE,GAAW,AAAQ,EAAO,EAAO,JAAU,HAAG,JAC5C,DAAM,HAAQ,EAAK,OAAO,KAAK,CAAC,IAAU,EAAM,aAAe,CAAU,EACzE,GAAI,GAAO,QACT,OAAO,EAAM,QAIjB",
|
|
8
|
+
"debugId": "3A5B936F2C5A6EC964756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"description": "SwapKit - Helpers",
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@swapkit/toolboxes": "1.0.0-beta.
|
|
11
|
+
"@swapkit/toolboxes": "1.0.0-beta.18"
|
|
12
12
|
},
|
|
13
13
|
"exports": {
|
|
14
14
|
".": {
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"type-check:go": "tsgo"
|
|
56
56
|
},
|
|
57
57
|
"type": "module",
|
|
58
|
-
"version": "3.0.0-beta.
|
|
58
|
+
"version": "3.0.0-beta.13"
|
|
59
59
|
}
|
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import { AssetValue, BaseDecimal, Chain, RequestClient, SwapKitNumber } from "@swapkit/helpers";
|
|
2
|
-
import type {
|
|
3
|
-
BorrowerDetails,
|
|
4
|
-
MemberDetailsMayachain,
|
|
5
|
-
MemberDetailsThorchain,
|
|
6
|
-
SaverDetails,
|
|
7
|
-
THORNameDetails,
|
|
8
|
-
} from "./types";
|
|
2
|
+
import type { MemberDetailsMayachain, MemberDetailsThorchain, THORNameDetails } from "./types";
|
|
9
3
|
|
|
10
4
|
function getMidgardBaseUrl(isThorchain = true) {
|
|
11
5
|
return isThorchain ? "https://midgard.ninerealms.com" : "https://midgard.mayachain.info";
|
|
@@ -16,18 +10,6 @@ function getNameServiceBaseUrl(isThorchain = true) {
|
|
|
16
10
|
return isThorchain ? `${baseUrl}/v2/thorname` : `${baseUrl}/v2/mayaname`;
|
|
17
11
|
}
|
|
18
12
|
|
|
19
|
-
function getBorrowerDetailRaw(baseUrl: string) {
|
|
20
|
-
return function getBorrowerDetail(address: string) {
|
|
21
|
-
return RequestClient.get<BorrowerDetails>(`${baseUrl}/v2/borrower/${address}`);
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function getSaverDetailRaw(baseUrl: string) {
|
|
26
|
-
return function getSaverDetail(address: string) {
|
|
27
|
-
return RequestClient.get<SaverDetails>(`${baseUrl}/v2/saver/${address}`);
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
13
|
function getLiquidityPositionRaw<Chain extends Chain.THORChain | Chain.Maya>(baseUrl: string) {
|
|
32
14
|
return function getLiquidityPosition(
|
|
33
15
|
address: string,
|
|
@@ -56,50 +38,10 @@ function getNamesByOwner(baseUrl: string) {
|
|
|
56
38
|
};
|
|
57
39
|
}
|
|
58
40
|
|
|
59
|
-
function getBorrowerDetail(borrowerDetailGetter: ReturnType<typeof getBorrowerDetailRaw>) {
|
|
60
|
-
return async function getBorrowerDetail(address: string) {
|
|
61
|
-
const rawBorrowerDetail = await borrowerDetailGetter(address);
|
|
62
|
-
|
|
63
|
-
return rawBorrowerDetail.pools.map((p) => ({
|
|
64
|
-
collateral_deposited: AssetValue.from({
|
|
65
|
-
asset: p.collateral_asset,
|
|
66
|
-
value: p.collateral_deposited,
|
|
67
|
-
fromBaseDecimal: BaseDecimal.THOR,
|
|
68
|
-
}),
|
|
69
|
-
collateral_withdrawn: AssetValue.from({
|
|
70
|
-
asset: p.collateral_asset,
|
|
71
|
-
value: p.collateral_withdrawn,
|
|
72
|
-
fromBaseDecimal: BaseDecimal.THOR,
|
|
73
|
-
}),
|
|
74
|
-
debt_issued_tor: SwapKitNumber.fromBigInt(BigInt(p.debt_issued_tor), BaseDecimal.THOR),
|
|
75
|
-
debt_repaid_tor: SwapKitNumber.fromBigInt(BigInt(p.debt_repaid_tor), BaseDecimal.THOR),
|
|
76
|
-
last_open_loan_timestamp: p.last_open_loan_timestamp,
|
|
77
|
-
last_repay_loan_timestamp: p.last_repay_loan_timestamp,
|
|
78
|
-
target_assets: p.target_assets.map((asset) => AssetValue.from({ asset })),
|
|
79
|
-
}));
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
41
|
function getPoolAsset({ asset, value }: { asset: string; value: string }) {
|
|
84
42
|
return AssetValue.from({ asset, value, fromBaseDecimal: BaseDecimal.THOR });
|
|
85
43
|
}
|
|
86
44
|
|
|
87
|
-
function getSaverDetail(saverDetailGetter: ReturnType<typeof getSaverDetailRaw>) {
|
|
88
|
-
return async function getSaverDetail(address: string) {
|
|
89
|
-
const rawSaverPositions = await saverDetailGetter(address);
|
|
90
|
-
|
|
91
|
-
return rawSaverPositions.pools.map((p) => ({
|
|
92
|
-
assetAdded: getPoolAsset({ asset: p.pool, value: p.assetAdded }),
|
|
93
|
-
assetDeposit: getPoolAsset({ asset: p.pool, value: p.assetDeposit }),
|
|
94
|
-
assetRedeem: getPoolAsset({ asset: p.pool, value: p.assetRedeem }),
|
|
95
|
-
assetWithdrawn: getPoolAsset({ asset: p.pool, value: p.assetWithdrawn }),
|
|
96
|
-
assetRegisteredAddress: p.assetAddress,
|
|
97
|
-
dateFirstAdded: p.dateFirstAdded,
|
|
98
|
-
dateLastAdded: p.dateLastAdded,
|
|
99
|
-
}));
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
45
|
function getLiquidityPosition<IsThorchain extends boolean = true>({
|
|
104
46
|
liquidityPositionGetter,
|
|
105
47
|
isThorchain,
|
|
@@ -134,15 +76,9 @@ function getMidgardMethodsForProtocol<T extends Chain.THORChain | Chain.Maya>(ch
|
|
|
134
76
|
const midgardBaseUrl = getMidgardBaseUrl(isThorchain);
|
|
135
77
|
const nameServiceBaseUrl = getNameServiceBaseUrl(isThorchain);
|
|
136
78
|
const liquidityPositionGetter = getLiquidityPositionRaw<T>(midgardBaseUrl);
|
|
137
|
-
const borrowerDetailGetter = getBorrowerDetailRaw(midgardBaseUrl);
|
|
138
|
-
const saverDetailGetter = getSaverDetailRaw(midgardBaseUrl);
|
|
139
79
|
|
|
140
80
|
return {
|
|
141
81
|
getLiquidityPositionRaw: liquidityPositionGetter,
|
|
142
|
-
getBorrowerDetailRaw: borrowerDetailGetter,
|
|
143
|
-
getSaverDetailRaw: saverDetailGetter,
|
|
144
|
-
getBorrowerDetail: getBorrowerDetail(borrowerDetailGetter),
|
|
145
|
-
getSaversDetail: getSaverDetail(saverDetailGetter),
|
|
146
82
|
getNameDetails: getNameDetails(nameServiceBaseUrl),
|
|
147
83
|
getNamesByAddress: getNamesByAddress(nameServiceBaseUrl),
|
|
148
84
|
getNamesByOwner: getNamesByOwner(nameServiceBaseUrl),
|
package/src/api/midgard/types.ts
CHANGED
|
@@ -1,52 +1,3 @@
|
|
|
1
|
-
export type BorrowerDetails = {
|
|
2
|
-
/** @description List details of all the loans identified with the given address */
|
|
3
|
-
pools: BorrowerPool[];
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
export type BorrowerPool = {
|
|
7
|
-
/** @description The asset that the borrower used as collateral */
|
|
8
|
-
collateral_asset: string;
|
|
9
|
-
/** @description Int64(e8), The total amount of collateral that user deposited */
|
|
10
|
-
collateral_deposited: string;
|
|
11
|
-
/** @description Int64(e8), The total amount of collateral the system paid back to the user */
|
|
12
|
-
collateral_withdrawn: string;
|
|
13
|
-
/** @description Int64(e8), The total amount of debt issued as debt for user. denominated in TOR. */
|
|
14
|
-
debt_issued_tor: string;
|
|
15
|
-
/** @description Int64(e8), The total amount of debt that the user paid back. denominated in TOR. */
|
|
16
|
-
debt_repaid_tor: string;
|
|
17
|
-
/** @description Int64, Unix timestamp for the last time borrower opened a loan */
|
|
18
|
-
last_open_loan_timestamp: string;
|
|
19
|
-
/** @description Int64, Unix timestamp for the last time borrower repayment occurred */
|
|
20
|
-
last_repay_loan_timestamp: string;
|
|
21
|
-
target_assets: string[];
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export type SaverDetails = {
|
|
25
|
-
/** @description List details of all the savers identified with the given address */
|
|
26
|
-
pools: SaverPool[];
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export type SaverPool = {
|
|
30
|
-
/** @description Int64(e8), total asset added in the saver pool by member */
|
|
31
|
-
assetAdded: string;
|
|
32
|
-
/** @description saver address used by the member */
|
|
33
|
-
assetAddress: string;
|
|
34
|
-
/** @description Int64(e8), total asset that is currently deposited by the member */
|
|
35
|
-
assetDeposit: string;
|
|
36
|
-
/** @description Int64(e8), total asset can be redeemed from the saver pool by member */
|
|
37
|
-
assetRedeem: string;
|
|
38
|
-
/** @description Int64(e8), total asset withdrawn from the saver pool by member */
|
|
39
|
-
assetWithdrawn: string;
|
|
40
|
-
/** @description Int64, Unix timestamp for the first time member deposited into the saver pool */
|
|
41
|
-
dateFirstAdded: string;
|
|
42
|
-
/** @description Int64, Unix timestamp for the last time member deposited into the saver pool */
|
|
43
|
-
dateLastAdded: string;
|
|
44
|
-
/** @description The Pool rest of the data are refering to (only those pools can show up which have a corresponding saver pool) */
|
|
45
|
-
pool: string;
|
|
46
|
-
/** @description Int64, saver liquidity units that belong the the member */
|
|
47
|
-
saverUnits: string;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
1
|
export type MemberDetailsMayachain = {
|
|
51
2
|
/** @MemberPool List details of all the liquidity providers identified with the given address */
|
|
52
3
|
pools: MemberPoolMayachain[];
|
|
@@ -47,9 +47,8 @@ export async function getSwapQuote(json: QuoteRequest) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
return parsedResponse.data;
|
|
50
|
-
} catch (
|
|
50
|
+
} catch (_error) {
|
|
51
51
|
// throw new SwapKitError("api_v2_invalid_response", error);
|
|
52
|
-
console.warn(error);
|
|
53
52
|
return response;
|
|
54
53
|
}
|
|
55
54
|
}
|
|
@@ -2,7 +2,6 @@ import {
|
|
|
2
2
|
type AssetValue,
|
|
3
3
|
Chain,
|
|
4
4
|
ChainId,
|
|
5
|
-
ErrorCode,
|
|
6
5
|
FeeTypeEnum,
|
|
7
6
|
ProviderName,
|
|
8
7
|
WarningCodeEnum,
|
|
@@ -20,6 +19,7 @@ import {
|
|
|
20
19
|
unknown,
|
|
21
20
|
type z,
|
|
22
21
|
} from "zod";
|
|
22
|
+
import { ErrorCode } from "../../types/quotes";
|
|
23
23
|
|
|
24
24
|
export enum PriorityLabel {
|
|
25
25
|
CHEAPEST = "CHEAPEST",
|
|
@@ -794,6 +794,32 @@ describe("AssetValue", () => {
|
|
|
794
794
|
type: "Native",
|
|
795
795
|
}),
|
|
796
796
|
);
|
|
797
|
+
|
|
798
|
+
const trxAsset = AssetValue.from({ chain: Chain.Tron });
|
|
799
|
+
expect(trxAsset).toEqual(
|
|
800
|
+
expect.objectContaining({
|
|
801
|
+
chain: Chain.Tron,
|
|
802
|
+
decimal: BaseDecimal.TRX,
|
|
803
|
+
isGasAsset: true,
|
|
804
|
+
isSynthetic: false,
|
|
805
|
+
symbol: "TRX",
|
|
806
|
+
ticker: "TRX",
|
|
807
|
+
type: "Native",
|
|
808
|
+
}),
|
|
809
|
+
);
|
|
810
|
+
|
|
811
|
+
const trxAssetFromString = AssetValue.from({ asset: "TRX.TRX" });
|
|
812
|
+
expect(trxAssetFromString).toEqual(
|
|
813
|
+
expect.objectContaining({
|
|
814
|
+
chain: Chain.Tron,
|
|
815
|
+
decimal: BaseDecimal.TRX,
|
|
816
|
+
isGasAsset: true,
|
|
817
|
+
isSynthetic: false,
|
|
818
|
+
symbol: "TRX",
|
|
819
|
+
ticker: "TRX",
|
|
820
|
+
type: "Native",
|
|
821
|
+
}),
|
|
822
|
+
);
|
|
797
823
|
});
|
|
798
824
|
|
|
799
825
|
test("keep SOL address casing", () => {
|
|
@@ -811,6 +837,22 @@ describe("AssetValue", () => {
|
|
|
811
837
|
}),
|
|
812
838
|
);
|
|
813
839
|
});
|
|
840
|
+
|
|
841
|
+
test("TRC20 tokens are not marked as gas assets", () => {
|
|
842
|
+
const tronUsdt = AssetValue.from({
|
|
843
|
+
asset: "TRX.USDT-TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
|
|
844
|
+
});
|
|
845
|
+
expect(tronUsdt).toEqual(
|
|
846
|
+
expect.objectContaining({
|
|
847
|
+
address: "tr7nhqjekqxgtci8q8zy4pl8otszgjlj6t",
|
|
848
|
+
chain: Chain.Tron,
|
|
849
|
+
isGasAsset: false,
|
|
850
|
+
isSynthetic: false,
|
|
851
|
+
symbol: "USDT-tr7nhqjekqxgtci8q8zy4pl8otszgjlj6t",
|
|
852
|
+
ticker: "USDT",
|
|
853
|
+
}),
|
|
854
|
+
);
|
|
855
|
+
});
|
|
814
856
|
});
|
|
815
857
|
});
|
|
816
858
|
|
|
@@ -27,11 +27,11 @@ describe("SwapKitNumber", () => {
|
|
|
27
27
|
|
|
28
28
|
const skNumber5 = new SwapKitNumber({ value: 0.1005, decimal: 3 });
|
|
29
29
|
expect(skNumber5.getValue("string")).toBe("0.101");
|
|
30
|
-
expect(skNumber5.getBaseValue("bigint")).toBe(
|
|
30
|
+
expect(skNumber5.getBaseValue("bigint")).toBe(101n);
|
|
31
31
|
|
|
32
32
|
const skNumber6 = new SwapKitNumber({ value: -0.1005, decimal: 3 });
|
|
33
33
|
expect(skNumber6.getValue("string")).toBe("-0.101");
|
|
34
|
-
expect(skNumber6.getBaseValue("bigint")).toBe(-
|
|
34
|
+
expect(skNumber6.getBaseValue("bigint")).toBe(-101n);
|
|
35
35
|
expect(skNumber6.decimal).toBe(3);
|
|
36
36
|
expect(skNumber6.getValue("number")).toBe(-0.101);
|
|
37
37
|
expect(skNumber6.decimalMultiplier).toBe(100000000n);
|
|
@@ -355,9 +355,8 @@ describe("SwapKitNumber", () => {
|
|
|
355
355
|
|
|
356
356
|
const result = skNumber1.mul(skNumber2);
|
|
357
357
|
|
|
358
|
-
// The exact result of 1.23 * 4.56 is 5.6088
|
|
359
358
|
expect(result.getValue("string")).toBe("5.609");
|
|
360
|
-
expect(result.getBaseValue("bigint")).toBe(
|
|
359
|
+
expect(result.getBaseValue("bigint")).toBe(5609n);
|
|
361
360
|
|
|
362
361
|
const skNumber3 = new SwapKitNumber({ decimal: 2, value: 1.23 });
|
|
363
362
|
const skNumber4 = new SwapKitNumber(-1.234567891);
|
|
@@ -367,7 +366,7 @@ describe("SwapKitNumber", () => {
|
|
|
367
366
|
// The exact result of 1.23 * -1.234567891 is -1,518518505
|
|
368
367
|
// If we round it to 2 decimal places, we should get 5.61
|
|
369
368
|
expect(result2.getValue("string")).toBe("-1.52");
|
|
370
|
-
expect(result2.getBaseValue("bigint")).toBe(-
|
|
369
|
+
expect(result2.getBaseValue("bigint")).toBe(-152n);
|
|
371
370
|
});
|
|
372
371
|
});
|
|
373
372
|
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { getAddress } from "ethers";
|
|
2
|
+
import { match } from "ts-pattern";
|
|
3
|
+
|
|
1
4
|
import {
|
|
2
5
|
BaseDecimal,
|
|
3
6
|
Chain,
|
|
@@ -19,7 +22,6 @@ import { warnOnce } from "../utils/others";
|
|
|
19
22
|
import { type TokenListName, loadTokenLists } from "../utils/tokens";
|
|
20
23
|
import { validateIdentifier } from "../utils/validators";
|
|
21
24
|
|
|
22
|
-
import { getAddress } from "ethers";
|
|
23
25
|
import type { NumberPrimitives } from "./bigIntArithmetics";
|
|
24
26
|
import { BigIntArithmetics, formatBigIntToSafeValue } from "./bigIntArithmetics";
|
|
25
27
|
import { SwapKitError } from "./swapKitError";
|
|
@@ -213,33 +215,15 @@ or by passing asyncTokenLookup: true to the from() function, which will make it
|
|
|
213
215
|
export function getMinAmountByChain(chain: Chain) {
|
|
214
216
|
const asset = AssetValue.from({ chain });
|
|
215
217
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
case Chain.Avalanche:
|
|
227
|
-
case Chain.Ethereum:
|
|
228
|
-
case Chain.Arbitrum:
|
|
229
|
-
case Chain.BinanceSmartChain:
|
|
230
|
-
return asset.set(0.00000001);
|
|
231
|
-
|
|
232
|
-
case Chain.THORChain:
|
|
233
|
-
case Chain.Maya:
|
|
234
|
-
return asset.set(0);
|
|
235
|
-
|
|
236
|
-
case Chain.Cosmos:
|
|
237
|
-
case Chain.Kujira:
|
|
238
|
-
return asset.set(0.000001);
|
|
239
|
-
|
|
240
|
-
default:
|
|
241
|
-
return asset.set(0.00000001);
|
|
242
|
-
}
|
|
218
|
+
return match(chain)
|
|
219
|
+
.with(Chain.Bitcoin, Chain.Litecoin, Chain.BitcoinCash, Chain.Dash, () => asset.set(0.00010001))
|
|
220
|
+
.with(Chain.Dogecoin, () => asset.set(1.00000001))
|
|
221
|
+
.with(Chain.Avalanche, Chain.Ethereum, Chain.Arbitrum, Chain.BinanceSmartChain, () =>
|
|
222
|
+
asset.set(0.00000001),
|
|
223
|
+
)
|
|
224
|
+
.with(Chain.THORChain, Chain.Maya, () => asset.set(0))
|
|
225
|
+
.with(Chain.Cosmos, Chain.Kujira, () => asset.set(0.000001))
|
|
226
|
+
.otherwise(() => asset.set(0.00000001));
|
|
243
227
|
}
|
|
244
228
|
|
|
245
229
|
async function createAssetValue(identifier: string, value: NumberPrimitives = 0) {
|
|
@@ -54,6 +54,17 @@ export function formatBigIntToSafeValue({
|
|
|
54
54
|
);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
function divideBigIntWithRounding(n: bigint, d: bigint) {
|
|
58
|
+
if (d === 0n) {
|
|
59
|
+
throw new Error("Cannot divide by zero");
|
|
60
|
+
}
|
|
61
|
+
const half = d / 2n;
|
|
62
|
+
if ((n >= 0n && d >= 0n) || (n < 0n && d < 0n)) {
|
|
63
|
+
return (n + half) / d;
|
|
64
|
+
}
|
|
65
|
+
return (n - half) / d;
|
|
66
|
+
}
|
|
67
|
+
|
|
57
68
|
export class BigIntArithmetics {
|
|
58
69
|
decimalMultiplier: bigint = 10n ** 8n;
|
|
59
70
|
bigIntValue = 0n;
|
|
@@ -153,7 +164,7 @@ export class BigIntArithmetics {
|
|
|
153
164
|
getBaseValue<T extends AllowedNumberTypes>(type: T, decimal?: number): NumberPrimitivesType[T] {
|
|
154
165
|
const divisor =
|
|
155
166
|
this.decimalMultiplier / toMultiplier(decimal || this.decimal || BaseDecimal.THOR);
|
|
156
|
-
const baseValue = this.bigIntValue
|
|
167
|
+
const baseValue = divideBigIntWithRounding(this.bigIntValue, divisor);
|
|
157
168
|
|
|
158
169
|
switch (type) {
|
|
159
170
|
case "number":
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { SKConfig } from "./swapKitConfig";
|
|
2
|
+
import { SwapKitError } from "./swapKitError";
|
|
2
3
|
|
|
3
4
|
type Options = RequestInit & {
|
|
4
5
|
/**
|
|
5
|
-
* @deprecated Use onSuccess instead
|
|
6
|
+
* @deprecated Use onSuccess instead - will be removed in next major @MarkedV4
|
|
6
7
|
*/
|
|
7
8
|
responseHandler?: (response: any) => any;
|
|
8
9
|
json?: unknown;
|
|
@@ -24,40 +25,53 @@ export const RequestClient = {
|
|
|
24
25
|
function fetchWithConfig(method: "GET" | "POST", extendOptions: Options = {}) {
|
|
25
26
|
return async <T>(url: string, options: Options = {}): Promise<T> => {
|
|
26
27
|
const { searchParams, json, body, headers: headersOptions } = { ...extendOptions, ...options };
|
|
27
|
-
const { swapKit } = SKConfig.get("apiKeys");
|
|
28
28
|
|
|
29
|
-
const isJson = json || url.endsWith(".json");
|
|
29
|
+
const isJson = !!json || url.endsWith(".json");
|
|
30
30
|
const bodyToSend = isJson ? JSON.stringify(json) : body;
|
|
31
|
-
const urlInstance = new URL(url);
|
|
32
|
-
|
|
33
|
-
if (searchParams) {
|
|
34
|
-
urlInstance.search = new URLSearchParams(searchParams).toString();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const headers = {
|
|
38
|
-
...headersOptions,
|
|
39
|
-
...(isJson ? { "Content-Type": "application/json" } : {}),
|
|
40
|
-
...(swapKit ? { "x-api-key": swapKit } : {}),
|
|
41
|
-
};
|
|
42
31
|
|
|
43
32
|
try {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
headers,
|
|
49
|
-
});
|
|
33
|
+
const requestUrl = buildUrl(url, searchParams);
|
|
34
|
+
const headers = buildHeaders(isJson, headersOptions);
|
|
35
|
+
|
|
36
|
+
const response = await fetch(requestUrl, { ...options, method, body: bodyToSend, headers });
|
|
50
37
|
|
|
51
38
|
if (!response.ok) {
|
|
52
39
|
const message = await response.text();
|
|
53
|
-
throw new
|
|
40
|
+
throw new SwapKitError("helpers_invalid_response", {
|
|
41
|
+
status: response.status,
|
|
42
|
+
statusText: response.statusText,
|
|
43
|
+
message,
|
|
44
|
+
});
|
|
54
45
|
}
|
|
55
46
|
|
|
56
47
|
const body = await response.json();
|
|
57
48
|
|
|
58
49
|
return options.onSuccess?.(body) || options.responseHandler?.(body) || body;
|
|
59
50
|
} catch (error) {
|
|
60
|
-
|
|
51
|
+
if (options.onError) {
|
|
52
|
+
return options.onError(error);
|
|
53
|
+
}
|
|
54
|
+
throw error;
|
|
61
55
|
}
|
|
62
56
|
};
|
|
63
57
|
}
|
|
58
|
+
|
|
59
|
+
function buildHeaders(isJson: boolean, headersOptions?: HeadersInit) {
|
|
60
|
+
const { swapKit } = SKConfig.get("apiKeys");
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
...headersOptions,
|
|
64
|
+
...(isJson ? { "Content-Type": "application/json" } : {}),
|
|
65
|
+
...(swapKit ? { "x-api-key": swapKit } : {}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function buildUrl(url: string, searchParams?: Record<string, string>) {
|
|
70
|
+
const urlInstance = new URL(url);
|
|
71
|
+
|
|
72
|
+
if (searchParams) {
|
|
73
|
+
urlInstance.search = new URLSearchParams(searchParams).toString();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return urlInstance.toString();
|
|
77
|
+
}
|