Skip to content

Commit

Permalink
feat: packages unification (#1196)
Browse files Browse the repository at this point in the history
* chore: wallet migration

* chore: split api into separate parts to not override endpoints

* chore: enter beta versioning for v4

* chore: use plugin typing

* feat: unify wallets to use same interface for declaration

* chore: remove unnecessary undefined pass
  • Loading branch information
ice-chillios authored Feb 19, 2025
1 parent 1ee20b3 commit 5aa9487
Show file tree
Hide file tree
Showing 73 changed files with 2,405 additions and 2,559 deletions.
6 changes: 0 additions & 6 deletions .changeset/giant-ants-walk.md

This file was deleted.

20 changes: 20 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"mode": "pre",
"tag": "beta",
"initialVersions": {
"@swapkit/core": "3.2.20",
"@swapkit/helpers": "2.4.3",
"@swapkit/plugins": "0.1.0",
"@swapkit/sdk": "2.2.17",
"@swapkit/toolboxes": "0.1.0",
"@swapkit/wallets": "2.0.20",
"@swapkit/wizard": "2.1.0",
"@internal/playground-nextjs": "0.0.0",
"@internal/playground-node": "0.0.0",
"@internal/playground-vite": "0.0.0",
"@internal/tools-builder": "0.0.0",
"@internal/tools-github": "0.0.0",
"@internal/tools-typescript": "0.0.0"
},
"changesets": []
}
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"bitcoincash",
"bitcoincashjs",
"bitcoinjs",
"bitget",
"blakejs",
"blockchair",
"Chunkv",
Expand All @@ -49,6 +50,7 @@
"Pancakeswapeth",
"Pathv",
"pino",
"polkadotjs",
"privkey",
"Ripemd",
"Stargatearb",
Expand Down
8 changes: 4 additions & 4 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
},
"playgrounds/nextjs": {
"name": "@internal/playground-nextjs",
"version": "0.1.0",
"version": "0.0.0",
"dependencies": {
"@hookform/resolvers": "3.9.1",
"@radix-ui/react-alert-dialog": "1.1.4",
Expand Down Expand Up @@ -264,18 +264,18 @@
},
"tools/builder": {
"name": "@internal/tools-builder",
"version": "0.0.1",
"version": "0.0.0",
"devDependencies": {
"bun-types": "1.1.42",
},
},
"tools/github": {
"name": "@internal/tools-github",
"version": "0.1.0",
"version": "0.0.0",
},
"tools/typescript": {
"name": "@internal/tools-typescript",
"version": "0.1.0",
"version": "0.0.0",
},
},
"trustedDependencies": [
Expand Down
80 changes: 47 additions & 33 deletions docs/MIGRATE_CORE_v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ $ bun install @swapkit/toolbox-cosmos @swapkit/toolbox-evm @swapkit/toolbox-radi
# Plugins
$ bun install @swapkit/plugin-chainflip @swapkit/plugin-evm @swapkit/plugin-kado @swapkit/plugin-radix @swapkit/plugin-thorchain

# Wallets
$ bun install @swapkit/wallet-bitget @swapkit/wallet-coinbase @swapkit/wallet-ctrl @swapkit/wallet-evm-extensions @swapkit/wallet-exodus @swapkit/wallet-keepkey @swapkit/wallet-keepkey-bex @swapkit/wallet-ledger @swapkit/wallet-okx @swapkit/wallet-phantom @swapkit/wallet-polkadotjs @swapkit/wallet-radix @swapkit/wallet-tailsman @swapkit/wallet-trezor @swapkit/wallet-wc

# Helpers and others
$ bun install @swapkit/helpers @swapkit/api @swapkit/contracts @swapkit/tokens
```
Expand All @@ -31,40 +34,58 @@ $ bun install @swapkit/toolboxes
# Plugins
$ bun install @swapkit/plugins

# Wallets
$ bun install @swapkit/wallets

# Helpers and others
$ bun install @swapkit/helpers
```

#### Imports
| Old | New |
| -------------------------- | ---------------------------- |
| @swapkit/api | @swapkit/helpers/api |
| @swapkit/contracts | @swapkit/helpers/contracts |
| @swapkit/tokens | @swapkit/helpers/tokens |
| @swapkit/plugin-chainflip | @swapkit/plugins/chainflip |
| @swapkit/plugin-evm | @swapkit/plugins/evm |
| @swapkit/plugin-kado | @swapkit/plugins/kado |
| @swapkit/plugin-radix | @swapkit/plugins/radix |
| @swapkit/plugin-thorchain | @swapkit/plugins/thorchain |
| @swapkit/toolbox-cosmos | @swapkit/toolboxes/cosmos |
| @swapkit/toolbox-evm | @swapkit/toolboxes/evm |
| @swapkit/toolbox-radix | @swapkit/toolboxes/radix |
| @swapkit/toolbox-solana | @swapkit/toolboxes/solana |
| @swapkit/toolbox-substrate | @swapkit/toolboxes/substrate |
| @swapkit/toolbox-utxo | @swapkit/toolboxes/utxo |

| Old | New |
| ------------------------------ | ------------------------------- |
| @swapkit/api | @swapkit/helpers/api |
| @swapkit/contracts | @swapkit/helpers/contracts |
| @swapkit/tokens | @swapkit/helpers/tokens |
| @swapkit/plugin-chainflip | @swapkit/plugins/chainflip |
| @swapkit/plugin-evm | @swapkit/plugins/evm |
| @swapkit/plugin-kado | @swapkit/plugins/kado |
| @swapkit/plugin-radix | @swapkit/plugins/radix |
| @swapkit/plugin-thorchain | @swapkit/plugins/thorchain |
| @swapkit/toolbox-cosmos | @swapkit/toolboxes/cosmos |
| @swapkit/toolbox-evm | @swapkit/toolboxes/evm |
| @swapkit/toolbox-radix | @swapkit/toolboxes/radix |
| @swapkit/toolbox-solana | @swapkit/toolboxes/solana |
| @swapkit/toolbox-substrate | @swapkit/toolboxes/substrate |
| @swapkit/toolbox-utxo | @swapkit/toolboxes/utxo |
| @swapkit/wallet-bitget | @swapkit/wallets/bitget |
| @swapkit/wallet-coinbase | @swapkit/wallets/coinbase |
| @swapkit/wallet-ctrl | @swapkit/wallets/ctrl |
| @swapkit/wallet-evm-extensions | @swapkit/wallets/evm-extensions |
| @swapkit/wallet-exodus | @swapkit/wallets/exodus |
| @swapkit/wallet-keepkey | @swapkit/wallets/keepkey |
| @swapkit/wallet-keepkey-bex | @swapkit/wallets/keepkey-bex |
| @swapkit/wallet-ledger | @swapkit/wallets/ledger |
| @swapkit/wallet-okx | @swapkit/wallets/okx |
| @swapkit/wallet-phantom | @swapkit/wallets/phantom |
| @swapkit/wallet-polkadotjs | @swapkit/wallets/polkadotjs |
| @swapkit/wallet-radix | @swapkit/wallets/radix |
| @swapkit/wallet-tailsman | @swapkit/wallets/tailsman |
| @swapkit/wallet-trezor | @swapkit/wallets/trezor |
| @swapkit/wallet-wc | @swapkit/wallets/walletconnect |

### <font color="lightGreen">Added</font>

#### @swapkit/helpers

- `SKConfig` - configuration module for handling api keys and other configuration options
- `createPlugin` - factory function to create a new plugin
- Now combines `@swapkit/api`, `@swapkit/contracts`, `@swapkit/tokens` under a single package

#### @swapkit/plugins

- `createPlugin` - factory function to create a new plugin
- Now combines `@swapkit/plugin-evm`, `@swapkit/plugin-cosmos`, `@swapkit/plugin-solana`, `@swapkit/plugin-substrate`, `@swapkit/plugin-utxo` under a single package
- Now combines `@swapkit/plugin-chainflip`, `@swapkit/plugin-evm`, `@swapkit/plugin-kado`, `@swapkit/plugin-radix`, `@swapkit/plugin-thorchain` under a single package


#### @swapkit/toolboxes

Expand All @@ -74,10 +95,17 @@ $ bun install @swapkit/helpers

#### @swapkit/api => @swapkit/helpers/api

Methods were scoped under `SwapKitApi` object and split into related api endpoints.

- `computeHashForGet` -> use `computeHash` directly
- `computeHashForPost` -> use `computeHash` directly
- `getSwapQuoteV2` -> use `getSwapQuote` directly
- `getTokenListV2` -> use `getTokenList` directly
- for thornode requests use `SwapKitApi.thornode`
- for mayachain midgard requests use `SwapKitApi.mayachainMidgard`
- for thorchain midgard requests use `SwapKitApi.thorchainMidgard`
- for microgard requests use `SwapKitApi.microgard`


#### @swapkit/core

Expand All @@ -88,17 +116,3 @@ $ bun install @swapkit/helpers

- `ensureEVMApiKeys` -> removed
- `setRequestClientConfig` -> removed - use `SKConfig` directly


#### @swapkit/toolbox-cosmos => @swapkit/toolboxes/cosmos
#### @swapkit/toolbox-evm => @swapkit/toolboxes/evm
#### @swapkit/toolbox-radix => @swapkit/toolboxes/radix
#### @swapkit/toolbox-solana => @swapkit/toolboxes/solana
#### @swapkit/toolbox-substrate => @swapkit/toolboxes/substrate
#### @swapkit/toolbox-utxo => @swapkit/toolboxes/utxo
#### @swapkit/plugin-chainflip => @swapkit/plugins/chainflip
#### @swapkit/plugin-evm => @swapkit/plugins/evm
#### @swapkit/plugin-kado => @swapkit/plugins/kado
#### @swapkit/plugin-radix => @swapkit/plugins/radix
#### @swapkit/plugin-thorchain => @swapkit/plugins/thorchain

2 changes: 1 addition & 1 deletion lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pre-commit:
commands:
lint:
glob: "*.{jsx,tsx,ts,js,json}"
run: biome check --write {staged_files} && git add {staged_files}
run: bun biome check --write {staged_files} && git add {staged_files}
type-check:
glob: "*.{ts,tsx}"
run: bun type-check
66 changes: 18 additions & 48 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Chain,
type ChainWallet,
type ConditionalAssetValueReturn,
type CryptoChain,
type EVMChain,
EVMChains,
type FeeOption,
Expand All @@ -16,9 +15,9 @@ import {
SKConfig,
type SKConfigState,
SwapKitError,
type SwapKitPluginParams,
type SwapKitWallet,
type SwapParams,
type createPlugin,
type createWallet,
} from "@swapkit/helpers";
import type { TransferParams as CosmosTransferParams } from "@swapkit/toolboxes/cosmos";
import type { TransferParams as EVMTransferParams } from "@swapkit/toolboxes/evm";
Expand All @@ -29,28 +28,16 @@ import {
getExplorerTxUrl as getTxUrl,
} from "./helpers/explorerUrls";

export type PluginsType = {
[key in string]: {
plugin: (params: SwapKitPluginParams) => any;
config?: any;
};
};

export type WalletsType = {
[key in string]: SwapKitWallet<any[]>;
};

export type SwapKitParams<P, W> = {
config?: SKConfigState;
plugins?: P;
wallets?: W;
};

export function SwapKit<Plugins extends PluginsType, Wallets extends WalletsType>({
config,
plugins,
wallets = {} as Wallets,
}: SwapKitParams<Plugins, Wallets> = {}) {
export function SwapKit<
Plugins extends ReturnType<typeof createPlugin>,
Wallets extends ReturnType<typeof createWallet>,
>({ config, plugins, wallets = {} as Wallets }: SwapKitParams<Plugins, Wallets> = {}) {
if (config) {
SKConfig.set(config);
}
Expand All @@ -59,25 +46,27 @@ export function SwapKit<Plugins extends PluginsType, Wallets extends WalletsType
const connectedWallets = {} as FullWallet;

const availablePlugins = Object.entries(plugins || {}).reduce(
(acc, [pluginName, { plugin }]) => {
(acc, [pluginName, plugin]) => {
const methods = plugin({ getWallet });

// @ts-expect-error key is generic and cannot be indexed
acc[pluginName] = methods;
return acc;
},
{} as { [key in PluginName]: ReturnType<Plugins[key]["plugin"]> },
{} as { [key in PluginName]: ReturnType<Plugins[key]> },
);

const connectWalletMethods = Object.entries(wallets).reduce(
const connectWalletMethods = Object.entries(wallets || {}).reduce(
(acc, [walletName, wallet]) => {
const connectWallet = wallet(addChain);
const connectWallet = wallet.connectWallet({ addChain });

// @ts-expect-error walletName is generic and cannot be indexed
// @ts-expect-error key is generic and cannot be indexed
acc[walletName] = connectWallet;
return acc;
},
{} as { [key in keyof Wallets]: ReturnType<Wallets[key]> },
{} as {
[key in keyof typeof wallets]: ReturnType<(typeof wallets)[key]["connectWallet"]>;
},
);

function getSwapKitPlugin<T extends PluginName>(pluginName: T) {
Expand All @@ -90,7 +79,7 @@ export function SwapKit<Plugins extends PluginsType, Wallets extends WalletsType
return plugin;
}

function getSwapKitPluginForSKProvider(pluginName: PluginNameEnum): Plugins[keyof Plugins] {
function getSwapKitPluginForSKProvider(pluginName: PluginNameEnum) {
const plugin = Object.values(availablePlugins).find((plugin) =>
plugin.supportedSwapkitProviders?.includes(pluginName),
);
Expand All @@ -102,7 +91,7 @@ export function SwapKit<Plugins extends PluginsType, Wallets extends WalletsType
return plugin;
}

function addChain<T extends CryptoChain>(connectWallet: ChainWallet<T>) {
function addChain<T extends Chain>(connectWallet: ChainWallet<T>) {
const currentWallet = getWallet(connectWallet.chain);

connectedWallets[connectWallet.chain] = { ...currentWallet, ...connectWallet };
Expand All @@ -122,9 +111,11 @@ export function SwapKit<Plugins extends PluginsType, Wallets extends WalletsType

if (plugin) {
if (type === ApproveMode.CheckOnly && "isAssetValueApproved" in plugin) {
// @ts-expect-error TODO: add optional approve for plugin
return plugin.isAssetValueApproved({ assetValue }) as ApproveReturnType<T>;
}
if (type === ApproveMode.Approve && "approveAssetValue" in plugin) {
// @ts-expect-error TODO: add optional approve for plugin
return plugin.approveAssetValue({ assetValue }) as ApproveReturnType<T>;
}

Expand Down Expand Up @@ -393,27 +384,6 @@ export function SwapKit<Plugins extends PluginsType, Wallets extends WalletsType
}
}

// TODO: REMOVE THAT:
// const swapkitConfig = config.swapkitConfig || {};
// const swapkitApiKey = swapkitConfig?.swapkitApiKey || config?.swapkitApiKey;
// const referer = swapkitConfig.useHashedApiKey ? swapkitConfig.referer : undefined;

// const api = swapkitApiKey
// ? {
// getGasRate: () => SwapKitApi.getGasRate(isDev, swapkitApiKey, referer),
// getPrice: (body: PriceRequest) => SwapKitApi.getPrice(body, isDev, swapkitApiKey, referer),
// getSwapQuote: (params: QuoteRequest) =>
// SwapKitApi.getSwapQuote(params, isDev, swapkitApiKey, referer),
// getTokenList: (provider: string) => SwapKitApi.getTokenList(provider),
// getTokenListProviders: () =>
// SwapKitApi.getTokenListProvidersV2(isDev, swapkitApiKey, referer),
// getTokenTradingPairs: (providers: PluginNameEnum[]) =>
// SwapKitApi.getTokenTradingPairs(providers, isDev, swapkitApiKey, referer),
// getTrackerDetails: (payload: TrackerParams) =>
// SwapKitApi.getTrackerDetails(payload, swapkitApiKey, referer),
// }
// : { undefined };

return {
...availablePlugins,
...connectWalletMethods,
Expand Down
Loading

0 comments on commit 5aa9487

Please sign in to comment.