@swapkit/core 1.0.0-rc.121 → 1.0.0-rc.122

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
@@ -1,26 +1,21 @@
1
1
  {
2
2
  "author": "swapkit-oss",
3
3
  "dependencies": {
4
- "@swapkit/helpers": "1.0.0-rc.85",
5
- "@swapkit/types": "1.0.0-rc.49"
4
+ "@swapkit/api": "1.0.0-rc.51",
5
+ "@swapkit/helpers": "1.0.0-rc.86"
6
6
  },
7
7
  "description": "SwapKit - Core",
8
8
  "devDependencies": {
9
- "@swapkit/tokens": "1.0.0-rc.46",
10
- "@swapkit/toolbox-cosmos": "1.0.0-rc.102",
11
- "@swapkit/toolbox-evm": "1.0.0-rc.91",
12
- "@swapkit/toolbox-substrate": "1.0.0-rc.20",
13
- "@swapkit/toolbox-utxo": "1.0.0-rc.93",
14
- "bun-types": "1.1.1"
9
+ "@swapkit/tokens": "1.0.0-rc.47",
10
+ "@swapkit/toolbox-cosmos": "1.0.0-rc.103",
11
+ "@swapkit/toolbox-evm": "1.0.0-rc.92",
12
+ "@swapkit/toolbox-substrate": "1.0.0-rc.21",
13
+ "@swapkit/toolbox-utxo": "1.0.0-rc.94",
14
+ "bun-types": "1.1.2"
15
15
  },
16
16
  "peerDependencies": {
17
- "@swapkit/helpers": "1.0.0-rc.85",
18
- "@swapkit/tokens": "1.0.0-rc.46",
19
- "@swapkit/toolbox-cosmos": "1.0.0-rc.102",
20
- "@swapkit/toolbox-evm": "1.0.0-rc.91",
21
- "@swapkit/toolbox-substrate": "1.0.0-rc.20",
22
- "@swapkit/toolbox-utxo": "1.0.0-rc.93",
23
- "@swapkit/types": "1.0.0-rc.49"
17
+ "@swapkit/api": "1.0.0-rc.51",
18
+ "@swapkit/helpers": "1.0.0-rc.86"
24
19
  },
25
20
  "files": [
26
21
  "src/",
@@ -37,7 +32,7 @@
37
32
  },
38
33
  "scripts": {
39
34
  "build": "bun run ./build.ts",
40
- "clean": "rm -rf .turbo dist node_modules tsconfig.tsbuildinfo",
35
+ "clean": "rm -rf .turbo dist node_modules *.tsbuildinfo",
41
36
  "lint": "biome check --apply ./src",
42
37
  "test": "echo 'bun test'",
43
38
  "test:coverage": "echo 'bun test --coverage'",
@@ -45,5 +40,5 @@
45
40
  },
46
41
  "type": "module",
47
42
  "types": "./src/index.ts",
48
- "version": "1.0.0-rc.121"
43
+ "version": "1.0.0-rc.122"
49
44
  }
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from "bun:test";
2
- import { Chain, ChainToExplorerUrl, CosmosChains, EVMChains, UTXOChains } from "@swapkit/types";
2
+ import { Chain, ChainToExplorerUrl, CosmosChains, EVMChains, UTXOChains } from "@swapkit/helpers";
3
3
  import { getExplorerAddressUrl, getExplorerTxUrl } from "../helpers/explorerUrls.ts";
4
4
 
5
5
  describe("Explorer URLs", () => {
@@ -1,4 +1,4 @@
1
- import type { QuoteRoute } from "@swapkit/helpers";
1
+ import type { QuoteRoute } from "@swapkit/api";
2
2
 
3
3
  import type { AGG_CONTRACT_ADDRESS } from "./contracts/index.ts";
4
4
  import { lowercasedGenericAbiMappings } from "./contracts/index.ts";
@@ -1,51 +1,32 @@
1
- import { AssetValue, SwapKitError } from "@swapkit/helpers";
2
- import { Chain } from "@swapkit/types";
1
+ import {
2
+ ApproveMode,
3
+ type ApproveReturnType,
4
+ AssetValue,
5
+ type AvailableProviders,
6
+ type BaseWallet,
7
+ Chain,
8
+ type ChainWallet,
9
+ type PluginName,
10
+ SwapKitError,
11
+ type SwapKitPlugin,
12
+ type SwapKitPlugins,
13
+ type SwapParams,
14
+ } from "@swapkit/helpers";
15
+ import type { CosmosWallets, ThorchainWallets } from "@swapkit/toolbox-cosmos";
16
+ import type { BaseEVMWallet, EVMWallets } from "@swapkit/toolbox-evm";
17
+ import type { SubstrateWallets } from "@swapkit/toolbox-substrate";
18
+ import type { UTXOWallets } from "@swapkit/toolbox-utxo";
3
19
  import {
4
20
  getExplorerAddressUrl as getAddressUrl,
5
21
  getExplorerTxUrl as getTxUrl,
6
- } from "../helpers/explorerUrls.ts";
7
- import type {
8
- ChainWallet,
9
- ConnectWalletParamsLocal as ConnectWalletParams,
10
- SwapWithRouteParams,
11
- } from "../types.ts";
12
-
13
- export type PluginName = "thorchain" | "chainflip" | "mayachain";
14
-
15
- export enum ApproveMode {
16
- Approve = "approve",
17
- CheckOnly = "checkOnly",
18
- }
19
-
20
- export type ApproveReturnType<T extends ApproveMode> = T extends "checkOnly"
21
- ? Promise<boolean>
22
- : Promise<string>;
23
-
24
- type SwapKitPlugins = {
25
- [K in PluginName]?: ProviderMethods;
26
- };
27
-
28
- type GenericSwapParams = {
29
- buyAsset: AssetValue;
30
- sellAsset: AssetValue;
31
- recipient: string;
32
- };
33
-
34
- export type SwapParams = (SwapWithRouteParams | GenericSwapParams) & {
35
- provider?: {
36
- name: PluginName;
37
- config: Record<string, Todo>;
38
- };
39
- };
22
+ } from "./helpers/explorerUrls.ts";
23
+ import type { ConnectWalletParamsLocal as ConnectWalletParams } from "./types.ts";
40
24
 
41
25
  export type SwapKitReturnType = SwapKitPlugins & {
42
26
  getAddress: (chain: Chain) => string;
43
- getWallet: (chain: Chain) => ChainWallet<Chain> | undefined;
44
- getWalletWithBalance: (
45
- chain: Chain,
46
- potentialScamFilter?: boolean,
47
- ) => Promise<ChainWallet<Chain>>;
48
- getBalance: (chain: Chain, potentialScamFilter?: boolean) => AssetValue[];
27
+ getWallet: (chain: Chain) => ChainWallet | undefined;
28
+ getWalletWithBalance: (chain: Chain, potentialScamFilter?: boolean) => Promise<ChainWallet>;
29
+ getBalance: (chain: Chain, refresh?: boolean) => Promise<AssetValue[]>;
49
30
  getExplorerTxUrl: typeof getTxUrl;
50
31
  getExplorerAddressUrl: typeof getAddressUrl;
51
32
  swap: (params: SwapParams) => Promise<string>;
@@ -60,17 +41,9 @@ export type SwapKitReturnType = SwapKitPlugins & {
60
41
  ) => boolean | Promise<boolean>;
61
42
  };
62
43
 
63
- export type Wallets = { [K in Chain]?: ChainWallet<K> };
64
- export type AvailableProviders<T> = T | { [K in PluginName]?: ProviderMethods };
65
- export type ProviderMethods = {
66
- swap: (swapParams: SwapParams) => Promise<string>;
67
- [key: string]: Todo;
68
- };
69
-
70
- export type SwapKitPlugin = ({ wallets, stagenet }: { wallets: Wallets; stagenet?: boolean }) => {
71
- name: PluginName;
72
- methods: ProviderMethods;
73
- };
44
+ export type Wallet = BaseWallet<
45
+ EVMWallets & CosmosWallets & ThorchainWallets & UTXOWallets & SubstrateWallets
46
+ >;
74
47
 
75
48
  export type SwapKitWallet = {
76
49
  connectMethodName: string;
@@ -94,8 +67,8 @@ export function SwapKit<
94
67
  config?: Record<string, Todo>;
95
68
  apis: Record<string, Todo>;
96
69
  rpcUrls: Record<string, Todo>;
97
- }): SwapKitReturnType & ConnectWalletMethods & AvailableProviders<ExtendedProviders> {
98
- const connectedWallets: Wallets = {};
70
+ }) {
71
+ const connectedWallets = {} as Wallet;
99
72
  const availablePlugins: AvailableProviders<ExtendedProviders> = {};
100
73
 
101
74
  for (const plugin of plugins) {
@@ -105,8 +78,9 @@ export function SwapKit<
105
78
  }
106
79
 
107
80
  const connectWalletMethods = wallets.reduce((acc, wallet) => {
108
- // @ts-expect-error
81
+ // @ts-expect-error TODO
109
82
  acc[wallet.connectMethodName] = wallet.connect({
83
+ // @ts-expect-error TODO
110
84
  addChain,
111
85
  config,
112
86
  apis,
@@ -132,8 +106,9 @@ export function SwapKit<
132
106
  return plugin;
133
107
  }
134
108
 
135
- function addChain(connectWallet: ChainWallet<Chain>) {
136
- (connectedWallets[connectWallet.chain as Chain] as ChainWallet<Chain>) = connectWallet;
109
+ function addChain(connectWallet: Wallet[Chain]) {
110
+ // @ts-expect-error TODO
111
+ connectedWallets[connectWallet.chain] = connectWallet;
137
112
  }
138
113
 
139
114
  /**
@@ -143,7 +118,7 @@ export function SwapKit<
143
118
  function approve<T extends ApproveMode>({
144
119
  assetValue,
145
120
  type = "checkOnly" as T,
146
- contractAddress,
121
+ contractAddress: spenderAddress,
147
122
  }: {
148
123
  type: T;
149
124
  assetValue: AssetValue;
@@ -157,18 +132,14 @@ export function SwapKit<
157
132
  return Promise.resolve(type === "checkOnly" ? true : "approved") as ApproveReturnType<T>;
158
133
  }
159
134
 
160
- const walletMethods =
161
- connectedWallets[chain as Chain.Ethereum | Chain.BinanceSmartChain | Chain.Avalanche];
162
-
135
+ const walletMethods = connectedWallets[chain] as BaseEVMWallet;
163
136
  const walletAction = type === "checkOnly" ? walletMethods?.isApproved : walletMethods?.approve;
164
-
165
137
  if (!walletAction) throw new SwapKitError("core_wallet_connection_not_found");
166
138
 
167
139
  const from = getAddress(chain);
168
-
169
- if (!(address && from)) throw new SwapKitError("core_approve_asset_address_or_from_not_found");
170
-
171
- const spenderAddress = contractAddress;
140
+ if (!(address && from)) {
141
+ throw new SwapKitError("core_approve_asset_address_or_from_not_found");
142
+ }
172
143
 
173
144
  return walletAction({
174
145
  amount: assetValue.getBaseValue("bigint"),
@@ -188,17 +159,22 @@ export function SwapKit<
188
159
  function getAddress(chain: Chain) {
189
160
  return getWallet(chain)?.address || "";
190
161
  }
191
- function getBalance(chain: Chain) {
162
+ async function getBalance(chain: Chain, refresh?: boolean) {
163
+ if (refresh) {
164
+ const wallet = await getWalletWithBalance(chain, true);
165
+ return wallet.balance || [];
166
+ }
167
+
192
168
  return getWallet(chain)?.balance || [];
193
169
  }
194
170
  /**
195
171
  * TODO: Figure out validation without connecting to wallet
196
172
  */
197
173
  function validateAddress({ address, chain }: { address: string; chain: Chain }) {
198
- return getWallet(chain)?.validateAddress?.(address);
174
+ getWallet(chain)?.validateAddress?.(address);
199
175
  }
200
176
 
201
- async function getWalletWithBalance(chain: Chain, potentialScamFilter?: boolean) {
177
+ async function getWalletWithBalance(chain: Chain, potentialScamFilter = true) {
202
178
  const defaultBalance = [AssetValue.fromChainOrSignature(chain)];
203
179
  const wallet = getWallet(chain);
204
180
 
@@ -206,8 +182,10 @@ export function SwapKit<
206
182
  throw new SwapKitError("core_wallet_connection_not_found");
207
183
  }
208
184
 
209
- const balance = await wallet?.getBalance(wallet.address, potentialScamFilter);
210
- wallet.balance = balance?.length ? balance : defaultBalance;
185
+ if ("getBalance" in wallet) {
186
+ const balance = await wallet.getBalance(wallet.address, potentialScamFilter);
187
+ wallet.balance = balance?.length ? balance : defaultBalance;
188
+ }
211
189
 
212
190
  return wallet;
213
191
  }
@@ -247,3 +225,5 @@ export function SwapKit<
247
225
  validateAddress,
248
226
  };
249
227
  }
228
+
229
+ export type SwapKitClient<T extends {}, K> = ReturnType<typeof SwapKit<T, K>>;
@@ -1,4 +1,4 @@
1
- import { Chain, ChainToExplorerUrl } from "@swapkit/types";
1
+ import { Chain, ChainToExplorerUrl } from "@swapkit/helpers";
2
2
 
3
3
  export function getExplorerTxUrl({ chain, txHash }: { txHash: string; chain: Chain }) {
4
4
  const baseUrl = ChainToExplorerUrl[chain];
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
- export { SwapKit } from "./client/index.ts";
2
- export * from "./client/old.ts";
3
- export * from "./client/index.ts";
4
- export * from "./types.ts";
1
+ export * from "@swapkit/api";
5
2
  export * from "@swapkit/helpers";
6
- export * from "@swapkit/types";
3
+
4
+ export { SwapKit } from "./client.ts";
5
+ export * from "./client.ts";
6
+ export * from "./types.ts";
package/src/types.ts CHANGED
@@ -1,149 +1,89 @@
1
- import type { AssetValue, QuoteRoute, SwapKitNumber } from "@swapkit/helpers";
1
+ import type { ChainWallet } from "@swapkit/helpers";
2
2
  import type {
3
- BinanceToolbox,
4
- DepositParam,
5
- GaiaToolbox,
6
- KujiraToolbox,
7
- ThorchainToolboxType,
8
- } from "@swapkit/toolbox-cosmos";
9
- import type {
10
- ARBToolbox,
11
- AVAXToolbox,
12
- BSCToolbox,
13
- ETHToolbox,
14
- MATICToolbox,
15
- OPToolbox,
16
- } from "@swapkit/toolbox-evm";
17
- import type { ChainflipToolbox, PolkadotToolbox } from "@swapkit/toolbox-substrate";
18
- import type {
19
- BCHToolbox,
20
- BTCToolbox,
21
- DASHToolbox,
22
- DOGEToolbox,
23
- LTCToolbox,
24
- } from "@swapkit/toolbox-utxo";
25
- import {
26
3
  Chain,
27
- type ConnectConfig,
28
- type CosmosChain,
29
- type EVMChain,
30
- type FeeOption,
31
- type UTXOChain,
32
- type WalletOption,
33
- } from "@swapkit/types";
34
-
35
- export type CoreTxParams = {
36
- assetValue: AssetValue;
37
- recipient: string;
38
- memo?: string;
39
- feeOptionKey?: FeeOption;
40
- feeRate?: number;
41
- data?: string;
42
- from?: string;
43
- expiration?: number;
44
- };
45
-
46
- export type AddLiquidityTxns = {
47
- runeTx?: string;
48
- assetTx?: string;
49
- };
50
-
51
- export type UpgradeParams = {
52
- runeAmount: SwapKitNumber;
53
- recipient: string;
54
- };
55
-
56
- export type OldChainWallet = {
57
- address: string;
58
- balance: AssetValue[];
59
- walletType: WalletOption;
60
- };
61
-
62
- export type OldWallet = { [key in Chain]: OldChainWallet | null };
63
-
64
- export type ChainWallet<T extends Chain> = WalletMethods[T] & {
65
- chain: Chain;
66
- address: string;
67
- balance: AssetValue[];
68
- walletType: WalletOption;
69
- };
70
-
71
- export type Wallet = {
72
- [key in Chain]?: ChainWallet<key>;
73
- };
74
-
75
- export type ThorchainWallet = ThorchainToolboxType & {
76
- transfer: (params: CoreTxParams) => Promise<string>;
77
- deposit: (params: DepositParam) => Promise<string>;
78
- };
79
-
80
- export type CosmosBasedWallet<T extends typeof BinanceToolbox | typeof GaiaToolbox> =
81
- ReturnType<T> & {
82
- transfer: (params: CoreTxParams) => Promise<string>;
83
- };
84
-
85
- export type SubstrateBasedWallet<T extends typeof PolkadotToolbox | typeof ChainflipToolbox> =
86
- Awaited<ReturnType<T>>;
87
-
88
- export type EVMWallet<
89
- T extends typeof AVAXToolbox | typeof BSCToolbox | typeof ETHToolbox | typeof OPToolbox,
90
- > = ReturnType<T> & {
91
- transfer: (params: CoreTxParams) => Promise<string>;
92
- };
93
-
94
- export type UTXOWallet<
95
- T extends typeof BCHToolbox | typeof BTCToolbox | typeof DOGEToolbox | typeof LTCToolbox,
96
- > = ReturnType<T> & {
97
- transfer: (prams: CoreTxParams) => Promise<string>;
98
- };
99
-
100
- export type WalletMethods = {
101
- [Chain.Arbitrum]: EVMWallet<typeof ARBToolbox> | null;
102
- [Chain.Avalanche]: EVMWallet<typeof AVAXToolbox> | null;
103
- [Chain.BinanceSmartChain]: EVMWallet<typeof BSCToolbox> | null;
104
- [Chain.Binance]: CosmosBasedWallet<typeof BinanceToolbox> | null;
105
- [Chain.BitcoinCash]: UTXOWallet<typeof BCHToolbox> | null;
106
- [Chain.Bitcoin]: UTXOWallet<typeof BTCToolbox> | null;
107
- [Chain.Chainflip]: SubstrateBasedWallet<typeof ChainflipToolbox> | null;
108
- [Chain.Cosmos]: CosmosBasedWallet<typeof GaiaToolbox> | null;
109
- [Chain.Dash]: UTXOWallet<typeof DASHToolbox> | null;
110
- [Chain.Dogecoin]: UTXOWallet<typeof DOGEToolbox> | null;
111
- [Chain.Ethereum]: EVMWallet<typeof ETHToolbox> | null;
112
- [Chain.Kujira]: CosmosBasedWallet<typeof KujiraToolbox> | null;
113
- [Chain.Litecoin]: UTXOWallet<typeof LTCToolbox> | null;
114
- [Chain.Maya]: ThorchainWallet | null;
115
- [Chain.Optimism]: EVMWallet<typeof OPToolbox> | null;
116
- [Chain.Polkadot]: SubstrateBasedWallet<typeof PolkadotToolbox> | null;
117
- [Chain.Polygon]: EVMWallet<typeof MATICToolbox> | null;
118
- [Chain.THORChain]: ThorchainWallet | null;
119
- };
120
-
121
- export type QuoteRouteV2 = {
122
- buyAsset: string;
123
- sellAsset: string;
124
- sellAmount: number;
125
- destinationAddress: string;
126
- sourceAddress: string;
127
- providers: string[];
128
- };
129
-
130
- export type SwapWithRouteParams = {
131
- recipient: string;
132
- route: QuoteRoute | QuoteRouteV2;
133
- feeOptionKey?: FeeOption;
134
- quoteId?: string;
135
- streamSwap?: boolean;
136
- };
137
-
138
- type ApisType = { [key in UTXOChain]?: string | Todo } & {
139
- [key in EVMChain]?: string | Todo;
140
- } & {
4
+ ConnectConfig,
5
+ CosmosChain,
6
+ EVMChain,
7
+ // type FeeOption,
8
+ UTXOChain,
9
+ } from "@swapkit/helpers";
10
+ // import type {
11
+ // BinanceToolbox,
12
+ // DepositParam,
13
+ // GaiaToolbox,
14
+ // KujiraToolbox,
15
+ // ThorchainToolboxType,
16
+ // } from "@swapkit/toolbox-cosmos";
17
+ // import type {
18
+ // ARBToolbox,
19
+ // AVAXToolbox,
20
+ // BSCToolbox,
21
+ // ETHToolbox,
22
+ // MATICToolbox,
23
+ // OPToolbox,
24
+ // } from "@swapkit/toolbox-evm";
25
+ // import type { ChainflipToolbox, PolkadotToolbox } from "@swapkit/toolbox-substrate";
26
+ // import type {
27
+ // BCHToolbox,
28
+ // BTCToolbox,
29
+ // DASHToolbox,
30
+ // DOGEToolbox,
31
+ // LTCToolbox,
32
+ // } from "@swapkit/toolbox-utxo";
33
+
34
+ // export type ThorchainWallet = ThorchainToolboxType & {
35
+ // transfer: (params: CoreTxParams) => Promise<string>;
36
+ // deposit: (params: DepositParam) => Promise<string>;
37
+ // };
38
+
39
+ // export type CosmosBasedWallet<T extends typeof BinanceToolbox | typeof GaiaToolbox> =
40
+ // ReturnType<T> & {
41
+ // transfer: (params: CoreTxParams) => Promise<string>;
42
+ // };
43
+
44
+ // export type SubstrateBasedWallet<T extends typeof PolkadotToolbox | typeof ChainflipToolbox> =
45
+ // Awaited<ReturnType<T>>;
46
+
47
+ // export type EVMWallet<
48
+ // T extends typeof AVAXToolbox | typeof BSCToolbox | typeof ETHToolbox | typeof OPToolbox,
49
+ // > = ReturnType<T> & {
50
+ // transfer: (params: CoreTxParams) => Promise<string>;
51
+ // };
52
+
53
+ // export type UTXOWallet<
54
+ // T extends typeof BCHToolbox | typeof BTCToolbox | typeof DOGEToolbox | typeof LTCToolbox,
55
+ // > = ReturnType<T> & {
56
+ // transfer: (prams: CoreTxParams) => Promise<string>;
57
+ // };
58
+
59
+ // export type WalletMethods = {
60
+ // [Chain.Arbitrum]: EVMWallet<typeof ARBToolbox> | null;
61
+ // [Chain.Avalanche]: EVMWallet<typeof AVAXToolbox> | null;
62
+ // [Chain.BinanceSmartChain]: EVMWallet<typeof BSCToolbox> | null;
63
+ // [Chain.Binance]: CosmosBasedWallet<typeof BinanceToolbox> | null;
64
+ // [Chain.BitcoinCash]: UTXOWallet<typeof BCHToolbox> | null;
65
+ // [Chain.Bitcoin]: UTXOWallet<typeof BTCToolbox> | null;
66
+ // [Chain.Chainflip]: SubstrateBasedWallet<typeof ChainflipToolbox> | null;
67
+ // [Chain.Cosmos]: CosmosBasedWallet<typeof GaiaToolbox> | null;
68
+ // [Chain.Dash]: UTXOWallet<typeof DASHToolbox> | null;
69
+ // [Chain.Dogecoin]: UTXOWallet<typeof DOGEToolbox> | null;
70
+ // [Chain.Ethereum]: EVMWallet<typeof ETHToolbox> | null;
71
+ // [Chain.Kujira]: CosmosBasedWallet<typeof KujiraToolbox> | null;
72
+ // [Chain.Litecoin]: UTXOWallet<typeof LTCToolbox> | null;
73
+ // [Chain.Maya]: ThorchainWallet | null;
74
+ // [Chain.Optimism]: EVMWallet<typeof OPToolbox> | null;
75
+ // [Chain.Polkadot]: SubstrateBasedWallet<typeof PolkadotToolbox> | null;
76
+ // [Chain.Polygon]: EVMWallet<typeof MATICToolbox> | null;
77
+ // [Chain.THORChain]: ThorchainWallet | null;
78
+ // };
79
+
80
+ type ApisType = { [key in UTXOChain]?: string } & { [key in EVMChain]?: string } & {
141
81
  [key in CosmosChain]?: string;
142
82
  };
143
83
 
144
84
  export type ConnectWalletParamsLocal = {
145
- addChain: (params: ChainWallet<Chain>) => void;
85
+ addChain: (params: ChainWallet) => void;
86
+ apis: ApisType;
146
87
  config: ConnectConfig;
147
88
  rpcUrls: { [chain in Chain]?: string };
148
- apis: ApisType;
149
89
  };