Skip to content

Commit

Permalink
chore: sdk improvements (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
towanTG committed Jun 22, 2024
1 parent a18194b commit ab5c3b3
Show file tree
Hide file tree
Showing 41 changed files with 35,654 additions and 37,611 deletions.
13 changes: 13 additions & 0 deletions .changeset/strong-zebras-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@swapkit/toolbox-substrate": patch
"@swapkit/toolbox-cosmos": patch
"@swapkit/helpers": patch
"@swapkit/tokens": patch
"@swapkit/toolbox-utxo": patch
"@swapkit/toolbox-evm": patch
"@swapkit/core": patch
"@swapkit/api": patch
"@swapkit/sdk": patch
---

TokenList changes, exposing of tx estimation methods, fixes types and other improvements
8 changes: 5 additions & 3 deletions packages/swapkit/api/src/thorswapApiV2/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ export async function getSwapQuoteV2(searchParams: QuoteRequestV2, isDev = false
}
}

export function getTokenListProvidersV2() {
return RequestClient.get<TokenListProvidersResponse>(`${baseUrl}/providers`);
export async function getTokenListProvidersV2() {
const response = await RequestClient.get<TokenListProvidersResponse>(`${baseUrl}/providers`);
return response;
}

export function getTokenListV2(provider: ProviderName) {
return RequestClient.get<TokensResponseV2>(`${baseUrl}/tokens?provider=${provider}`);
const response = RequestClient.get<TokensResponseV2>(`${baseUrl}/tokens?provider=${provider}`);
return response;
}
113 changes: 112 additions & 1 deletion packages/swapkit/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
type ConnectConfig,
type EVMChain,
EVMChains,
type ProviderName as PluginNameEnum,
type FeeOption,
ProviderName as PluginNameEnum,
SwapKitError,
type SwapParams,
type UTXOChain,
type WalletChain,
isGasAsset,
} from "@swapkit/helpers";
import {
type BaseEVMWallet,
Expand All @@ -20,10 +23,12 @@ import {

import {
type TransferParams as CosmosTransferParams,
estimateTransactionFee as cosmosTransactionFee,
cosmosValidateAddress,
} from "@swapkit/toolbox-cosmos";
import { substrateValidateAddress } from "@swapkit/toolbox-substrate";
import { type UTXOTransferParams, utxoValidateAddress } from "@swapkit/toolbox-utxo";
import { lowercasedContractAbiMapping } from "./aggregator/contracts/index.ts";
import {
getExplorerAddressUrl as getAddressUrl,
getExplorerTxUrl as getTxUrl,
Expand Down Expand Up @@ -304,6 +309,111 @@ export function SwapKit<
delete connectedWallets[chain];
}

// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO clean this up
async function estimateTransactionFee<T extends PluginName>({
type,
feeOptionKey,
params,
}: (
| { type: "swap"; params: SwapParams<T> & { assetValue: AssetValue } }
| {
type: "transfer";
params: UTXOTransferParams | EVMTransferParams | CosmosTransferParams;
}
| {
type: "approve";
params: {
assetValue: AssetValue;
contractAddress: string | PluginName;
feeOptionKey?: FeeOption;
};
}
) & {
feeOptionKey: FeeOption;
}): Promise<AssetValue | undefined> {
const { assetValue } = params;
const chain = params.assetValue.chain as WalletChain;
if (!connectedWallets[chain]) throw new SwapKitError("core_wallet_connection_not_found");
switch (chain) {
case Chain.Arbitrum:
case Chain.Avalanche:
case Chain.Ethereum:
case Chain.BinanceSmartChain:
case Chain.Polygon: {
const wallet = connectedWallets[chain as Exclude<EVMChain, Chain.Optimism>];
if (type === "transfer") {
const txObject = await wallet.createTransferTx(params);
return wallet.estimateTransactionFee(txObject, feeOptionKey);
}
if (type === "approve" && !isGasAsset(assetValue)) {
wallet.estimateTransactionFee(
await wallet.createApprovalTx({
assetAddress: assetValue.address as string,
spenderAddress: params.contractAddress as string,
amount: assetValue.getBaseValue("bigint"),
from: wallet.address,
}),
feeOptionKey,
);
}
if (type === "swap") {
const plugin = params.route.providers[0] as PluginNameEnum;
if (plugin === PluginNameEnum.CHAINFLIP) {
const txObject = await wallet.createTransferTx({
from: wallet.address,
recipient: wallet.address,
assetValue,
});
return wallet.estimateTransactionFee(txObject, feeOptionKey);
}
const {
route: { evmTransactionDetails },
} = params;
if (
!(
evmTransactionDetails &&
lowercasedContractAbiMapping[evmTransactionDetails.contractAddress]
)
)
return undefined;
wallet.estimateCall({
contractAddress: evmTransactionDetails.contractAddress,
// biome-ignore lint/style/noNonNullAssertion: TS cant infer the type
abi: lowercasedContractAbiMapping[evmTransactionDetails.contractAddress]!,
funcName: evmTransactionDetails.contractMethod,
funcParams: evmTransactionDetails.contractParams,
});
}
return AssetValue.fromChainOrSignature(chain, 0);
}
case Chain.Bitcoin:
case Chain.BitcoinCash:
case Chain.Dogecoin:
case Chain.Dash:
case Chain.Litecoin: {
const wallet = connectedWallets[chain as UTXOChain];
return wallet.estimateTransactionFee({
...params,
feeOptionKey,
from: wallet.address,
recipient: wallet.address,
});
}
case Chain.THORChain:
case Chain.Maya:
case Chain.Kujira:
case Chain.Cosmos: {
return cosmosTransactionFee(params);
}
case Chain.Polkadot: {
const wallet = connectedWallets[chain as Chain.Polkadot];
return wallet.estimateTransactionFee({ ...params, recipient: wallet.address });
}
default:
return undefined;
}
}

return {
...availablePlugins,
...connectWalletMethods,
Expand All @@ -317,6 +427,7 @@ export function SwapKit<
getAllWallets,
getWalletWithBalance,
isAssetValueApproved,
estimateTransactionFee,
swap,
transfer,
validateAddress,
Expand Down
21 changes: 11 additions & 10 deletions packages/swapkit/helpers/src/modules/requestClient.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import type { KyInstance, Options } from "ky";
import ky from "ky";

let kyClient: typeof ky;
let kyClientConfig: Options & { apiKey?: string } = {};

export const defaultRequestHeaders =
typeof window !== "undefined"
? ({} as Record<string, string>)
: { referrer: "https://sk.thorswap.net", referer: "https://sk.thorswap.net" };

export function setRequestClientConfig({ apiKey, ...config }: Options & { apiKey?: string }) {
kyClient = ky.create({
...config,
headers: { ...defaultRequestHeaders, ...config.headers, "x-api-key": apiKey },
});
kyClientConfig = { ...config, apiKey };
}

function getKyClient() {
if (kyClient) return kyClient;
kyClient = ky.create({ headers: defaultRequestHeaders });
return kyClient;
const { apiKey, ...config } = kyClientConfig;
return ky.create({
...config,
headers: { ...defaultRequestHeaders, ...config.headers, "x-api-key": apiKey },
});
}

const getTypedBaseRequestClient = (ky: KyInstance) => ({
get: <T>(url: string | URL | Request, options?: Options) => ky.get(url, options).json<T>(),
post: <T>(url: string | URL | Request, options?: Options) => ky.post(url, options).json<T>(),
get: async <T>(url: string | URL | Request, options?: Options) =>
(await ky.get(url, options)).json<T>(),
post: async <T>(url: string | URL | Request, options?: Options) =>
(await ky.post(url, options)).json<T>(),
});

export const RequestClient = {
Expand Down
32 changes: 18 additions & 14 deletions packages/swapkit/helpers/src/types/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import type {
ChainflipList,
CoinGeckoList,
MayaList,
PancakeswapETHList,
OneInchList,
PancakeswapList,
PangolinList,
StargateARBList,
SushiswapList,
ThorchainList,
TraderjoeList,
UniswapList,
WoofiList,
TraderjoeV1List,
TraderjoeV2List,
UniswapV2List,
UniswapV3List,
} from "@swapkit/tokens";

export type TokenTax = { buy: number; sell: number };

export type TokenNames =
| (typeof ThorchainList)["tokens"][number]["identifier"]
| (typeof CoinGeckoList)["tokens"][number]["identifier"]
| (typeof ChainflipList)["tokens"][number]["identifier"]
| (typeof MayaList)["tokens"][number]["identifier"]
| (typeof PancakeswapETHList)["tokens"][number]["identifier"]
| (typeof OneInchList)["tokens"][number]["identifier"]
| (typeof PancakeswapList)["tokens"][number]["identifier"]
| (typeof PangolinList)["tokens"][number]["identifier"]
| (typeof StargateARBList)["tokens"][number]["identifier"]
| (typeof SushiswapList)["tokens"][number]["identifier"]
| (typeof TraderjoeList)["tokens"][number]["identifier"]
| (typeof WoofiList)["tokens"][number]["identifier"]
| (typeof UniswapList)["tokens"][number]["identifier"]
| (typeof ChainflipList)["tokens"][number]["identifier"];
| (typeof ThorchainList)["tokens"][number]["identifier"]
| (typeof TraderjoeV1List)["tokens"][number]["identifier"]
| (typeof TraderjoeV2List)["tokens"][number]["identifier"]
| (typeof UniswapV2List)["tokens"][number]["identifier"]
| (typeof UniswapV3List)["tokens"][number]["identifier"];
// | (typeof CoinGeckoList)["tokens"][number]["identifier"]
// | (typeof PancakeswapETHList)["tokens"][number]["identifier"]
// | (typeof StargateARBList)["tokens"][number]["identifier"]
// | (typeof TraderjoeList)["tokens"][number]["identifier"]
// | (typeof WoofiList)["tokens"][number]["identifier"]
// | (typeof UniswapList)["tokens"][number]["identifier"];
4 changes: 2 additions & 2 deletions packages/swapkit/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type Params = Omit<
>;

export const createSwapKit = <
P extends Partial<typeof defaultPlugins>,
W extends Partial<typeof defaultWallets>,
P extends Partial<typeof defaultPlugins> = typeof defaultPlugins,
W extends Partial<typeof defaultWallets> = typeof defaultWallets,
>({
config,
plugins,
Expand Down
15 changes: 8 additions & 7 deletions packages/swapkit/tokens/fetchTokenLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ function parseIdentifier(identifier: string) {
return identifier;
}

const providers = await SwapKitApi.getTokenListProvidersV2();
const providers = (await SwapKitApi.getTokenListProvidersV2()).filter(
(provider) => provider.provider !== "THORCHAIN_STREAMING",
);

console.info(
`🚀 Fetching token lists from ${providers.length} providers:\n${providers
Expand All @@ -22,19 +24,18 @@ console.info(
);

for (const { provider } of providers) {
if (provider.includes("whitelist")) continue;

try {
const tokenList = await SwapKitApi.getTokenListV2(provider);

if (!tokenList) continue;

console.info(`✅ ${provider} token list fetched (${tokenList.tokens.length} tokens)`);

const tokens = tokenList.tokens
.map(({ address, chain, identifier, decimals, logoURI }) => ({
address,
.map(({ address, chain, chainId, identifier, ticker, decimals, logoURI }) => ({
chain: parseChain(chain),
address,
chainId,
ticker,
identifier: parseIdentifier(identifier),
decimals,
logoURI,
Expand All @@ -44,7 +45,7 @@ for (const { provider } of providers) {
const tokenListWithTokens = { ...tokenList, tokens };

await Bun.write(
`src/tokenLists/${provider}.ts`,
`src/tokenLists/${provider.toLowerCase()}.ts`,
`export const list = ${JSON.stringify(tokenListWithTokens)} as const;`,
);
} catch (_error) {
Expand Down
25 changes: 12 additions & 13 deletions packages/swapkit/tokens/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
export { list as OneInchList } from "./tokenLists/1inch.ts";
export { list as CoinGeckoList } from "./tokenLists/Coingecko.ts";
export { list as MayaList } from "./tokenLists/Maya.ts";
export { list as PancakeswapList } from "./tokenLists/Pancakeswap.ts";
export { list as PancakeswapETHList } from "./tokenLists/Pancakeswapeth.ts";
export { list as PangolinList } from "./tokenLists/Pangolin.ts";
export { list as StargateARBList } from "./tokenLists/Stargatearb.ts";
export { list as SushiswapList } from "./tokenLists/Sushiswap.ts";
export { list as ThorchainList } from "./tokenLists/Thorchain.ts";
export { list as TraderjoeList } from "./tokenLists/Traderjoe.ts";
export { list as UniswapList } from "./tokenLists/Uniswap.ts";
export { list as WoofiList } from "./tokenLists/Woofi.ts";
export { list as ChainflipList } from "./tokenLists/Chainflip.ts";
export { list as ChainflipList } from "./tokenLists/chainflip.ts";
export { list as MayaList } from "./tokenLists/mayachain.ts";
export { list as OneInchList } from "./tokenLists/oneinch.ts";
export { list as PancakeswapList } from "./tokenLists/pancakeswap.ts";
export { list as PangolinList } from "./tokenLists/pangolin_v1.ts";
export { list as SushiswapList } from "./tokenLists/sushiswap_v2.ts";
export { list as ThorchainList } from "./tokenLists/thorchain.ts";
export { list as ThorchainStreamingList } from "./tokenLists/thorchain_streaming.ts";
export { list as TraderjoeV1List } from "./tokenLists/traderjoe_v1.ts";
export { list as TraderjoeV2List } from "./tokenLists/traderjoe_v2.ts";
export { list as UniswapV2List } from "./tokenLists/uniswap_v2.ts";
export { list as UniswapV3List } from "./tokenLists/uniswap_v3.ts";
Loading

0 comments on commit ab5c3b3

Please sign in to comment.