@swapkit/helpers 1.0.0-rc.8 → 1.0.0-rc.80

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.
@@ -1,5 +1,4 @@
1
- import type { Chain } from '@swapkit/types';
2
- import { MemoType } from '@swapkit/types';
1
+ import { Chain, MemoType } from "@swapkit/types";
3
2
 
4
3
  export type ThornameRegisterParam = {
5
4
  name: string;
@@ -10,25 +9,14 @@ export type ThornameRegisterParam = {
10
9
  expiryBlock?: string;
11
10
  };
12
11
 
13
- const getShortenedSymbol = ({
14
- symbol,
15
- ticker,
16
- chain,
17
- }: {
18
- ticker: string;
19
- symbol: string;
20
- chain: string | Chain;
21
- }) => (chain === 'ETH' && ticker !== 'ETH' ? `${ticker}-${symbol.slice(-3)}` : symbol);
22
-
23
- type WithAddress<T = {}> = T & { address: string };
24
- type WithChain<T = {}> = T & { chain: Chain };
12
+ type WithChain<T extends {}> = T & { chain: Chain };
25
13
 
26
14
  export type MemoOptions<T extends MemoType> = {
27
- [MemoType.BOND]: WithAddress;
28
- [MemoType.LEAVE]: WithAddress;
29
- [MemoType.CLOSE_LOAN]: WithAddress<{ asset: string; minAmount?: string }>;
30
- [MemoType.OPEN_LOAN]: WithAddress<{ asset: string; minAmount?: string }>;
31
- [MemoType.UNBOND]: WithAddress<{ unbondAmount: number }>;
15
+ [MemoType.BOND]: { address: string };
16
+ [MemoType.LEAVE]: { address: string };
17
+ [MemoType.CLOSE_LOAN]: { address: string; asset: string; minAmount?: string };
18
+ [MemoType.OPEN_LOAN]: { address: string; asset: string; minAmount?: string };
19
+ [MemoType.UNBOND]: { address: string; unbondAmount: number };
32
20
  [MemoType.DEPOSIT]: WithChain<{ symbol: string; address?: string; singleSide?: boolean }>;
33
21
  [MemoType.WITHDRAW]: WithChain<{
34
22
  ticker: string;
@@ -37,7 +25,7 @@ export type MemoOptions<T extends MemoType> = {
37
25
  targetAssetString?: string;
38
26
  singleSide?: boolean;
39
27
  }>;
40
- [MemoType.THORNAME_REGISTER]: Omit<ThornameRegisterParam, 'preferredAsset' | 'expiryBlock'>;
28
+ [MemoType.THORNAME_REGISTER]: Omit<ThornameRegisterParam, "preferredAsset" | "expiryBlock">;
41
29
  }[T];
42
30
 
43
31
  export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions<T>) => {
@@ -50,29 +38,43 @@ export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions
50
38
 
51
39
  case MemoType.UNBOND: {
52
40
  const { address, unbondAmount } = options as MemoOptions<MemoType.UNBOND>;
53
- return `${memoType}:${address}:${unbondAmount * 10 ** 8}`;
41
+ return `${memoType}:${address}:${unbondAmount}`;
54
42
  }
55
43
 
56
44
  case MemoType.THORNAME_REGISTER: {
57
45
  const { name, chain, address, owner } = options as MemoOptions<MemoType.THORNAME_REGISTER>;
58
- return `${memoType}:${name}:${chain}:${address}${owner ? `:${owner}` : ''}`;
46
+ return `${memoType}:${name}:${chain}:${address}${owner ? `:${owner}` : ""}`;
59
47
  }
60
48
 
61
49
  case MemoType.DEPOSIT: {
62
50
  const { chain, symbol, address, singleSide } = options as MemoOptions<MemoType.DEPOSIT>;
63
51
 
52
+ const getPoolIdentifier = (chain: Chain, symbol: string): string => {
53
+ switch (chain) {
54
+ case Chain.Litecoin:
55
+ return "l";
56
+ case Chain.Dogecoin:
57
+ return "d";
58
+ case Chain.BitcoinCash:
59
+ return "c";
60
+ default:
61
+ return `${chain}.${symbol}`;
62
+ }
63
+ };
64
+
64
65
  return singleSide
65
- ? `${memoType}:${chain}/${symbol}::t:0`
66
- : `${memoType}:${chain}.${symbol}:${address || ''}:t:0`;
66
+ ? `${memoType}:${chain}/${symbol}`
67
+ : `${memoType}:${getPoolIdentifier(chain, symbol)}:${address || ""}`;
67
68
  }
68
69
 
69
70
  case MemoType.WITHDRAW: {
70
71
  const { chain, ticker, symbol, basisPoints, targetAssetString, singleSide } =
71
72
  options as MemoOptions<MemoType.WITHDRAW>;
72
73
 
73
- const target = !singleSide && targetAssetString ? `:${targetAssetString}` : '';
74
- const shortenedSymbol = getShortenedSymbol({ chain, symbol, ticker });
75
- const assetDivider = singleSide ? '/' : '.';
74
+ const shortenedSymbol =
75
+ chain === "ETH" && ticker !== "ETH" ? `${ticker}-${symbol.slice(-3)}` : symbol;
76
+ const target = !singleSide && targetAssetString ? `:${targetAssetString}` : "";
77
+ const assetDivider = singleSide ? "/" : ".";
76
78
 
77
79
  return `${memoType}:${chain}${assetDivider}${shortenedSymbol}:${basisPoints}${target}`;
78
80
  }
@@ -85,6 +87,6 @@ export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions
85
87
  }
86
88
 
87
89
  default:
88
- return '';
90
+ return "";
89
91
  }
90
92
  };
@@ -1,68 +1,42 @@
1
1
  // 10 rune for register, 1 rune per year
2
2
  // MINIMUM_REGISTRATION_FEE = 11
3
- export const getTHORNameCost = (year: number) => {
4
- if (year < 0) throw new Error('Invalid number of year');
3
+ export function getTHORNameCost(year: number) {
4
+ if (year < 0) throw new Error("Invalid number of year");
5
5
  return 10 + year;
6
- };
6
+ }
7
7
 
8
- export const validateTHORName = (name: string) => {
8
+ // 10 CACAO for register
9
+ // 1.0512 CACAO per year
10
+ export function getMAYANameCost(year: number) {
11
+ if (year < 0) throw new Error("Invalid number of year");
12
+ // round to max 10 decimals
13
+ return Math.round((10 + year * 1.0512) * 1e10) / 1e10;
14
+ }
15
+
16
+ export function validateTHORName(name: string) {
9
17
  if (name.length > 30) return false;
10
18
 
11
19
  const regex = /^[a-zA-Z0-9+_-]+$/g;
12
20
 
13
21
  return !!name.match(regex);
14
- };
15
-
16
- export const derivationPathToString = ([network, chainId, account, change, index]: number[]) => {
17
- const shortPath = typeof index !== 'number';
18
-
19
- return `${network}'/${chainId}'/${account}'/${change}${shortPath ? '' : `/${index}`}`;
20
- };
21
-
22
- export const getRequest = async <T>(
23
- url: string,
24
- params?: { [key in string]?: any },
25
- ): Promise<T> => {
26
- try {
27
- const queryParams = Object.entries(params || {}).reduce(
28
- (acc, [key, value]) => {
29
- if (value) {
30
- acc[key] = value;
31
- }
32
-
33
- return acc;
34
- },
35
- {} as { [key in string]: any },
36
- );
22
+ }
37
23
 
38
- const response = await fetch(
39
- `${url}${params ? `?${new URLSearchParams(queryParams).toString()}` : ''}`,
40
- { method: 'GET', mode: 'cors', credentials: 'omit', referrer: 'https://sk.thorswap.net' },
41
- );
42
-
43
- return response.json();
44
- } catch (error) {
45
- console.error(error);
46
- return {} as T;
47
- }
48
- };
24
+ export function validateMAYAName(name: string) {
25
+ if (name.length > 30) return false;
49
26
 
50
- export const postRequest = async <T>(
51
- url: string,
52
- body: string,
53
- headers?: Record<string, string>,
54
- parseAsString = false,
55
- ): Promise<T> => {
56
- try {
57
- const response = await fetch(`${url}`, {
58
- body,
59
- headers,
60
- method: 'POST',
61
- referrer: 'https://sk.thorswap.net',
62
- });
27
+ const regex = /^[a-zA-Z0-9+_-]+$/g;
63
28
 
64
- return parseAsString ? response.text() : response.json();
65
- } catch (error) {
66
- return {} as T;
67
- }
68
- };
29
+ return !!name.match(regex);
30
+ }
31
+
32
+ export function derivationPathToString([network, chainId, account, change, index]: [
33
+ number,
34
+ number,
35
+ number,
36
+ number,
37
+ number | undefined,
38
+ ]) {
39
+ const shortPath = typeof index !== "number";
40
+
41
+ return `${network}'/${chainId}'/${account}'/${change}${shortPath ? "" : `/${index}`}`;
42
+ }
@@ -1,17 +1,18 @@
1
- import { Chain } from '@swapkit/types';
1
+ import { Chain } from "@swapkit/types";
2
2
 
3
- const supportedChains = Object.values(Chain);
3
+ // Backward compatibility
4
+ const supportedChains = [...Object.values(Chain), "TERRA"];
4
5
 
5
- export const validateIdentifier = (identifier: string = '') => {
6
+ export function validateIdentifier(identifier = "") {
6
7
  const uppercasedIdentifier = identifier.toUpperCase();
7
8
 
8
- const [chain] = uppercasedIdentifier.split('.') as [Chain, string];
9
+ const [chain] = uppercasedIdentifier.split(".") as [Chain, string];
9
10
  if (supportedChains.includes(chain)) return true;
10
11
 
11
- const [synthChain] = uppercasedIdentifier.split('/') as [Chain, string];
12
+ const [synthChain] = uppercasedIdentifier.split("/") as [Chain, string];
12
13
  if (supportedChains.includes(synthChain)) return true;
13
14
 
14
15
  throw new Error(
15
16
  `Invalid identifier: ${identifier}. Expected format: <Chain>.<Ticker> or <Chain>.<Ticker>-<ContractAddress>`,
16
17
  );
17
- };
18
+ }
package/src/index.ts CHANGED
@@ -1,15 +1,17 @@
1
+ export * from "@swapkit/api";
2
+
1
3
  /**
2
4
  * Helpers
3
5
  */
4
- export * from './helpers/asset.ts';
5
- export * from './helpers/liquidity.ts';
6
- export * from './helpers/memo.ts';
7
- export * from './helpers/others.ts';
6
+ export * from "./helpers/asset.ts";
7
+ export * from "./helpers/liquidity.ts";
8
+ export * from "./helpers/memo.ts";
9
+ export * from "./helpers/others.ts";
8
10
 
9
11
  /**
10
12
  * Modules
11
13
  */
12
- export * from './modules/assetValue.ts';
13
- export * from './modules/bigIntArithmetics.ts';
14
- export * from './modules/swapKitError.ts';
15
- export * from './modules/swapKitNumber.ts';
14
+ export * from "./modules/assetValue.ts";
15
+ export * from "./modules/bigIntArithmetics.ts";
16
+ export * from "./modules/swapKitError.ts";
17
+ export * from "./modules/swapKitNumber.ts";