@swapkit/helpers 1.2.4 → 1.3.1

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/package.json CHANGED
@@ -2,17 +2,17 @@
2
2
  "author": "swapkit-oss",
3
3
  "description": "SwapKit - Helpers",
4
4
  "dependencies": {
5
- "@swapkit/tokens": "1.0.3",
6
- "picocolors": "1.0.1",
7
- "zod": "3.23.8"
5
+ "@swapkit/contracts": "1.0.4",
6
+ "@swapkit/tokens": "1.1.0",
7
+ "picocolors": "1.0.1"
8
8
  },
9
9
  "devDependencies": {
10
- "@swapkit/toolbox-cosmos": "1.0.14",
11
- "@swapkit/toolbox-evm": "1.1.8",
12
- "@swapkit/toolbox-solana": "1.0.13",
13
- "@swapkit/toolbox-radix": "1.0.13",
14
- "@swapkit/toolbox-substrate": "1.1.8",
15
- "@swapkit/toolbox-utxo": "1.0.13"
10
+ "@swapkit/toolbox-cosmos": "1.0.17",
11
+ "@swapkit/toolbox-evm": "1.1.10",
12
+ "@swapkit/toolbox-solana": "1.0.15",
13
+ "@swapkit/toolbox-radix": "1.0.15",
14
+ "@swapkit/toolbox-substrate": "1.1.10",
15
+ "@swapkit/toolbox-utxo": "1.0.15"
16
16
  },
17
17
  "files": [
18
18
  "src/",
@@ -37,5 +37,5 @@
37
37
  },
38
38
  "type": "module",
39
39
  "types": "./src/index.ts",
40
- "version": "1.2.4"
40
+ "version": "1.3.1"
41
41
  }
@@ -51,4 +51,21 @@ describe("getAssetBy", () => {
51
51
  expect(assetByIdentifier).toBeUndefined();
52
52
  expect(assetByChainAndContract).toBeUndefined();
53
53
  });
54
+
55
+ test("find asset by chain and radix resource", async () => {
56
+ const assetByChainAndContract = await findAssetBy({
57
+ chain: Chain.Radix,
58
+ contract: "resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
59
+ });
60
+ expect(assetByChainAndContract?.toUpperCase()).toBe(
61
+ "XRD.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75".toUpperCase(),
62
+ );
63
+ });
64
+
65
+ test("find asset by radix identifier", async () => {
66
+ const assetByChainAndContract = await findAssetBy({
67
+ identifier: "XRD.XRD",
68
+ });
69
+ expect(assetByChainAndContract?.toUpperCase()).toBe("XRD.XRD".toUpperCase());
70
+ });
54
71
  });
@@ -259,7 +259,9 @@ export const filterAssets = (
259
259
  });
260
260
 
261
261
  export async function findAssetBy(
262
- params: { chain: EVMChain; contract: string } | { identifier: `${Chain}.${string}` },
262
+ params:
263
+ | { chain: EVMChain | Chain.Radix; contract: string }
264
+ | { identifier: `${Chain}.${string}` },
263
265
  ) {
264
266
  const tokenPackages = await import("@swapkit/tokens");
265
267
 
@@ -2,111 +2,19 @@ import { SwapKitError } from "../modules/swapKitError";
2
2
  import { Chain } from "../types/chains";
3
3
  import { MemoType } from "../types/sdk";
4
4
 
5
- type WithChain<T extends {}> = T & { chain: Chain };
6
-
7
- type WithAffiliate<T extends {}> = T & {
8
- affiliateAddress?: string;
9
- affiliateBasisPoints?: number;
10
- };
11
-
12
- function addAffiliate(memo: string, { affiliateAddress, affiliateBasisPoints }: WithAffiliate<{}>) {
13
- const affiliatePart = affiliateAddress ? `:${affiliateAddress}:${affiliateBasisPoints || 0}` : "";
14
-
15
- return `${memo}${affiliatePart}`;
16
- }
17
-
18
- function getPoolIdentifier({
19
- chain,
20
- symbol,
21
- }: {
22
- chain: Chain;
23
- symbol: string;
24
- }) {
25
- switch (chain) {
26
- case Chain.Bitcoin:
27
- case Chain.Dogecoin:
28
- case Chain.Litecoin:
29
- return chain.slice(0, 1).toLowerCase();
30
- case Chain.BitcoinCash:
31
- return "c";
32
-
33
- default:
34
- return `${chain}.${symbol}`;
35
- }
36
- }
37
-
38
- export function getMemoForLeaveAndBond({
39
- type,
40
- address,
41
- }: {
42
- type: MemoType.BOND | MemoType.LEAVE;
43
- address: string;
44
- }) {
5
+ export function getMemoForLeaveAndBond({ type, address }: BondOrLeaveParams) {
45
6
  return `${type}:${address}`;
46
7
  }
47
8
 
48
- export function getMemoForUnbond({
49
- address,
50
- unbondAmount,
51
- }: {
52
- address: string;
53
- unbondAmount: number;
54
- }) {
9
+ export function getMemoForUnbond({ address, unbondAmount }: UnbondParams) {
55
10
  return `${MemoType.UNBOND}:${address}:${unbondAmount}`;
56
11
  }
57
12
 
58
- export function getMemoForNameRegister({
59
- name,
60
- chain,
61
- address,
62
- owner,
63
- }: {
64
- name: string;
65
- chain: string;
66
- address: string;
67
- owner?: string;
68
- }) {
69
- const baseMemo = `${MemoType.NAME_REGISTER}:${name}:${chain}:${address}`;
70
- const ownerAssignmentOrChangePart = owner ? `:${owner}` : "";
71
-
72
- return `${baseMemo}${ownerAssignmentOrChangePart}`;
73
- }
74
-
75
- export function getMemoForNamePreferredAssetRegister({
76
- name,
77
- chain,
78
- asset,
79
- payout,
80
- owner,
81
- }: {
82
- name: string;
83
- chain: Chain;
84
- asset: string;
85
- payout: string;
86
- owner: string;
87
- }) {
88
- const memo = [name, chain, payout, owner, asset].join(":");
89
-
90
- return `${MemoType.NAME_REGISTER}:${memo}`;
91
- }
92
-
93
- export function getMemoForLoan(
94
- memoType: MemoType.OPEN_LOAN | MemoType.CLOSE_LOAN,
95
- {
96
- asset,
97
- address,
98
- minAmount,
99
- ...affiliate
100
- }: WithAffiliate<{
101
- address: string;
102
- asset: string;
103
- minAmount?: string;
104
- }>,
105
- ) {
106
- const baseMemo = `${memoType}:${asset}:${address}`;
107
- const minAmountPart = minAmount ? `:${minAmount}` : "";
108
-
109
- return addAffiliate(`${baseMemo}${minAmountPart}`, affiliate);
13
+ /**
14
+ * Deposit
15
+ */
16
+ export function getMemoForRunePoolDeposit(affiliate?: WithAffiliate<{}>) {
17
+ return addAffiliate(MemoType.RUNEPOOL_DEPOSIT, affiliate);
110
18
  }
111
19
 
112
20
  export function getMemoForSaverDeposit({
@@ -128,12 +36,14 @@ export function getMemoForDeposit({
128
36
  address?: string;
129
37
  }>) {
130
38
  const poolIdentifier = getPoolIdentifier({ chain, symbol });
131
- const hasAffiliateInfo = !!affiliate.affiliateAddress;
132
- const addressPart = address ? `:${address}` : hasAffiliateInfo ? ":" : "";
39
+ const addressPart = address ? `:${address}:` : ":";
133
40
 
134
41
  return addAffiliate(`${MemoType.DEPOSIT}:${poolIdentifier}${addressPart}`, affiliate);
135
42
  }
136
43
 
44
+ /**
45
+ * Withdraw
46
+ */
137
47
  export function getMemoForSaverWithdraw({
138
48
  chain,
139
49
  symbol,
@@ -148,13 +58,7 @@ export function getMemoForWithdraw({
148
58
  ticker,
149
59
  basisPoints,
150
60
  targetAsset,
151
- }: {
152
- chain: Chain;
153
- symbol: string;
154
- ticker: string;
155
- basisPoints: number;
156
- targetAsset?: string;
157
- }) {
61
+ }: WithdrawParams) {
158
62
  const shortenedSymbol =
159
63
  chain === "ETH" && ticker !== "ETH" ? `${ticker}-${symbol.slice(-3)}` : symbol;
160
64
  const targetPart = targetAsset ? `:${targetAsset}` : "";
@@ -162,10 +66,6 @@ export function getMemoForWithdraw({
162
66
  return `${MemoType.WITHDRAW}:${chain}.${shortenedSymbol}:${basisPoints}${targetPart}`;
163
67
  }
164
68
 
165
- export function getMemoForRunePoolDeposit() {
166
- return `${MemoType.RUNEPOOL_DEPOSIT}`;
167
- }
168
-
169
69
  export function getMemoForRunePoolWithdraw({
170
70
  basisPoints,
171
71
  ...affiliate
@@ -174,62 +74,120 @@ export function getMemoForRunePoolWithdraw({
174
74
  }
175
75
 
176
76
  /**
177
- * @deprecated - Use separate functions per each memo type like getMemoForDeposit, getMemoForWithdraw, etc.
77
+ * TNS
78
+ */
79
+ export function getMemoForNameRegister({ name, chain, address, owner }: NameRegisterParams) {
80
+ const baseMemo = `${MemoType.NAME_REGISTER}:${name}:${chain}:${address}`;
81
+ const ownerAssignmentOrChangePart = owner ? `:${owner}` : "";
82
+
83
+ return `${baseMemo}${ownerAssignmentOrChangePart}`;
84
+ }
85
+
86
+ export function getMemoForNamePreferredAssetRegister({
87
+ name,
88
+ chain,
89
+ asset,
90
+ payout,
91
+ owner,
92
+ }: PreferredAssetRegisterParams) {
93
+ return `${MemoType.NAME_REGISTER}:${name}:${chain}:${payout}:${owner}:${asset}`;
94
+ }
95
+
96
+ export function getMemoForLoan(
97
+ memoType: MemoType.OPEN_LOAN | MemoType.CLOSE_LOAN,
98
+ {
99
+ asset,
100
+ address,
101
+ minAmount,
102
+ ...affiliate
103
+ }: WithAffiliate<{ address: string; asset: string; minAmount?: string }>,
104
+ ) {
105
+ const baseMemo = `${memoType}:${asset}:${address}`;
106
+ const minAmountPart = minAmount ? `:${minAmount}` : "";
107
+
108
+ return addAffiliate(`${baseMemo}${minAmountPart}`, affiliate);
109
+ }
110
+
111
+ /**
112
+ * Internal helpers
178
113
  */
179
- export type MemoOptions<T extends MemoType> = {
180
- [MemoType.BOND]: { address: string };
181
- [MemoType.LEAVE]: { address: string };
182
- [MemoType.CLOSE_LOAN]: { address: string; asset: string; minAmount?: string };
183
- [MemoType.OPEN_LOAN]: { address: string; asset: string; minAmount?: string };
184
- [MemoType.UNBOND]: { address: string; unbondAmount: number };
185
- [MemoType.DEPOSIT]: WithChain<{
186
- symbol: string;
187
- address?: string;
188
- singleSide?: boolean;
189
- }>;
190
- [MemoType.WITHDRAW]: WithChain<{
191
- ticker: string;
192
- symbol: string;
193
- basisPoints: number;
194
- targetAssetString?: string;
195
- singleSide?: boolean;
196
- }>;
197
- [MemoType.NAME_REGISTER]: { name: string; chain: string; address: string };
198
- [MemoType.RUNEPOOL_DEPOSIT]: {};
199
- [MemoType.RUNEPOOL_WITHDRAW]: {
200
- basisPoints: number;
201
- affiliateAddress?: string;
202
- affiliateBasisPoints?: number;
203
- };
204
- }[T];
114
+ function addAffiliate(
115
+ memo: string,
116
+ { affiliateAddress, affiliateBasisPoints }: WithAffiliate<{}> = {},
117
+ ) {
118
+ const affiliatedMemo = `${memo}${affiliateAddress ? `:${affiliateAddress}:${affiliateBasisPoints || 0}` : ""}`;
119
+
120
+ return affiliatedMemo.endsWith(":") ? affiliatedMemo.slice(0, -1) : affiliatedMemo;
121
+ }
122
+
123
+ function getPoolIdentifier({
124
+ chain,
125
+ symbol,
126
+ }: {
127
+ chain: Chain;
128
+ symbol: string;
129
+ }) {
130
+ switch (chain) {
131
+ case Chain.Bitcoin:
132
+ case Chain.Dogecoin:
133
+ case Chain.Litecoin:
134
+ return chain.slice(0, 1).toLowerCase();
135
+ case Chain.BitcoinCash:
136
+ return "c";
137
+
138
+ default:
139
+ return `${chain}.${symbol}`;
140
+ }
141
+ }
142
+
143
+ type WithAffiliate<T extends {}> = T & {
144
+ affiliateAddress?: string;
145
+ affiliateBasisPoints?: number;
146
+ };
147
+
148
+ type BondOrLeaveParams = { type: MemoType.BOND | MemoType.LEAVE; address: string };
149
+ type UnbondParams = { address: string; unbondAmount: number };
150
+ type NameRegisterParams = { name: string; chain: string; address: string; owner?: string };
151
+ type PreferredAssetRegisterParams = {
152
+ name: string;
153
+ chain: Chain;
154
+ asset: string;
155
+ payout: string;
156
+ owner: string;
157
+ };
158
+ type WithdrawParams = {
159
+ chain: Chain;
160
+ symbol: string;
161
+ ticker: string;
162
+ basisPoints: number;
163
+ targetAsset?: string;
164
+ };
205
165
 
206
166
  /**
207
167
  * @deprecated - Use separate functions per each memo type like getMemoForDeposit, getMemoForWithdraw, etc.
208
168
  */
209
- export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions<T>) => {
169
+ export const getMemoFor = <T extends MemoType>(memoType: T, options: NotWorth) => {
210
170
  switch (memoType) {
211
171
  case MemoType.LEAVE:
212
172
  case MemoType.BOND: {
213
- const { address } = options as MemoOptions<MemoType.BOND>;
214
- return getMemoForLeaveAndBond({ type: memoType, address });
173
+ return getMemoForLeaveAndBond({ type: memoType, address: options?.address });
215
174
  }
216
175
 
217
176
  case MemoType.UNBOND: {
218
- const { address, unbondAmount } = options as MemoOptions<MemoType.UNBOND>;
219
- return getMemoForUnbond({ address, unbondAmount });
177
+ return getMemoForUnbond({ address: options?.address, unbondAmount: options?.unbondAmount });
220
178
  }
221
179
 
222
180
  case MemoType.NAME_REGISTER: {
223
- return getMemoForNameRegister(options as MemoOptions<MemoType.NAME_REGISTER>);
181
+ return getMemoForNameRegister(options);
224
182
  }
225
183
 
226
184
  case MemoType.OPEN_LOAN:
227
185
  case MemoType.CLOSE_LOAN: {
228
- return getMemoForLoan(memoType, options as MemoOptions<MemoType.OPEN_LOAN>);
186
+ return getMemoForLoan(memoType, options);
229
187
  }
230
188
 
231
189
  case MemoType.DEPOSIT: {
232
- const { chain, symbol, address, singleSide } = options as MemoOptions<MemoType.DEPOSIT>;
190
+ const { chain, symbol, address, singleSide } = options;
233
191
 
234
192
  if (singleSide) {
235
193
  return getMemoForSaverDeposit({ chain, symbol });
@@ -246,7 +204,7 @@ export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions
246
204
  basisPoints,
247
205
  targetAssetString: targetAsset,
248
206
  singleSide,
249
- } = options as MemoOptions<MemoType.WITHDRAW>;
207
+ } = options;
250
208
 
251
209
  if (singleSide) {
252
210
  return getMemoForSaverWithdraw({ chain, symbol, basisPoints });
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "@swapkit/contracts";
1
2
  export * from "./types/index.ts";
2
3
 
3
4
  /**
@@ -576,32 +576,32 @@ describe("AssetValue", () => {
576
576
  }),
577
577
  );
578
578
 
579
- // TODO enable when BE fixes case sensitivity
580
- // const thor = AssetValue.from({ asset: "ETH.THOR" });
581
- // expect(thor).toEqual(
582
- // expect.objectContaining({
583
- // address: "0xa5f2211b9b8170f694421f2046281775e8468044",
584
- // chain: Chain.Ethereum,
585
- // decimal: 18,
586
- // isGasAsset: false,
587
- // isSynthetic: false,
588
- // symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
589
- // ticker: "THOR",
590
- // }),
591
- // );
592
-
593
- // const vthor = AssetValue.from({ asset: "ETH.vTHOR" });
594
- // expect(vthor).toEqual(
595
- // expect.objectContaining({
596
- // address: "0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
597
- // chain: Chain.Ethereum,
598
- // decimal: 18,
599
- // isGasAsset: false,
600
- // isSynthetic: false,
601
- // symbol: "vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
602
- // ticker: "vTHOR",
603
- // }),
604
- // );
579
+ const thor = AssetValue.from({ asset: "ETH.THOR" });
580
+ expect(thor).toEqual(
581
+ expect.objectContaining({
582
+ address: "0xa5f2211b9b8170f694421f2046281775e8468044",
583
+ chain: Chain.Ethereum,
584
+ decimal: 18,
585
+ isGasAsset: false,
586
+ isSynthetic: false,
587
+ symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
588
+ ticker: "THOR",
589
+ }),
590
+ );
591
+
592
+ // FIXME: just some casing? is it safe to change
593
+ // const vthor = AssetValue.from({ asset: "ETH.vTHOR" });
594
+ // expect(vthor).toEqual(
595
+ // expect.objectContaining({
596
+ // address: "0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
597
+ // chain: Chain.Ethereum,
598
+ // decimal: 18,
599
+ // isGasAsset: false,
600
+ // isSynthetic: false,
601
+ // symbol: "vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
602
+ // ticker: "vTHOR",
603
+ // }),
604
+ // );
605
605
 
606
606
  const arbAsset = AssetValue.from({ chain: Chain.Arbitrum });
607
607
  expect(arbAsset).toEqual(
@@ -1,6 +1,3 @@
1
- export * from "./abis/erc20";
2
- export * from "./abis/mayaEvmVaults";
3
- export * from "./abis/tcEthVault";
4
1
  export * from "./chains";
5
2
  export * from "./commonTypes";
6
3
  export * from "./derivationPath";